| Author |
Just wondering there're some better approaches
|
Max Bean
Ranch Hand
Joined: Mar 09, 2006
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
Joined: Sep 11, 2006
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.
|
SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
|
 |
Red Smith
Ranch Hand
Joined: Aug 05, 2007
Posts: 105
|
|
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
Joined: Jun 04, 2007
Posts: 318
|
|
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
|
------------------------
Bob
SCJP - 86% - June 11, 2009
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
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?
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
Raghavan Muthu
Ranch Hand
Joined: Apr 20, 2006
Posts: 3327
|
|
Thats what there is a pattern called Strategy Pattern. Below are some of the links which could help you. Wikipedia - Strategy Pattern Strategy for Success - a JavaWorld article Strategy, The Template Method and Bridge Pattern
|
Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
|
 |
 |
|
|
subject: Just wondering there're some better approaches
|
|
|