DEV Community

Discussion on: Code consistency vs using new features

Collapse
 
alainvanhout profile image
Alain Van Hout

When adding or changing code of any kind, predictability and mental load always come into play. That's no different when we're talking about either using a new language feature or sticking to the older language features that are typically used in the piece of code you're working with.

You need to ask yourself:

Does using the new language feature decrease predictability and/or increase mental load for the next developer that will work on this code?

If that's not the case, then using the new language feature does no harm (i.e. all else being equal, it's fine to use the new language feature).

Do note though that reduced mental load is not the same as
(1) being a cool new feature
(2) requiring less code/characters

As an obvious example of the first, the above change from using an if block to using Optional does increase mental load, because you're essentially writing an if statement in a roundabout rather than a straightforward way.

An example of the second, using an actually if-else statement is typically more transparent and requires less mental load than an inline conditional, despite the latter requiring less characters to write:

if (x) {
  doA();
} else {
  doB();
}

x ? doA() : doB()
Collapse
 
jsn1nj4 profile image
Elliot Derhay

And once again, the answer is "it depends".