Writing Quality Code
CS 240 – Advanced Programming Concepts
Writing Quality Code Introduction
Why Quality Code Matters
3
What’s Wrong With This Function?
void handleStuff( CORP_DATA inputRec, int crntQtr,
EMP_DATA empRec, float estimRevenue, float ytdRevenue,
int screenX, int screenY, COLOR_TYPE newColor,
COLOR_TYPE prevColor, STATUS_TYPE status,
int expenseType )
{
for ( int i = 1; i <= 100; ++i ) {
inputRec.revenue[ i ] = 0;
inputRec.expense[ i ] = corpExpense[ crntQtr, i ];
}
UpdateCorpDatabase( EmpRec );
estimRevenue = ytdRevenue * 4.0 / (float)crntQtr ;
newColor = prevColor;
status = Success;
if ( expenseType == 1 ) {
for ( int i = 1; i <= 12; ++i )
profit[ i ] = revenue[ i ] - expense.type1[ i ];
}
else if ( expenseType == 2 ) {
profit[ i ] = revenue[ i ] - expense.type2[ i ];
}
else if ( expenseType == 3 )
{
profit[ i ] = revenue[ i ] - expense.type3[ i ];
}
}
4
Quality Code Example
5
BLANK SLIDE
Cohesion and Method Naming
Strong Cohesion
8
Good Method Names
9
Good Method Names (cont.)
10
BLANK SLIDE
Three Reasons for Creating Methods
Reasons for Creating Methods
13
1: Algorithm Decomposition
14
Comments
15
2: Avoid Code Duplication
16
3: Deep Nesting
17
BLANK SLIDE
Method Parameters
Parameters
20
BLANK SLIDE
Initializing Data
Guidelines for Initializing Data
23
BLANK SLIDE
Code Layout
Code Layout
26
Whitespace
27
Expressions
28
while (startPath+pos<=length(pathName)&&
pathName[startPath+pos]!=';') {
…
}
while (((startPath + pos) <= length(pathName)) &&
pathName[startPath + pos] != ';') {
…
}
Expressions – Separate Conditions
29
if (('0' <= inChar && inChar <= '9') || ('a' <= inChar &&
inChar <= 'z') || ('A' <= inChar && inChar <= 'Z')) {
…
}
if (('0' <= inChar && inChar <= '9') ||
('a' <= inChar && inChar <= 'z') ||
('A' <= inChar && inChar <= 'Z')) {
…
}
Expressions - Submethods
boolean isDigit = ('0' <= inChar && inChar <= '9');
boolean isLowerAlpha = ('a' <= inChar && inChar <= 'z');
boolean isUpperAlpha = ('A' <= inChar && inChar <= 'Z');
if (isDigit || isLowerAlpha || isUpperAlpha) {
…
}
…
}
boolean isAlphaNumeric(char c) {
return (isDigit(c) || isLowerAlpha(c) || isUpperAlpha(c));
}
30
Placing Curly Braces
for (int i=0; i < MAX; ++i) {
values[i] = 0;
}
for (int i=0; i < MAX; ++i)
{
values[i] = 0;
}
for (int i=0; i < MAX; ++i)
{ values[i] = 0;
}
for (int i=0; i < MAX; ++i)
{
values[i] = 0;
}
31
Placing Curly Braces (cont.)
for (int i=0; i < MAX; ++i)
values[i] = 0;
32
Method parameters
33
WebCrawler.crawl(rootURL,outputDir,stopWordsFile);
WebCrawler.crawl( rootURL, outputDir, stopWordsFile );
WebCrawler.crawl(rootURL, outputDir, stopWordsFile);
One Statement Per Line
34
int * p, q; // oops!
int * p; // correct
int * q;
x = 0; y = 0;
x = 0;
y = 0;
Wrapping Long Lines
35
Wrapping Long Lines (cont.)
private boolean isNthDayOfWeekInMonth(Calendar date, int n,
int dayOfWeek, int month) {
...
}
target = addDependenciesToTarget(dependencyGraph, targetName,
dependencyList);
DailySchedule newDailySchedule =
new DailySchedule(getNextSchedulableDay(today));
return (date.get(Calendar.DAY_OF_WEEK) == dayOfWeek &&
date.get(Calendar.MONTH) == month &&
date.get(Calendar.DAY_OF_WEEK_IN_MONTH) == n);
36
BLANK SLIDE
Pseudo-Code
Pseudo-Code
39
Example of Good Pseudo-Code
40
Keep track of current number of resources in use
If another resource is available
Allocate a dialog box structure
If a dialog box structure could be allocated
Note that one more resource is in use
Initialize the resource
Store the resource number at the location
provided by the caller
EndIf
EndIf
Return TRUE if a new resource was created
else return FALSE
Example of Bad Pseudo-Code
41
increment resource number by 1
allocate a dlg struct using malloc
if malloc() returns NULL then return 1
invoke OSrsrc_init to initialize a resource
for the operating system
*hRsrcPtr = resource number
return 0
BLANK SLIDE
Variable Names
Choose Good Variable Names
44
Naming Conventions
45
Creating Readable Names
46
Creating Readable Names (cont.)
47
Abbreviation Guidelines
When you abbreviate:
48
BLANK SLIDE