|
|
|
|
|
by Mawr
1537 days ago
|
|
1. Missing spaces around operators like "==". The first code example has them, but the author proceeds to remove them with no mention as to why. 2. More than one statement on a line: int f(int x){
int v;
if(b(x)) v=f1(x);
else v=f2(x);
return v;
}
This is so much harder to read than: int f(int x){
int v;
if(b(x)) {
v=f1(x);
}else{
v=f2(x);
}
return v;
}
3. Control flow statements placed arbitrarily far down a line: int f(int x){
if(b(x)) return f1(x);
return f2(x);
}
Always keep them at the start of lines: int f(int x){
if(b(x))
return f1(x);
return f2(x);
}
4. Nested ternary operators, yikes: int a(int i, int j){
return i?a(i-1, j?a(i, j-1):1):j+1;
}
The language I'm testing is missing the ?: conditional expression, so I had to reword function a using ordinary if statements.
int a(int i, int j){
if(i==0) return j+1;
if(j==0) return a(i-1, 1);
return a(i-1, a(i, j-1));
}
Much better.
It's scary that the author only rewrote this due to a language limitation. The original is painful to read. |
|