User Tools

Site Tools


mgenay:technique:cppnamingconvention
  1. Names representing types must be in mixed case starting with upper case.
    Line, SavingsAccount
  2. Variable names must be in mixed case starting with lower case.
     line, savingsAccount 
  3. Named constants (including enumeration values) must be all uppercase using underscore to separate words.
     MAX_ITERATIONS, COLOR_RED, PI 
  4. Names representing methods or functions must be verbs and written in mixed case starting with lower case.
     getName(), computeTotalWidth() 
  5. Names representing namespaces should be all lowercase.
     model::analyzer, io::iomanager, common::math::geometry 
  6. Names representing template types should be a single uppercase letter.
     template<class T> ... template<class C, class D> ... 
  7. Abbreviations and acronyms must not be uppercase when used as name [4].
     exportHtmlSource(); // NOT: exportHTMLSource(); openDvdPlayer(); // NOT: openDVDPlayer(); 


    Using all uppercase for the base name will give conflicts with the naming conventions given above. A variable of this type whould have to be named dVD, hTML etc. which obviously is not very readable.
    Another problem is illustrated in the examples above; When the name is connected to another, the readbility is seriously reduced; the word following the abbreviation does not stand out as it should.

  8. Global variables should always be referred to using the :: operator.
     
     ::mainWindow.open(), ::applicationContext.getName() 
    In general, the use of global variables should be avoided. Consider using singleton objects instead.
  9. Private class variables should have underscore suffix.
     class SomeClass { private: int length_; } 
  10. Generic variables should have the same name as their type.
     void setTopic(Topic* topic) // NOT: void setTopic(Topic* value) // NOT: void setTopic(Topic* aTopic) // NOT: void setTopic(Topic* t) void connect(Database* database) // NOT: void connect(Database* db) // NOT: void connect (Database* oracleDB) 
    Non-generic variables have a role. These variables can often be named by combining role and type: 
     Point  startingPoint, centerPoint;
     Name   loginName;
  11. All names should be written in English.
     fileName; // NOT: filNavn 
  12. Variables with a large scope should have long names, variables with a small scope can have short names [1].
    Scratch variables used for temporary storage or indices are best kept short. A programmer reading such variables should be able to assume that its value is not used outside a few lines of code. Common scratch variables for integers are i, j, k, m, n and for characters c and d.
  13. The name of the object is implicit, and should be avoided in a method name.
     line.getLength(); // NOT: line.getLineLength(); 
  14. The terms get/set must be used where an attribute is accessed directly.
     employee.getName(); employee.setName(name); matrix.getElement(2, 4);
      - The term compute can be used in methods where something is computed.<code>
     valueSet->computeAverage(); matrix->computeInverse() 
    this is a potential time consuming operation, and if used repeatedly, consider caching the result.
      - The term find can be used in methods where something is looked up.<code>
     vertex.findNearestVertex(); matrix.findMinElement(); 
    this is a simple look up method with a minimum of computations involved. 
  15. The term initialize can be used where an object or a concept is established.
     printer.initializeFontSet(); 
  16. Variables representing GUI components should be suffixed by the component type name.
     mainWindow, propertiesDialog, widthScale, loginText, leftScrollbar, mainForm, fileMenu, minLabel, exitButton, yesToggle etc. 
  17. Plural form should be used on names representing a collection of objects.
     vector<Point> points; int values[]; 
  18. The prefix n should be used for variables representing a number of objects.
     nPoints, nLines 
      - The suffix No should be used for variables representing an entity number.<code>
     tableNo, employeeNo 
    An elegant alternative is to prefix such variables with an i: iTable, iEmployee. This effectively makes them named iterators. 
  19. Iterator variables should be called i, j, k etc.
     for (int i = 0; i < nTables); i++) { : } for (vector<MyClass>::iterator i = list.begin(); i != list.end(); i++) { Element element = *i; ... } 
  20. The prefix is should be used for boolean variables and methods.
     isSet, isVisible, isFinished, isFound, isOpen 
    There are a few alternatives to the is prefix that fits better in some situations. These are the has, can and should prefixes: 
      bool hasLicense();
      bool canEvaluate();
      bool shouldSort();
  21. Complement names must be used for complement operations [1].
     get/set, add/remove, create/destroy, start/stop, insert/delete, increment/decrement, old/new, begin/end, first/last, up/down, min/max, next/previous, old/new, open/close, show/hide, suspend/resume, etc. 
  22. Abbreviations in names should be avoided.
     computeAverage(); // NOT: compAvg(); 
  23. Naming pointers specifically should be avoided.
     Line* line; // NOT: Line* pLine; // NOT: LIne* linePtr; 
  24. Negated boolean variable names must be avoided.
     bool isError; // NOT: isNoError bool isFound; // NOT: isNotFound 
  25. Enumeration constants can be prefixed by a common type name.
     enum Color { COLOR_RED, COLOR_GREEN, COLOR_BLUE }; 
    An alternative approach is to always refer to the constants through their common type: Color::RED. 
  26. Exception classes should be suffixed with Exception.
     class AccessException { : } 
  27. Functions (methods returning something) should be named after what they return and procedures (void methods) after what they do.'''
mgenay/technique/cppnamingconvention.txt · Last modified: by 127.0.0.1