DEV Community

Discussion on: Keeping your code clean by sweeping out "if" statements

Collapse
 
jasperhorn profile image
JasperHorn • Edited

Hi Thomaz,

I did notice the Map.of and realized that it returned an immutable map, but since your post never mentions any programming language, I responded to the language-agnostic advice rather than the Java example. Not all languages can express such immutability, and it wasn't actually mentioned outside of the example either.

The other thing I meant to imply is that you have to keep the map initialization code close to the using the map. I think it's easy to forget the two are linked when you're writing unrelated code and you may end up placing that code between the initializing code and the using code, even when you're an experienced developer. It's not the kind of thing of thing that stands out in a code review either, I'd say. In my opinion, writing good ifs is about skill rather than discipline. Therefore, it's something people actually grow in. It's also something that stands out much more in a code review in my opinion.

(The difference I see between skill and discipline is that skill is a level you're at, closely related to experience, while discipline is a resource to be managed. A developer may or may not have different total discipline levels throughout their career, and they should probably learn to manage it better as they gain more experience, but that's not nearly as strongly coupled to experience, and there are always going to be moments of low discipline. On top of that, eliminating (or minimizing) the need for discipline wherever possible will free up more for where you do need it. I'm getting pretty philosophical here, though...)

When looking at the examples just now, there was something else that stood out to me. Note that this an observation without an opinion (it hasn't gestated enough to become an opinion yet) even if it may sound like an opinion. Both examples use enums. I used to like enums a lot a long time ago, but these days I don't use them much because I believe that in most cases they aren't the best tool for the job. Looking at the examples in that light, the second example looks to me like it would be better as three methods without arguments in which case there would neither be ifs nor a map. (The first example would still use the ifs or the map without the enum, I think.)

Thanks for the discussion so far! It's good to be able to debate things like this.