Programming Principles

Prefer Readability over De-duplication

Duplicated business logic is a problem, duplicated code is less so. Removing duplication by adding conditionals makes code more difficult to read and harder to maintain. Duplication is easy to refactor, conditionals are not.

Prefer Convention over Configuration

Attempt to remove the need to explicitly configure things; instead rely on sensible defaults.

Keep it Simple, Stupid (KISS)

The simplest solution is usually the best.

Boy Scout Rule

Leave the codebase cleaner than you found it. If you can add tests, do so, if you can refactor something, do so. We don’t need to ask permission, this is our job.

Prefer Polymorphism over Conditionals

Conditionals add to method complexity making testing harder and can violate the open/closed principle. If we instead encapsulate the “then” condition in a polymorphic class we bread down code into smaller, more easily testable units and allow new conditions to be added without changing the existing code.

Prefer Descriptive Naming over Concise Naming

There’s nothing wrong with long names.

Prefer Consistency over Improvement

We need to continuously improve the codebase but not at the expense of creating an inconsistent mess. If there’s an existing approach to something think very carefully about changing it in one instance, unless you’re going to refactor it all.

Vertical Separation

Related code lives closely together. For example a variable should be initialised just before it’s used. The practical reason for this is to minimise how much the person reading our code has to scroll up and down.

Step-down Principle & Newspaper Metaphor

Code, just like a newspaper article, we read vertically. The more abstract information should be at the top. As we read downward, the details increase. More abstract methods should be at the top of a class with the details such as private methods further down. Methods that are called should be directly below the calling method.

High Cohesion, Low Coupling

Cohesion is the degree to which elements of a module or class belong together. Related code should live closely together or in other words code that’s changed together should live together.

Coupling is the degree to which modules and classes depend on each other. The more independent they are the better, as a change in one is less likely to have an unintended effect on another.

Prefer Grouping by Feature over Layer

Try to group code by feature and/or component. When code is grouped by layer at a high level it makes files harder to navigate. Someone is more likely to need to find all layers of module x than every service class in an application. Similarly to vertical separation, when related files are grouped together it requires less jumping around a file tree and is cognitively that bit easier to keep the problem in your head.

Prefer Exceptions to Returning Error Codes

Error codes have to be handled; that handling can be inadvertently omitted. Exceptions can be allowed to bubble up to the top level handler reducing logic in the application. Exceptions can hold more contextual information and better convey the error when used properly.

Command Query Seperation

Methods either return something or do something, not both. If we follow this principle throughout our codebase we can know that any method that returns something will not have unwanted side effects by manipulating the state of the application.

SOLID

See my separate article on the SOLID Principles

Suggestions

If you have suggestions of other principles that you think are missing please drop them in the comments below.

--

--

--

#scotland #coding #outdoors #beer #cheese #cats.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Construction Owner Tips: How to Navigate Software Licensing

Secure your Environment Variable inside Azure Function using Key Vault.

Balance is the key!

CGPL shares best practices in waste management

CXL Institue Conversion Rate Optimization Schplarship : Week 12

PHP: Named arguments are your friends

Allocate server to current project in Eclipse

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gavin Stewart

Gavin Stewart

#scotland #coding #outdoors #beer #cheese #cats.

More from Medium

In Defence Of Lazy Code Documentation

The best way to learn code is by doing tests for it

Getting Through the Numerous Obstacles of Writing Code!

Breaking down George Pólya’s methods of problem-solving.