At The Daily WTF there was a series of WTFs at universities a while ago telling stories of “curious perversions in information technology” at universities.
Here and today I going to tell my own WTF story from my university. It’s quite simple since it merely consists of some code snippets: We had lectures on programming the last two semesters, more precisely in Java (an incredibly awful programming language, in my opinion). And when it was the first time for you to write programs you might not have noticed how awful the demonstrated program code and style was. But when you already were able to program such as I was, then .... then you shall always keep in mind during reading the following code snippets that we shall be educated to future project leaders, lead programmers, project designers, ...:
The class BinTree implements a binary search tree. In the original code German and English names are used side by side. To make it readable for you, I translated all German variable and method names. My personal highlight is the implementation of prefixValues() which shall give the values of the tree in prefix order. encode() returns a string containing “0”s and “1”s, “00” if a node has no childs, “10” if a node has a left but no right, “01” if it has a right but no left and “11” if it has both iterating through the nodes in prefix order.
class Node {
int value;
BinTree leftBranch, rightBranch; 1
Node(int x) {
value = x;
leftBranch = new BinTree();
rightBranch = new BinTree();
}
}
class BinTree {
Node root = null; 2
public boolean isEmpty() {
return root == null;
}
public void insert(int x) {
// ...
}
public String encode() {
return encode(“”);
}
private String encode(String code) {
if(isEmpty()) return code;
String l = “1”, r = “1”;
if(root.leftBranch.isEmpty()) l = “0”;
if(root.rightBranch.isEmpty()) r = “0”; 3
code = root.leftBranch.encode(code + l + r);
code = root.rightBranch.encode(code);
return code;
}
static int[] values; 4
static int freeInValues;
public int[] prefixValues(int n) { 5
values = new int[n]; 6
freeInValues = 0;
prefixValues();
return values;
}
private void prefixValues() {
if(!isEmpty()) {
values[freeInValues++] = root.value;
root.leftBranch.prefixValues();
root.rightBranch.prefixValues();
}
}
// ...
}
class Test {
public static void main(String[] arg) {
BinTree b = new BinTree();
String code;
int[] values;
b.insert(3);
// ...
b.insert(2);
b.insert(5);
code = b.encode();
values = b.prefixValues(code.length()/2); 7
// ...
}
}
1, 2 Look how beautiful this two classes are chained together ... forever.
3 How clearly the logical structure of the program is appearing at this point, wonderful!
4 Eh, a static variable for values, what is it for?
5, 6 Ah, okay, there the prefix values are stored. But wait in a static variable? Is there only one BinTree instance supposed to be? And why does a public method destined to return the nodes of the binary tree in prefix order need the number of nodes in the tree from the calling code ....?
7 Sure, the code string of the tree is as twice as long as the tree has nodes -_- That’s an ... interesting ... way to get the number of nodes ....
So, all instances share the same reference to the array storing the values of the tree? You probably can imagine what happens if you use BinTree in different threads ...