Title: COMP102 Lab 4
1COMP102 Lab 4
2Coding Style
- Compilation will succeed if your code does not
contain syntax errors - However, error-free code doesnt imply that it is
good enough - To assist you (and your partners) to understand,
write and maintain the code, there are a few
coding guideline in addition to syntax rules - It also helps to eliminate careless mistakes
3Coding Style (Cont)
- Start the program with a header that tells what
the program does - Use meaningful variable names
- Document each variable declaration with a comment
telling what the variable is used for - Place each executable statement on a single line
- A segment of code is a sequence of executable
statements that belong together - Use blank lines to separate different segments of
code - Document each segment of code with a comment
telling what the segment does - Sub-block with indentation
4A Good Example
/ this application is to judge whether a
person is fit or not. / include
ltiostream.hgt void main() int weight // the
weight of the person int height // the height
of the person int gender // the gender of the
person // ask user to input his/her
weight cout ltlt "pls input your weight (kg)
" cin gtgt weight // ask user to input his/her
height cout ltlt "pls input your height (cm)
" cin gtgt height // some more code...
5A Good Example (Cont)
// more... if ( 0 gender ) constant
110 else if ( 1 gender ) constant
105 else cout ltlt "wrong input!" return
// more...