




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Guidelines for writing self-documented code through effective naming conventions and coding principles. It covers general and specific naming conventions for variables, constants, methods, and templates in c++. The document also discusses the use of get/set methods, boolean variables, and complement names. Additionally, it advises against using abbreviations, negated boolean variable names, and magic numbers.
Typology: Slides
1 / 117
This page cannot be seen from the preview
Don't miss anything!





























































































Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Simple
No tricks - Clear - Easy to understand - Flexible - Can accommodate changes
Batter Names
Towards Self Documented Code
Names representing methods and functions should be verbs and written in mixed case starting with lowercase - getName(), computeTotalWidth() - Names representing template types in C++ should be a single uppercase letter - template
Private class variables should be distinguished from temporary variables in the class
Private class variables should
suffix
class SomeClass - { - Private int length_; - }
All names should be written in English - Variables with large scopes should have long names, variables with small scope can have short namesScratch 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. - i, j, k, m, n for integers and c, d for characters
The name of the object is implicit and should be avoided in a method name
line.getLength(); // NOT line.getLineLength()
is prefix should be used for boolean variables and methods
isSet, isVisible, isFinished, isFound, isOpen Using the is prefix solves a common problem of choosing bad boolean names like status or flag. isStatus or isFlag simply does not fit and the programmer is forced to chose more meaningful names - boolean hasLicense(); boolean canEvaluate(); - Boolean shouldAbort();
The term compute can be used in methods where something is computed
matrix.computeInverse(); - valueSet.computeAverage(); - The term find can be used in methods where something is looked up - vertex.findNearestVertex(); - matrix.findMinElement();
Complement names must be used for complement entities
get/set, add/remove - create/destroy, start/stop - insert/delete, increment/decrement - old/new, begin/end, first/last - up/down, next/previous - open/close, show/hide
Abbreviations in names should be avoided
computeAverage(); // NOT compAvg() - cmd instead of command - cp instead of copy - pt instead of point - comp instead of compute or compare - init instead of initialize
Functions (methods returning an object) should be named after what they return and procedures (void methods) after what they do.
header files should have the extension .h. Source files can have the extension .c++
.C, .cc or .cpp - MyClass.c++, MyClass.h