“Deliberately Ignore” pattern

We all strive to be more precise and at the same time more expressive in code. I came across one more thing that we can do to improve code in some places – I call it “Deliberately Ignore” pattern. One good example is having “switch” statement with default clause. This usually looks like this:

One good example is having “switch” statement with default clause. This usually looks like this:

What’s the problem then? For me – I still don’t know if developer meant what is out there. What if there were few more links we don’t know if he should handle? Or if he knew that there are a few other options he simply didn’t know that they exist?

The “Deliberately Ignore” pattern

Well written expressive code could look like this:

There are a few considerations here to be made.

Expressiveness is high – we have a straightforward way to say “hey I know that there is something like “child” case but I don’t want to handle it”. Moreover, you can eliminate the need of having comments in code.

No performance degradation – common misconception is that what’s written in code, exist also in final assembly. If you are worried about performance – add “Conditional” attribute.

Not applicable everywhere – there is no silver bullet. If switch statement would be very big “Deliberately Ignore” pattern becomes an anti-pattern.

 

Leave a Reply