Typing != Complexity

Developers of all experience levels commonly share a false belief: more typing means more complexity. The most powerful idioms and tools often require a few extra keystrokes, but dramatically reduce complexity.

The final keyword in Java is a bit of extra typing. The final keyword can be applied to variables, method parameters, instance members, static members, methods, and classes. By making these final, their values will not depend on their context. They will be known constants through their lifetime. Their constancy removes a large cognitive load. The developer no longer needs to worry if the value was not set or if it was set multiple times. Many developers forgo this benefits to avoid typing the extra keyword, to their own detriment. Later, they spend too much time chasing down defects caused by mutable state.

Developers will often shun creating small types and interfaces because of the extra typing involved. Interfaces can abstract away many different implementations behind a simpler facade. Adding new implementations would only require creating a new class. Instead, many developers will create long-winded conditional chains of all the behaviors. These chains are repeated in many places, opening the possibility for defects through inconsistencies. Conditionals are harder to understand and change than focused classes. Small types allow us to use the compiler to find defects and document expectations in the code. A primitive integer can represent a distance, a duration, or any number of other numeric values. A wrong value in the wrong place can be disastrous. A Duration instance cannot be wrongly used in place of a Distance though. This guarantee is enforced by the compiler, and will not be overlooked or side-stepped.

Programming languages, like natural languages, are built for people. And like natural languages, they cannot always be maximally terse and maximally expressive. For people, expressiveness is the more valuable attribute. With programming languages, the expressiveness carries added benefits. We can create stronger assurances against defects, and fail earlier. All for the price of a little extra typing.

Comments

Popular posts from this blog

Break Down Code to Make It DRYer

Untangling Nested Code

When Technical Debt Costs You MORE!