May 12th, 2010 . by Daniel Hölbling
I spent almost 2 hours of debugging Java code yesterday due to one assumption that proved fatally wrong: I assumed a Stack, by definition a LIFO data structure would iterate over it’s elements from the top of the stack to the bottom.
So inserting 1, 2, 3 the resulting order when iterating through the stack should be 3, 2, 1.
Well, at least that’s what .NET does. Java is different. Look at the following Java code:
Stack<Integer> stack = new Stack<Integer>();
stack.push(1);
stack.push(2);
stack.push(3);
for(Integer i : stack) {
System.out.println(i);
}
And this C#:
var stack = new Stack<int>();
stack.Push(1);
stack.Push(2);
stack.Push(3);
foreach (var i in stack)
{
Console.WriteLine(i);
}
Well, they look exactly the same, but Java will return 1,2,3 while C# will honor the Stack’s special semantics and return 3,2,1. Great stuff isn’t it?
The workaround was quite simple, yet it had cost me 2 hours of my life trying to hunt a bug in my code, never thinking the bug could lie in a simple foreach iteration through the stack..
Lesson learned: Never assume you know anything about data structure semantics unless you have checked that your assumptions are indeed true. Implementations differ and sometimes this will bite you.
Oh and did I mention that the way Java does it is simply wrong?
Posted in .NET, Coding, Java |
View Comments
December 14th, 2007 . by Daniel Hölbling
Now and then I delve through some Java tutorials or Java applications for my studies.
And everytime I do so I immediately lack the getter/setter language features of C#.
To make myself clear (to non-C# Developers).
Java has either Public or Private properties, but no way to add validation Logic etc to those.
It is usually considered bad coding technique to expose instance variables outside of the class so almost everything has to be accessed through functions.
So to expose a integer variable called State you would have to create something similar to this:
It works and once you’re used to it you will create setter/getter methods in your sleep.
But, when pulling up your IntelliSense (ok, you don’t have that in Java, but you know what I mean) on that class you will be somewhat overwhelmed by get/set functions
.
So, how did C# handle this?
They borrowed a bit from Delphi and C# and created properties with get/set blocks (nothing new).
So our code from above would look like:
The great thing: Intellisense gets easier, you have something clearly marked as property not as function (that’s used to access a property).
Usability for your API has just increased
.
But, despite the awesomeness of this function (already in the .NET Framework since 1.0) in .NET 3.5 they managed to improve this feature a bit.
How? Imagine all those cases where you don’t have any validator logic on your properties, you just want a simple public field in your class.
In older Frameworks you’d have to create get/set Blocks for this Field although you don’t want any logic there (exposing Instance-Variables is stupid no matter how you do it).
So, 3.5 Introduced this:

Great, we came down to 1 line of code, and what happened?
.NET creates the private instance-variable automatically and handles the simple get/set stuff for us. When we decide we need validation or something in the get/set blocks we can just come back and expand those blocks without having to change any code outside the class (it was a get/set property all the time)
Posted in .NET, Coding, Java |
View Comments
November 8th, 2007 . by Daniel Hölbling
Ich glaube mir ist aus Versehen was interessantes gelungen.
Im Rechnernetze Kurs an der Uni war von uns gefragt ein eigenes Protokoll für einen Socket Server unter Java zu entwerfen.
Im Grunde wollten man wohl ein relativ simples HTTP ähnliches Protokoll mit GET, POST und LIST von uns. Ich musste aber natürlich über das Ziel hinwegballern.
Rausgekommen ist ein IMAP ähnlicher Socket-Server der nicht nur für jedes Socket einen Thread öffnet sondern auch für jeden Command einen weiteren startet um nie zu blockieren.
Und auch wenn das jetzt ziemlich brutal klingt, der tiefere Sinn dahinter war der dass man dadurch Befehle aneinanderreihen kann und das im Server sehr simpel über Thread.join() gelöst wird.
Spezielles zum Konzept und der Paket Syntax gibts unter more.
Read the rest of this entry »
Posted in Coding, Java |
View Comments
October 6th, 2007 . by Daniel Hölbling
Gut dass ich mir sicher sein kann das das nicht wirklich viele Leute lesen, sonst müsste ich wohl einen Flame-War allein wegen der Überschrift fürchten, ganz zu schweigen von dem was noch kommt.
Aber zurück zum Thema.
Gute Vorsätze fürs Semester und so wollte heute erstmalig umgesetzt werden und ich hab mich an die Rechnernetze Aufgabe rangemacht.
Die Sache fing simpel an, Eclipse runterladen und installieren, und los gehts.
Zwei vorgefertigte Klassen implementieren und jeweils eine Read und Write Methode erstellen die die Klasse auf die Festplatte als Datei schreiben würde.
Bisher eigentlich ganz simpel, und eigentlich sollte es auch so bleiben dachte ich. Löblicherweise wurde sogar ein Link zu einem DataStream Tutorial gegeben und nach einigem herumgesuche hab ich es sogar geschafft (unter Zuhilfenahme der Javadoc) ein halbwegs funktionierendes Programm zusammenzuzimmern.
Nun, warum hasse ich also Java?
Ein 30 Zeilen Programm hat mich fast 2 Stunden Arbeit gekostet. Herumprobieren mit dem Code, herumspielen mit der IDE, Debugger testen (Guter Tipp: Exception.printStackTrace() ist dein bester Freund!)
Und das beste: Ich bin nicht fertig und habe noch 3 Punkte am Aufgabenblatt vor mir.
Denn: Nachdem der erste Teil der Aufgabe simpel war (Ein Objekt das nicht Serializable ist auf die Platte zu schreiben) scheitere ich wohl an der zweiten Aufgabenstellung. Nämlich ein Collection Objekt in eine File zu schreiben.
An und für sich auch simpel, ich habe nur keinerlei Lust den gesamten Stream.writeUTF Mist den ich schon für das einzelne Objekt geschriebne habe nochmal in der Collection Klasse erneut zu schreiben mit irgendwelchen armen Count variablen etc. Eklige Sache.
Nun, eigentlich kann Java garnichts dafür, unter .NET wäre die Sache ähnlich verzwickt, nur dass ich mich da mit der Sprache soviel sicherer fühle dass das ganze bis zu dem aktuellen Punkt wohl nur 10 Minuten gedauert hätte.
Übrigens, sagte ich schon dass ich das neue Eclipse ziemlich gut finde? Vor allem das Word-Hopping
. Mit CRTL+Pfeiltaste kommt man innerhalb eines Wortes anhand des Camel Casings weiter. Also bis zum nächsten großgeschriebenen Wort (genial wenn alle Variablen z.B. strInputParam2 etc heißen)
Posted in Java |
View Comments