Coding%20Practices - PowerPoint PPT Presentation

About This Presentation
Title:

Coding%20Practices

Description:

What is the output from this piece of code? Names. Give functions and variables meaningful names ... What is the output from this piece of code? Output is 'false' ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 14
Provided by: chr1192
Learn more at: http://www.cs.tufts.edu
Category:

less

Transcript and Presenter's Notes

Title: Coding%20Practices


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
  • Dont duplicate code
  • Dont 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
  • Bad names make code hard to read and understand

What does this code do?
  • int func1(int a, int b)
  • if (a lt 24 b lt 60)
  • return 1
  • else
  • return 0

7
Names
  • Bad names make code hard to read and understand

How about this code?
What does this code do?
  • int func1(int a, int b)
  • if (a lt 24 b lt 60)
  • return 1
  • else
  • return 0

int validateTime(int hours, int minutes) if
(hours lt 24 minutes lt 60) return
1 else return 0
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.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 dont 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
Write a Comment
User Comments (0)
About PowerShow.com