# Just wondering there're some better approaches

Max Bean
Ranch Hand
Posts: 31
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

Freddy Wong
Ranch Hand
Posts: 959
There are no fixed rules. But if you have too many nested conditions, it's normally considered a bad programming habit.

You may want to read on Head First Design Patterns. It's one of the best book that I've ever read.

Red Smith
Ranch Hand
Posts: 136
1
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 ]

Bob Ruth
Ranch Hand
Posts: 320
Maximilian,

Is it your algorithm or the format of your source that they are not caring for?

Take a look at this link from Sun and read through it looking at the suggested format for some of these constructs such as the if - else if - else type of construct. This might help a little in terms of read-ability.

Bob

http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
One approach to this is to move the code you would execute for each choice into its own class. We might see something like:

Strategy is maybe an interface with just the method execute(). You make an implementation of Strategy for each different thing you want to do. Each one codes execute() to do its own thing. We have to load up the map, maybe like this:

map.put( "sing", new SingingStrategy() );
map.put( "dance", new DancingStrategy() );

Does that much make sense?

Raghavan Muthu
Ranch Hand
Posts: 3381
Thats what there is a pattern called Strategy Pattern.