How to Maintain Code Quality at All Stages During IT Project Outsourcing With Husky & Lint-Staged

How to Maintain Code Quality at All Stages During IT Project Outsourcing With Husky & Lint-Staged

Whether your company is a start-up or a well-established business, you can’t get where you want by compromising on quality. IT project outsourcing should always be about return on investment, long-term value, and high-quality user experience

Digital solutions that deliver high performance, efficiency, and reliability can bring back much more, resulting in increased productivity and profitability gains as well as the trust of your users. To achieve this, expert software development, which includes maintaining consistent code quality, is key.

While outsourcing an IT project, using such tools as ESLint and Prettier can help you keep the general code style, however sooner or later you may face an issue that somebody from your team will push shoddy code into a remote repository. The article is about how to prevent this from happening and set up a robust environment for project development.

The challenge of enforcing code quality during IT project outsourcing management

IT project outsourcing helps businesses cover their talent gaps, reduce IT-related expenses, and deliver products to the market faster. It also offers greater flexibility for companies to scale up or down the number of specialists for hire depending on the project needs. 

However, when collaborating on a project with several other developers, keeping a consistent code style is essential as it has a direct effect on code readability, maintainability, and the overall quality of a software product. The process can be automated using the right tools to make sure the code is clean and formatted correctly before it comes up in GitHub repository. 

The ESLint and Prettier tools are helpful as they point out the mistakes you've made in your code that don’t follow the set rules, however, are not that effective just by themselves. When a lot of people are collaborating on one software development project, those mistakes may be easily overlooked and flawed changes added to the common repository on GitHub. 

If the code has a few errors, the app may still work but sooner or later it will result in some broken functionality. Keeping one code style while outsourcing software development projects can prevent this from happening. 

Your project can be set in a way that will simply not allow developers to submit changes with mistakes to the GitHub repository. This is what Husky and Lint-Staged will help us deal with.

Using Husky and Lint-Staged in your JavaScript and TypeScript projects

Let’s start with Husky: This package is built for working with git hooks that execute scripts for various git actions. We can add husky with this command:

npx husky-init && npm install

It’ll create a .husky folder with a pre-commit file. Also, it’ll add the prepare script into our package.json. This script will create husky files every time after installing node_modules so the only files that we need to keep and share via GitHub are hook script files like pre-commit or pre-push:

"prepare": "husky install"

Next step is adding lint command to package.json:

"lint": "eslint --fix"

And edit pre-commit like below so on every git commit we will run lint and check our project files. If some code breaks ESLint code rules, Husky will throw an error and will not allow commiting changes:

#!/usr/bin/env sh
. "$(dirname -- "$0")/_/"

npm run lint

Committing changes is a frequent process and linting all the project files is a long and extensive procedure. Here is where Lint-Staged helps us up. It allows us to lint only staged (marked with git add command) files that minimize linting time and keep every commit clean. 

Let’s add it to our project with: 

npm i -D lint-staged                                                                                                                        

Then modify package.json by adding the lint-staged command from which we will run lint:

"lint-staged": {
"*.{js,jsx,ts,tsx}": "npm run lint"

And replace the executing command in the pre-commit file:

from npm run lint to npx lint-staged

If we also want to run tsc on staged files, we can’t just do it in the same way as the lint command because of a TypeScript issue that doesn’t allow you to apply the project's tsconfig setting while checking specified files.

To fix it, let’s install tsc-files package:

npm i -D tsc-files

And include it to lint-staged command, so that it looks like this:

"lint-staged": {
    "*.{js,jsx,ts,tsx}": "npm run lint",
    "*.{ts,tsx}": "tsc-files --noEmit"

Now it will also run tsc --noEmit only on staged files, but you have to remember that importing invalid TypeScript code in your staged files will also throw an error.

Another thing that we want to add to our project is a pre-push hook. Pushing isn’t so frequent and we may make eslint checking, run tsc, and tests (if we have to) for all project files, just in case these code changes affect other parts of our app functionality.

To create this file with a corresponding command, just run the instruction below:

npx husky add .husky/pre-push "npx eslint . --fix && npm run tsc && npm run tests"

That’s it, the setup is ready for use. 

With Husky and lint-staged, you can set up a development environment that will keep the code error-free and make sure no one from your team pushes the flawed code to the repository. 

These tools will help you always check your code style when new functionality is added: only the changed files while committing, and the whole code of the project while pushing the changes to the GitHub repository. In this way, you can be confident that new changes don’t interfere with the software development project in a negative way and that everything works well together.

Code review: Its role in maintaining a high-quality code in IT projects

Lint-staged and Husky are very useful tools needed to detect those lines of code that don't follow the rules set on your IT project. However, they also have their limits. 

We can avoid some simple mistakes this way, but more complex ones, such as logic mistakes, cannot be discovered by those tools, only with the help of other people. That’s why performing code review is important. Code review is the practice of analyzing the software code of one developer by the other to detect bugs and logic inconsistencies before adding this code to the main branch.

So, how does it work in IT project outsourcing?

  • At Apiko, we look through the code first and check it for the presence of orthographic mistakes or some complex logic that can be written more simply. Such mistakes are not exactly defined in the ESLint and Prettier rules, so only developers can see them. Machines are not able to detect the meaning behind your code, which is really important in software development.
  • One more thing that we do is let the developer, who performs a code review, launch our code to see how the project and that particular feature work. It reduces the amount of mistakes that later will be discovered by testers, some rough errors. And then, if the code is error-free, it gets merged. 
  • If not, in GitHub you can leave comments about the mistakes discovered during code review and the developer who wrote this code initially fixes them. Then the code is reviewed one more time and if everything works well, the code gets merged into the main branch and becomes part of an app.

By setting up the right tools such as Husky and lint-staged and performing code reviews, you can ensure that your code is bug-free, properly formatted, and stable. These are the simple but effective steps you can use to guarantee the highest quality of your code while working on an IT project.

Outsourcing software development projects: summing up

IT project outsourcing can be a way out for a lot of companies that want to develop high-quality software products in a cost-efficient way. Working with proven software project outsourcing companies plays an important role in securing the right skills and implementing your organization’s vision. 

Since the demand for experienced software developers keeps growing, using outsourcing services enables cost-saving and flexible cooperation, faster product launch, and a steady supply of talented professionals for your business growth. Give it a try.

Apiko can set up, scale and manage a remote dedicated development team or provide you with IT outstaffing services to work as a seamless extension of your in-house unit. Feel free to contact us for more details.