CSC/ECE 517 Summer 2008/wiki2 2 ao: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 22: Line 22:
* Contants should have every letter capitalized, with underscores between words.
* Contants should have every letter capitalized, with underscores between words.


=== Final conclusions and tips ===
=== Final conclusions ===
The advantages of using naming conventions outweigh the disadvantages.  Readability is the biggest derived benefit, and its importance cannot be overstated.  Variable types, as well as their purpose (for classes and modules too) can be determined at a quick glance.  Readers unfamiliar with the code can derive meaning and purpose quicker, without having to study large portions of the code and refer to various places within.  The following tips should be used when choosing names for variables, classes, modules, etc.:


* Prefix the variable names with their scope and their data types  
* Prefix the variable names with their scope and their data types  
Line 28: Line 29:
* Be wary of long names  
* Be wary of long names  
* Use your conventions consistently
* Use your conventions consistently


=== ''References'' ===
=== ''References'' ===

Revision as of 01:47, 26 June 2008

Variable, Class, and Module Naming

When programming, choosing appropriate names for variables, classes, modules, etc. can often be taken too lightly. It's always important to choose descriptive names with straightforward meanings. Certain conventions should be followed to ensure code can be read, followed, and edited easily.

Naming conventions advantages

  • to provide additional information (ie, metadata) about the use to which an identifier is put
  • to help formalize expectations and promote consistency within a development team
  • to enable the use of automated refactoring or search and replace tools with minimal potential for error
  • to enhance clarity in cases of potential ambiguity
  • to enhance the aesthetic and professional appearance of work product (for example, by disallowing overly long names, comical or "cute" names, or abbreviations)
  • to help avoid "naming collisions" that might occur when the work product of different organizations is combined
  • to provide meaningful data to be used in project handovers which require submission of program source code and all relevant documentation

Naming conventions disadvantages

  • When a variable type is changed, you may have to go through and change all the instances of that variable within your code. The find-and-replace feature of many editors alleviates this issue a bit, but it may still be tedious.
  • If your language changes, i.e. is updated, code may need to be changed so the naming convention still fits.
  • Naming conventions may defeat the purpose of encapsulation. The "black box" aspect is eliminated when the programmer needs to know what specific type is being returned.

Common conventions

  • Lowercase letters should be used for variable and method names. A name consisting of several words should be concatenated into one word (take out the spaces). The first word should be all lowercase, and each subsequent word should have its first letter capitalized.
  • Class names should have the first letter of each word capitalized. Multi-worded names should follow the rule above, except that the first word should also be capitalized.
  • Contants should have every letter capitalized, with underscores between words.

Final conclusions

The advantages of using naming conventions outweigh the disadvantages. Readability is the biggest derived benefit, and its importance cannot be overstated. Variable types, as well as their purpose (for classes and modules too) can be determined at a quick glance. Readers unfamiliar with the code can derive meaning and purpose quicker, without having to study large portions of the code and refer to various places within. The following tips should be used when choosing names for variables, classes, modules, etc.:

  • Prefix the variable names with their scope and their data types
  • Use meaningful variable names
  • Be wary of long names
  • Use your conventions consistently

References

External Links