Share the worst code you have ever seen...

If you wanna see ugly code, take anything that is produced in my company I’m forced to work for … :stuck_out_tongue: :-[ :’(

[quote]If you wanna see ugly code, take anything that is produced in my company I’m forced to work for … :stuck_out_tongue: :-[ :’(
[/quote]
Uhm, I guess they dont read these boards either?

We have a lot of nasty code in some of our j2me stuff - like methods performing differently depending on which thread called it :o. but it’s a required thing to shave bytes :confused:

Now, that’s just golden!

shmoove

[quote]Now, that’s just golden!
[/quote]
Not to mention uncompilable, “this” not being a variable and all… ;D

Actually, u can write java like that, you just have to write bytecode directly.

The 1st local variable of an instance method contains a reference to this - however there is nothing wrong with reassigning it to reference some other object.

Decompilers usually have problems translating that back into meaningful java (as it is unexpressable through the java syntax) hence you get assignment to this.

Heres a new implementation of the Rectangle class I just found in some code im working on :-


      //default constructor
      public Rect()
      {
            mLeft = 0;
            mTop = 0;
            mRight = 0;
            mBottom = 0;
            mWidth = 0;
            mHeight = 0;
      }

Hey if you have CPU use it as much as you can… always a great way to code ;D

I should dig up some of my old assembly-code. That’s a really ugly language, and dangerous as well :slight_smile:

[quote]He used XOR for exchanging?
Why? xchng eax, ebx wasn’t available?
[/quote]
xchg takes 3 cycles for r/r, and is non-pairable.
xor takes 1 cycle each, and IS pairable, so in the simplest case you could exchange two pairs of numbers in the time taken to ‘xchg’ just one. :slight_smile:

On PII and above the xchg IS pairable, but still takes 3 cycles.

Also, there is the chance the compiler may try to xchg with a memory pointer instead of two registers. this is extremely slow and forces a memory lock to occur (>15 cycles!).

Plus, not using the temprary register means that the instructions are much easier to interleave with other operations, making the compilers job significantly easier increasing the chance that they would end up paired (taking an average of 1.5 cycles)

Google for ‘Agner Fogg’ - he has written what I would consider to be the bible of pentium optimisation :slight_smile:

[quote]I should dig up some of my old assembly-code. That’s a really ugly language, and dangerous as well
[/quote]
You should see PS2 vector microcode… 2 instructions per line, branch delay slots - yum!

Believe it or not I do have some really old code around.

Silly beginners listener code.


static int click=0;
      
      public static void main(String[] args)
      {
            ...
            frame.validate();
            Button butt = new Button("CLICK ME!");
            butt.setSize(200,40);
            
            final JLabel label = new JLabel();
            label.setText();
            
            frame.getContentPane().add(butt);            
            frame.getContentPane().add(label);
            
            frame.validate();
            
            butt.addActionListener
            (
                  new ActionListener()
                  {
                        public void actionPerformed(ActionEvent e)
                        {
                              click++;
                              if(click == 1)
                              {
                                    label.setText("1 click");
                              }
                              else
                              {
                                    label.setText("else");
                              }
                        }//public void
                  }//create new action listener
            );//adding action listener
      }

Hi,
I’ve found code like the following in my company:


String a = ....

[...]

String b = (String) a.toString();

We want to be sure that it will be an String…

and:


Vector v = new Vector();
if(v != null)
{
.....
}

It seems to me a bit paranoic…

Funny thread :wink:

From a very early web application I wrote (that is funnily enough still working fine. It’s not terrible code in itself, unlike the lines of string1+=string2, but the variable naming is a little unorthodox:


public static int[][] swapTheSwippa(int[][] swippaswappa, int stop)
      {

      int start = getfromhere(swippaswappa);
      int[][] swuppaswuppa = new int[swippaswappa.length][4];
      if (start!=stop)
            {      
            for(int i=0; i<start; i++)            
                  swuppaswuppa[i] = swippaswappa[i];
            swuppaswuppa[start]=swippaswappa[stop];      
            for(int i=(start+1); i<=stop; i++)      
                  swuppaswuppa[i]=swippaswappa[i-1];      
            for(int i=stop+1;i<swippaswappa.length;i++)
                  swuppaswuppa[i]=swippaswappa[i];
            }
      else swuppaswuppa=swippaswappa;
            
      swuppaswuppa[start][3]=1;
      return swuppaswuppa;
      }

This may have come from an expert C++ programmer, where strange things happen when there’s no memory, rather than just seeing an exception fly by.

You never know how “new” was overloaded.

re crystalsquid
Pairing? Ignoring lastest version of CPU? Oh dear, just again that ugly microoptimalizations for old CPU. It was a reason why Intel didn’t improved it fast enough.

if(!(user.equals(""))&&!(user.equals(" "))&&!(user.equals("  ")) 
&&!(user.equals("   "))&&!(user.equals("    "))
&&!(user.equals("     "))&&!(user.equals("      "))
&&!(user.equals("       "))&&!(user.equals("        "))
&&!(user.equals("         "))&&!(user.equals("            "))
&&!(user.length()>=10)&&!(user.equals("Bitte Namen eingeben"))){
      [...]
      }

What? Trim you say? :slight_smile:

Stacktraces? We need no stinking stacktraces!

catch (Exception e) { output.setText("ERROR"); }

(output is a JTextArea)

Ah well, he’s still a newbe and promised to not do anything like that ever again ;D

For quite some time we’ve been seeing entries in a history log that just say “message”. I found the cause today, after uncovering code similar to the following:

public void method(String message) {
  [...]
  statement.setString("message");
  [...]
}

Hmm…

Nope, wont do it. Refuse to do it…will not post all of my code here!! :wink:

My code for work isnt too bad…my game code is spaghetti.
What do you expect though, I studied Cellular Biology and Chemistry in college (university for those of you 'cross the pond). LOL

[quote]re crystalsquid
Pairing? Ignoring lastest version of CPU? Oh dear, just again that ugly microoptimalizations for old CPU. It was a reason why Intel didn’t improved it fast enough.
[/quote]
I like assembler & optimising at this level :wink:

If you know what you’re doing you can usually double the speed of any decently large function (such as vector transform & perspectivise, software renderers etc.)

The insights from learning how to optimise can help you write C/Java code that runs at least 10% faster just by knowing a few tricks and how code is really translated - and once it becomes second nature then it doesn’t cost any more development time - and done properly it rarely produces code that would appear in this thread :slight_smile:

One clipping routine I came across had the comments:


// its just a clip to the left...

// And then a clip to the ri-i-i-ight...

// You put the verts on your knees...

// And pull the bounds in ti-i-i-ight

// Do the nested thrusts...

// It'll drive you insa-a-a-ne

// Lets do the clipping again!

It’s not big, & its not clever :slight_smile: