tags:

views:

339

answers:

6
+1  Q: 

Bad Code example

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.

+2  A: 

Way back then I used to compare strings using "==" instead of equals() method.

Zaki
I used to use `==` instead of `===` in JavaScript :)
Nick Craver
@Zaki, for what language was that? I can safely use it with .Net.
Dykam
@Dykam, for Java.
Zaki
@Dykam, you cant use it 'safely' even in .net. you need to use equals() for strings and other objects too as "==" compares only the reference variables that reference the object but not the object itself.
Zaki
@Dykam - for example, you should use `string.Equals(string1, string2, comparer)`
Nick Craver
You both are wrong: http://csharp.pastebin.com/3EqPY9fL The comparison operator is overloaded for strings. Cast both sides to an object if reference comparison is wanted.
Dykam
More specific: == is perfectly legal for a char-by-char invariant comparison.
Dykam
+3  A: 

I used to do this way back when:

setTimeout("getThing(5)", 100);

Which should always be:

setTimeout(function() { getThing(5); }, 100);
Nick Craver
+2  A: 

Doing several separate SQL queries instead of using JOIN

Benjamin Ortuzar
It's funny to me that we're back around to this if you write the linq incorrectly :)
Nick Craver
+1  A: 

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

`

Giuseppe Guerrini
+1  A: 

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..

Nir Levy
That's good practice though isn't it for cohesion reasons? Each function should have one purpose. Oh I see what you mean actually. You might as well just inline the ternary operator. Still I think the version with Min is more readable not less.
Martin Smith
Good compilers inline this anyway.
Dykam
Perhaps the point here is that some programmers tend to reinvent things that the standard headers and library already implement.
Giuseppe Guerrini
edited answer to clarify my point
Nir Levy
+1  A: 

Doing big blocks of code such as below :

public void method(String str) {

   if (str != null) {
   ....
   ....
   ....
   ....
   ....
   ....
   ....
   ....
   }
} 
fastcodejava
but how big? thousands of lines of code?
C Johnson