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. Don't tell me how to declare a string!

Don't tell me how to declare a string!

Scheduled Pinned Locked Moved The Weird and The Wonderful
databasetutorial
25 Posts 14 Posters 39 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 Pete OHanlon

    This reminds me of a consultancy developer who wrote all of his SQL statements like this:

    "SELECT field1, \r\n" +
    "       field2, \r\n" + 
    "       field3 \r\n" +
    "FROM \r\n" +
    "       myTable \r\n";
    

    It still makes me shudder.

    Deja View - the feeling that you've seen this post before.

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

    I would do that as:

    @"
    SELECT field1,
    field2,
    field3
    FROM
    mytable
    ";

    It still puts the newlines in the string for when I dump a CommandText to a log.

    1 Reply Last reply
    0
    • V Vasudevan Deepak Kumar

      But a horror for the compiler. :confused:

      Vasudevan Deepak Kumar Personal Homepage Tech Gossips

      K Offline
      K Offline
      KarstenK
      wrote on last edited by
      #16

      The compiler makes one string of it all and so the empty string is "Over the hills". I like readable code so much, that I often write a little more code and comments and think about value and class names up to 10 minutes. It helps me to make a 'master architecture' for my work. It has often paid off, because I can easy reuse code. I have to maintain 6 projects in which a lot of code is reused. The changes and enhancements for Vista I wrote 1 time and use it 6 times.:cool:

      Greetings from Germany

      1 Reply Last reply
      0
      • P Pete OHanlon

        This reminds me of a consultancy developer who wrote all of his SQL statements like this:

        "SELECT field1, \r\n" +
        "       field2, \r\n" + 
        "       field3 \r\n" +
        "FROM \r\n" +
        "       myTable \r\n";
        

        It still makes me shudder.

        Deja View - the feeling that you've seen this post before.

        J Offline
        J Offline
        Jorgen Sigvardsson
        wrote on last edited by
        #17

        That makes sense sometimes, especially if you're debugging [or logging rather] the actual SQL code sent to the DBMS.

        -- Smell-o-vision users, insert nostril tubes now

        P 1 Reply Last reply
        0
        • J Jorgen Sigvardsson

          That makes sense sometimes, especially if you're debugging [or logging rather] the actual SQL code sent to the DBMS.

          -- Smell-o-vision users, insert nostril tubes now

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

          To be honest, the time it takes you to do this is wasted time - especially, as I noted in the OP, when you are a consultant and thus billing by the hour.

          Deja View - the feeling that you've seen this post before.

          1 Reply Last reply
          0
          • T TClarke

            I found something that made me blink with its absurdity the other day and what was more absurd was the defense the developer used when confronted with it. while looking through countless strings storing SQL queries I noticed they were all written as so:

            private static final String SQL = ""

            • " SELECT "
            • " nvl( vss.structure_address, "
            • " ifsapp.serial_structure_template_api.get_pos(top_part_no, "
              etc...

            When I asked him about the first empty string he looked at me as though I was suggesting replacing all hiss method calls with gotos "You can't just add a sting to nothing you know!" he snarled "you need an empty base" :laugh:

            A Offline
            A Offline
            Adam Maras
            wrote on last edited by
            #19

            This is why I like the @"string" literal operator in C#.

            private static final String SQL = @"SELECT
            nvl( vss.structure_address,
            ifsapp.serial_structure_template_api.get_pos(top_part_no, ...";

            1 Reply Last reply
            0
            • P Pete OHanlon

              This reminds me of a consultancy developer who wrote all of his SQL statements like this:

              "SELECT field1, \r\n" +
              "       field2, \r\n" + 
              "       field3 \r\n" +
              "FROM \r\n" +
              "       myTable \r\n";
              

              It still makes me shudder.

              Deja View - the feeling that you've seen this post before.

              J Offline
              J Offline
              joebeam
              wrote on last edited by
              #20

              no joke...this is from a code generator developed by an earlier employee. SQL = "INSERT INTO Orders (" & _ " OrderID, " & _ " CustomerID, " & _ " EmployeeID, " & _ " OrderDate, " & _ " RequiredDate, " & _ " ShippedDate, " & _ " ShipVia, " & _ " Freight, " & _ " ShipName, " & _ " ShipAddress, " & _ " ShipCity, " & _ " ShipRegion, " & _ " ShipPostalCode, " & _ " ShipCountry " & _ " )" & _ "VALUES (" & _ "'" & Orders.OrderID & "', " & _ "'" & Orders.CustomerID & "', " & _ "'" & Orders.EmployeeID & "', " & _ DataCommonFunctions.SQLDate(Orders.OrderDate) & ", " & _ DataCommonFunctions.SQLDate(Orders.RequiredDate) & ", " & _ DataCommonFunctions.SQLDate(Orders.ShippedDate) & ", " & _ "'" & Orders.ShipVia & "', " & _ "'" & Orders.Freight & "', " & _ "'" & Orders.ShipName & "', " & _ "'" & Orders.ShipAddress & "', " & _ "'" & Orders.ShipCity & "', " & _ "'" & Orders.ShipRegion & "', " & _ "'" & Orders.ShipPostalCode & "', " & _ "'" & Orders.ShipCountry & "') " Sql Injection anyone?!? Whenever I put sql in code it's always on one line and I'm using parameters!

              1 Reply Last reply
              0
              • L Luc Pattyn

                TClarke wrote:

                private static final String SQL = "" + " SELECT " + " nvl( vss.structure_address, " + " ifsapp.serial_structure_template_api.get_pos(top_part_no," etc...

                Without the "" on the first line, you couldn't start the second line with a + hence the indentation might be at risk, i.e. the third and following lines might not align with the second one; this may become very important when the code is worthwhile and is likely be published on one or more forums. BTW: the code itself is OK, the compiler does the concatenation for you.

                Luc Pattyn [Forum Guidelines] [My Articles]


                this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/AllLanguages/General - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


                D Offline
                D Offline
                dojohansen
                wrote on last edited by
                #21

                Indentation-wise, I find that this works well: string query = "SELECT [foo], [bar] " + "FROM [f] " + "WHERE [id] = @id";

                L 1 Reply Last reply
                0
                • D dojohansen

                  Indentation-wise, I find that this works well: string query = "SELECT [foo], [bar] " + "FROM [f] " + "WHERE [id] = @id";

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #22

                  That's fine by me, the only risk is you might omit a space somewhere, which you would notice more easily if everything were on one line.

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


                  1 Reply Last reply
                  0
                  • C Chris Losinger

                    compilers don't have feelings. they cannot be horrified. ;)

                    image processing toolkits | batch image processing | blogging

                    L Offline
                    L Offline
                    LFirth
                    wrote on last edited by
                    #23

                    I bet it was comments like this that led to SkyNet waging war on man....

                    1 Reply Last reply
                    0
                    • L Luc Pattyn

                      Compilers are smart enough to understand that the concatenation of two literals yields a bigger literal. They don't generate run-time code to get that done. Also -- not applicable in this case -- the CLR compilers are known to use a StringBuilder if your code is performing complex concatenation involving not just literals, even when the source does not call for a StringBuilder explicitly. :)

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/AllLanguages/General - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


                      R Offline
                      R Offline
                      Ri Qen Sin
                      wrote on last edited by
                      #24

                      Oh nice! I didn't know that… Now I can write sloppy string concatenation code without worrying about the performance penalty! ^_^ Does this feature count as encouraging bad coding practices?

                      ROFLOLMFAO

                      L 1 Reply Last reply
                      0
                      • R Ri Qen Sin

                        Oh nice! I didn't know that… Now I can write sloppy string concatenation code without worrying about the performance penalty! ^_^ Does this feature count as encouraging bad coding practices?

                        ROFLOLMFAO

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #25

                        Ri Qen-Sin wrote:

                        Does this feature count as encouraging bad coding practices?

                        Most people dont need to be encouraged in the wrong direction. Compiler optimizations typically improve the simple cases, and seldom drastically improve the complex cases (where most could be gained!). So staying vigilent would pay off frequently. :)

                        Luc Pattyn [Forum Guidelines] [My Articles]


                        this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


                        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