Amusing or annoying code

For questions and postings not covered by the other forums
allistar
Posts: 156
Joined: Fri Aug 14, 2009 11:02 am
Location: Mount Maunganui, Tauranga

Amusing or annoying code

Postby allistar » Fri Nov 26, 2010 9:04 am

Anyone who has been a developer in a large (and not so large) system long enough stumbles across what I call "WTF code". In some cases a sense of humility kicks in when you realise you wrote it years ago but now know better.

Here's a couple I can think of:

Playing with Booleans:

Code: Select all

if (someBoolean = true) then //do something elseif (someBoolean = false) then //do something else endif;

Code: Select all

if (someBoolean) then boolean1 := true; boolean2 := true; else boolean1 := false; boolean2 := false; endif;
Eek!

What kind of annoying, amusing or pointless code have you seen in production systems that fit this category?

User avatar
BeeJay
Posts: 312
Joined: Tue Jun 30, 2009 2:42 pm
Location: Christchurch, NZ

Re: Amusing or annoying code

Postby BeeJay » Fri Nov 26, 2010 9:33 am

One example of pointless code that I see frequently that irritates me no end:

Code: Select all

if someCollection.size > 0 then foreach obj in collection ... endforeach; endif;
Cheers,
BeeJay.

User avatar
BeeJay
Posts: 312
Joined: Tue Jun 30, 2009 2:42 pm
Location: Christchurch, NZ

Re: Amusing or annoying code

Postby BeeJay » Fri Nov 26, 2010 9:51 am

Try to work out what the developer was intending this code to achieve:

Code: Select all

if not client.isSpecialClient() = false then

allistar
Posts: 156
Joined: Fri Aug 14, 2009 11:02 am
Location: Mount Maunganui, Tauranga

Re: Amusing or annoying code

Postby allistar » Fri Nov 26, 2010 10:18 am

A particular coding style I don't like it when indentation gets too deep, especially for something like error checking:

Code: Select all

hasError := doSomething(); if (not hasError) then hasError := doSomethingElse(); if (not hasError) then hasError := doSomethingYetAgain(); if (not hasError) then //you get the picture endif; endif; endif;
I have seen systems do that where there are as many as a dozen nested if statements.

User avatar
Dr Danyo
Posts: 56
Joined: Fri Aug 21, 2009 8:59 am

Re: Amusing or annoying code

Postby Dr Danyo » Fri Nov 26, 2010 9:12 pm

Try to work out what the developer was intending this code to achieve:

Code: Select all

if not client.isSpecialClient() = false then
:) Nice.

Perhaps the simplest refactor there is, but what a different it makes to understanding.

http://www.refactoring.com/catalog/remo ... ative.html

Dr Danyo.

User avatar
ghosttie
Posts: 181
Joined: Sat Aug 15, 2009 1:25 am
Location: Atlanta, GA, USA
Contact:

Re: Amusing or annoying code

Postby ghosttie » Thu Dec 02, 2010 3:47 am

The one I hate the most is using untyped data - for example passing an Object around when it should really be a more specific type - inviting runtime errors at a later date.

The ultimate manifestation of this is methods like the following, which will break if any of the contents of the object arrays aren't the right class, but also unless all of the arrays are the exact same size:

Code: Select all

methodName(param1 : ObjectArray; param2 : ObjectArray; param3 : ObjectArray); vars iPos : Integer; begin foreach iPos in 1 to param1.size do param1[iPos].MyClass1.doSomething(param2[iPos].MyClass2, param3[iPos].MyClass3); endforeach; end;
This "pattern" seems to be most common with developers that originally learned programming in a non-OO language and never adjusted.
I have a catapult. Give me all the money or I will fling an enormous rock at your head.

User avatar
andy-b
Posts: 24
Joined: Mon Jul 05, 2010 2:00 pm
Location: Dunedin, NZ

Re: Amusing or annoying code

Postby andy-b » Thu Dec 02, 2010 7:56 am

Not sure if this qualifies as amusing or annoying? I found this on a method that checks to see if a user has security access to an item on the menu (have removed the processing code, but you get the idea)

Code: Select all

doesUserHaveAccessToNavigationItem( pNavigatableItemName : String ) : Boolean begin return true; [ Continue doing proper validation checking here and return value as appropriate... ] end;
So this is a pretty slutty method that will let anyone do anything. The method was last modified in 2001 :o
Actually not that bad as is seems...I have found out this is a secondary security measure that is not usually relied upon.

JohnP
Posts: 73
Joined: Mon Sep 28, 2009 8:41 am
Location: Christchurch

Re: Amusing or annoying code

Postby JohnP » Fri Dec 03, 2010 8:42 am

How about a line of dodgy code, with a comment saying "// TEMP!!", and dated 12 years ago?

User avatar
ghosttie
Posts: 181
Joined: Sat Aug 15, 2009 1:25 am
Location: Atlanta, GA, USA
Contact:

Re: Amusing or annoying code

Postby ghosttie » Fri Dec 03, 2010 8:52 am

The one I'm guilty of is

Code: Select all

// ugly hack (TM)
:)
I have a catapult. Give me all the money or I will fling an enormous rock at your head.

Stokes
Posts: 66
Joined: Wed Oct 13, 2010 2:06 pm
Location: QLD, Australia

Re: Amusing or annoying code

Postby Stokes » Mon Dec 06, 2010 12:16 pm

we have got a global constant called "Min_Positive" which is defined as a Real equal to 0.

So instead of just putting in 0 for example

if collection.size > 0 then

we have a developer who puts in

if collection.size > Min_Positive then

he tells me this is the proper way of developing in Jade.... I find that hard to believe


Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 3 guests