13
Coding Practices

Coding Practices. Why do we care? Good code is more than just functionality Other people will read your code You will forget what you code does Debugging

Embed Size (px)

Citation preview

Page 1: Coding Practices. Why do we care? Good code is more than just functionality Other people will read your code You will forget what you code does Debugging

Coding Practices

Page 2: Coding Practices. Why do we care? Good code is more than just functionality Other people will read your code You will forget what you code does Debugging

Why do we care?

• Good code is more than just functionality

• Other people will read your code

• You will forget what you code does

• Debugging bad code is time consuming and frustrating

Page 3: Coding Practices. Why do we care? Good code is more than just functionality Other people will read your code You will forget what you code does Debugging

Basic Coding Practices

• Use meaningful names

• Don’t duplicate code

• Don’t reinvent the wheel

• Use comments effectively

• Test early and often

• Code maintenance

Page 4: Coding Practices. Why do we care? Good code is more than just functionality Other people will read your code You will forget what you code does Debugging

Names

• Give functions and variables meaningful names

• Bad names include ‘temp’, ‘integer1’, ‘l’

int l = 6;if (1 == 6) { print(“true”);}else { print(“false”);}

What is the output from this piece of code?

Page 5: Coding Practices. Why do we care? Good code is more than just functionality Other people will read your code You will forget what you code does Debugging

Names

• Give functions and variables meaningful names

• Bad names include ‘temp’, ‘integer1’, ‘l’

int l = 6;if (1 == 6) { print(“true”);}else { print(“false”);}

What is the output from this piece of code?

Output is “false”. The variable in line 1 is a lower case L. In some fonts this is confused with the character for the number one, as found on the second line.

Page 6: Coding Practices. Why do we care? Good code is more than just functionality Other people will read your code You will forget what you code does Debugging

Names

int func1(int a, int b)

{

if (a < 24 && b < 60)

return 1;

else

return 0;

}

• Bad names make code hard to read and understand

What does this code do?

Page 7: Coding Practices. Why do we care? Good code is more than just functionality Other people will read your code You will forget what you code does Debugging

Names

int func1(int a, int b)

{

if (a < 24 && b < 60)

return 1;

else

return 0;

}

• Bad names make code hard to read and understand

int validateTime(int hours, int minutes)

{

if (hours < 24 && minutes < 60)

return 1;

else

return 0;

}

What does this code do? How about this code?

Page 8: Coding Practices. Why do we care? Good code is more than just functionality Other people will read your code You will forget what you code does Debugging

Code Duplication

• Duplicated code adds confusion

• Duplicated code leads to more bugs

• Replace duplicated code with functions

Page 9: Coding Practices. Why do we care? Good code is more than just functionality Other people will read your code You will forget what you code does Debugging

Reinventing the Wheel

• The “I can do it better trap”

• Read and understand support code, especially so called “helper” functions

• Explore the standard librariese.g. www.cppreference.com

Page 10: Coding Practices. Why do we care? Good code is more than just functionality Other people will read your code You will forget what you code does Debugging

Comments

• Comments help the reader understand the code

• Bad comments include:– repeating what the code does– lengthy explanations of badly written code

Page 11: Coding Practices. Why do we care? Good code is more than just functionality Other people will read your code You will forget what you code does Debugging

Comments

• Good comments include:– summarizing blocks of code – describing the programmers intent– identifying logical divisions in the code– identifying unconventional methods

Page 12: Coding Practices. Why do we care? Good code is more than just functionality Other people will read your code You will forget what you code does Debugging

Testing

• Test early and often – don’t write everything and the start testing

• Identify separate components that can be tested individually – Code in Blocks

• If necessary write your own test cases, but start small

• Test user inputs (and function inputs)

Page 13: Coding Practices. Why do we care? Good code is more than just functionality Other people will read your code You will forget what you code does Debugging

Code Maintenance

• Periodically backup your work

• Every time you get a particular feature to work, save a copy of your files

• More advanced source control – RCS or CVS