• Post Reply Bookmark Topic Watch Topic
  • New Topic

Ifs substitution  RSS feed

 
You Gin
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys!

Sometimes it happens I'm using a lot of Ifs in my code and well, it doesn't bothers me much, but back to some particular peace of code after some time makes it time consuming to understand all the conditions and statements. That means I'm producing not really readable code. Thanks god nobody reads it exept of me, lol.

Ok. So I have a foreboding I need to avoid Ifs as much as possible, but I'm not sure what is the good way of their substitution.

Thank you.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It actually depends on the situation/the problem, whether or not you need to use ifs.
Sometimes you can just walk off with a ternary operator(an effective replacement for a simple if-else), sometimes you can go for a switch-case construct(if it involves byte,short,int or char), and someother times you will have to go with if-else constructs.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rather than having a lot of if/then statements all in one method and/or deeply nested, consider refactoring out the statement bodies into their own methods. This may make it easier to understand each individual step/decision. High cyclomatic complexity (loosely: the number of decision points in a method) *does* make it difficult to reason about code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!