Abuse
|
 |
«
Posted
2004-11-18 00:47:55 » |
|
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 ^_^ Heres a few to get started :- 1
| Thread Thread = new Thread(Thread()); |
And it even compiles :d 1 2 3 4 5
| public static Graphics CurrentGraphics; public void paint(Graphics g) { CurrentGraphics = g; } |
When you see that, you know your in for a porting nightmare :S 1 2 3 4 5 6 7 8 9 10 11
| 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. 1 2 3 4
| 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! This is a personal favourite of mine ^_^ and suprising how often you see it! I like changing constants too! o_0 1 2 3 4 5
| 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 =/ 1 2 3 4 5 6 7 8 9 10 11
| switch(a) { case 1: return 2; case 2: return 3; case 3: return 4; default: return a+1; } |
Must.... Kill..... Incompetent... Eastern.... European.... 'Coders' 1 2 3 4
| 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!
|
Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! 
|
|
|
cfmdobbie
|
 |
«
Reply #1 - Posted
2004-11-18 00:52:49 » |
|
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...
|
Hellomynameis Charlie Dobbie.
|
|
|
oNyx
|
 |
«
Reply #2 - Posted
2004-11-18 00:56:52 » |
|
CONST_STR_5 \  / That switch thingy is also... creative 
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Abuse
|
 |
«
Reply #3 - Posted
2004-11-18 00:58:10 » |
|
Just a few 1liners then  I won't tell if you don't 
|
Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! 
|
|
|
oNyx
|
 |
«
Reply #4 - Posted
2004-11-18 02:21:18 » |
|
1 2 3 4
| public void processKeyEvent(KeyEvent ke) { controls[ke.getKeyCode()&0xFF] = (ke.getID()==KeyEvent.KEY_RELEASED)?0:1; } |
Heh. Just kidding  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  and the rest is... err  An insane mix of german/english/french comments/variables... horrible structure... and no naming convention or anything like that... 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| [...] 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) { String option; do { info.malflaeche.setCubes(0,0); info.txtSumme.setText("-"); info.txtPlayer.setText(String.valueOf(activePlayer+1)); info.txtMsg.setText("Knast"); 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  Ok. That connect 4 game where I used getpixel (TurboC) for looking up the fields was also a major wtf  (I hadn't known how to use 2d arrays... well, it worked and I also had some kind of ok-ish ai.)
|
|
|
|
kevglass
|
 |
«
Reply #5 - Posted
2004-11-18 06:29:26 » |
|
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...  Kev
|
|
|
|
ap_kelly
Junior Member  
Java rocks!
|
 |
«
Reply #6 - Posted
2004-11-18 12:27:01 » |
|
1 2 3 4 5
| 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
|
|
|
|
blahblahblahh
|
 |
«
Reply #7 - Posted
2004-11-18 13:30:20 » |
|
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: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| 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 
|
malloc will be first against the wall when the revolution comes...
|
|
|
Middy
Junior Member  
Java games rock!
|
 |
«
Reply #8 - Posted
2004-11-18 13:42:46 » |
|
Well not as funny as the rest of the stuff. I'll post some of my own trash code :-) 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 1 2 3 4 5 6 7 8 9 10 11 12 13
| 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); |
|
When do I get my makeMyGameAsILike() extension?
|
|
|
Abuse
|
 |
«
Reply #9 - Posted
2004-11-25 14:01:19 » |
|
if( FE_Task == 2 ) FE_Task2( ); else if( FE_Task == 4 ) FE_Task4( ); else if( FE_Task == 0 ) FE_Task0( ); else if( FE_Task == 1 ) FE_Task1( ); else if( FE_Task == 3 ) FE_Task3( ); else if( FE_Task == 5 ) FE_Task5( );
meaningful variable/method names? named constants? switch statement? structured code? /me has very little hair left.
|
Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! 
|
|
|
Games published by our own members! Check 'em out!
|
|
Tzan
|
 |
«
Reply #10 - Posted
2004-11-26 17:01:04 » |
|
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 
|
|
|
|
|
princec
|
 |
«
Reply #11 - Posted
2004-11-26 17:27:02 » |
|
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: 1 2
| for i = 1 to 100000000 next |
etc. Cas 
|
|
|
|
swpalmer
|
 |
«
Reply #12 - Posted
2004-11-28 01:38:16 » |
|
I just saw a bit of code use to convert a number from a hexidecimal string to an integer. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| 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 
|
|
|
|
oNyx
|
 |
«
Reply #13 - Posted
2004-11-28 02:19:47 » |
|
There's also no break... uhm... and there's also Integer.parseInt(bla,16)... and... 
|
|
|
|
swpalmer
|
 |
«
Reply #14 - Posted
2004-11-28 13:29:41 » |
|
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.
|
|
|
|
cep21
Junior Member  
Java games rock!
|
 |
«
Reply #15 - Posted
2004-11-29 16:34:44 » |
|
I remember in C there was this to swap two numbers 1 2 3
| void swap(int & a,int & b){ a^=b^=a^=b; } |
|
|
|
|
|
shmoove
|
 |
«
Reply #16 - Posted
2004-11-30 06:57:22 » |
|
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
|
|
|
|
|
Raghar
Junior Member  
Ue ni taete 'ru hitomi ni kono mi wa dou utsuru
|
 |
«
Reply #17 - Posted
2004-11-30 19:32:04 » |
|
He used XOR for exchanging? Why? xchng eax, ebx wasn't available?
|
|
|
|
|
MGodehardt
Junior Member  
why does the chicken cross the road?
|
 |
«
Reply #18 - Posted
2004-12-07 09:06:22 » |
|
this was seen in java code ... this = null; 
|
|
|
|
|
Herkules
|
 |
«
Reply #19 - Posted
2004-12-07 10:57:33 » |
|
He used XOR for exchanging? Why? xchng eax, ebx wasn't available? 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!
|
|
|
|
Herkules
|
 |
«
Reply #20 - Posted
2004-12-07 10:58:57 » |
|
|
|
|
|
Matzon
|
 |
«
Reply #21 - Posted
2004-12-07 12:03:32 » |
|
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  . but it's a required thing to shave bytes :/
|
|
|
|
shmoove
|
 |
«
Reply #22 - Posted
2004-12-08 08:02:57 » |
|
this was seen in java code ... this = null;  Now, that's just golden! shmoove
|
|
|
|
|
cfmdobbie
|
 |
«
Reply #23 - Posted
2004-12-08 08:20:22 » |
|
Now, that's just golden! Not to mention uncompilable, "this" not being a variable and all... 
|
Hellomynameis Charlie Dobbie.
|
|
|
Abuse
|
 |
«
Reply #24 - Posted
2004-12-09 09:23:14 » |
|
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 :- 1 2 3 4 5 6 7 8 9 10
| public Rect() { mLeft = 0; mTop = 0; mRight = 0; mBottom = 0; mWidth = 0; mHeight = 0; } |
|
Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! 
|
|
|
Backmask
Junior Member  
586: The average IQ needed to understand a PC
|
 |
«
Reply #25 - Posted
2004-12-09 10:58:48 » |
|
1 2 3 4 5 6 7 8 9 10
| 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 
|
|
|
|
MasterDirk
Senior Newbie 
Somebody set up us the bomb...
|
 |
«
Reply #26 - Posted
2004-12-09 11:03:42 » |
|
I should dig up some of my old assembly-code. That's a really ugly language, and dangerous as well 
|
|
|
|
|
crystalsquid
Junior Member  
... Boing ...
|
 |
«
Reply #27 - Posted
2004-12-12 11:26:07 » |
|
He used XOR for exchanging? Why? xchng eax, ebx wasn't available? 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.  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  I should dig up some of my old assembly-code. That's a really ugly language, and dangerous as well You should see PS2 vector microcode... 2 instructions per line, branch delay slots - yum!
|
|
|
|
|
K.I.L.E.R
Senior Member   
Java games rock!
|
 |
«
Reply #28 - Posted
2004-12-13 11:22:49 » |
|
Believe it or not I do have some really old code around. Silly beginners listener code. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| 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"); } } } ); } |
|
Vorax: Is there a name for a "redneck" programmer? Jeff: Unemployed. 
|
|
|
zarzus
JGO Visitor
Java games rock!
|
 |
«
Reply #29 - Posted
2004-12-15 14:29:11 » |
|
Hi, I've found code like the following in my company: 1 2 3 4 5
| String a = ....
[...]
String b = (String) a.toString(); |
We want to be sure that it will be an String.... and: 1 2 3 4 5
| Vector v = new Vector(); if(v != null) { ..... } |
It seems to me a bit paranoic.... Funny thread 
|
|
|
|
|
|