Image of Lucian Ghinda writing for notes.ghinda.com
April 18th, 2024

Using a Case statement or Metaprogramming?

Matt posted this question:

Question about using a case vs meta-programming
Question about using a case vs meta-programming

A case statement does not add complexity when thinking about readability or maintainability. I know that various tools will report it as creating too many branches (or paths), but to me there are situations where a case statement is the best way to say something explicitely with code. 

Second, I assume that phases, tasks, and gates are ActiveRecord models, so the chances that this case will grow very quickly and very big are small. 

On the other hand, introducing meta-programming may seem tempting as it can significantly reduce code length to a single line. However, it's important to be cautious as it can complicate debugging and potentially lead to unexpected false positives (where something works and it should raise an exception or throw an error). This could make searching within the code base more challenging, a concern that I always keep in my mind when coding. 

So my conclusion is: the case statement should stay unless there is a team style guide that will force it to change. In that case listen to the team or try to change the guideline.Â