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. The Lounge
  3. How would you code it?

How would you code it?

Scheduled Pinned Locked Moved The Lounge
csharpcsshelpquestion
59 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.
  • A Offline
    A Offline
    Al Beback
    wrote on last edited by
    #1

    Here's a simple code snippet (in C#):

    string hello = "Hello";
    string cp = "CP";
    DateTime today = DateTime.Today;

    // Desired result: "Hello CP! Today is Friday";

    string option1 = string.Format("{0} {1}! Today is {3:dddd}", hello, cp, today);

    string option2 = hello + " " + cp + "! Today is " + today.ToString("dddd");

    Vote 1 if you prefer option1. Vote 5 if you prefer option2. I prefer option2 since it's 1. More readable 2. Less error-prone (note the subtle error in option1 which the compiler won't catch) 3. More efficient (no CPU cycles spent scanning the format string looking for matching curly braces). Cheers!


    Man is a marvelous curiosity ... he thinks he is the Creator's pet ... he even believes the Creator loves him; has a passion for him; sits up nights to admire him; yes and watch over him and keep him out of trouble. He prays to him and thinks He listens. Isn't it a quaint idea. - Mark Twain

    A M P S L 8 Replies Last reply
    0
    • A Al Beback

      Here's a simple code snippet (in C#):

      string hello = "Hello";
      string cp = "CP";
      DateTime today = DateTime.Today;

      // Desired result: "Hello CP! Today is Friday";

      string option1 = string.Format("{0} {1}! Today is {3:dddd}", hello, cp, today);

      string option2 = hello + " " + cp + "! Today is " + today.ToString("dddd");

      Vote 1 if you prefer option1. Vote 5 if you prefer option2. I prefer option2 since it's 1. More readable 2. Less error-prone (note the subtle error in option1 which the compiler won't catch) 3. More efficient (no CPU cycles spent scanning the format string looking for matching curly braces). Cheers!


      Man is a marvelous curiosity ... he thinks he is the Creator's pet ... he even believes the Creator loves him; has a passion for him; sits up nights to admire him; yes and watch over him and keep him out of trouble. He prays to him and thinks He listens. Isn't it a quaint idea. - Mark Twain

      A Offline
      A Offline
      Andy Brummer
      wrote on last edited by
      #2

      I find 1 more readable and easier to update. If you have to worry about the cpu cycles consumed in a string.Format call, either you aren't using string.format correctly or .net is probably not a good choice for your application. I wouldn't really call passing an invalid index to a fixed size array a subtle bug.


      This blanket smells like ham

      M A 2 Replies Last reply
      0
      • A Andy Brummer

        I find 1 more readable and easier to update. If you have to worry about the cpu cycles consumed in a string.Format call, either you aren't using string.format correctly or .net is probably not a good choice for your application. I wouldn't really call passing an invalid index to a fixed size array a subtle bug.


        This blanket smells like ham

        M Offline
        M Offline
        malharone
        wrote on last edited by
        #3

        Agree. I prefer the first approach for two reasons: - Readablility: (in my opinion) - Performance: string concat. is heavy, which does not happen in the string.format - Localization: it's easier to put the Format string in your RESX file and depending upon the locale, one can re-order the "{xx}".

        A A B 3 Replies Last reply
        0
        • M malharone

          Agree. I prefer the first approach for two reasons: - Readablility: (in my opinion) - Performance: string concat. is heavy, which does not happen in the string.format - Localization: it's easier to put the Format string in your RESX file and depending upon the locale, one can re-order the "{xx}".

          A Offline
          A Offline
          Andy Brummer
          wrote on last edited by
          #4

          malharone wrote:

          Performance: string concat. is heavy, which does not happen in the string.format

          In a loop, yeah it can suck. In either of these cases it's not an issue. For slightly simpler string concatenation then this example it is the fastest of all, since it is optimized by the compiler.


          This blanket smells like ham

          1 Reply Last reply
          0
          • A Andy Brummer

            I find 1 more readable and easier to update. If you have to worry about the cpu cycles consumed in a string.Format call, either you aren't using string.format correctly or .net is probably not a good choice for your application. I wouldn't really call passing an invalid index to a fixed size array a subtle bug.


            This blanket smells like ham

            A Offline
            A Offline
            Al Beback
            wrote on last edited by
            #5

            Andy Brummer wrote:

            I find 1 more readable and easier to update.

            I guess it's a matter of opinion, but I find those curly place holders ugly. And I've found that the more of those you have, the more difficult it is to maintain. You have to start matching the index number with the location inside the Format statement. X|

            Andy Brummer wrote:

            I wouldn't really call passing an invalid index to a fixed size array a subtle bug.

            I suppose it's not subtle in that an exception will be thrown. My point is that it's easy to screw up the number, and not notice it until that piece of logic is executed. If it's an error message, it could be a while. (Yet another reason for unit testing.)


            Man is a marvelous curiosity ... he thinks he is the Creator's pet ... he even believes the Creator loves him; has a passion for him; sits up nights to admire him; yes and watch over him and keep him out of trouble. He prays to him and thinks He listens. Isn't it a quaint idea. - Mark Twain

            A 1 Reply Last reply
            0
            • M malharone

              Agree. I prefer the first approach for two reasons: - Readablility: (in my opinion) - Performance: string concat. is heavy, which does not happen in the string.format - Localization: it's easier to put the Format string in your RESX file and depending upon the locale, one can re-order the "{xx}".

              A Offline
              A Offline
              Al Beback
              wrote on last edited by
              #6

              malharone wrote:

              Localization: it's easier to put the Format string in your RESX file and depending upon the locale, one can re-order the "{xx}".

              That's the only compelling reason I have for using string.Format.


              Man is a marvelous curiosity ... he thinks he is the Creator's pet ... he even believes the Creator loves him; has a passion for him; sits up nights to admire him; yes and watch over him and keep him out of trouble. He prays to him and thinks He listens. Isn't it a quaint idea. - Mark Twain

              D 1 Reply Last reply
              0
              • M malharone

                Agree. I prefer the first approach for two reasons: - Readablility: (in my opinion) - Performance: string concat. is heavy, which does not happen in the string.format - Localization: it's easier to put the Format string in your RESX file and depending upon the locale, one can re-order the "{xx}".

                B Offline
                B Offline
                Big Daddy Farang
                wrote on last edited by
                #7

                malharone wrote:

                Performance: string concat. is heavy, which does not happen in the string.format

                That's what I thought as well. Although the OP stated that the concat. approach was more efficient, but I'm not so sure I buy his reasoning for it. I think he's fishing for "5" votes. ;) BDF

                M D 2 Replies Last reply
                0
                • A Al Beback

                  Here's a simple code snippet (in C#):

                  string hello = "Hello";
                  string cp = "CP";
                  DateTime today = DateTime.Today;

                  // Desired result: "Hello CP! Today is Friday";

                  string option1 = string.Format("{0} {1}! Today is {3:dddd}", hello, cp, today);

                  string option2 = hello + " " + cp + "! Today is " + today.ToString("dddd");

                  Vote 1 if you prefer option1. Vote 5 if you prefer option2. I prefer option2 since it's 1. More readable 2. Less error-prone (note the subtle error in option1 which the compiler won't catch) 3. More efficient (no CPU cycles spent scanning the format string looking for matching curly braces). Cheers!


                  Man is a marvelous curiosity ... he thinks he is the Creator's pet ... he even believes the Creator loves him; has a passion for him; sits up nights to admire him; yes and watch over him and keep him out of trouble. He prays to him and thinks He listens. Isn't it a quaint idea. - Mark Twain

                  M Offline
                  M Offline
                  Member 96
                  wrote on last edited by
                  #8

                  I absolutely prefer #2 and that's what we use here. I don't know about efficiency but your reason 1 and 2 are the reason why we do it that way.


                  Never trust machinery more complicated than a knife and fork. - Jubal Harshaw in Stranger in a Strange Land

                  1 Reply Last reply
                  0
                  • B Big Daddy Farang

                    malharone wrote:

                    Performance: string concat. is heavy, which does not happen in the string.format

                    That's what I thought as well. Although the OP stated that the concat. approach was more efficient, but I'm not so sure I buy his reasoning for it. I think he's fishing for "5" votes. ;) BDF

                    M Offline
                    M Offline
                    malharone
                    wrote on last edited by
                    #9

                    Big Daddy Farang wrote:

                    I think he's fishing for "5" votes.

                    In that case, he'd asked for "vote 5 for option 1. vote 5 for option 2" !. - MS

                    A 1 Reply Last reply
                    0
                    • A Al Beback

                      Andy Brummer wrote:

                      I find 1 more readable and easier to update.

                      I guess it's a matter of opinion, but I find those curly place holders ugly. And I've found that the more of those you have, the more difficult it is to maintain. You have to start matching the index number with the location inside the Format statement. X|

                      Andy Brummer wrote:

                      I wouldn't really call passing an invalid index to a fixed size array a subtle bug.

                      I suppose it's not subtle in that an exception will be thrown. My point is that it's easy to screw up the number, and not notice it until that piece of logic is executed. If it's an error message, it could be a while. (Yet another reason for unit testing.)


                      Man is a marvelous curiosity ... he thinks he is the Creator's pet ... he even believes the Creator loves him; has a passion for him; sits up nights to admire him; yes and watch over him and keep him out of trouble. He prays to him and thinks He listens. Isn't it a quaint idea. - Mark Twain

                      A Offline
                      A Offline
                      Andy Brummer
                      wrote on last edited by
                      #10

                      Al Beback wrote:

                      I guess it's a matter of opinion, but I find those curly place holders ugly.

                      Coding in C# and thinking curly braces are ugly? :laugh:

                      Al Beback wrote:

                      You have to start matching the index number with the location inside the Format statement.

                      Once that becomes an issue, I think you've really just reached the point where you just have complex output requirements. At that point I have trouble putting the output all together in my mind, so it gets to be just as much of a pain to update. Sometimes I'm more in the mood that day to use format strings other times I'll just munge the strings together.


                      This blanket smells like ham

                      A P 2 Replies Last reply
                      0
                      • A Al Beback

                        Here's a simple code snippet (in C#):

                        string hello = "Hello";
                        string cp = "CP";
                        DateTime today = DateTime.Today;

                        // Desired result: "Hello CP! Today is Friday";

                        string option1 = string.Format("{0} {1}! Today is {3:dddd}", hello, cp, today);

                        string option2 = hello + " " + cp + "! Today is " + today.ToString("dddd");

                        Vote 1 if you prefer option1. Vote 5 if you prefer option2. I prefer option2 since it's 1. More readable 2. Less error-prone (note the subtle error in option1 which the compiler won't catch) 3. More efficient (no CPU cycles spent scanning the format string looking for matching curly braces). Cheers!


                        Man is a marvelous curiosity ... he thinks he is the Creator's pet ... he even believes the Creator loves him; has a passion for him; sits up nights to admire him; yes and watch over him and keep him out of trouble. He prays to him and thinks He listens. Isn't it a quaint idea. - Mark Twain

                        P Offline
                        P Offline
                        PIEBALDconsult
                        wrote on last edited by
                        #11

                        The first is more readable, less error-prone, etc. The second is only for they who don't know about string.Format. But I (being me) format it as follows so I can more clearly see and count the values:

                        string option1 = string.Format
                        (
                        "{0} {1}! Today is {2:dddd}"
                        ,
                        hello
                        ,
                        cp
                        ,
                        today
                        ) ;

                        A 1 Reply Last reply
                        0
                        • A Andy Brummer

                          Al Beback wrote:

                          I guess it's a matter of opinion, but I find those curly place holders ugly.

                          Coding in C# and thinking curly braces are ugly? :laugh:

                          Al Beback wrote:

                          You have to start matching the index number with the location inside the Format statement.

                          Once that becomes an issue, I think you've really just reached the point where you just have complex output requirements. At that point I have trouble putting the output all together in my mind, so it gets to be just as much of a pain to update. Sometimes I'm more in the mood that day to use format strings other times I'll just munge the strings together.


                          This blanket smells like ham

                          A Offline
                          A Offline
                          Al Beback
                          wrote on last edited by
                          #12

                          Andy Brummer wrote:

                          Coding in C# and thinking curly braces are ugly

                          Got me there. :) For code blocks no. As placeholders yes.


                          Man is a marvelous curiosity ... he thinks he is the Creator's pet ... he even believes the Creator loves him; has a passion for him; sits up nights to admire him; yes and watch over him and keep him out of trouble. He prays to him and thinks He listens. Isn't it a quaint idea. - Mark Twain

                          1 Reply Last reply
                          0
                          • P PIEBALDconsult

                            The first is more readable, less error-prone, etc. The second is only for they who don't know about string.Format. But I (being me) format it as follows so I can more clearly see and count the values:

                            string option1 = string.Format
                            (
                            "{0} {1}! Today is {2:dddd}"
                            ,
                            hello
                            ,
                            cp
                            ,
                            today
                            ) ;

                            A Offline
                            A Offline
                            Al Beback
                            wrote on last edited by
                            #13

                            PIEBALDconsult wrote:

                            The first is more readable, less error-prone

                            My example demonstrates how it's not less error prone. It's easy to mess up the numbering, or to pass in more or less parameters than expected, or to pass them in the wrong order.


                            Man is a marvelous curiosity ... he thinks he is the Creator's pet ... he even believes the Creator loves him; has a passion for him; sits up nights to admire him; yes and watch over him and keep him out of trouble. He prays to him and thinks He listens. Isn't it a quaint idea. - Mark Twain

                            P D 2 Replies Last reply
                            0
                            • A Al Beback

                              PIEBALDconsult wrote:

                              The first is more readable, less error-prone

                              My example demonstrates how it's not less error prone. It's easy to mess up the numbering, or to pass in more or less parameters than expected, or to pass them in the wrong order.


                              Man is a marvelous curiosity ... he thinks he is the Creator's pet ... he even believes the Creator loves him; has a passion for him; sits up nights to admire him; yes and watch over him and keep him out of trouble. He prays to him and thinks He listens. Isn't it a quaint idea. - Mark Twain

                              P Offline
                              P Offline
                              PIEBALDconsult
                              wrote on last edited by
                              #14

                              Big deal. If you're afraid of making mistakes go right ahead and use option 2.

                              1 Reply Last reply
                              0
                              • A Al Beback

                                Here's a simple code snippet (in C#):

                                string hello = "Hello";
                                string cp = "CP";
                                DateTime today = DateTime.Today;

                                // Desired result: "Hello CP! Today is Friday";

                                string option1 = string.Format("{0} {1}! Today is {3:dddd}", hello, cp, today);

                                string option2 = hello + " " + cp + "! Today is " + today.ToString("dddd");

                                Vote 1 if you prefer option1. Vote 5 if you prefer option2. I prefer option2 since it's 1. More readable 2. Less error-prone (note the subtle error in option1 which the compiler won't catch) 3. More efficient (no CPU cycles spent scanning the format string looking for matching curly braces). Cheers!


                                Man is a marvelous curiosity ... he thinks he is the Creator's pet ... he even believes the Creator loves him; has a passion for him; sits up nights to admire him; yes and watch over him and keep him out of trouble. He prays to him and thinks He listens. Isn't it a quaint idea. - Mark Twain

                                S Offline
                                S Offline
                                Shog9 0
                                wrote on last edited by
                                #15

                                I use #1 almost exclusively. Why? Because #1 gives me a good idea at first-glance of how the end-result will be structured. value-space-value-!-space-Today is-space-formatted value. And yeah, there's a bug, which will show up the first time this code actually runs if no one sees it sooner - so it's really only an issue if no-one will ever test the code. #2 i find exceedingly tedious to mentally parse into an idea of what the result will look like. For performance, i'll just go with a StringBuilder or some such thing.


                                Last modified: 6mins after originally posted -- See how easy index errors are to catch at runtime? ;P

                                every night, i kneel at the foot of my bed and thank the Great Overseeing Politicians for protecting my freedoms by reducing their number, as if they were deer in a state park. -- Chris Losinger, Online Poker Players?

                                P A 2 Replies Last reply
                                0
                                • A Andy Brummer

                                  Al Beback wrote:

                                  I guess it's a matter of opinion, but I find those curly place holders ugly.

                                  Coding in C# and thinking curly braces are ugly? :laugh:

                                  Al Beback wrote:

                                  You have to start matching the index number with the location inside the Format statement.

                                  Once that becomes an issue, I think you've really just reached the point where you just have complex output requirements. At that point I have trouble putting the output all together in my mind, so it gets to be just as much of a pain to update. Sometimes I'm more in the mood that day to use format strings other times I'll just munge the strings together.


                                  This blanket smells like ham

                                  P Offline
                                  P Offline
                                  PIEBALDconsult
                                  wrote on last edited by
                                  #16

                                  Andy Brummer wrote:

                                  Coding in C# and thinking curly braces are ugly?

                                  Well, in the other uses of braces one could employ the C preprocessor and define begin and end appropriately, but that won't work in a string. :-D I still find the .net way preferable to the C way.

                                  1 Reply Last reply
                                  0
                                  • S Shog9 0

                                    I use #1 almost exclusively. Why? Because #1 gives me a good idea at first-glance of how the end-result will be structured. value-space-value-!-space-Today is-space-formatted value. And yeah, there's a bug, which will show up the first time this code actually runs if no one sees it sooner - so it's really only an issue if no-one will ever test the code. #2 i find exceedingly tedious to mentally parse into an idea of what the result will look like. For performance, i'll just go with a StringBuilder or some such thing.


                                    Last modified: 6mins after originally posted -- See how easy index errors are to catch at runtime? ;P

                                    every night, i kneel at the foot of my bed and thank the Great Overseeing Politicians for protecting my freedoms by reducing their number, as if they were deer in a state park. -- Chris Losinger, Online Poker Players?

                                    P Offline
                                    P Offline
                                    PIEBALDconsult
                                    wrote on last edited by
                                    #17

                                    Shog9 wrote:

                                    Because #2 gives

                                    I think you mean #1. #2 is, well... #2 :-D

                                    S 1 Reply Last reply
                                    0
                                    • P PIEBALDconsult

                                      Shog9 wrote:

                                      Because #2 gives

                                      I think you mean #1. #2 is, well... #2 :-D

                                      S Offline
                                      S Offline
                                      Shog9 0
                                      wrote on last edited by
                                      #18

                                      Corrected. Obviously this is the problem with using literal index values... :rolleyes:

                                      every night, i kneel at the foot of my bed and thank the Great Overseeing Politicians for protecting my freedoms by reducing their number, as if they were deer in a state park. -- Chris Losinger, Online Poker Players?

                                      P 1 Reply Last reply
                                      0
                                      • A Al Beback

                                        malharone wrote:

                                        Localization: it's easier to put the Format string in your RESX file and depending upon the locale, one can re-order the "{xx}".

                                        That's the only compelling reason I have for using string.Format.


                                        Man is a marvelous curiosity ... he thinks he is the Creator's pet ... he even believes the Creator loves him; has a passion for him; sits up nights to admire him; yes and watch over him and keep him out of trouble. He prays to him and thinks He listens. Isn't it a quaint idea. - Mark Twain

                                        D Offline
                                        D Offline
                                        Dave Kreskowiak
                                        wrote on last edited by
                                        #19

                                        Have you seen the crap concatenated by the newbies when they "build" their SQL statements?? That stuff isn't easy to read and it's a bitch to debug, hence they post it here wondering why it doesn't work.

                                        A guide to posting questions on CodeProject[^]
                                        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                             2006, 2007

                                        1 Reply Last reply
                                        0
                                        • S Shog9 0

                                          I use #1 almost exclusively. Why? Because #1 gives me a good idea at first-glance of how the end-result will be structured. value-space-value-!-space-Today is-space-formatted value. And yeah, there's a bug, which will show up the first time this code actually runs if no one sees it sooner - so it's really only an issue if no-one will ever test the code. #2 i find exceedingly tedious to mentally parse into an idea of what the result will look like. For performance, i'll just go with a StringBuilder or some such thing.


                                          Last modified: 6mins after originally posted -- See how easy index errors are to catch at runtime? ;P

                                          every night, i kneel at the foot of my bed and thank the Great Overseeing Politicians for protecting my freedoms by reducing their number, as if they were deer in a state park. -- Chris Losinger, Online Poker Players?

                                          A Offline
                                          A Offline
                                          Al Beback
                                          wrote on last edited by
                                          #20

                                          Shog9 wrote:

                                          Why? Because #2 gives me a good idea ...

                                          Don't you mean #1?

                                          Shog9 wrote:

                                          #2 i find exceedingly tedious to mentally parse into an idea of what the result will look like.

                                          I have the same opinion of #1. I have to mentally keep substituting the placeholders for their corresponding values. The more of those there are, the more difficult to visualize. With #2 the values are already neatly layed out in their proper place.


                                          Man is a marvelous curiosity ... he thinks he is the Creator's pet ... he even believes the Creator loves him; has a passion for him; sits up nights to admire him; yes and watch over him and keep him out of trouble. He prays to him and thinks He listens. Isn't it a quaint idea. - Mark Twain

                                          S P P 3 Replies 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