Skip to content

Best Practices

img

Clean Code

Clear Names:

Baby Steps:

Short Functions:

Use Comments Only When Necessary:

Organization Matters:

Avoid Repetition:

Use Objects:

Test Your Code:

Clean Code Matters:

Learn and Improve:

Keep It Simple Stupid - KISS

Clean tests follow the rules of the acronym FIRST (Fast, Indepedent, Repeatable, Self-validation, Timely).

Don’t Repeat Yourself - DRY

DRY is the acronym for Don’t repeat yourself. It is the concept that says that each part of the system's knowledge must have only one representation. This way, avoiding code ambiguity. In other words, there must not be two parts of the program that perform the same function, that is, the famous copy and paste in the code.

But why avoid repetition? Simple!

Anyone who has a second home on the beach, or in the countryside, knows how complicated it is to ensure the maintenance of both. Even though repetition may seem harmless in simpler programs, it can become a problem as the software grows and maintenance and development become increasingly complex.

A good way to avoid code duplication is to correctly apply the single responsibility technique. For each function or method, use only one part of the method (or function). The correct thing is to abstract just this part and create a new one!

Some condition ambiguities are not so destructive to the code, but over time they can be. Therefore, try to avoid them as much as possible.

Better safe than sorry. This famous saying applies to software development as well. Good developers think that things can go wrong, because they will eventually. This way, the code must be prepared to deal with these problems that arise.

Today most languages have resources to handle errors in code through Exceptions and try-catch blocks.

Exceptions: mechanism that signals exceptional events. For example, trying to insert the character “a” into an integer variable;

Try-catch blocks: catch the aforementioned exceptions. Therefore, they must be used globally. After all, the methods already have their functions (which is not to handle errors).

To conclude this topic, an excellent tip to avoid generating errors in your code is simply not to use “null”, either as a parameter or as a return in functions. These returns often require unnecessary checks that, if not done, can generate errors.

YAGNI - You Aren’t Gona Need It

"You aren't gonna need it" (YAGNI) is a principle of Extreme Programming (XP) that states that the programmer should not add any functionality until it is actually needed.

"Always implement features when you really need them, and never when you anticipate you will need them."

It seems obvious, doesn't it, but think about it. How many times have you implemented a feature in a project just because you thought the client would love it, or that it would give the project that edge?

This doesn't mean you should avoid writing flexible code.

It means that you should not include features in your code based on the fact that you may need them later.

There are two main reasons for practicing YAGNI:

You save time, because you avoid writing code that you won't need at the moment;

Your code gets better, because you avoid polluting the code with 'guesses' that end up, in most cases, being wrong guesses;

1- If you don't need the functionality now then don't implement it. You don't need it.

2- Do you really think you will save time overall by spending more time now than in the future?

Semantic Version 2.0.0

Given a MAJOR.MINOR.PATCH version number, increment the:

  1. Major version (MAJOR): when you make incompatible changes to the API,
  2. Minor version(MINOR): when adding features while maintaining compatibility, and
  3. Correction version (PATCH): when correcting errors while maintaining compatibility.

Additional labels for pre-release and metadata build are available as an extension to the MAJOR.MINOR.PATCH format.

Conventional Commits

The Conventional Commits specification is a lightweight convention on top of commit messages.

It provides an easy set of rules for creating an explicit commit history; which makes it easier to write automated tools on top of.

This convention dovetails with SemVer, by describing the features, fixes, and breaking changes made in commit messages.

Conventional Commits Format

<type>[optional scope]: <description>

[optional body]

[optional footer(s)]

Types

feat: A new feature

fix: A bug fix

docs: Documentation only changes

style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)

refactor: A code change that neither fixes a bug nor adds a feature

perf: A code change that improves performance

test: Adding missing or correcting existing tests

build: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)

ci: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)

chore: Other changes that don't modify src or test files

revert: Reverts a previous commit

Examples

feat: add new payment method

fix: resolve issue with login timeout

docs: update API documentation with new endpoints

style: format code with prettier

refactor: reorganize code structure for user module

perf: improve database query performance

test: add unit tests for authentication service

build: update npm dependencies

ci: configure Travis CI for automated testing

chore: update README file

revert: revert "feat: add new payment method"

Scopes (Optional)

feat(auth): add JWT authentication

fix(ui): correct button alignment issue

docs(readme): update contributing guidelines

style(css): apply consistent styling to header

refactor(api): simplify request handling logic

perf(db): optimize user retrieval queries

test(router): add tests for route protection

build(deps): bump lodash from 4.17.20 to 4.17.21

ci(github-actions): add workflow for automated testing

chore(env): update environment variables

Body (Optional)

fix(login): resolve issue with login timeout

The login timeout issue was caused by incorrect session handling in the backend. This fix ensures sessions are properly managed and prevents premature timeouts.
fix(user-profile): correct user age calculation

BREAKING CHANGE: The method `getUserAge` has been renamed to `calculateUserAge` to better reflect its purpose.

feat(payment): add support for credit card payments

ISSUES CLOSED: #123, #124

Breaking Changes

feat: change authentication mechanism

BREAKING CHANGE: The authentication mechanism now uses OAuth 2.0. All clients must update to the new authentication flow.