• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Just wondering there're some better approaches

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  •  
    pie. tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic