“Clean code is simple and direct. Clean code reads like well-written prose. Clean code never obscures the designer’s intent but rather is full of crisp abstractions and straightforward lines of control.”
– Grady Booch
The quote above was famously attributed to Grady Booch, the author of Object Oriented Analysis and Design with Applications. In chapter 1 of ‘Clean Code: A Handbook of Agile Software Craftsmanship’ by Robert C. Martin, Grady Booch was highlighted for his focus on code readability. In support of Grady’s statement, I would venture that information hiding is a critical concept for improving code quality and readability.
As defined in this article of baeldung.com, ‘information hiding is a programming principle that promotes the idea of restricting access to internal implementation details.’ Modular programming is a programming methodology that aims to break up programs into their most basic (and often reusable) components. Information hiding is a principal of modular programming that encourages obscuring and restricting information about the underlying implementation details of a component.
Obscuring information about the underlying implementation comes with several benefits:
- Hiding implementation details of a component improves high level readability of code
- Obscuring or restricting visibility on the implementation of a component discourages frequent changes or tampering
Information hiding also encourages mitigating certain ‘code smells’. ‘code smells’ referring to a list of bad behaviors as outlined in ‘Refactoring – Improving the Design of Existing Code’ by Martin Fowler.
- Duplicated Code – Relying on a well known & reliable component (with information obfuscated) for repeated operations mitigates code redundancy.
- Long Parameter List – When information of the underlying implementation is hidden, inputs and outputs need to be well understood by the end user. Information hiding encourages developers to re-consider what inputs are necessary for an end user.
- Comments – While information obscurity will not magically improve the quality of your comments, it does reduce the margin of error. Code implementation is kept deliberately obfuscated from the end user, and only the interface (inputs, outputs, etc) are presented. With a code based more focused on high level logic, writing concise and meaningful comments becomes easier.
Just recently, my professional mentor taught me information hiding as a means of improving my system design & code quality. There is no ‘magic bullet’ in programming, but I have come to respect that information hiding has a ‘magical’ tendency to drive code quality while discouraging common coding mistakes.