Jump to page content

public class Solve

or, How you are really not supposed to code.

A certain acquaintaince of mine was given the following page of Java code by her computer science teacher basically as a check sheet to make sure we did all of our methods right; it also serves as a good example of the coding style he advocates in class. I have now typed up the code as plain text; you may still view the original scanned image where all the code is in bold serif face for no apparent reason).

public class Solve { public static boolean cond=true,cond2=true; public static double x1,x2,x3,x4,x5,y5,z5,r6,s6,t6,u6,v6,w6,x6,y6,z6; // [1] //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ // FIND ABSOLUTE VALUE public static double abs(double x1){if(x1<0) return -x1; else return x1;} //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ // ROUND TO THE NEAREST INTEGER public static double round(double x2){if(x2>=0) return (int)(x2+.5); else return (int)(x2-.5);} // [2] //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ // INTEGER FUNCTION // [3] public static double floor(double x3){if(x3>=0||x3==(int)x3)return (int)x3; else return (int)(x3-1);{ //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ // CEILING FUNCTION public static double ceil(double x4){if(x4<0||x4==(int)x4)return (int)x4; else return (int)(x4+1);} //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ // SQUARE ROOT public static double sqrt(double x5){y5=.5*x5;z5=0; if(x5<0) {System.out.println("NA-");return 0;} // [4] while(z5!=y5){z5=y5;y5=.5*(y5+x5/y5);}return y5;} //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ // SOLVE A^B public static double pow(double x6,double y6){w6=1;s6=2;z6=(int)y6;cond=true; if(y6==1)return x6;if(x6!=0 && y6==0)return 1;if(x6==0 && y6!=0)return 0; if((x6==0) && y6<=0)||(x6<0 && y6!=(int)y6)){System.out.println("NA-");return 0;} // [5] if(y6<0){y6=-y6;z6=-z6;cond=false;} for(t6=1;t6<=z6;t6++) w6=w6*x6;v6=y6-z6;u6=.5;r6=x6;while(v6>.0000000001) {s6=sqrt(r6);if(v6>=u6){w6=w6*s6;v6=v6-u6;}u6*=.5;r6=s6;} // [6] if(cond)return w6; else return 1/w6;} }

The quality of the code, or the lack thereof, stuns me. The abundance of dollar signs (and consequent lack of blank lines) – as obstructive to reading the code as this becomes – is only a minor complaint in comparison to the real horrors. After all, this bloke believes that all methods’ arguments need prototyping and that all calculations should be done with public class fields. So you can wave goodbye to any recursion in the code or any hope of running the code threaded. I know that this girl’s CS class is not at that level yet, but why would you try to screw people up for life with coding style like this? Of course, one must not forget that it leaves the code almost entirely unreadable. Such niceties as variable naming schemes and formatting style clearly went out the window.

At least each method does not trip over the variables of the others; x1 belongs to method 1 (abs()), x2 belongs to method 2 (round()), x5, y5, and z5 belong to method 5 (sqrt()), and so on. Which works fine, until you remove and insert methods in the class and none of it makes any sense any more. Notice also that cond and the seemingly unused cond2 don’t follow this pattern, so even he doesn’t seem to know what he is up to. I guess I should be happy that at least the methods have sensible names, even if there really is no wisdom in reinventing java.lang.Math.

A few other notes:

  1. Half of these are ignored anyway as they’re eclipsed by function parameters of the same name!
  2. I will credit him with one thing: this algorithm is very nifty, and is useful for systems that don’t offer a mathematical round(). It’s quite possibly also the only one that’s comprehensible without a maths degree.
  3. Is it too much to ask that the function and comment use the same name?
  4. We are doing text output in the middle of a math library function?
  5. We are still doing text output in the middle of a math library function?
  6. Notice the sneaky *= in there? I love how he has thrown in tricky little C constructs like that; if his CS class members are only at the understanding level of the rest of his code, I can’t imagine them knowing what *= is. And even I misread it as =, not *=.

But it doesn’t stop there…

Another US educational establishment was also advocating some bad code on on their Web site (as another acquaintance pointed out separately); this has long ceased to be available but I kept a copy of the code and this is it:

Private Sub DeepDishCrust If DeepDish = True Then If Vegetarian = True Then If Vegan = True Then PizzaToppingsList = Fruit, Vegetables, Meatless Sauces Else PizzaToppingsList = Fruit, Vegetables, Meatless Sauces, Cheeses End If Else PizzaToppingsList = Fruit, Vegetables, Meatless Sauces, Greasy Sauces, Meats, Cheeses End If Else If Vegetarian = True Then If Vegan = True Then PizzaToppingsList = Fruit, Vegetables, Meatless Sauces Else PizzaToppingsList = Fruit, Vegetables, Meatless Sauces, Cheeses End If Else PizzaToppingsList = Fruit, Vegetables, Meatless Sauces, Greasy Sauces, Meats, Cheeses End If End If End Sub

In a sense, I can see the rationale behind it, in that it keeps the code simple to follow for absolute beginners, but the majority of the code is duplicated, and this is the beginning of a maintenance nightmare. At times, constructs like this can become necessary when the outcome can be visualised as a grid of results dependent on both axes (as standard programming structures do not support this), but there is no indication here that this is the case: the choice of a deep dish crust is behaving as a separate, independent option to the choice of topping. The code would be substantially simpler if it began with a list of universal toppings and then added first those suitable for vegetarians, and then those suitable for meat eaters.

I guess this is what happens when Computer Science classes (the US term for nothing more than cheesy Visual Basic/Java programming classes) are targeted at your average Joe instead of people who learnt all the basics on their home micro long before they decided to take CS, and have a real passion for the subject and a mind to handle it.

One need no longer wonder why modern software is so badly designed and has such a propensity to crash and barf all over your hard drive. Incidentally, there are far more bugs in modern software written in helpful, friendly languages and development environments than there were in complex video games written in assembler code in the 1980s.