Best Coding Practices

Yajana Rao
3 min readNov 7, 2021

Collection of tips and tricks to write better code.

Photo by Arnold Francisca on Unsplash

1. Be consistence with your editor

It might be your IDE or code editor, Please don’t change them often. Code editors or IDE’s has there own key bindings, themes and functionality which takes a while for to get comfortable with.

Make some space for your code

Remove unwanted distractions apart from code from the editor space

Know your editor well.

Understand the shortcuts for navigation and text input. It improves your performance

2. Create a wrapper around external libraries before using

In case if you want to replace the library, You don’t need to change every where if you have a wrapper created around it.

3. Understand SOLID design principle

SOLID is a popular set of design principles that are used in object-oriented software development. SOLID is an acronym that stands for five key design principles:

  • Single responsibility principle

A class should have one, and only one, reason to change.

  • Open-closed principle

You should be able to extend a class’s behavior without modifying it. Or simply open for extension and closed for modification.

  • Liskov substitution principle

The principle simply requires that every derived class should be substitutable for its parent class.

What is wanted here is something like the following substitution property: if for each object O1 of type S there is an object O2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when O1 is substituted for O2 then S is a subtype of T.

  • Interface segregation principle

The general idea of interface segregation principle is that it’s better to have a lot of smaller interfaces than a few bigger ones. Martin explains this principle by advising, “Make fine grained interfaces that are client-specific. Clients should not be forced to implement interfaces they do not use.”

  • Dependency inversion principle

Developer should depend on abstractions, not on concretions. high level modules should not depend upon low level modules. Both should depend on abstractions. abstractions should not depend on details. Details should depend upon abstractions.

4. DRY ( Don’t repeat yourself )

The DRY Principle states that “Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.” This is also sometimes known as DIE: Duplication Is Evil.

Simply stated, the same piece of code should never be repeated over and over.

5. WET ( Write everything twice )

You can ask yourself “Haven’t I written this before?” two times, but never three.

The goal is to make sure that a class with the same logic has been independently written for at least 2 separate products before you can think about extracting it into a reusable component for a component library. This way our reusable components will be more mature, more stable, have generalized parameters/names/internals, with significantly reduced need for a potentially breaking refactoring.

6. AHA ( Avoid hasty abstractions )

prefer duplication over the wrong abstraction

AHA stands for “Avoid Hasty Abstractions”, described by Kent C. Dodds as optimizing for change first, and avoiding premature optimization. AHA programming assumes that both WET and DRY solutions inevitably create software that is rigid and difficult to maintain. Instead of starting with an abstraction, or abstracting at a specific number of duplications, software can be more flexible and robust if abstraction is done when it is needed, or, when the duplication itself has become the barrier and it is known how the abstraction needs to function.

7. Keep it simple

Just as one of the coding best practices is to keep your code readable, you should also strive to keep it simple. Complex code means the likelihood of more bugs and more mistakes.

References:

--

--

Yajana Rao

🧑🏻‍💻Software Engineer | React, React Native | Tech Blogger