Code that changes together should reside close together.

Finding code shouldn’t require a search party.

When working with a large codebase, it’s always frustrating when you need to search for code and don’t know where to look. Sure, you can easily find instances of specific names with any modern code editor. But sometimes, it’s harder to find code that’s merely related.

It’s natural and appropriate for logic to live in different classes. The Single Responsibility Principle will drive you to this, breaking up legacy classes and moving logic to other files. There’s nothing wrong with that.

The problem comes when you have various bits of logic that, while covering different responsibilities, are likely to change simultaneously.

For example, suppose you have a lending application and are implementing a new rule about interest rate limits. When working with administrative functions that enter interest rates, the limits will come into play. They may also require you to add information about a user’s region that you didn’t track before if the enforcement of the rule is regional. (Maybe a town in Kansas has a law that says no rates can be evenly divisible by 5.) The point is that you’ll need to make multiple changes to your codebase to make this happen.

Wouldn’t it be easier if all of the related code was nearby?

Try to locate code that relates to these concerns close together. For example, class files can be in the same directory. And then, within a class, methods that relate to each other should be located near each other. Don’t make future developers go hunting. Instead, make life easier for them and yourself.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: