This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

should IF statement NOT be used in OO ??

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it true that use of If-Else should be avoided in OO Programming ??
If so, how can it be achieved ??
Thanks in advance ,
sree
 
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's nothing about OO which implies "if" should not be used. Just that in some cases OO offers alternatives which purely procedural languages don't.
The most common type of "if" which can be eliminated in an OO system is the one which checks the type of some data, and decides what to do with it. A more typical OO approach is to use polymorphism and let the data itself decide what to do.
 
Author
Posts: 6049
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To elaborate on what Frnak said, deciding which to use can be a tough call. Some genral rules of thumb:
If it's a simple conditional, e.g. if amt < 100, use an if.
If it's a complex switch statement, where the switch options are fundamentally different behaviors (behaviors which would be well encapsulated by distinct objects), consider using polymorphism. (However there are still plenty of times when a simply switch statement is justified.)

--Mark
 
sree kov
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Mark and Frank.
My doubt is cleared.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at
http://www.refactoring.com/catalog/replaceTypeCodeWithSubclasses.html
and
http://www.refactoring.com/catalog/replaceTypeCodeWithStateStrategy.html
I recommend reading the "Refactoring"-book by Martin Fowler. IMO it's a must for serious oo-programmers (as important as "Design Patterns").
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't the on line site enough? or the book is required?
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Avijeet Dash:
Isn't the on line site enough? or the book is required?


The Book goes much deeper into detail of the single refactorings (motivation, mechanics, examples etc.) and has some chapters on the fundamentals of refactoring - the why, when, how etc. It discusses the importance of unittests and the nature of 'code smells'. And it's really easy (and fun!) to read. Don't miss it!
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
how could u think a OO language without "if" statment
there is nothing like if statements r bad programing habbit
just goto is a bad programing habbit in any language
rahul
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did an experiement a while back. I wrote a small program with the requirement of not using IFs and Switches. It was very interesting and kind of fun, but after writing it, it was obvious that not having IFs was going to far. I also kept myself from cheating by using do while as IFs.
anyway, it's a cool little thing to try, but not really something I would recommend in your job.
------------------
David Roberts - SCJP2,MCP
 
Would anybody like some fudge? I made it an hour ago. And it goes well with a 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
    Bookmark Topic Watch Topic
  • New Topic