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

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

2011/2012

Uploaded on 07/17/2012

pankaja
pankaja 🇮🇳

5

(4)

56 documents

1 / 117

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CodingStyles
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

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