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.
  • P Paulo Zemek

    You are talking about the real Best Practice. But the "Best Practice" is to replace any string concatenation, even in consts, by a StringBuilder.

    B Offline
    B Offline
    BobJanova
    wrote on last edited by
    #24

    const string sql = "select stuff "+
    "from table"; // Best practice doesn't apply to consts

    1 Reply Last reply
    0
    • B BobJanova

      What's wrong with it? I mean, it's simple enough I'd put it on one line, but I don't see anything in principle wrong with putting it on several, and as C# doesn't have multi-line string constants, you have to write it as it is there. Edit: apparently @ strings will let you do multi-line constants. Making code a stored procedure hides it away from the developer and makes it harder to see. Select queries should almost never be in one because it makes you go and look at the database to find out what the code is doing ... or, to put it another way, those queries are part of the business logic and should be in the code. But I have a somewhat old fashioned view of the database as essentially a minimally intelligent data store.

      R Offline
      R Offline
      R Giskard Reventlov
      wrote on last edited by
      #25

      I like the database to take that strain: it's what it is there for, after all. And I never, ever put SQL in code: I either use a view or a stored procedure.

      "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. nils illegitimus carborundum me, me, me

      J 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.

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #26

        Unless you're talking about a really old (.NET 1) version of the compiler, this is translated internally into the following il:

        .field private static literal string SQL = string('SELECT ID, NAME, BIRTHDAY FROM TABLE WHERE NAME LIKE @PARAM')

        As you can see, there's no concatenation in there at all.

        I was brought up to respect my elders. I don't respect many people nowadays.
        CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

        P B 2 Replies Last reply
        0
        • P Pete OHanlon

          Unless you're talking about a really old (.NET 1) version of the compiler, this is translated internally into the following il:

          .field private static literal string SQL = string('SELECT ID, NAME, BIRTHDAY FROM TABLE WHERE NAME LIKE @PARAM')

          As you can see, there's no concatenation in there at all.

          I was brought up to respect my elders. I don't respect many people nowadays.
          CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

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

          I know there is no concatenation. Any const must be resolved at compile time. When I said that I "think it optimizes" I put the :doh: because it is obvious.

          P 1 Reply Last reply
          0
          • P Pete OHanlon

            Unless you're talking about a really old (.NET 1) version of the compiler, this is translated internally into the following il:

            .field private static literal string SQL = string('SELECT ID, NAME, BIRTHDAY FROM TABLE WHERE NAME LIKE @PARAM')

            As you can see, there's no concatenation in there at all.

            I was brought up to respect my elders. I don't respect many people nowadays.
            CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

            B Offline
            B Offline
            BobJanova
            wrote on last edited by
            #28

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

            K 1 Reply Last reply
            0
            • P Paulo Zemek

              I know there is no concatenation. Any const must be resolved at compile time. When I said that I "think it optimizes" I put the :doh: because it is obvious.

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #29

              That was meant for you to beat them round the head with, rather than for your benefit.

              I was brought up to respect my elders. I don't respect many people nowadays.
              CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

              1 Reply Last reply
              0
              • B Brady Kelly

                Looks pretty damned neat to me. I always write out my SQL with each identifier or keyword on its own line. Much easier to read and diagnose.

                J Offline
                J Offline
                jschell
                wrote on last edited by
                #30

                Brady Kelly wrote:

                Looks pretty damned neat to me.

                I don't know what you are referring to. I have no problem with multi-line SQL constructs.

                1 Reply Last reply
                0
                • P PIEBALDconsult

                  No, I don't use consts for SQL.

                  J Offline
                  J Offline
                  jschell
                  wrote on last edited by
                  #31

                  PIEBALDconsult wrote:

                  No, I don't use consts for SQL.

                  Then we have a term definition problem because I responded to what you said... "And I [PIEBALDconsult] write it as ...private const string SQL"

                  P 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".

                    J Offline
                    J Offline
                    jschell
                    wrote on last edited by
                    #32

                    Chad3F wrote:

                    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?

                    At the time when people did in fact think the world was flat then in fact is was foolhardy to sail too far out because one was very likely to not return. Which was a loss both in lives and commerce.

                    C 1 Reply Last reply
                    0
                    • J jschell

                      PIEBALDconsult wrote:

                      No, I don't use consts for SQL.

                      Then we have a term definition problem because I responded to what you said... "And I [PIEBALDconsult] write it as ...private const string SQL"

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

                      I meant "that".

                      J 1 Reply Last reply
                      0
                      • J jschell

                        Chad3F wrote:

                        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?

                        At the time when people did in fact think the world was flat then in fact is was foolhardy to sail too far out because one was very likely to not return. Which was a loss both in lives and commerce.

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

                        Yes, those that sailed too far likely had a greater chance of something going wrong (per voyage).. but statistically over all, I expect out of all ships/lives lost that only a small percentage were from those sailing out too far. Most had no reason to try (unless they were explorers), so most of the risk was while in known waters (so why not have a best practice of never sailing period). This would be like most people not routinely driving more than 1000 miles (or kilometers) from their home (unless it was required of their job).. hence the [paraphrased] expression of "most [driving] accidents occur within X distance of ones home", simply because that is where the are [on the road] most of the time. In the end, just because something is done for the wrong reasons (i.e. to not fall off the end of the world) and happens to be useful (i.e. less lost lives/cargo), doesn't justify the basis (a variation of "the ends don't justify the means" I suppose). As an example, if a parent tells a young child not to talk to strangers, "just because", but not why explain why (since the child would unlikely be capable of comprehending why anyway), then that may work on the short term.. but, if left at that, in the long term it could be disastrous. Imagine if that child follows that directive to the letter "just because" their parent said so and one day there is a house fire, firefighters come and surround the house with the kid still inside. Now the child sees all these strangers and has to make a choice.. does he go outside, where all these strangers are, and break the rule.. or does he stay inside (or even hide, since several strangers are entering the house for some reason) to avoid them? A slightly contrived scenario (but not impossible) that illustrates how blindly following a "rule" without understanding why can [eventually] lead to a worse outcome than what the rule was indented to avoid. In some cases, like a young child's limited comprehension, you have to try and account for exceptions when stating the rule.. but when comprehension is possible, ignorant, "just because" logic is never an acceptable reason to do something. One should know _why_ something is better to use than another, in which case they shouldn't _need_ to be told to what [specifically] to do for the best, as it is the automatic choice (including _when_ to break the default choice). If I know a wood chipper chops up materials you put in it, and I know one is [potentially] running, then I implicitly choose not to put my hand in it.. I don't need to be told

                        J 1 Reply Last reply
                        0
                        • B BobJanova

                          What's wrong with it? I mean, it's simple enough I'd put it on one line, but I don't see anything in principle wrong with putting it on several, and as C# doesn't have multi-line string constants, you have to write it as it is there. Edit: apparently @ strings will let you do multi-line constants. Making code a stored procedure hides it away from the developer and makes it harder to see. Select queries should almost never be in one because it makes you go and look at the database to find out what the code is doing ... or, to put it another way, those queries are part of the business logic and should be in the code. But I have a somewhat old fashioned view of the database as essentially a minimally intelligent data store.

                          E Offline
                          E Offline
                          ENOTTY
                          wrote on last edited by
                          #35

                          Also you can fake multiline strings (in VB.NET, say) with an inline XML document, of which the text is then converted to a string.

                          1 Reply Last reply
                          0
                          • P PIEBALDconsult

                            I meant "that".

                            J Offline
                            J Offline
                            jschell
                            wrote on last edited by
                            #36

                            PIEBALDconsult wrote:

                            I meant "that".

                            No idea what that means.

                            1 Reply Last reply
                            0
                            • C Chad3F

                              Yes, those that sailed too far likely had a greater chance of something going wrong (per voyage).. but statistically over all, I expect out of all ships/lives lost that only a small percentage were from those sailing out too far. Most had no reason to try (unless they were explorers), so most of the risk was while in known waters (so why not have a best practice of never sailing period). This would be like most people not routinely driving more than 1000 miles (or kilometers) from their home (unless it was required of their job).. hence the [paraphrased] expression of "most [driving] accidents occur within X distance of ones home", simply because that is where the are [on the road] most of the time. In the end, just because something is done for the wrong reasons (i.e. to not fall off the end of the world) and happens to be useful (i.e. less lost lives/cargo), doesn't justify the basis (a variation of "the ends don't justify the means" I suppose). As an example, if a parent tells a young child not to talk to strangers, "just because", but not why explain why (since the child would unlikely be capable of comprehending why anyway), then that may work on the short term.. but, if left at that, in the long term it could be disastrous. Imagine if that child follows that directive to the letter "just because" their parent said so and one day there is a house fire, firefighters come and surround the house with the kid still inside. Now the child sees all these strangers and has to make a choice.. does he go outside, where all these strangers are, and break the rule.. or does he stay inside (or even hide, since several strangers are entering the house for some reason) to avoid them? A slightly contrived scenario (but not impossible) that illustrates how blindly following a "rule" without understanding why can [eventually] lead to a worse outcome than what the rule was indented to avoid. In some cases, like a young child's limited comprehension, you have to try and account for exceptions when stating the rule.. but when comprehension is possible, ignorant, "just because" logic is never an acceptable reason to do something. One should know _why_ something is better to use than another, in which case they shouldn't _need_ to be told to what [specifically] to do for the best, as it is the automatic choice (including _when_ to break the default choice). If I know a wood chipper chops up materials you put in it, and I know one is [potentially] running, then I implicitly choose not to put my hand in it.. I don't need to be told

                              J Offline
                              J Offline
                              jschell
                              wrote on last edited by
                              #37

                              Chad3F wrote:

                              and happens to be useful (i.e. less lost lives/cargo), doesn't justify the basis

                              Yes as a matter of a fact it does. Most software development exists to fulfill a need, often either an explicitly or even implicitly commercial. Just as sailing a ship did. Exploring might be 'fun' or 'cool' but the rate of return is very low. Whereas staying with the known routes provided a known rate of return. And that is what businesses want. They don't want fun/cool. They want money.

                              Chad3F wrote:

                              Teach that same person 10 concepts, with understanding, and now they know 100's or even 1000's of things (i.e. the meaningful combinations of those 10 concepts)

                              Of course that is what one wants. But one also wants every one to be a multi-talented genius as well and it just doesn't happen.

                              1 Reply Last reply
                              0
                              • R R Giskard Reventlov

                                I like the database to take that strain: it's what it is there for, after all. And I never, ever put SQL in code: I either use a view or a stored procedure.

                                "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair. nils illegitimus carborundum me, me, me

                                J Offline
                                J Offline
                                James Lonero
                                wrote on last edited by
                                #38

                                Oh yes, I have to agree. Prevents SQL injection attacks.

                                1 Reply Last reply
                                0
                                • B Brisingr Aerowing

                                  Yeah. The compiler should concatenate all of those together when run. So using a stringbuilder everywhere that was used basically shows the person (people?) that wrote the document had no clue as to what they were talking about. Basically:

                                  BRAINWAVE/1.0
                                  Status-Code: 404
                                  Status-Text: The requested brain could not be found. It may have been deleted or never installed.

                                  So there.

                                  Gryphons Are Awesome! ‮Gryphons Are Awesome!‬

                                  T Offline
                                  T Offline
                                  Thomas Daniels
                                  wrote on last edited by
                                  #39

                                  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 1 Reply Last reply
                                  0
                                  • 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
                                          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