Conditionals Expressions Considered Harmful
Can you guess what she thinks of your conditional expression? Pexels, CC0, "Public Domain Pictures"

Conditionals Expressions Considered Harmful

Use a Factory instead.

How much simpler can code get than a humble if/else statement or a casual ternary expression?

I learned the answer to that question myself from Sandi Metz (youtube). In the linked talk, Metz shares some example code in Ruby, where she starts with code containing 4 boolean conditionals resulting in 16 possible code paths. To test that code, I will need 16 tests. Instead, Metz goes on to refactor the code, hiding the conditional expressions in a Factory class. In the resulting code, there is only 1 code path in each of the 5 non-factory objects. I have nearly twice as much code, but each object is completely independent and has a single responsibility.

Metz is a Ruby girl, but I wanted to reproduce this learning for myself in a language I am more familiar with. I usually work in JS/TS/C#/C++, and I chose C#/dotnet for this exercise.

I started with the following code. I have omitted some defensive code validating the arguments, and etcetera. I never tested this code, so please excuse errors you may encounter, the correctness of the code is not the point. You can probably follow the code fairly easily, it's not that complicated. It counts the number of rows in a CSV or Excel spreadsheet, and prints that count to the console.

Code showing a simple procedure to count the number of lines in a CSV or spreadsheet.

There are three code paths here. Two happy paths, one sad path.

The first thing that Metz suggests is to isolate the things that I want to change in an object.

I take the CSV-specific code and put it in a dedicated class.

No alt text provided for this image

I should inject Console as a dependency, but to keep this article focused I will not do that just now.

I won't show you the ExcelTableReader, you already have a good idea what that looks like.

Now Metz tells me to hide my conditional in a Factory. Here it is.

The factory object contains only the conditional expression. Nothing else.

Finally, my main function is now simply this:

A simplified version of the original code. The conditional expression has been removed, because it now lives in a factory object.

I have done more than merely break my logic into smaller pieces. I've reduced the number of code paths in my actual code. My Main function only has one code path. Each TableReader implementation has only one code path. The TableReaderFactory still has three code paths, but the conditional expression is the only thing it has. It has only one responsibility. And that means the system as a whole is much easier to understand than the original code. That's a win.

Sandi Metz is one of my favourite conference speakers, if you enjoyed this little nugget, I encourage you to look her up on Youtube, she has a lot of valuable ideas to share.

Further Reading

  • I finally learned this important lesson about object oriented design from a talk by Sandi Metz, Polly Want a Message.
  • I used Carbon to generate nice screenshots of code.
  • I am using ClosedXML for reading the Excel file.

要查看或添加评论,请登录

Ben Ernst的更多文章

  • CI/CD is FREEDOM

    CI/CD is FREEDOM

    I post about three general areas that I am passionate about: systems engineering as it applies to organization design;…

  • Do you know your TDD?

    Do you know your TDD?

    I post about three general areas that I am passionate about: Systems engineering as it applies to organization design…

    1 条评论
  • Autonomy is a critical differentiator in any knowledge-based enterprise.

    Autonomy is a critical differentiator in any knowledge-based enterprise.

    I post about three general areas that I am passionate about: Systems engineering as it applies to organization design…

    1 条评论
  • You're Getting More Value Out of Your User Feedback Than You Might Appreciate

    You're Getting More Value Out of Your User Feedback Than You Might Appreciate

    When you spend a lot of time engaging with your users and gathering feedback, sometimes you might come away feeling…

    2 条评论
  • Ditch the Daily Stand-Up

    Ditch the Daily Stand-Up

    The daily stand-up. Indispensable, right? Without that 15 minutes a day, how would we communicate about what we're…

  • Host your Passion Project (for free)

    Host your Passion Project (for free)

    When interviewing junior software engineers, one thing that makes a huge impression is when you have a hobby project to…

社区洞察

其他会员也浏览了