Friday, August 17, 2012

Naming in Programming and Common Coding Practices

When I started coding in C, I wrote a code lot with a name like p1, A(), fact(), i, j, obj etc which means nothing to others. For small and simple programs (like Primer Number), it's not a problem with this type of naming. However, when codes are long, and many functions, classes, namespaces used in coding, then this type of naming makes us ambiguous. If we use ac instead of accountNumber then ac doesn't make any sense. 

Therefore, it is very important to use a meaningful name and follow some conventions to write these names. Naming convention, not a hard and fast rule but if you follow conventions in naming, then your code will be more readable and understandable. 

You may use the following naming conventions:
  • camelCase (First letter of first word is Lower-Case and first letter of each subsequent word is Upper-Case).
          Variable: counter, firstName, accountNumber, noOfStudents etc.
         Object: studentObject, saveButton etc.
  • PascalCase (First letter of each word is Upper-Case)
          Function: ProcessPayment(), Addition(), CalculateSalary()
         Class: Account, Student, Salary
         UI: CalculatorUI
         Sturture and Union: Student
         Enum: EmployeeType, DaysOfWeek
  • UPPER_CASE (each letter is in Upper-Case form and underscore is used for separating each word)
          Constant: PI, INTEREST_RATE

Common Coding Practices:

  • Names(variable, function, class etc) must be meaningful with conventions.
  • Code Commenting: Commenting your code for better understanding. To know more about code commenting Click here.
  • Code indentation: Indentation makes the long and complex program - simple and easily readable. To know more about code indentation Click here.
  • Code Clarity: Organize your code into sections and paragraphs.
  • Simplicity: Avoid complex logic and make your program simple and easy to understand as possible.
  • Finally, don't try to be clever. Clever makes your program unreadable and unmaintainable.