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. Question on variable naming style (i.e. r versus random)

Question on variable naming style (i.e. r versus random)

Scheduled Pinned Locked Moved The Lounge
questionlounge
44 Posts 25 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.
  • S Slacker007

    r versus random. sw versus streamWriter, etc... Our shop only allows single character variable names on iterators, and even then, a meaningful iterator name is desired, especially if you have iterators in nested statements. You have to have something meaningful in the name. What's your take on this? My personal opinion is that you should use meaningful names. I should not have to go to the variable declaration to remember that r is random - you get the point.

    A Offline
    A Offline
    Agent__007
    wrote on last edited by
    #6

    I use "meaningful" names for variables everywhere except for the "throw-away" variables in lambda expressions, for which I always use single char names.

    You have just been Sharapova'd.

    S L 2 Replies Last reply
    0
    • S Slacker007

      r versus random. sw versus streamWriter, etc... Our shop only allows single character variable names on iterators, and even then, a meaningful iterator name is desired, especially if you have iterators in nested statements. You have to have something meaningful in the name. What's your take on this? My personal opinion is that you should use meaningful names. I should not have to go to the variable declaration to remember that r is random - you get the point.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #7

      Slacker007 wrote:

      My personal opinion is that you should use meaningful names.

      You use what is appropriate. I do not need a 255-char "meaningfull" variable name if it is only used twice. It makes stuff harder to read. I do not need a "r" variable that is used everywhere. If the scope is limited, limit the name. Large scopes should have longer names, for clarity.

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

      S 1 Reply Last reply
      0
      • A Abhinav S

        Sometimes even a variable name like random may not be enough. What does random stand for - Random temperature value? Random foreign exchange value? Random number? Random forum post number? So use r and consult the documentation anyway.

        Mobile Apps - Sound Meter | Color Analyzer | SMBC | Football Doodles

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

        Excellent point.

        1 Reply Last reply
        0
        • A Agent__007

          I use "meaningful" names for variables everywhere except for the "throw-away" variables in lambda expressions, for which I always use single char names.

          You have just been Sharapova'd.

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

          :thumbsup:

          1 Reply Last reply
          0
          • L Lost User

            Slacker007 wrote:

            My personal opinion is that you should use meaningful names.

            You use what is appropriate. I do not need a 255-char "meaningfull" variable name if it is only used twice. It makes stuff harder to read. I do not need a "r" variable that is used everywhere. If the scope is limited, limit the name. Large scopes should have longer names, for clarity.

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

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

            Good points. Question, do you work by yourself, or in a team (a shop). Will there ever be a scenario where 8 months down the road someone else has to maintain your most excellent code?

            L 1 Reply Last reply
            0
            • N Nagy Vilmos

              The greater the scope, the bigger the name is as good a maxim as you'll get:

              class Stuff
              {
              private int variable;

              void doSomething (int thing)
              {
              Random rnd = new Random();
              for (int i = 0; i< thing;i++)
              {
              int x = rnd.nextBoolean() ? i * 2 : -i;
              this.variable += x;
              }
              }

              }

              God that looks silly :)

              veni bibi saltavi

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

              Just curious as to why you can't actually spell out random instead of using "rnd". Are you really coding that fast and time is so crucial that you can't spell the word out? Just curious, Nagy. :)

              N 1 Reply Last reply
              0
              • S Slacker007

                r versus random. sw versus streamWriter, etc... Our shop only allows single character variable names on iterators, and even then, a meaningful iterator name is desired, especially if you have iterators in nested statements. You have to have something meaningful in the name. What's your take on this? My personal opinion is that you should use meaningful names. I should not have to go to the variable declaration to remember that r is random - you get the point.

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #12

                Context rules in variable names. "Hey You" is perfectly acceptable in some circumstances (in addition to song titles), while your full name is required in others (even "Slacker007" is allowed, in suspicious websites).

                S 1 Reply Last reply
                0
                • D den2k88

                  It really depends. If I have to cycle through 3 levels of hierarchy i, j, k work better than any other name IMHO - even reading the code. Only for matrixes I use r and c to distinguish between rows and columns. n and m may also work... they ARE meaningful, but their meaning usually is very limited in scope so it's pointless to create long names for them.

                  Geek code v 3.12 {      GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- r++>+++ y+++*      Weapons extension: ma- k++ F+2 X } If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver

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

                  Meaningful names don't have to be long. We shun long names where I work.

                  D 1 Reply Last reply
                  0
                  • C CPallini

                    Context rules in variable names. "Hey You" is perfectly acceptable in some circumstances (in addition to song titles), while your full name is required in others (even "Slacker007" is allowed, in suspicious websites).

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

                    :laugh:

                    1 Reply Last reply
                    0
                    • S Slacker007

                      r versus random. sw versus streamWriter, etc... Our shop only allows single character variable names on iterators, and even then, a meaningful iterator name is desired, especially if you have iterators in nested statements. You have to have something meaningful in the name. What's your take on this? My personal opinion is that you should use meaningful names. I should not have to go to the variable declaration to remember that r is random - you get the point.

                      V Offline
                      V Offline
                      V 0
                      wrote on last edited by
                      #15

                      It kind off depends? If you have a nested for loop you could use i and j eg as is often done, but I have seen (and have done :sigh: ) situations that ended up in having i, j, k, l, ... or worse i, ii, iii etc... I use simple one character variables or short abbreviations only when it remains clear. for single or double for loops that is i and j. If it becomes more complicated I use more descriptive naming. For StreamWriter I usually use a short name if it is in a "write" method eg. If I would use it throughout the class or if I would use multipe instances, naming would be more descriptive. shorter names tend to make it, for me at least, more easy to read and understand.

                      V.
                      (MQOTD rules and previous solutions)

                      1 Reply Last reply
                      0
                      • S Slacker007

                        Just curious as to why you can't actually spell out random instead of using "rnd". Are you really coding that fast and time is so crucial that you can't spell the word out? Just curious, Nagy. :)

                        N Offline
                        N Offline
                        Nagy Vilmos
                        wrote on last edited by
                        #16

                        Simply put, I can't be bothered to type the full word.

                        veni bibi saltavi

                        S 1 Reply Last reply
                        0
                        • S Slacker007

                          Good points. Question, do you work by yourself, or in a team (a shop). Will there ever be a scenario where 8 months down the road someone else has to maintain your most excellent code?

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #17

                          I have worked in brownfields for over 15 years. I know that code should be readable. In that time I've seen unreadable code due to insanely long names, and I've seen unreadable code due to one-letter naming. You do not choose either, you use what is readable.

                          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

                          S 1 Reply Last reply
                          0
                          • S Slacker007

                            r versus random. sw versus streamWriter, etc... Our shop only allows single character variable names on iterators, and even then, a meaningful iterator name is desired, especially if you have iterators in nested statements. You have to have something meaningful in the name. What's your take on this? My personal opinion is that you should use meaningful names. I should not have to go to the variable declaration to remember that r is random - you get the point.

                            B Offline
                            B Offline
                            Brady Kelly
                            wrote on last edited by
                            #18

                            My first coding job was on SAP, and I grew to to like the abbreviated table and column names, where most table names were always four characters, and most column names five, e.g. BKPF-BELNR, or the Accounting Document Header table is BKPF and BELNR is Document Number. This way it's easy to remember after a few months, and there is no uncertainty like in using full names for e.g. underscores vs dashes, misspellings, spaces, etc.

                            No object is so beautiful that, under certain conditions, it will not look ugly. - Oscar Wilde

                            S J E 3 Replies Last reply
                            0
                            • S Slacker007

                              r versus random. sw versus streamWriter, etc... Our shop only allows single character variable names on iterators, and even then, a meaningful iterator name is desired, especially if you have iterators in nested statements. You have to have something meaningful in the name. What's your take on this? My personal opinion is that you should use meaningful names. I should not have to go to the variable declaration to remember that r is random - you get the point.

                              A Offline
                              A Offline
                              Amarnath S
                              wrote on last edited by
                              #19

                              Depends on payment.

                              If (paid more, or also paid zero (as in open source)) {
                              Use meaningful names
                              } else {
                              Use cryptic names as a means of self-obfuscation
                              }

                              1 Reply Last reply
                              0
                              • S Slacker007

                                r versus random. sw versus streamWriter, etc... Our shop only allows single character variable names on iterators, and even then, a meaningful iterator name is desired, especially if you have iterators in nested statements. You have to have something meaningful in the name. What's your take on this? My personal opinion is that you should use meaningful names. I should not have to go to the variable declaration to remember that r is random - you get the point.

                                J Offline
                                J Offline
                                Jorgen Andersson
                                wrote on last edited by
                                #20

                                In the very old times when storage was expensive, you had to shorten the variable names to the extreme levels where "user" was shortened to "usr". Today it's just pointless. There's no point to use overly short variable names outside the fact that you are used to it. You should of course use a name as short as possible, what random stands for would in most cases be obvious from the context of the scope, and only in the few cases where it isn't, or where you need more than one random variable you would need to specify RandomTemp or RandomPostCode. <edit>I should probably add that I find it ok to use abbreviations like sw for StreamWriter when they are standardized, but as soon as there is room for ambiguity I spell them out.</edit>

                                Wrong is evil and must be defeated. - Jeff Ello

                                H 1 Reply Last reply
                                0
                                • B Brady Kelly

                                  My first coding job was on SAP, and I grew to to like the abbreviated table and column names, where most table names were always four characters, and most column names five, e.g. BKPF-BELNR, or the Accounting Document Header table is BKPF and BELNR is Document Number. This way it's easy to remember after a few months, and there is no uncertainty like in using full names for e.g. underscores vs dashes, misspellings, spaces, etc.

                                  No object is so beautiful that, under certain conditions, it will not look ugly. - Oscar Wilde

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

                                  No thank you. :confused:

                                  1 Reply Last reply
                                  0
                                  • S Slacker007

                                    r versus random. sw versus streamWriter, etc... Our shop only allows single character variable names on iterators, and even then, a meaningful iterator name is desired, especially if you have iterators in nested statements. You have to have something meaningful in the name. What's your take on this? My personal opinion is that you should use meaningful names. I should not have to go to the variable declaration to remember that r is random - you get the point.

                                    G Offline
                                    G Offline
                                    glennPattonWork3
                                    wrote on last edited by
                                    #22

                                    TRhank got me thinking the first time I had to code a pointer I used the name pointy (which was released), I tend to use i, j, k for loops and short names like Cars[] for array of Car types (don't ask!) I tend to sw for Stream Writer, sr for Stream Reader. The only place I have had to follow a strict coding style was when I was at uni...

                                    1 Reply Last reply
                                    0
                                    • L Lost User

                                      I have worked in brownfields for over 15 years. I know that code should be readable. In that time I've seen unreadable code due to insanely long names, and I've seen unreadable code due to one-letter naming. You do not choose either, you use what is readable.

                                      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

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

                                      Eddy Vluggen wrote:

                                      you use what is readable.

                                      I agree. I, and our team, use what is readable. :thumbsup:

                                      1 Reply Last reply
                                      0
                                      • N Nagy Vilmos

                                        Simply put, I can't be bothered to type the full word.

                                        veni bibi saltavi

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

                                        Nagy Vilmos wrote:

                                        I can't be bothered to type the full word.

                                        :-D Fair enough.

                                        G 1 Reply Last reply
                                        0
                                        • S Slacker007

                                          Meaningful names don't have to be long. We shun long names where I work.

                                          D Offline
                                          D Offline
                                          den2k88
                                          wrote on last edited by
                                          #25

                                          well, "random" is long, longer than "rand" or "rnd" for example. So elementIterator or eleIter or elIt? In a couple of month I'd forget what "elIt" may be, but the complete form is way too long. It really depends and there is nothing exact...

                                          Geek code v 3.12 {      GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- r++>+++ y+++*      Weapons extension: ma- k++ F+2 X } If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver

                                          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