ConstructiCat Logo
CodeBust.
Browse section ▾

Simplifying Conditional Expressions.

Code Smells Mascot
Consolidate Conditional Expression

Problem: You have multiple conditionals that lead to the same result or action. Solution: Consolidate all these conditionals in a single expression.

001
Antipatterns Mascot
Consolidate Duplicate Conditional Fragments

Problem: Identical code can be found in all branches of a conditional. Solution: Move the code outside of the conditional.

002
Antipatterns Mascot
Decompose Conditional

Problem: You have a complex conditional (if-then/else or switch). Solution: Decompose the complicated parts of the conditional into separate methods: the condition, then and else.

003
Refactoring Mascot
Introduce Assertion

Problem: For a portion of code to work correctly, certain conditions or values must be true. Solution: Replace these assumptions with specific assertion checks.

004
Code Smells Mascot
Introduce Null Object

Problem: Since some methods return null instead of real objects, you have many checks for null in your code. Solution: Instead of null, return a null object that exhibits the default behavior.

005
Code Smells Mascot
Remove Control Flag

Problem: You have a boolean variable that acts as a control flag for multiple boolean expressions. Solution: Instead of the variable, use break, continue and return.

006
Architectural Patterns Mascot
Replace Conditional with Polymorphism

Problem: You have a conditional that performs various actions depending on object type or properties. Solution: Create subclasses matching the branches of the conditional. In them, create a shared method and move code from the corresponding branch of the conditional to it. Then replace the conditional with the relevant method call. The result is that the proper implementation will be attained via polymorphism depending on the object class.

007
Code Smells Mascot
Replace Nested Conditional with Guard Clauses

Problem: You have a group of nested conditionals and it’s hard to determine the normal flow of code execution. Solution: Isolate all special checks and edge cases into separate clauses and place them before the main checks. Ideally, you should have a “flat” list of conditionals, one after the other.

008