Worst 5 Code Snippets Till Now

September 7, 2009 § 1 Comment

Last week I changed my Job, and started working in Rockwell Automation, after spending the last two years working in web development using Java. In this post I would like to share the worst moments in those years. Do you know all the theory we learnt in universities about Object Oriented, code reuse, encapsulation, cohesion, robust systems that don’t break because of a small change? I didn’t see anything of that, projects where started by unexperienced programmers who just heard that Java is Object Oriented without knowing what the hell does that mean.

Working on those spaghetti projects was very frustrating, but in the same time very challenging, it was amazing how some people managed to write indigestible code that makes you through up. This is my top 5 worst codes till now:

1. Do You Call This Validation?
Try to guess what this programmer is trying to do using this piece of code? exactly he is trying to validate the number of days in a month as a part of a bigger date validation method, if the month is 4,6,9,11 it should be 30 days, while if its 1,3,5,7,8,10,12 it should have 31 days, with a special consideration of month 2 and leap years.

Yes maybe the code is working, but can you read it easily? are you sure its bug free? in your opinion how many people out there needed to validate a date string? maybe something around 1000000 programmers right? Good, So don’t you think that there should be some magical method that will do this for you? What about trying to parse it with a DateFormat instance and catch an exception if something goes wrong?

Whenever you are up to start coding something just stop and do this conversation with your self:

yourself: Hi you.
you: Hi yourself.
yourself: do you think this operation is very common?
you: Yes, I Think Date validation is done by many people…
yourself: do you know something that will do it for you?
you: No.
yourself: ok, so ask master Google about “Java Date Validation”.
you: oooh here is a ready solution, thanks yourself.
yourself: you are welcome you, see you the next time, bye.
you: bye yourself.

2. Is This a Constructor?
Here a programmer wrote this constructor (I took the image using my iPhone), some of you may think yes its a big constructor but he used eclipse automatic code generation tools, NOO, in this constructor there was a tiny bug that I discovered, which made me conclude that this constructor was written by hand. (by the way this is only the top part of the constructor, its not complete).

For the programmer who wrote this constructor I have two questions, one, why the hell did you write this constructor by hand? two, do you think there is someone much crazier than you that will call a constructor with all that parameters? ok maybe there will be a 0.001% chance that this constructor is needed in that class. But writing it by hand? if I was a manager I would fire him, I don’t need programmers that waste 30 minutes for something that could be done in 1 second (and yet do it wrong).

3. The Power of Encryption
This one is really great, its clear that someone is trying disparately to encode a string. The problem is that this is not encoding, this is starting to become encryption somehow WTF!!. what about performance? how many times is the String parsed and parsed again? never heard about a StringBuilder or StringBuffer? couldn’t you read a bit about encoding before starting your own encoding solutions? guys all this makes our life difficult 😦

4. Ever Heard About Data Integrity?
During my work I was located for 3 months on a project which was using a postgres database and till now everything was wonderful, the problem was that the people who created the database schema never heard about Data Integrity, Primary Keys, Foreign keys …etc, working with that DB was like putting your data in a trash (it just accepts anything you put in it!!), come on man at least put primary keys for God sake!!

The nice excuse of not forcing data integrity was … “we don’t need it, all DB access is done through Hibernate”, oooh really? so you are saying that Hibernate doesn’t need constraints? and you are so sure that there will be no other application in the future that will access the database directly? How the hell could you be so sure that you don’t have corrupted data if you don’t have constraints?

After working on that project I soon realized that there was a lot of trash in the DB, orphan tuples, missing data …etc. Take this advice, if you see someone using a database without forcing data integrity, and you have the power, just fire him and send him home, there are so many dump ass programmers in this field that we have to get rid of.

5. Best Comments Ever
Everybody should place comments in their code, its good to comment your code. YES!! but if your comments are not useful just don’t write them down and waste my time, how many times did you see something like this:

//print the value of x if its bigger than 10
if( x > 10 ) {

Is this comment useful? I can already see that if x is greater than 10 it will be printed, I am a programmer who reads Java code faster than your comments, writing a comment like this one is an insult for programmers (Yes we know what is an IF statement), I don’t want to see those stupid comments in the code, its even a maintenance nightmare, later when the specification changes, and x should be printed when its greater than 9 instead of 10, I will have to change the code and the stupid comment too. What about this one:

//the constructor
public Person( ) {
//some code

Ok now lets talk seriously, we are Java developers, we are working on a serious project, do you think that someone needs your help, to tell him that, that was a constructor? if your answer is yes, fire him and you will save time and money. If you find comments like this do two things:

1. Delete them.
2. Try to locate the person who wrote them and shoot him.

These where the worst codes I ever seen, have you seen codes worst than those? please post them and lets all have fun 🙂


Where Am I?

You are currently viewing the archives for September, 2009 at Omar Al Kababji.