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. I'll see your curly braces & raise you a variable naming convention

I'll see your curly braces & raise you a variable naming convention

Scheduled Pinned Locked Moved The Lounge
androidcsharpc++javascriptswift
36 Posts 15 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.
  • raddevusR raddevus

    So which type of convention do you use when naming variables with multiple words? * camelCase * PascalCase * snake_case // Rust requires this for variable, function and struct member names and the compiler will warn (gripe at you) if you use other. I really like Rust, but this drives me crazy because it is YAC (Yet Another Convention) Back in C++ & C# I always used PascalCase Then in recent years JavaScript has effected my mind and I use a lot of camelCase these days. Kotlin (Android dev) & Swift (iOS dev) seem to use camelCase too. So it becoming kind of a standard. How about you? See more here[^].

    R Offline
    R Offline
    Ron Anders
    wrote on last edited by
    #16

    PascaleCase

    1 Reply Last reply
    0
    • raddevusR raddevus

      So which type of convention do you use when naming variables with multiple words? * camelCase * PascalCase * snake_case // Rust requires this for variable, function and struct member names and the compiler will warn (gripe at you) if you use other. I really like Rust, but this drives me crazy because it is YAC (Yet Another Convention) Back in C++ & C# I always used PascalCase Then in recent years JavaScript has effected my mind and I use a lot of camelCase these days. Kotlin (Android dev) & Swift (iOS dev) seem to use camelCase too. So it becoming kind of a standard. How about you? See more here[^].

      T Offline
      T Offline
      trønderen
      wrote on last edited by
      #17

      In my first 10-15 years of programming, hardcopy printouts were still common. Both my university and my first employer used a printer model placing the underscore so low that it almost hit the top of the characters on the line below; you certainly didn't see it as tying two characters together. In a listing, a name with underscores looked like several space separated words. For variables starting / ending with an underscore, the underscore was easily overlooked. This could at times be really confusing. So I came to strongly dislike underscores in identifiers. If you really wanted to underscore a text line for emphasis, like in a header, these printers were fine - they didn't clutter up the text. Other printers, placing the underscore at the character baseline, were not suitable for underscoring. E.g. an underscored h would be very similar to a b, an underscored n was close to an o. So for emphasizing text, our printers were great, but not for underscore used as a printable character in an identifier.

      raddevusR 1 Reply Last reply
      0
      • raddevusR raddevus

        So which type of convention do you use when naming variables with multiple words? * camelCase * PascalCase * snake_case // Rust requires this for variable, function and struct member names and the compiler will warn (gripe at you) if you use other. I really like Rust, but this drives me crazy because it is YAC (Yet Another Convention) Back in C++ & C# I always used PascalCase Then in recent years JavaScript has effected my mind and I use a lot of camelCase these days. Kotlin (Android dev) & Swift (iOS dev) seem to use camelCase too. So it becoming kind of a standard. How about you? See more here[^].

        honey the codewitchH Offline
        honey the codewitchH Offline
        honey the codewitch
        wrote on last edited by
        #18

        I use pascal case in .NET, camel case in JS, and snake case in my libraries in C++.

        To err is human. Fortune favors the monsters.

        raddevusR 1 Reply Last reply
        0
        • raddevusR raddevus

          So which type of convention do you use when naming variables with multiple words? * camelCase * PascalCase * snake_case // Rust requires this for variable, function and struct member names and the compiler will warn (gripe at you) if you use other. I really like Rust, but this drives me crazy because it is YAC (Yet Another Convention) Back in C++ & C# I always used PascalCase Then in recent years JavaScript has effected my mind and I use a lot of camelCase these days. Kotlin (Android dev) & Swift (iOS dev) seem to use camelCase too. So it becoming kind of a standard. How about you? See more here[^].

          S Offline
          S Offline
          Slacker007
          wrote on last edited by
          #19

          I pretty much do whatever strikes my fancy at the moment, throwing structure, conventions, and consistency to the wind. I find that adopting a care-free attitude to coding makes for a more interesting and fun time while testing and debugging my code in Production.

          raddevusR 1 Reply Last reply
          0
          • Mircea NeacsuM Mircea Neacsu

            I know I'm not going to win this argument but on my keyboard, underscore is right above the P. I promise I'm not going to continue this argument :laugh:

            Mircea

            T Offline
            T Offline
            trønderen
            wrote on last edited by
            #20

            Another reason that made me dislike underscores (the the first one is hardcopy readability): Although you are right about upper case also being a double stroke, both the shift and the letter have identical positions on all keyboards, and you can keep your hands in the same position (as long as you have learned to use your left pinky for the shift key). If you switch between different keyboards - especially keyboards adapted to different languages - punctuation and special characters such as underscore jumps all over the place. It took a few years to define a Norwegian standard placement for all those new characters that came with PCs, unknown on traditional typewriters, so if I dig up my oldest keyboards, even on Norwegian keyboards, they jump around. Using a Norwegian keyboard at home, a US English one at the office, and sometimes some machinery with its own keyboard layout, can be very frustrating. Nowadays, on a Norwegian keyboard, underscore is shift-hyphen, the rightmost key in the bottom row, next to right shift. You have to move your right hand three-quarters off the keyboard to hit it. (That also goes for hyphen, even if it is un-shifted.)

            Mircea NeacsuM 1 Reply Last reply
            0
            • T trønderen

              Another reason that made me dislike underscores (the the first one is hardcopy readability): Although you are right about upper case also being a double stroke, both the shift and the letter have identical positions on all keyboards, and you can keep your hands in the same position (as long as you have learned to use your left pinky for the shift key). If you switch between different keyboards - especially keyboards adapted to different languages - punctuation and special characters such as underscore jumps all over the place. It took a few years to define a Norwegian standard placement for all those new characters that came with PCs, unknown on traditional typewriters, so if I dig up my oldest keyboards, even on Norwegian keyboards, they jump around. Using a Norwegian keyboard at home, a US English one at the office, and sometimes some machinery with its own keyboard layout, can be very frustrating. Nowadays, on a Norwegian keyboard, underscore is shift-hyphen, the rightmost key in the bottom row, next to right shift. You have to move your right hand three-quarters off the keyboard to hit it. (That also goes for hyphen, even if it is un-shifted.)

              Mircea NeacsuM Offline
              Mircea NeacsuM Offline
              Mircea Neacsu
              wrote on last edited by
              #21

              trønderen wrote:

              to define a Norwegian standard placement for all those new characters

              You, guys have too many characters in your language! Just get rid of those! :laugh: (says he, whose mother tongue has 5 extra characters... or 10 if you count the caps form).

              Mircea

              T 1 Reply Last reply
              0
              • Mircea NeacsuM Mircea Neacsu

                trønderen wrote:

                to define a Norwegian standard placement for all those new characters

                You, guys have too many characters in your language! Just get rid of those! :laugh: (says he, whose mother tongue has 5 extra characters... or 10 if you count the caps form).

                Mircea

                T Offline
                T Offline
                trønderen
                wrote on last edited by
                #22

                You didn't keep your promise ...

                1 Reply Last reply
                0
                • T trønderen

                  In my first 10-15 years of programming, hardcopy printouts were still common. Both my university and my first employer used a printer model placing the underscore so low that it almost hit the top of the characters on the line below; you certainly didn't see it as tying two characters together. In a listing, a name with underscores looked like several space separated words. For variables starting / ending with an underscore, the underscore was easily overlooked. This could at times be really confusing. So I came to strongly dislike underscores in identifiers. If you really wanted to underscore a text line for emphasis, like in a header, these printers were fine - they didn't clutter up the text. Other printers, placing the underscore at the character baseline, were not suitable for underscoring. E.g. an underscored h would be very similar to a b, an underscored n was close to an o. So for emphasizing text, our printers were great, but not for underscore used as a printable character in an identifier.

                  raddevusR Offline
                  raddevusR Offline
                  raddevus
                  wrote on last edited by
                  #23

                  Great point about printers and the challenges of underscores. Thanks for sharing. This is also the reason we used to like Hungarian notation so much back then too. We did "offline" code reviews where we sat around a table and looked at code print-outs. Hungarian made sense because you could be a couple pages down and still know the type without looking back up at the declaration. :thumbsup: Of course, there was no intellisense back then either so... These kids don't know what they got!!! :rolleyes:

                  J T 2 Replies Last reply
                  0
                  • honey the codewitchH honey the codewitch

                    I use pascal case in .NET, camel case in JS, and snake case in my libraries in C++.

                    To err is human. Fortune favors the monsters.

                    raddevusR Offline
                    raddevusR Offline
                    raddevus
                    wrote on last edited by
                    #24

                    wow! you use snake_case in C++? I only use snake_case in Rust bec I'm forced to. (oldish c/c++ dev here).

                    honey the codewitchH 1 Reply Last reply
                    0
                    • raddevusR raddevus

                      wow! you use snake_case in C++? I only use snake_case in Rust bec I'm forced to. (oldish c/c++ dev here).

                      honey the codewitchH Offline
                      honey the codewitchH Offline
                      honey the codewitch
                      wrote on last edited by
                      #25

                      particularly for my libraries. I also tend to make heavy use of Generic Programming and thus templates, and so my libraries tend to be very STLish as it is. Consequently, I tend to follow STL patterns.

                      To err is human. Fortune favors the monsters.

                      raddevusR 1 Reply Last reply
                      0
                      • honey the codewitchH honey the codewitch

                        particularly for my libraries. I also tend to make heavy use of Generic Programming and thus templates, and so my libraries tend to be very STLish as it is. Consequently, I tend to follow STL patterns.

                        To err is human. Fortune favors the monsters.

                        raddevusR Offline
                        raddevusR Offline
                        raddevus
                        wrote on last edited by
                        #26

                        Ahh...never used much STL stuff because I (oddly) took up C# (in 2000/2001 very early) & that was when STL was coming on more strongly. My loss / lack of experience.

                        honey the codewitchH 1 Reply Last reply
                        0
                        • S Slacker007

                          I pretty much do whatever strikes my fancy at the moment, throwing structure, conventions, and consistency to the wind. I find that adopting a care-free attitude to coding makes for a more interesting and fun time while testing and debugging my code in Production.

                          raddevusR Offline
                          raddevusR Offline
                          raddevus
                          wrote on last edited by
                          #27

                          I actually love this bec I feel very similarly. But it is probably the latent Artist hiding in my brain which is usually beaten senseless by my inner Computer Scientist. :laugh:

                          1 Reply Last reply
                          0
                          • raddevusR raddevus

                            So which type of convention do you use when naming variables with multiple words? * camelCase * PascalCase * snake_case // Rust requires this for variable, function and struct member names and the compiler will warn (gripe at you) if you use other. I really like Rust, but this drives me crazy because it is YAC (Yet Another Convention) Back in C++ & C# I always used PascalCase Then in recent years JavaScript has effected my mind and I use a lot of camelCase these days. Kotlin (Android dev) & Swift (iOS dev) seem to use camelCase too. So it becoming kind of a standard. How about you? See more here[^].

                            K Offline
                            K Offline
                            kmoorevs
                            wrote on last edited by
                            #28

                            I'm a solo dev and as such have complete autonomy to do as I please. It's funny that it has changed through the years as IDEs/autocomplete have gotten better, and my eyesight has gotten worse! :laugh: In the beginning, I followed the way I was taught at uni, which was either scopeTypeName for public/global or typeName for locals. Back then before IDEs got smarter, I also used very short variable names. As time wore on and my eyesight got worse, I started to favor snake_case_descriptive_names so that it was easier for me to read and understand. Additionally, IDEs had improved such that long names weren't an issue as far as typing and type recognition while reading/debugging. As for generic objects like iterators, indexes, database objects (connections, datatables, readers, etc.) I have a stock set that I have used for 20+ years. IMHO, these are distinctive enough as to basically serve as a digital fingerprint. Currently, I'm the only on working on my code, but I realize that may not always be the case. I'd like to think that between descriptive variable name, my shorthand, and sparse commenting, that the next poor chap who sees it can understand what this mess actually does! :laugh: Regarding curly braces, when I have to use them, I like the opening brace on a new line with everything indented between.

                            "Go forth into the source" - Neal Morse "Hope is contagious"

                            1 Reply Last reply
                            0
                            • raddevusR raddevus

                              Ahh...never used much STL stuff because I (oddly) took up C# (in 2000/2001 very early) & that was when STL was coming on more strongly. My loss / lack of experience.

                              honey the codewitchH Offline
                              honey the codewitchH Offline
                              honey the codewitch
                              wrote on last edited by
                              #29

                              I don't use it a lot because it doesn't handle IoT very well without custom allocators and such, and even then it is heavy on little allocs and deallocs. I do study it though, because I believe in generic programming as a concept, when it comes to C++. If you aren't coding with GP, you aren't using the most powerful, flexible feature of C++. Templates really are where it's at with this language. They are first class, and if they aren't first class in your code, you're probably not using it to its full potential.

                              To err is human. Fortune favors the monsters.

                              1 Reply Last reply
                              0
                              • Mircea NeacsuM Mircea Neacsu

                                Programs should be read as books. What would you prefer, aBookThatIsTypesetInLongWordsAlmostLikeGerman, or a_more_relaxed_one_that_leaves_spaces_between_words? PS: Is it St. Sterile de Bates Day today?

                                Mircea

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

                                Mircea Neacsu wrote:

                                Programs should be read as books.

                                Nope. I read books. I work on programs. Those two are not the same. I read very fast and I can even skip paragraphs and even pages and still understand the flow even when it is text book. If I do that when writing a program or reviewing others code (which I do extensively and in detail) problems will be missed.

                                Mircea Neacsu wrote:

                                aBookThatIsTypesetInLongWordsAlmostLikeGerman

                                Flawed example. The variable name is too long and very likely the logic is flawed since the only need for that would be if the context was so complex (too complex) that it required that.

                                Mircea NeacsuM 1 Reply Last reply
                                0
                                • raddevusR raddevus

                                  Great point about printers and the challenges of underscores. Thanks for sharing. This is also the reason we used to like Hungarian notation so much back then too. We did "offline" code reviews where we sat around a table and looked at code print-outs. Hungarian made sense because you could be a couple pages down and still know the type without looking back up at the declaration. :thumbsup: Of course, there was no intellisense back then either so... These kids don't know what they got!!! :rolleyes:

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

                                  raddevus wrote:

                                  so much back then too.

                                  ...you could be a couple pages down and still know the type without looking back up at the declaration

                                  Also when it was less clear that a context that was pages long was probably going to end up being a problem for maintenance no matter what it looked like. Like an actual C++ class that I worked on that I calculated had 20,000 lines of code, hundreds of variables and hundreds of methods. It also spanned multiple files. I might not have even found all of the files.

                                  1 Reply Last reply
                                  0
                                  • J jschell

                                    Mircea Neacsu wrote:

                                    Programs should be read as books.

                                    Nope. I read books. I work on programs. Those two are not the same. I read very fast and I can even skip paragraphs and even pages and still understand the flow even when it is text book. If I do that when writing a program or reviewing others code (which I do extensively and in detail) problems will be missed.

                                    Mircea Neacsu wrote:

                                    aBookThatIsTypesetInLongWordsAlmostLikeGerman

                                    Flawed example. The variable name is too long and very likely the logic is flawed since the only need for that would be if the context was so complex (too complex) that it required that.

                                    Mircea NeacsuM Offline
                                    Mircea NeacsuM Offline
                                    Mircea Neacsu
                                    wrote on last edited by
                                    #32

                                    jschell wrote:

                                    Those two are not the same. I read very fast and I can even skip paragraphs and even pages and still understand the flow even when it is text book. If I do that when writing a program or reviewing others code (which I do extensively and in detail) problems will be missed.

                                    Even if I would agree with your point, which I don't, how is camel case making life better? I fail to see the connection. My eyes, which have been used since a young age with the beauty and the rhythm of a nicely typeset page, are just offended by the ugliness of camel case. Snake case is slightly better but if someone, one day invents an invisible link, I would gladly switch to that. Getting back to the issue of programs like books, not that I want to hide behind the authority of others, I think I'm in good company if I remind myself of Knuth's literate programming concept and Hal Abelson's quote: "Programs must be written for people to read, and only incidentally for machines to execute". Anyway, that's at least how I try to practice my trade and, if I have to agonize for the best name for a structure or a method, so be it.

                                    Mircea

                                    J 1 Reply Last reply
                                    0
                                    • raddevusR raddevus

                                      Great point about printers and the challenges of underscores. Thanks for sharing. This is also the reason we used to like Hungarian notation so much back then too. We did "offline" code reviews where we sat around a table and looked at code print-outs. Hungarian made sense because you could be a couple pages down and still know the type without looking back up at the declaration. :thumbsup: Of course, there was no intellisense back then either so... These kids don't know what they got!!! :rolleyes:

                                      T Offline
                                      T Offline
                                      trønderen
                                      wrote on last edited by
                                      #33

                                      raddevus wrote:

                                      Hungarian made sense because you could be a couple pages down and still know the type without looking back up at the declaration.

                                      A couple of pages? The biggest type declaration I have been almost in touch with (I was offered an opportunity to see it, but declined :-)) was about 8300 lines long. 72 lines per page makes a 120 page book for that declaration alone. For the curious ones: The type declaration appeared in the CHILL source code of the System 12 telephone switch, when the very first software version was being prepared. My guess is that it grew considerably further throughout the lifetime of System 12. It defined a message format, with variant fields for different function codes all embedded in a single type declaration, 'to have it all in one place'. The number of codes / variants most likely grew in later releases.

                                      1 Reply Last reply
                                      0
                                      • raddevusR raddevus

                                        So which type of convention do you use when naming variables with multiple words? * camelCase * PascalCase * snake_case // Rust requires this for variable, function and struct member names and the compiler will warn (gripe at you) if you use other. I really like Rust, but this drives me crazy because it is YAC (Yet Another Convention) Back in C++ & C# I always used PascalCase Then in recent years JavaScript has effected my mind and I use a lot of camelCase these days. Kotlin (Android dev) & Swift (iOS dev) seem to use camelCase too. So it becoming kind of a standard. How about you? See more here[^].

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

                                        PascalCase for non-locals, sometimes prefixed by "_" for private/protected values. snake_case for locals. For some reason camelCase has always offended my sensibilities.

                                        Software Zen: delete this;

                                        1 Reply Last reply
                                        0
                                        • Mircea NeacsuM Mircea Neacsu

                                          jschell wrote:

                                          Those two are not the same. I read very fast and I can even skip paragraphs and even pages and still understand the flow even when it is text book. If I do that when writing a program or reviewing others code (which I do extensively and in detail) problems will be missed.

                                          Even if I would agree with your point, which I don't, how is camel case making life better? I fail to see the connection. My eyes, which have been used since a young age with the beauty and the rhythm of a nicely typeset page, are just offended by the ugliness of camel case. Snake case is slightly better but if someone, one day invents an invisible link, I would gladly switch to that. Getting back to the issue of programs like books, not that I want to hide behind the authority of others, I think I'm in good company if I remind myself of Knuth's literate programming concept and Hal Abelson's quote: "Programs must be written for people to read, and only incidentally for machines to execute". Anyway, that's at least how I try to practice my trade and, if I have to agonize for the best name for a structure or a method, so be it.

                                          Mircea

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

                                          Mircea Neacsu wrote:

                                          how is camel case making life better?

                                          You provided an example which I commented on.

                                          Mircea Neacsu wrote:

                                          My eyes...are just offended by the ugliness of camel case

                                          Which is obviously subjective. In terms of maintaining code neither of the following is objectively better. int userCount; int user_count;

                                          Mircea Neacsu wrote:

                                          I remind myself of Knuth's literate programming concept and Hal Abelson's quote: "Programs must be written for people to read, and only incidentally for machines to execute"

                                          Which was exactly my point when I pointed out that I work on them and review them.

                                          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