World's Worst Programming Practices

Status
Not open for further replies.

dockeen

Ars Tribunus Militum
1,600
Restilian

I have, mostly many years ago, had some similar problems with
loops in BASIC. Including such relatively innocent looking
stuff as

for x = 0.0 to 10.0 step 0.1
sum = sum + f(x)
next x

"Modern" BASIC is better, but I learned to watch having
a float as your loop control variable. Its also much
slower than having an integer loop control variable and
creating your working variable from it.

I don't know how many times I was burned in
programming before I learned never to look for equality to
zero. Or anything else :)

Wayne
 


I once was overjoyed to come across this little gem:


Code:
strncpy(destination, source, strlen(source));


The code was written by an MS employee, unhappily enough.


<hr>


Hungarian notation is pure shit. It's fucking demented. <tt>r</tt> for <tt>float</tt> because <tt>r</tt> means real. That C calls them <tt>float</tt> and <tt>double</tt>/<tt>long double</tt>, and even goes so far as to use <tt>f</tt> on the end of numbers to force them to be <tt>float</tt>s is apparently not a sufficiently good reason to use <tt>f</tt> for <tt>float</tt>. <tt>f</tt> is for flags. Unless it's a single flag, in which case it's <tt>b</tt>, for <tt>BOOL</tt> or <tt>bool</tt>. Except when you use <tt>b</tt> to mean byte. Using <tt>r</tt> for <tt>float</tt> then means that you can't use <tt>r</tt> for references, even though it would make much more sense. Then we have pointers. Whilst <tt>l</tt> would have been a useful qualification a decade ago, it's nowadays completely redundant, as there are no near, far, or huge pointers left. They're all <tt>l</tt>, so no useful information is imparted by the <tt>l</tt>, but still it's there.




Then we have wonderful things like <tt>w</tt>, for <tt>WORD</tt>. That <tt>WORD</tt> is an ill-defined concept at best (platforms all seem to disagree about how long a word actually is). Windows says it's 16 bits. Which is great, until we look at, say, WndProcs, which have <tt>wParam</tt>s which are, of course, 32-bits wide. Yes, they're <tt>DWORD</tt>s. Then there's the whole mess of <tt>DWORD</tt>s on Win64; there are a bunch of new <tt>DWORD</tt> types, including <tt>DWORD64</tt>s, which are 64 bits long no matter what, despite <tt>QWORD</tt>s not being lengthened to 128 bits.




<tt>c</tt>. What's it mean? It can mean count, usually in bytes, but sometimes something else such as <tt>TCHAR</tt>s. It can mean <tt>const</tt>. It can mean <tt>char</tt>. <tt>lpc</tt> being distinct from <tt>sz</tt> (or <tt>str</tt>) and <tt>wsz</tt> (or <tt>wstr</tt>), because <tt>sz</tt> means an ASCIIZ (and <tt>wsz</tt> UNICODEZ) string, rather than a simple <tt>char</tt> or <tt>wchar_t</tt> pointer.


If you want an <tt>int</tt>-type type, you might use <tt>n</tt>, unless you use <tt>i</tt>, and you may use <tt>u</tt> or <tt>s</tt> as <tt>unsigned</tt>/<tt>signed</tt> qualifiers, unless you use <tt>dw</tt> to mean <tt>DWORD</tt> which is <tt>typedef</tt>ed to <tt>unsigned long</tt> in any case, in which case you probably use <tt>l</tt> to mean <tt>long</tt>, which is <tt>signed</tt>.


Hungarian notation is truly hateful.


<hr>


There are many other hateful things. For instance, I'm currently fixing an application that has a number of classes with save methods. Except it doesn't. They all save their results to a database. Some are called <tt>DBSave()</tt>. Some just <tt>Save()</tt>. Others <tt>SaveToDB()</tt>. All notionally equivalent, all different. They all take similar or the same parameters, but they do not all take them in the same order. So one has to learn each class separately and think each time, "which save method does this class have?".




Visual Basic has a charming feature that lets you do this:


Code:
Dim MyType as MyType


which allows you to declare a variable of type <tt>MyType</tt> called <tt>MyType</tt>. This I find quite offensive. I don't mind in a language like C++, which is case-sensitive, where one might have classes with Initial Caps and instances of classes with lower-case initial letters. It keeps them separate from each other. But VB lets you mix the two namespaces for some unfathomable reason.

 

stickboy

Ars Tribunus Angusticlavius
6,218
Subscriptor
Originally posted by limbs:
You should _never_ cast the return of malloc in C - it can hide errors.

From K&R, section 7.8:
The pointer returned by malloc or calloc has the proper aliognment for the object in question, but it must be cast into the appropriate type, as in

int *ip;
ip = (int *) calloc(n, sizeof(int));

However, it later goes on to say, in section A6:
The ANSI standard specifically blesses the meeting of void * pointers with object pointers in assignments and relationals, while requiring explicit casts for other pointer mixtures.

Regardless, I don't see how it could hide errors. The cast is either explicit or implicit, but either way there's a cast. The only error check you need, as stated above, is to check it against NULL.
 

stickboy

Ars Tribunus Angusticlavius
6,218
Subscriptor
Oh, ok. I stand corrected. Incidentally, I looked it up in the C FAQ (print version), and it says:

Q: Why does some code carefully cast the values returned by malloc to the pointer type being allocated?

A: Before ANSI/ISO Standard C introduced the void * generic pointer type, these casts were typically required to silence warnings (and perhaps induce conversions) when assigning between incompatible pointer types.

Under ANSI/ISO Standard C, these casts are no longer necessary. Indeed, it can also be argued that they are now to be discouraged, since they can camouflage important warnings that would otherwise be generated if malloc happened not to be declared correctly....

On the other hand, some programmers prefer to make every conversion explicit, to record taht they have considered each case and decided exactly what should happen....

(By the way, the language in sections 6.5 and 7.8.5 of K&R2 suggesting that the casts are required is "overenthusiastic.")
 
Speaking of wierd comments, another programmer here likes to insert "spoink" and "spam" comments for some unknown reason, e.g.

draw_graph(game->overlay.currentGraph);//spam

or

update_batsman_per_wicket();//spoink

He also has way too much time on his hands:


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////// ___ //////////////////////// ___ ////////////////////////////////////////////////////////////////////////////////////
///////////////////////// \//_____/// _ //_____/// \/////////M/////M/EEEEEE/N/////N/U/////U///FFFF/U////U/N///N///CCC///SSS/////////////
////////////////////////| // | /// / \ // | //| /////////MM///MM/E//////NN////N/U/////U///F////U////U/NN//N//C///C/S////////////////
////////////////////////\___ // | ///| |// | //\___ ///spam//M/M/M/M/E//////N/N///N/U/////U///FF///U////U/N/N/N/C///////SSS/////////////
//////////////////////// \// | ///|___|// | // \/////////M//M//M/EEE////N//N//N/U/////U///F/////U//U//N//NN//C///C/////S////////////
//////////////////////// |// | ///| |// | // |/////////M/////M/E//////N///N/N/U/////U///F//////UU///N///N///CCC///SSS/////////////
////////////////////////\___/// | ///| |// | //\___//////////M/////M/E//////N////NN//U///U//////////////////////////////////////////////
//////////////////////// // /// // // /////////M/////M/EEEEEE/N/////N///UUU///////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


(Hope this shows up correctly, its a massive ascii art comment)
Guess not, it is supposed to read "Stats Menu".
 
Code:
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////// ___ ////////////////////////___  ////////////////////////////////////////////////////////////////////////////////////
/////////////////////////   \//_____/// _ //_____///    \ /////////M/////M/EEEEEE/N/////N/U/////U///FFFF/U////U/N///N///CCC///SSS/////////////
////////////////////////| //    | /// /  \ // | // |      /////////MM///MM/E//////NN////N/U/////U///F////U////U/NN//N//C///C/S////////////////
////////////////////////\___ // | ///|    |// | // \___   ///spam//M/M/M/M/E//////N/N///N/U/////U///FF///U////U/N/N/N/C///////SSS/////////////
////////////////////////    \// | ///|___ |// | //     \  /////////M//M//M/EEE////N//N//N/U/////U///F/////U//U//N//NN//C///C/////S////////////
////////////////////////    |// | ///|    |// | //      | /////////M/////M/E//////N///N/N/U/////U///F//////UU///N///N///CCC///SSS/////////////
////////////////////////\___/// | ///|    |// | // \___/  /////////M/////M/E//////N////NN//U///U//////////////////////////////////////////////
////////////////////////     //   ///  //  //             /////////M/////M/EEEEEE/N/////N///UUU///////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 

Midlands

Ars Praetorian
535
Subscriptor
...it was so big that, by default, Emacs refused to font-color it. Too long (at 16,932 lines).

It was fully 25% of all the code in the entire product. I had to work on it. The engineers in question knew better, but they didn't give a shit -- they were the "I'm a badass homeboy gangsta even though I'm from San Mateo and my daddy makes $250K a year and all the toys I have, he bought for me" types. Anyway, a few quotes from it -- the company in question was bankrupt a long time ago, so it's irrelevant now:

code:<HR>
Code:
  public static void gmfr_main(String[] args) throws Exception
  { 
    VitaDBCore db = new VitaDBCore();
    
    String[][] shit = db.getMessagesForRendering(0, Integer.parseInt(args[0]));
    
    for (int i=0; i<shit.length; ++i) {
      System.out.println("Row [" + i + "]") ;
        <P>    
      System.out.println("" + shit<I>[0]);
      System.out.println(" " + shit[i][1]);
      System.out.println("  " + shit[i][2]);
      System.out.println("   " + shit[i][3]);
      System.out.println("    " + shit[i][4]);
      System.out.println("     " + shit[i][5]);<P>      System.out.println("      " + shit[i][6]); 
      System.out.println("       " + shit[i][7]);
      System.out.println("        " + shit[i][8]);
      System.out.println("         " + shit[i][9]);
  
    }
  }

The string "gmfr" means absolutely *nothing* to this program.

More:

code:<HR>
Code:
    System.out.println("let' see them.");
    bitches = db.getConfigValues(0, "a");
    for (i=0; i<bitches.length; ++i) {System.out.println(bitches[i]);}
    bitches = db.getConfigValues(0, "b");
    for (i=0; i<bitches.length; ++i) {System.out.println(bitches[i]);}
    bitches = db.getConfigValues(0, "c");
    for (i=0; i<bitches.length; ++i) {System.out.println(bitches[i]);}

But the real gem was something that took me four days to eventually discover. The program in question did this overnight import of some data -- probably ~7K rows, tops -- into a SQL Server database. It was getting slower and slower each night until it was taking all night (13 hours) to do this.

The engineers who wrote the code honestly were so stupid that they thought (and said) "hey, it's inserting 7,000 rows, it's going to take a long time".

Turns out that, buried deep, they were issuing a SQL join with no join conditions -- thus producing 33.5 million rows -- and then iterating through every row, extracting a string from that row, and then throwing the results away.

The solution was to delete said query. The next night, the import ran in 36 seconds.
 

mebeSajid

Ars Praefectus
4,022
Subscriptor
Originally posted by Midlands:
...it was so big that, by default, Emacs refused to font-color it. Too long (at 16,932 lines).

It was fully 25% of all the code in the entire product. I had to work on it. The engineers in question knew better, but they didn't give a shit -- they were the "I'm a badass homeboy gangsta even though I'm from San Mateo and my daddy makes $250K a year and all the toys I have, he bought for me" types. Anyway, a few quotes from it -- the company in question was bankrupt a long time ago, so it's irrelevant now:

Quote: "
public static void gmfr_main(String[] args) throws Exception
{
VitaDBCore db = new VitaDBCore();

String[][] shit = db.getMessagesForRendering(0, Integer.parseInt(args[0]));

for (int i=0; i<shit.length; ++i) {
System.out.println("Row [" + i + "]") ;



System.out.println("" + shit_[0]);
System.out.println(" " + shit[1]);
System.out.println(" " + shit[2]);
System.out.println(" " + shit[3]);
System.out.println(" " + shit[4]);
System.out.println(" " + shit[5]);

System.out.println(" " + shit[6]);
System.out.println(" " + shit[7]);
System.out.println(" " + shit[8]);
System.out.println(" " + shit[9]);

}
}
"

The string "gmfr" means absolutely *nothing* to this program.

More:

Quote: "
System.out.println("let' see them.");
bitches = db.getConfigValues(0, "a");
for (i=0; i<bitches.length; ++i) {System.out.println(bitches);}
bitches = db.getConfigValues(0, "b");
for (i=0; i<bitches.length; ++i) {System.out.println(bitches);}
bitches = db.getConfigValues(0, "c");
for (i=0; i<bitches.length; ++i) {System.out.println(bitches);}
"



I've seen people write code like that. But only when they were drunk.

Sajid
 

Midlands

Ars Praetorian
535
Subscriptor
Originally posted by Citrus538:
I fear the day that roaming gangs of programmers roam the internet, inserting profanity into source code where ever they go.

I went to grad school with a guy who wrote a bunch of low-level cluster MPP communications software (an MPI implementation). He was from Hong Kong, and, since English was his second language, had a habit of using profanity in arbitrary and amusing places. In particular, whenever he got the least bit frustrated, 'motherf---er' was the first word out of his mouth.

Amusingly, this extended to his comments, too. You'd see things like:

code:<HR>
Code:
// why not this motherf***er working with MPI_Send?

This was all well and good, until we all went to a big conference and decided to burn all our source code to 5,000 CD-ROMs for distribution. He found out about it and decided to delete his comments, but came in at the wire, not sure if he'd submitted the change before or after the copy for burning left.

You should've seen him at the conference, jumping up and down, waiting until the box of CD-ROMs arrived to see whether 5,000 people (and probably more) were going to see code with his name all over it and profuse obscenities in every third line of code.

He escaped this time, but one of these times he won't... :D
 
You guys have got it good!

I was reading that How to Write Unmaintainable Code, except I wasn't laughing because almost every example in that list is actually done by my co-programmers.

Some have not been trained for many years, others not at all, and they don't have any clue what standards most software development code is held to. This is a true story: One guy's code kept outputting an error when he tried to run it. His solution? DELETE THE LINE THAT PRINTED OUT THE ERROR!

I would complain more but what does it say about me that this is the only place I can manage to keep a job?

Chambers
 

blankman42

Seniorius Lurkius
7
Programming in brainf*ck :)

But seriously, I had a professor last semester who overused the C comma operator so much that it seemed like he had something personal against curly braces. We would get example code like
code:<HR>
Code:
while(i++,A[i])
  foo(A[i]),
  bar(A[i-1]),
  A[i]+=2,
  if(A[i]==x)
    bar(x),
    baz(i),
    x++;

Considering these were short examples for a class, it still wasn't hard to understand, but a whole large program written like this would be pretty bad.

-Blank
 

mebeSajid

Ars Praefectus
4,022
Subscriptor
Originally posted by Chambers:
Some have not been trained for many years, others not at all, and they don't have any clue what standards most software development code is held to. This is a true story: One guy's code kept outputting an error when he tried to run it. His solution? DELETE THE LINE THAT PRINTED OUT THE ERROR!

Guilty. :(

Sajid
 
Status
Not open for further replies.