Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Coding Practices

2 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

3 Basic Coding Practices Use meaningful names Don’t duplicate code Don’t reinvent the wheel Use comments effectively Test early and often Code maintenance

4 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?

5 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.

6 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?

7 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?

8 Code Duplication Duplicated code adds confusion Duplicated code leads to more bugs Replace duplicated code with functions

9 Reinventing the Wheel The “I can do it better trap” Read and understand support code, especially so called “helper” functions Explore the standard libraries e.g. www.cppreference.comwww.cppreference.com

10 Comments Comments help the reader understand the code Bad comments include: –repeating what the code does –lengthy explanations of badly written code

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

12 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)

13 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


Download ppt "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."

Similar presentations


Ads by Google