Skip to main content

Posts

Showing posts with the label Why you should avoid if-else statements

Why You Should Avoid If-else Statements

Introduction: Developers tend to use a lot of conditional statements in their code.Of course, each flow has one specific path, its own specific validations, etc.  It is very natural that your code has a lot of decisions to be made at run time. but, mostly, when you use a lot of conditional statements in specific area of your code, probably you are not using it right.  When using lot of "if-else" statement , most of the code is common in between those blocks and compiling this common code repeatedly is obviously waste of timing as well as waste of bandwidth and it decreases the performance as well. Conditional statements should be used for small decisions, not for big decisions. (It really hurts than lose girlfriend to see chained else ifs in a method.Really...!!!) Want an example? Let's check the code below: --------------------------------------------------------------------------------------------- ...