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. Best Practices turned into Coding Horrors.

Best Practices turned into Coding Horrors.

Scheduled Pinned Locked Moved The Weird and The Wonderful
database
50 Posts 19 Posters 0 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.
  • T Thomas Daniels

    I can't find a "Delete brain" button on my head. Should I remove my ears to find the button?

    The quick red ProgramFOX jumps right over the Lazy<Dog>.

    G Offline
    G Offline
    Gary R Wheeler
    wrote on last edited by
    #40

    Some of our members use the GinAndTonic() method, which does a delete brain operation as a side effect.

    Software Zen: delete this;

    1 Reply Last reply
    0
    • P Paulo Zemek

      It is a well known good practice to use StringBuilders instead of doing many string concatenations. Yet, I got really impressed when I saw a document telling to replace things like this:

      private const string SQL =
      "SELECT " +
      " ID, " +
      " NAME, " +
      " BIRTHDAY " +
      "FROM " +
      " TABLE " +
      "WHERE " +
      " NAME LIKE @PARAM";

      By creating the StringBuilder everytime in the method where the SQL was being used. Maybe I am wrong :doh: , but I really believe consts aren't doing bad string concatenations all the time.

      K Offline
      K Offline
      KP Lee
      wrote on last edited by
      #41

      There are a lot of people who encounter "best practices", turn off the brain and spew out nonsense, a constant is evaluated once on the JIT compile and that is the last time it uses the string add. Of course the DBA that gives a programmer direct read access to a table needs to be led out and shot.... There are good reasons for using StringBuilder, but that isn't one of them. I intentionally did string concatenation in my code to see how much StringBuilder helps. I got the code to work adding the statistics I gathered into a string. It ran in about 35 minutes. I added StringBuilder and cut the time to 15 minutes. I then added code to only convert the stats I built when I found a solution (several thousand) instead of when I created the stats (several million) and cut it down to 7 minutes. Finally I played around with the order of how the puzzle was solved and cut it to 4 minutes. My next improvement cut it down to 2 minutes. (Purchaced a computer with twice the rate of processing.)

      1 Reply Last reply
      0
      • B BobJanova

        Didn't even the 1.0 compiler do that with string constants?

        K Offline
        K Offline
        KP Lee
        wrote on last edited by
        #42

        BobJanova wrote:

        Didn't even the 1.0 compiler do that

        I, too, was surprised by that, however I learned C# around 2.3 or later so I bought it even though it seemed odd with a const declaration.

        1 Reply Last reply
        0
        • P Paulo Zemek

          It is a well known good practice to use StringBuilders instead of doing many string concatenations. Yet, I got really impressed when I saw a document telling to replace things like this:

          private const string SQL =
          "SELECT " +
          " ID, " +
          " NAME, " +
          " BIRTHDAY " +
          "FROM " +
          " TABLE " +
          "WHERE " +
          " NAME LIKE @PARAM";

          By creating the StringBuilder everytime in the method where the SQL was being used. Maybe I am wrong :doh: , but I really believe consts aren't doing bad string concatenations all the time.

          C Offline
          C Offline
          Clifford Nelson
          wrote on last edited by
          #43

          I am not sure what you are trying to do, but see very little reason is having separate strings for this in the first place. Can be represented as a single string. I would also guess that the complier will concantinate everyting, so trying to use a StringBuilder will probalby slow everything down.

          P 1 Reply Last reply
          0
          • C Clifford Nelson

            I am not sure what you are trying to do, but see very little reason is having separate strings for this in the first place. Can be represented as a single string. I would also guess that the complier will concantinate everyting, so trying to use a StringBuilder will probalby slow everything down.

            P Offline
            P Offline
            Paulo Zemek
            wrote on last edited by
            #44

            The compiler will make everything into a single string... and I was presenting this as a coding horrer exactly because the many + that were done as const were being replaced by stringbuilders.

            1 Reply Last reply
            0
            • P PIEBALDconsult

              Indeed, not a candidate for StringBuilderhood. And I write it as

              private const string SQL =
              @"
              SELECT ID
              , NAME
              , BIRTHDAY
              FROM TABLE
              WHERE NAME LIKE @PARAM
              " ;

              so it prints out nice in error messages [added>>] and I can very easily copy/paste it between a code file and SSMS or other SQL executor.

              K Offline
              K Offline
              KP Lee
              wrote on last edited by
              #45

              PIEBALDconsult wrote:

              so it prints out nice in error messages.

              If that is where you want it printed out nicely, you could use: ...= "\nSELECT ID\n, NAME\n, BIRTHDAY\nFROM TABLE\nWHERE NAME LIKE @PARAM\n"; The thing I don't like about @"... is that intellesense puts in indents you don't want, while most of the time, intellesense is so handy, I don't like working without it. Of course, you might be one of those people who like readable code too. :)

              1 Reply Last reply
              0
              • K KRucker

                The "Best Practice" is not to use string concatenation in a loop. The reason is that under the hood when concatinating two strings, a third string will be created large enough to bold both source strings. The source strings will be copied to that new string, the original string destroyed, then recreated and the contents of the temporary string copied into it, then the temporary string destroyed. The concatination that you are showing should be fine, unless it is being performed in a loop.

                K Offline
                K Offline
                KP Lee
                wrote on last edited by
                #46

                KRucker wrote:

                The concatination(sic) that you are showing should be fine, unless it is being performed in a loop.

                How many const declarations run in a loop? Unless you are instantiating a class in a loop... which indicates the const field should also be static. You should really read posts more clearly, he wasn't complaining about the use of concatenations, but about people spouting off about best practices where the practices clearly don't apply. Makes those people look like they are doing things by rote without thinking about what they are saying or if what they say makes sense. Of course I have been guilty of that, I shouldn't throw stones when I live in a house with glass all around it. :) You forgot to mention each of those string parts get stuck in the intern table until the GC gets around to cleaning it up. ;P PS "that" I am guilty of includes: misreading items, not thinking things out, and saying inappropriate things. PPS Edited my post because Preview clearly showed your statement. On checking why posting didn't show, I missed some HTML tags that removed it from post.

                1 Reply Last reply
                0
                • C Chad3F

                  "Best practices" are "at best" someone's opinion. ;) In some cases that opinion may be shared by many, but that doesn't always make it right. After all, at one time, how many people had the opinion the world was flat and the best practice was not to sail too far out? While some things that are considered a best practice I do see reason to use over alternatives, I really don't like the idea of having an arbitrary list of "do these things for best results". They (you know, the "they" that killed Kenny) might as well call it "'boxes to use and not think outside of' practices" instead of "best practices".

                  K Offline
                  K Offline
                  KP Lee
                  wrote on last edited by
                  #47

                  I know the show that kills Kenny, because of an article I read that talks about it. (At least if I see the show's name, I'll also remember it kills Kenny) The article didn't say why they kill Kenny and I've only watched about 40 seconds tops to see if I wanted to watch it. Is it some group mindset that causes them to want to kill Kenny? Of course the writers put it in because it is funny to constantly kill the same person over and over and... (Which is why I could stand about 40 seconds of their humor.) What a scalawag that Columbus was, eh? I also like your alternative definition of best practices.

                  C 1 Reply Last reply
                  0
                  • K KP Lee

                    I know the show that kills Kenny, because of an article I read that talks about it. (At least if I see the show's name, I'll also remember it kills Kenny) The article didn't say why they kill Kenny and I've only watched about 40 seconds tops to see if I wanted to watch it. Is it some group mindset that causes them to want to kill Kenny? Of course the writers put it in because it is funny to constantly kill the same person over and over and... (Which is why I could stand about 40 seconds of their humor.) What a scalawag that Columbus was, eh? I also like your alternative definition of best practices.

                    C Offline
                    C Offline
                    Chad3F
                    wrote on last edited by
                    #48

                    The "Kenny" reference was to indicate that the "They" context I was using was not a specific, or even tangible, group (such as an official standards body, like IETF, ISO, IEEE, ANSI, etc..). But instead was the unknown and mysterious "powers that be" (in the context of defining so-called "best" anything) that define "best practice" lists. As for the origin behind the actual killing of Kenny.. I guess it started out as some form of joke in the show (e.g. this poor kid Kenny just can't catch a break), and just kept on going from there. Eventually they stopped doing it [regularly] (maybe they ran out of "interesting" ways for Kenny to die). I'm sure there is probably an official response from Trey Parker and/or Matt Stone (its creators) about the subject on some fan site/FAQ/forum out there.

                    1 Reply Last reply
                    0
                    • P Paulo Zemek

                      It is a well known good practice to use StringBuilders instead of doing many string concatenations. Yet, I got really impressed when I saw a document telling to replace things like this:

                      private const string SQL =
                      "SELECT " +
                      " ID, " +
                      " NAME, " +
                      " BIRTHDAY " +
                      "FROM " +
                      " TABLE " +
                      "WHERE " +
                      " NAME LIKE @PARAM";

                      By creating the StringBuilder everytime in the method where the SQL was being used. Maybe I am wrong :doh: , but I really believe consts aren't doing bad string concatenations all the time.

                      T Offline
                      T Offline
                      Tim Yen
                      wrote on last edited by
                      #49

                      Why not do ? private const string SQL = @"SELECT ID, NAME, BIRTHDAY FROM TABLE WHERE NAME LIKE @PARAM"; It allows you to cut n paste the code into a SQL dev tool to test it and its easier to maintain without the extra quotes and +'s.

                      P 1 Reply Last reply
                      0
                      • T Tim Yen

                        Why not do ? private const string SQL = @"SELECT ID, NAME, BIRTHDAY FROM TABLE WHERE NAME LIKE @PARAM"; It allows you to cut n paste the code into a SQL dev tool to test it and its easier to maintain without the extra quotes and +'s.

                        P Offline
                        P Offline
                        Paulo Zemek
                        wrote on last edited by
                        #50

                        Because it was a coding horror.

                        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