Best Practices in Programming
7 November 2016 by Luis (Güette) Güette
Software development now sits at the core of almost every business. Products are built on it, and technology companies depend on it entirely.
Poorly written software, however, can quietly undermine a product's credibility and steer a company in the wrong direction.
That's why applying best practices matters in any programming language. Here's a concise set of recommendations to get started:
- Maintain consistent indentation. Indenting your code is already good practice — but consistency is what makes it actually readable across a team.
- Avoid obvious comments. Commenting your code is fine; being redundant is not.
- Group related code together. Specific tasks often require only a few lines. Keep those tasks in discrete, self-contained blocks.
- Establish a naming convention. For example, camelCase (where every word after the first is capitalized) or snake_case (where words are separated by underscores).
- Apply the DRY principle — "Don't Repeat Yourself." This applies throughout the entire codebase: the same logic should never be duplicated.
- Limit line length. Shorter lines are easier to read. Long lines are a common source of friction during code review.
- Organize folders and files. Structure your code in well-defined directories and keep classes in separate files. The most widely used frameworks are a good reference for this.
- Refactor regularly. Refactoring means changing the internal structure of code without altering its behavior — essentially, cleaning it up and improving its quality.
- Use version control with Git. Version control is a system that records changes to a file or set of files over time, so any earlier state can be recovered when needed.
Beyond these fundamentals, it's worth following the SOLID principles — introduced below at a high level.
Visual representation of the SOLID principles of object-oriented design.
SOLID is an acronym for the first five object-oriented design (OOD) principles introduced by Robert C. Martin — widely known as Uncle Bob. These principles were brought together to make software easier to develop, maintain, and extend.
Each letter represents one principle:
- S — Single-Responsibility Principle
- O — Open-Closed Principle
- L — Liskov Substitution Principle
- I — Interface Segregation Principle
- D — Dependency Inversion Principle
Single-Responsibility Principle
A class should have only one reason to change — meaning it should serve a single purpose.
Open-Closed Principle
Objects or entities should be open for extension but closed for modification.
Liskov Substitution Principle
Objects in a program should be replaceable by instances of their subtypes without altering the correct behavior of the program.
Interface Segregation Principle
A client should never be forced to implement an interface it doesn't use, nor should it depend on methods it doesn't need.
Dependency Inversion Principle
Depend on abstractions, not on concrete implementations.
This is intentionally a primer — a starting point for writing cleaner, more maintainable code. From here, the natural next step is exploring design patterns and the language- or domain-specific practices that apply to your particular stack and product.
Those topics deserve their own treatment. More to come.
Best practices applied in everyday software development.