4 Expressiveness with patterns and the enhanced switch
This chapter covers
- Understanding expressiveness
- Using pattern matching for instanceof
- Exploring the enhanced switch
- Applying type and guarded pattern matching in an enhanced switch
We’ve tackled complexity and explored why relentlessly striving for simplicity is essential to writing beautiful, maintainable code. Now, let’s turn to another crucial dimension of beautiful code: expressiveness.
Expressiveness is about transforming logic into code effortlessly. When a programming language is expressive, it feels like a natural extension of your thoughts, a powerful tool that allows you to tell your story clearly and fluently. If you find yourself twisting the language, jumping through hoops, or hacking around its limitations to make something work, then it lacks expressiveness. You're facing its opposite: boilerplate-heavy, verbose, complicated, and opaque code.
Expressiveness can be notoriously challenging to define precisely. While it's embedded in many aspects of a language, such as its syntax, type system, APIs, and naming conventions, you can't measure it directly. However, you instantly recognize expressive code when it clearly reveals its intent, flows naturally, and feels effortless to read and write.