i am sure all of us must have written some bad code at some point in their life. I am a kind of guy who would love to learn from other people mistakes. So can you give some examples of bad code you have written and the way you corrected it.
Way back then I used to compare strings using "==" instead of equals() method.
I used to do this way back when:
setTimeout("getThing(5)", 100);
Which should always be:
setTimeout(function() { getThing(5); }, 100);
Here probably the worst thing I heave ever done:
/* File main.c */
#include <stdio.h>
/* ... other includes... */
/* AND NOW THE FUNNY PART! */
#include "mylybrary1.c"
#include "mylibrary2.c"
#include "part_1_of_my_program.c"
#include "part_2_of_my_program.c"
int main(argc, argv) /*...*/
So: no modularization, (only ONE object generated), only one big global namespace (indeed I didn't know what was the matter about "static" functions)... Of course, I have done many other horrible things in about 25 years of C programming, but I thing this is the worst one. Please forgive me: I was an absolute beginner, so young... :D
`
One of the most annoying things is having obvious comments:
// if not found then return nothing
if ( !found ) {
return;
}
Also, writing too many functions to perform trivial actions is a habit some programmers tend to do. It makes reading code very hard and usually have some impact on performance.
function min(x,y) {
return x<y?x:y
}
...
z=min(x,y);
EDIT: I'll try to explain myself better when I say "too many functions to perform trivial tasks". I had this developer hat had every tiny unit in a different function. This may be nice if there is re-use for function (maybe) but consider this case:
function fetch(i) {
int j=get_it(i);
if(j>1) {
do_something_important(i);
} else {
do_something_important(j);
}
}
function go(i) {
calculate_something(i);
sleep(i);
think_about_something(i);
}
function go_fetch(id) {
int i=id;
i++;
go(i);
fetch(i):
return i;
}
function main() {
int i=0;
i=get_i();
go_fetch(i);
}
Now, if each of these functions was on a different file then it would be even harder to read. For simple, non-repeatable logics you should keep your code linear:
function main() {
int i=0,j;
i=get_i();
i++
calculate_something(i);
sleep(i);
think_about_something(i);
j=get_it(i);
if(j>1) {
do_something_important(i);
} else {
do_something_important(j);
}
}
Must more readable IMHO and much less error prone..
Doing big blocks of code such as below :
public void method(String str) {
if (str != null) {
....
....
....
....
....
....
....
....
}
}