This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Replacing Conditionals with Polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Replacing Conditionals with Polymorphism" Watch "Replacing Conditionals with Polymorphism" New topic
Author

Replacing Conditionals with Polymorphism

Cory Hartford
Ranch Hand

Joined: May 16, 2011
Posts: 82

I know for the vets this has been discussed to death, but for the newer people I wanted to share. I set out yesterday to replace an if statement based on a boolean flag; I was convinced there was a better way.. And there was, but it took some mind twisting to get there. I learned a lot of really really important programing Philosophy that I'd sort of been foggy on previously. Granted it might have been overkill but I wanted understand the concept so I was ok with the simplistic usage.
The last code block in this first post gives a really great example of a very simplistic implementation of the idea. More importantly, I watched the Google Clean Code talk video (which is linked in the post right above the code I am referencing) and that really blew my mind. I had to watch it several times understand the specifics. Definitely worth the effort though.

Hope this is helpful to people.

‎"The greatest of all weaknesses is the fear of appearing weak." - JB Bossuet
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11169
    
  16

just a tip...you say "the last code block in this post". the problem I see is that people can continually add new answers to that page, thus putting in new code blocks at the end, and depending on how people vote, entire sections can be moved around.

Can you be more specific which block of code you are referring to?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Cory Hartford
Ranch Hand

Joined: May 16, 2011
Posts: 82

Hi Fred. What I found most helpful and what I was referring to in the post is the section of the linked post in between the heading marked "Your Thoughts below this line" and the Author's portrait.

Although it linked in the paragraph in that section, here is the link for the Google Clean Code talk.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11169
    
  16

My apologies...i think I missed the word 'first' in the "in this first post" when I read it initially.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Replacing Conditionals with Polymorphism
 
Similar Threads
new option() and &
question about refactoring conditional logic
html:select errors
Question about transfering data over sockets
isELIgnored="true" - HFSJ Question 6