Naming Conventions

Naming Conventions

The basic naming conventions apply to most if not all languages( I am mainly basing this off C++ as its what I have been working with recently, but from what I remember these also generally apply to Java, C, and C#), although the standard(if there is one) may vary depending on the language. The reason to follow naming conventions is to make your code more readable.

The basic rules for this are:

  • Use meaningful names
    • Try to be short, but still easily readable
      • a good rule of thumb is for it to be at least two words, but no more than 20-30 characters long.
        • also if you use abreviations make sure its a common abreviation
      • ex: call something studentNumber or studentNum not num
  • Use different styles for different kinds of identifiers
    • ex: use camal case for variables, but use pascals case for function names, and snakecase for class names
  • For global constant varibles use all caps with underscors between words
    • global as in declared outside of any function, which means it applies to the whole module
  • BE CONSISTANT
    • choose a convention and stick with it throughout the code, don’t randomly switch what you use for function names and variable names or something

There are 4 or so naming conventions:

  • camelCase:
    • the first letter of everyword but the first word is capitalized
  • PascalCase:
    • the first letter of every word is capitalized including the first word
  • snake_case:
    • no capital letters
    • put a underscore between differwnt words
  • sHungarianNotation:
    • in this notation you start the identifier with an abbreviation that tells you what the datatype or the purpose of the variable is
      • in this case sHungarianNotation would be a string as ‘s’ is a common abreviation of string
        • another example would be arrStudentNames, which would be an array
    • after the abreviation should be a descriptor of the variable
    • this also follows CamalCase, in how its capitalization

One other thing to note is that it is generally better for code to be over commented than under commented, so feel free to comment the purpose of the variable, especially if it’s something very specific.

Some typical cases where people don’t follow naming conventions is when it’s a temporary variable like the index for a loop, in which case people generally use something along the lines of i, or if i is already being used some other random letter like j or k. People may also commonly just name temporary variables temp.

Keep in mind that naming conventions are not enforced by the compiler, they simply are good practice in terms of making sure other programmers can actually read your code when necessary.