▲ | Archelaos 4 days ago | |
I have long desired such a language feature. It is a great addition to the language, because a single such expression helps to avoid potential bugs caused by mismatching double references in the conditional test and the execution statement of the traditional version, especially when refactoring longer code blocks. For example, if we have something like this:
and we introduce another category SpecialSettings, we need to split one code block into two and manually place each line in the correct code block:
With the new language feature the modification is easy and concise:
becomes:
and can be made for any other special setting in place, without the need to group them.Furthermore, I find the "Don't Overuse It" section of the article somewhat misleading. All the issues mentioned with regard to
would apply to the traditional version as well:
or:
If it really were a bug, when customer is null here, etc., then it would of course make sense to guard the code as detailed as described in the article. However, this is not a specific issue of the new language feature. Or to put it more bluntly:
is no replacement for
were we want an exception on null.BTW, with the new version, we can also make the code even clearer by placing each element on its own line:
|