Share the worst code you have ever seen...

During my J2ME porting & localising endeavours I’ve come into contact with some unbelievably bad code.
Is it just the norm? or is J2ME particularly well off in terms of bad coders?

Lets see some of the abominations you peeps have come into contact with too :slight_smile:

Heres a few to get started :-


Thread Thread = new Thread(Thread());

And it even compiles :d


public static Graphics CurrentGraphics;
public void paint(Graphics g)
{
   CurrentGraphics = g;
}

When you see that, you know your in for a porting nightmare :S

Line 300: public void doGame()
{
...
...
Line 2300: if(a<q & d>=j) return;
...
...
Line 4571: if(true) return;
...
...
Line 6784: }

Nothing like methods that span 25 pages, have misc returns throughout, and have huge areas of dead code.


public void save_LOAD_data(boolean b)
{
....
}

save/load pah, its all the same - lets chuck both in the same method!
Naming convention? what naming convention!

CONST_STR_5 = 7 - 3;

This is a personal favourite of mine :slight_smile:
and suprising how often you see it!

FILE_LENGTH--;

I like changing constants too! o_0


class Game
{
   public static int i,j,l,z,x,k,y,v,w,j;
   ......
}

Death used to terrify me, now it seems like a peaceful escape from the horror of tracing bugs in programs like the above =/


switch(a)
{
   case 1:
   return 2;
   case 2:
   return 3;
   case 3:
   return 4;
   default:
   return a+1;
}

Must… Kill… Incompetent… Eastern… European… ‘Coders’

public static final int SCR_WIDTH = 128, SCR_HEIGHT = 160;

.....
g.drawImage(img, (128-14)>>2, (159-26) >>2, g.TOP | Graphics.LEFT);

Constants are such a good idea… but using them all the time? that would just require too much effort!

I’d love to post the worst code I’ve ever seen, but unfortunately that would require posting about 50% of my company’s IP…

CONST_STR_5 ;D/

That switch thingy is also… creative :smiley:

Just a few 1liners then :wink:

I won’t tell if you don’t ::slight_smile:

public void processKeyEvent(KeyEvent ke) //thx Abuse! :D
{
      controls[ke.getKeyCode()&0xFF] = (ke.getID()==KeyEvent.KEY_RELEASED)?0:1;
}

Heh. Just kidding ;D

Hm…well… the code from TinyRivers is actually rather scary for the most part. Can’t even decide what to paste… it’s all scary :>

/me looks through old code…

Oh… that’s cute :smiley:

money[who]=howmany;

and the rest is… err :-[

An insane mix of german/english/french comments/variables… horrible structure… and no naming convention or anything like that…

[...]
class MoiListener implements ActionListener
      {
      public void actionPerformed(ActionEvent e)
            {                                                                  
            JFrame f=new JFrame();
            dbug.outmsg("Player "+String.valueOf(activePlayer+1)+" hat gewürfelt");
            if(spieler[activePlayer].getPosition()==40)      //spieler im knast
                  {
                  String option;
                  do
                        {
                        //show stats
                        info.malflaeche.setCubes(0,0); //let the cubes grin ;)
                        info.txtSumme.setText("-");
                        info.txtPlayer.setText(String.valueOf(activePlayer+1));
                        info.txtMsg.setText("Knast");//just for info
                        info.txtMoney.setText(String.valueOf(spieler[activePlayer].getGeld()));
[...]

See? And now imagine there are 5k lines of that stuff. shudder

Well, it was my first somewhat bigger java program (featuring a total of 27 classes, a txt file and 21 images). It was somewere in 2000 where you had fun with 1.2’s BSODs whenever you did some DB stuff (fortunately 1.3 came out by that time). The timeframe was 3 months and I also learned java in during that time (the architecture is totally “one way” and it’s quite odd that it actually works).

My god… I’m so ashamed now :frowning:

Ok. That connect 4 game where I used getpixel (TurboC) for looking up the fields was also a major wtf ;D (I hadn’t known how to use 2d arrays… well, it worked and I also had some kind of ok-ish ai.)

Careful, I got into trouble last time I tried to say that using a naming convention and attempting to write decent code was a good idea… :smiley:

Kev



String name = "bob"

name = "";

System.out.println(name);


I was then asked why the name was always blank, I explained that line 2 set it to empty string after the initialisation. I was then asked if the order of the instructions was important!!!

I didn’t know how to reply to that, I was just very quiet for about 15 mins, rubbing my temples as I walked away.

Andy

Worst I saw was on a commercial game, looking over a friend’s shoulder (I was just visiting their “office” at the time), wondering what they were wimpering about.

Code something like:


static int YES_HE_IS = 3;
private boolean r; -- nb, this stood for "the real variable"; you'll see why later...

...(assigns to r somewhere completely different)...

int boss = 5;
int gimp = YES_HE_IS;
...

...(now we get to the actual main code: )
int low_paying_bastard = 2;

if( r || boss == (gimp + low_paying_bastard) )
{
  ...
}


Names have been changed to protect the innocent (replace “boss” with the name of someone at the company…).

And other “creative” statements constructed around similar concepts :slight_smile:

Well not as funny as the rest of the stuff. I’ll post some of my own trash code :slight_smile:

I am too lazy to implement a matrix class in my own engine, and too lazy to change the code to use LWJGl util lib. So here is an ugly hybrid. I know… it sucks


      //transforming the modelGunvector to a worldvector
            Matrix4f matrix = new Matrix4f();
            matrix.load(posrot.getRotationTranslationMatrix());
            Vector4f gunPoint = new Vector4f();
            gunPoint.x =firedByGunPoint._x;
            gunPoint.y =firedByGunPoint._y;
            gunPoint.z =firedByGunPoint._z;      
            Vector4f pos = Matrix4f.transform(matrix,gunPoint,null);
            Vector3 position = new Vector3();
            position._x = pos.x+ posrot.get_position()._x ;
            position._y = pos.y+ posrot.get_position()._y ;
            position._z = pos.z+ posrot.get_position()._z ;
            posrot.set_position(position);


meaningful variable/method names?
named constants?
switch statement?
structured code?

/me has very little hair left.

Wow! and people got paid to write some of this stuff? Makes me feel rather good about my skills. Maybe I should try to get a programming job :slight_smile:

I used to dread having to look through the Yanks’ code years ago in one of those “We don’t do development in the UK” software ops that I worked at. Amazing things like:


for i = 1 to 100000000
next

etc.

Cas :slight_smile:

I just saw a bit of code use to convert a number from a hexidecimal string to an integer.


char a = next input char;
switch(a)
{
case '0':
 i += 0;
case '1':
 i += 1;
case '2':
 i += 2;
case '3':
 i += 3;
case '4':
 i += 4;
case '5':
 i += 5;
case '6':
 i += 6;
case '7':
 i += 7;
case '8':
 i += 8;
case '9':
 i += 9;
case 'a':
 i += 10;
case 'b':
 i += 11;
case 'c':
 i += 12;
case 'd':
 i += 13;
case 'e':
 i += 14;
case 'f':
 i += 15;
}

didn’t even handle uppercase :slight_smile:

There’s also no break… uhm… and there’s also Integer.parseInt(bla,16)… and… ;D

err…yeah – the breaks were there, I just typed it quick. It was also C code. I also left out the setup and final lines.

I remember in C there was this to swap two numbers


void swap(int & a,int & b){
a^=b^=a^=b;
}

That’s actually a well known method, it’s just written in a confusing manner. Looks like the programmer was trying to save line numbers. But I do think he has to check that the variables are not the same one or there could be some trouble.
In any case, it’s good to know because they like to ask this question on job interviews (how do you swap two numbers without a temp).

shmoove

He used XOR for exchanging?
Why? xchng eax, ebx wasn’t available?

this was seen in java code …

this = null;

??? :stuck_out_tongue: :-X

[quote]He used XOR for exchanging?
Why? xchng eax, ebx wasn’t available?
[/quote]
Changes registers, not variables, and is not available from C/C++/Java!

The xor method has to advantage that no additional register/memory is needed, thus may prevent from another memory page loaded to cache.

Love that!