4 ms·
Consider a few of the benefits not mentioned in the article: - You have reduced the cyclomatic complexity of each class, which makes the code less fragile and
by Nycto 17y ago
Consider a few of the benefits not mentioned in the article:
- You have reduced the cyclomatic complexity of each class, which makes the code less fragile and easier to unit test.
- When adding a new operator, you are now forced to implement everything it needs to work. If you don't define a method, it won't compile. This is not the case with a long list of if/else statements.
- "You still need a nest of conditionals when you populate the tree..." This is true, but now all the conditionals are in one place, rather than two (in their example, toString and evaluate). Again, this will make testing easier.
- scott_s 17y agoYour second point is actually a feature, and is equivalent to structural pattern matching in languages with algebraic types not allowing you to have unspecified cases.
- jrockway 17y agoI think he was advertising this as a feature. Failing to compile is much better than hitting your "this can't happen" conditional at runtime.
- scott_s 17y agoYou're right, I didn't understand his intentions.
- wlievens 17y agoAbsolutely. One of the advantages of static typing (NOT starting a type war here, oh gods) is that you can transform error conditions from the run-time to the compile-time world. Earlier failure = cheaper failure, so this is great. I try to take this as far as I can. For instance, when writing Servlet code in Java, I define all my request parameter names in an enum, so that I can never misspell any lest the compiler tell me.