Strategies for Writing Reusable Code
Every change in a software system implies making changes to the Design. Every programmer is a designer. The experience level could be relevant, but the structural changes made to the application still need to be made. The person with the hands on the keyboard is changing the Design. You’re always doing Design every time you write Code. Thus, any new change should move the system towards a more straightforward design. The question is, are you going to regret your design choices later?
To write Code that’s easy to change, we need to follow a couple of heuristics. Changes do not create unrelated side effects. Small changes in requirements suggest small code changes. The easiest way to add new functionality to a code base is by adding new Code rather than modifying what already exists. The existing Code is reusable. Code without side effects will be easy to change due to not worrying about cascading issues. We can acknowledge the code structure’s effectiveness by measuring the changes we must make in any code base when confronted with a new need.
Loosely coupled classes are not bound together. The objects define the role that different implementations can play. It is possible to substitute across the boundary of whoever plays the role. Manage dependencies by injection to promote composing objects in runtime. Loosely coupled objects enable independent context between components. Objects in the system can be composed in different ways depending on the context. The purpose of objects is context-independent when everything defined in a class or module is related to itself.
The interface Segregation Principle suggests splitting extensive interfaces. The idea is to make consumers rely on things that are useful to them. To be reusable, the object needs to know no more than it needs to work.
A programmer who writes clean Code is an artist who can take a blank screen through a series of transformations until it is a coded system. Clean Code is simple and direct. Clean Code expresses requirements using specific guidelines. Clean Code reads like well-written prose. Clean Code can be read and enhanced by a developer other than its original author. It has unit and acceptance tests, meaningful names, and a focus on simplicity and consistency. Clean Code should be literate since, depending on the language, not all necessary information can be expressed alone. It has minimal dependencies, which are defined, and provides a clear and minimal API.
Design is not the concern of architects and artists but also programmers. We can achieve an elegant and functional design by adhering to simplicity and separation of concerns. Few difficult-to-change applications are pleasant to work on. Object-oriented Design is about managing dependencies. It is a coding technique set that arranges dependencies so objects can tolerate change. The object-Oriented Design emphasizes flexibility rather than perfection. Object-Oriented Design is not in the business of acquiring perfection but in the business of flexibility. Tolerance of the ambiguity of a class of an object is the hallmark of a confident designer.