Originally posted by Maximilian Boonyoung:
From the view of algorithm, If I construct my logic like;
is it considered as stupid iterations/condition-checking?
I was several times blamed by some gurus as they saw my nested for loops or something like that. That's why this moment I concern a lot about good algorithm. Sorry about I know less on Computer Science and what a good algorithm should look like.
Any books/articles to head on will be very thankful
Max
What is in the Value class? What is an example of what happens if one of the if statements is true?
I think that checking a data value like that and doing things based on the value is considered procedural type programming. Sometimes the phrase "no if statements" is used to describe how object-oriented programming can differ from procedural. It may be possible to make your logic object-oriented. That would involve making an object hierarchy:
References to the base object of the hierarchy is what you would store in your Collection but the actual objects created would be the subclasses.
e.g.:
BaseClass myClass = new SubClass1()
myArrayList.set(myClass);
The method (or methods) of the objects would do what you are doing inside the if statement blocks. The data in the objects could possibly be the value that you are looking at in the if statement, or my likely, that value will determine the type of subclass object you create.
So it becomes
However .... to create the objects requires an if/else/else... statement (so much for "no if statements"), possibly as big as the one you are trying to replace. In order to avoid the object creation if, or at least hide it, you can create what is called a Factory object that creates the subclass objects.
e.g.
BaseObject myObject = MyFactory.CreateSubclass1();
BaseObject myObject = myFactory.CreateSubclass2();
or
BaseObject myObject2 = MyFactory.CreateObject("Subclass1");
Factory objects are described in some books, like the Head First one mentioned earlier.
Having said that, it's probably more work than what you want to do on a "quick and dirty" program or one that is a "one-time" deal. The payoff would come if you are going to be extending and modifying this code as time goes on.
[ August 08, 2007: Message edited by: Red Smith ]