Short Notes
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.ย