Code Style
Code Style
INTRODUCTION
• Right: • Wrong:
int main() int main()
{ {
return 0; return 0;
} }
INDENTATION
if (hours < 24 && minutes < 60 if (hours < 24 && minutes < 60 &&
seconds < 60)
&& seconds < 60) {
{
return true;
return true;
} else { }
return false; else
} {
return false;
}
INDENTATION
• Right: • Wrong:
i++; i ++;
Do place spaces around binary and ternary
operators.
• Right: • Wrong:
y = m * x + b; y=m*x+b;
f(a, b); f(a,b);
c = a | b; c = a|b;
return condition ? 1 : 0; return condition ? 1:0;
LINE BREAKING
Each statement should get its own line.
• Right: • Wrong:
x++; x++; y++;
y++; if (condition) doIt();
if (condition)
doIt();
BRACES
Function definitions: place each brace on its own line.
• Right: • Wrong:
int main() int main() {
{ ...
... }
}