Docsity

Naming Conventions and Coding Principles for Documented Code, Slides of Object Oriented Analysis and Design

Partial preview of the text

Download Naming Conventions and Coding Principles for Documented Code and more Slides Object Oriented Analysis and Design in PDF only on Docsity!

Coding

Styles

Fowler

Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

Style

Principles

Simple

No tricks - Clear - Easy to understand - Flexible - Can accommodate changes

Batter Names

Towards Self Documented Code

General

Naming

Conventions

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 … - Global variables in C++ should always be referred by using :: operator - ::mainWindow.open()

General

Naming

Conventions

Private class variables should be distinguished from temporary variables in the class

Private class variables should

_

suffix

class SomeClass - { - Private int length_; - }

General

Naming

Conventions

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

General

Naming

Conventions

The name of the object is implicit and should be avoided in a method name

line.getLength(); // NOT line.getLineLength()

Specific

Naming

Conventions

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();

Specific

Naming

Conventions

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();

Specific

Naming

Conventions

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

Specific

Naming

Conventions

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

Specific

Naming

Conventions

Functions (methods returning an object) should be named after what they return and procedures (void methods) after what they do.

File

Handling

Tips

C++

header files should have the extension .h. Source files can have the extension .c++

.C, .cc or .cpp - MyClass.c++, MyClass.h