Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. SA1122 : CSharp.Readability

SA1122 : CSharp.Readability

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharptutorialcomtoolshelp
30 Posts 15 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    Cause The C# code includes an empty string, written as “”. Rule Description A violation of this rule occurs when the code contains an empty string. For example: string s = ""; This will cause the compiler to embed an empty string into the compiled code. Rather than including a hard-coded empty string, use the static string.Empty property: string s = string.Empty; How to Fix Violations To fix a violation of this rule, replace the hard-coded empty string with string.Empty.

    So..

    string s = string.Empty;

    ..is more readable than..

    string s = "";

    Since when are two words more "readable" than the two symbols that half the world knows and uses? The word "hard coded" is equally out of place - it feels like it means to warn that the constant "might" change and that it therefore must be wrong to hard-code it. Yes, very likely scenario.

    Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

    Richard DeemingR Offline
    Richard DeemingR Offline
    Richard Deeming
    wrote on last edited by
    #20

    OK, let's see if this works. Based on a real question from QA. Copy and paste the following code block into Visual Studio:

    string s = "‭";
    Console.WriteLine(s.Length);

    Now explain why the output is 1. :-D


    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

    L 1 Reply Last reply
    0
    • Richard DeemingR Richard Deeming

      OK, let's see if this works. Based on a real question from QA. Copy and paste the following code block into Visual Studio:

      string s = "‭";
      Console.WriteLine(s.Length);

      Now explain why the output is 1. :-D


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #21

      Non-printable characters. Three of them, according to the hex-editor :)

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

      1 Reply Last reply
      0
      • L Lost User

        Cause The C# code includes an empty string, written as “”. Rule Description A violation of this rule occurs when the code contains an empty string. For example: string s = ""; This will cause the compiler to embed an empty string into the compiled code. Rather than including a hard-coded empty string, use the static string.Empty property: string s = string.Empty; How to Fix Violations To fix a violation of this rule, replace the hard-coded empty string with string.Empty.

        So..

        string s = string.Empty;

        ..is more readable than..

        string s = "";

        Since when are two words more "readable" than the two symbols that half the world knows and uses? The word "hard coded" is equally out of place - it feels like it means to warn that the constant "might" change and that it therefore must be wrong to hard-code it. Yes, very likely scenario.

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

        R Offline
        R Offline
        Rob Grainger
        wrote on last edited by
        #22

        I always go with "" now. There are some situations where string.Empty is disallowed, because it is not a compile-time constant (which itself is a major weakness of C# in my eyes - the lack of const-correctness and compile-time constructs). So, as I sometimes have to use "" anyway, I eventually just gave up on string.Empty, so at least my usage pattern is consistent.

        "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

        1 Reply Last reply
        0
        • L Lost User

          Cause The C# code includes an empty string, written as “”. Rule Description A violation of this rule occurs when the code contains an empty string. For example: string s = ""; This will cause the compiler to embed an empty string into the compiled code. Rather than including a hard-coded empty string, use the static string.Empty property: string s = string.Empty; How to Fix Violations To fix a violation of this rule, replace the hard-coded empty string with string.Empty.

          So..

          string s = string.Empty;

          ..is more readable than..

          string s = "";

          Since when are two words more "readable" than the two symbols that half the world knows and uses? The word "hard coded" is equally out of place - it feels like it means to warn that the constant "might" change and that it therefore must be wrong to hard-code it. Yes, very likely scenario.

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

          R Offline
          R Offline
          Rob Grainger
          wrote on last edited by
          #23

          There are contexts in which "" is the only acceptable form, as C# has no compile-time constants so string.Empty is invalid in some contexts. I can never remember exactly when it is invalid, so eventually gave up and started using "" everywhere. (Side rant - one of C#'s major failings in my opinion is the inability to express const-correctness, immutability, and compile-time constants - it leaves potential to have incorrect implementations of interface functions etc.)

          "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

          1 Reply Last reply
          0
          • C Chris Maunder

            What about

            string s = "";

            vs

            string s = “”;

            (yeah, yeah - I know that syntax colouring should make this sligtly obvious, and that the IDE will then underline it all wiggly-like, and then the compiler will barf. But still...) I think the problem here is they are saying it's "readability" when in fact it's more about efficiency and not creating a new object each time.

            cheers Chris Maunder

            T Offline
            T Offline
            TigerInside
            wrote on last edited by
            #24

            why not use: var s = string.empty?

            L 1 Reply Last reply
            0
            • T TigerInside

              why not use: var s = string.empty?

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #25

              First, there is no need for "var"; it is preferred to use the string-datatype. Second it would also be preferred to use a literal (or a constant pointing to the literal), not a class-member - that's what this entire thread was about. "var" would be useful if it was a linq-query, but it is not meant as a replacement for those cases where you know what type to expect.

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

              1 Reply Last reply
              0
              • L Lost User

                Cause The C# code includes an empty string, written as “”. Rule Description A violation of this rule occurs when the code contains an empty string. For example: string s = ""; This will cause the compiler to embed an empty string into the compiled code. Rather than including a hard-coded empty string, use the static string.Empty property: string s = string.Empty; How to Fix Violations To fix a violation of this rule, replace the hard-coded empty string with string.Empty.

                So..

                string s = string.Empty;

                ..is more readable than..

                string s = "";

                Since when are two words more "readable" than the two symbols that half the world knows and uses? The word "hard coded" is equally out of place - it feels like it means to warn that the constant "might" change and that it therefore must be wrong to hard-code it. Yes, very likely scenario.

                Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

                S Offline
                S Offline
                Stefan_Lang
                wrote on last edited by
                #26

                A bit late, but: Don't use function names with "empty"[^] ;) If you don't feel like checking out the article, the gist of this item is that the word, "empty" is ambiguous: it can refer to either an action on, or the state of an object. While in your particular example the use of "Empty" seems clear enough, it's inherent ambiguity still makes you hesitate for a moment, whereas the meaning of "" is instantly clear.

                GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                1 Reply Last reply
                0
                • L Lost User

                  Cause The C# code includes an empty string, written as “”. Rule Description A violation of this rule occurs when the code contains an empty string. For example: string s = ""; This will cause the compiler to embed an empty string into the compiled code. Rather than including a hard-coded empty string, use the static string.Empty property: string s = string.Empty; How to Fix Violations To fix a violation of this rule, replace the hard-coded empty string with string.Empty.

                  So..

                  string s = string.Empty;

                  ..is more readable than..

                  string s = "";

                  Since when are two words more "readable" than the two symbols that half the world knows and uses? The word "hard coded" is equally out of place - it feels like it means to warn that the constant "might" change and that it therefore must be wrong to hard-code it. Yes, very likely scenario.

                  Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

                  D Offline
                  D Offline
                  David A Gray
                  wrote on last edited by
                  #27

                  Eddy Vluggen wrote:

                  Since when are two words more "readable" than the two symbols that half the world knows and uses? The word "hard coded" is equally out of place - it feels like it means to warn that the constant "might" change and that it therefore must be wrong to hard-code it.

                  In the beginning, I used string.Empty, until I tried to use one in a switch block, and the compiler balked, because it is not seen as a true constant. Hence, I created a true constant, EMPTY_STRING, and made sure that it is in my root namespace, which I import into virtually every project more complicated than Hello, World. There are many more such goodies where that one lives, in my my DLLServices2 Class Library on GitHub.

                  David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting

                  1 Reply Last reply
                  0
                  • L Lost User

                    Cause The C# code includes an empty string, written as “”. Rule Description A violation of this rule occurs when the code contains an empty string. For example: string s = ""; This will cause the compiler to embed an empty string into the compiled code. Rather than including a hard-coded empty string, use the static string.Empty property: string s = string.Empty; How to Fix Violations To fix a violation of this rule, replace the hard-coded empty string with string.Empty.

                    So..

                    string s = string.Empty;

                    ..is more readable than..

                    string s = "";

                    Since when are two words more "readable" than the two symbols that half the world knows and uses? The word "hard coded" is equally out of place - it feels like it means to warn that the constant "might" change and that it therefore must be wrong to hard-code it. Yes, very likely scenario.

                    Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

                    F Offline
                    F Offline
                    F ES Sitecore
                    wrote on last edited by
                    #28

                    It becomes more readable when you use it as a standard, it isn't just string that supports Empty and your own classes should too (if relevant).

                    public class Person
                    {
                    public int ID { get; set; }
                    public string Name { get; set; }

                    public static Person Empty
                    {
                        get { return new Person {ID = 0, Name = string.Empty}; }
                    }
                    

                    }

                    class Program
                    {
                    static void Main(string[] args)
                    {
                    string myString = string.Empty;
                    Guid myGuid = Guid.Empty;
                    Person myPerson = Person.Empty;

                        Console.ReadLine();
                    }
                    

                    }

                    1 Reply Last reply
                    0
                    • L Lost User

                      Cause The C# code includes an empty string, written as “”. Rule Description A violation of this rule occurs when the code contains an empty string. For example: string s = ""; This will cause the compiler to embed an empty string into the compiled code. Rather than including a hard-coded empty string, use the static string.Empty property: string s = string.Empty; How to Fix Violations To fix a violation of this rule, replace the hard-coded empty string with string.Empty.

                      So..

                      string s = string.Empty;

                      ..is more readable than..

                      string s = "";

                      Since when are two words more "readable" than the two symbols that half the world knows and uses? The word "hard coded" is equally out of place - it feels like it means to warn that the constant "might" change and that it therefore must be wrong to hard-code it. Yes, very likely scenario.

                      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

                      S Offline
                      S Offline
                      sibling123
                      wrote on last edited by
                      #29

                      From my point of view string.Empty conveys the intention that the string is not to be null and have no characters. Seeing a magic value like "", and let's face it - it IS a magic value albeit a simple one, always makes me question the code: "Is that supposed to be like that, or was there some value forgotten in debugging?" I have yet to encounter having to use an empty string as a case in a switch, but now that I know I will try to avoid that, or use a const value.

                      L 1 Reply Last reply
                      0
                      • S sibling123

                        From my point of view string.Empty conveys the intention that the string is not to be null and have no characters. Seeing a magic value like "", and let's face it - it IS a magic value albeit a simple one, always makes me question the code: "Is that supposed to be like that, or was there some value forgotten in debugging?" I have yet to encounter having to use an empty string as a case in a switch, but now that I know I will try to avoid that, or use a const value.

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #30

                        sibling123 wrote:

                        From my point of view string.Empty conveys the intention that the string is not to be null and have no characters.

                        So do two quotes.

                        sibling123 wrote:

                        Seeing a magic value like "",

                        Two quotes are no magic value, but a global constant expression.

                        sibling123 wrote:

                        I will try to avoid that, or use a const value.

                        Duh. SA1122 is simply nonsense.

                        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups