Simplifying Conditional Logic

本文同步於个人部落格: Simplifying Conditional Logic

This article references the chapter 10 ” Simplifying Conditional Logic ” of Refactoring: Improving the Design of Existing Code (2nd Edition). Author had highlighted many important refactorings in this chapter.

I use .NET C# to practice with these refactorings and upload to Github.

Simplifying Conditional Logic

(Photo from Pixabay: https://pixabay.com/illustrations/direction-path-decision-goal-2320124/)

Decompose Conditional

Tips
  • If the condition is complex, extract the condition and its branches as functions.
Examples:
  • A simple example

Github


Consolidate Conditional Expression

Tips
  • Some conditions can be reduced into an “Ands” or “Ors” expression. Then this “Ands” or “Ors” expression can be extracted as a function.
  • Sequences combine with Or, nested if statements combine with And.
  • If the conditions have side-effect, use Separate Query from Modifier to process it.
Examples:
  • An Ors example
  • An Ands example

Github


Replace Nested Conditional with Guard Clauses

Tips
  • If a condition is not normal, check it and return if it’s true. This check is a Guard Clause
  • Guard Clause means that it is not the core logic of the function But if it happens, it does the necessary cleaning job to exit the function.
  • Choose the outermost condition and replace it by Guard Clause. If some Guard Clauses have the same result, use Consolidate Conditional Expression.
Examples:
  • An simple example
  • Reversing the Conditions example

Github

Replace Conditional with Polymorphism


Tips
  • If a condition is complex, split it by using polymorphism and make its logic clear
  • For a variation condition, we put the basic logic into a superclass, and put the variation logic into derived class.
  • Don’t abuse using polymorphism when the condition is simple.
  • If the current class has no polymorphism, we create a factory function to return its object instance. Then we put every condition logic into derived class.
Examples:
  • An simple example
  • Using Polymorphism for Variation

Github


Introduce Special Case

Tips
  • If clients always check a structure’s value, we separate this check into a special-case.
  • NullObject is a common pattern for null value.
  • Add the special-case check property to the target structure. Use Combine Functions into Class or Combine Functions into Transform to move the special-case logic into a new class. Then the previous special-case check property will return it when check triggers.
Examples:
  • A Class example
  • An Object Literal example
  • A Transform example

Github


Introduce Assertion

Tips
  • Assertion definitely marks the assumption(expression). Assertion avoid programmer’s error.
  • Don’t abuse assertion. We only use it when the expression must be true.
Examples:
// before refactoring
public void SaveMoneyIntoAccount(decimal money)
{
     acount += money;
}

// after refactoring
public void SaveMoneyIntoAccount(decimal money)
{
     Debug.Assert(money > 0);
     acount += money;
}

Conclusion

This chapter introduces me how to simplify conditional logic. I had ever written complex conditional branches and they were very ugly long…… I had no idea to reafactor them until I read this chapter. If you want to learn detailed motivation and mechanics, study the chapter 10 ” Simplifying Conditional Logic ” of Refactoring: Improving the Design of Existing Code (2nd Edition). and it improves our programmer’s ability.


<<:  【C++】Bubble Sort

>>:  【C#】Multi Value Return

Day 11: 回到原生环境!在Android上展示Ktor资料!

Keyword: Android ViewModel,Coroutine,LiveData,Recy...

[Day 03] tinyML开发板介绍

目前tinyML基金会并没有指定特定的开发板或开发平台,也没有限制可以跑那些项目,只期望功耗能在毫瓦...

Arduino 扩充版 W5100 - EEPROM 烧录

Arduino W5100 是一块含有网路及EEPROM功能的扩充版. 笔者在之前的文章中曾提过可以...

Day25 能不能给我一首歌的时间 这首歌我希望是十年

Record the questions 总是在紧急时特别会brain storming,今天再度...

Day20 少年维持着烦恼

Record the questions 原本犹豫是否要发这个主题,或是先继续介绍plotly(可...