DEV Community

Cover image for πŸš€ Why Use ESLint in Your Project?
Jean Lucas
Jean Lucas

Posted on

πŸš€ Why Use ESLint in Your Project?

When working on a project, maintaining code quality and consistency is just as important as writing functional code. And who has never encountered a piece of code that's hard to understand?

Nowadays, refactoring has become a common practice due to code being written without proper patterns and best programming practices. This is where ESLint comes in, helping developers prevent bugs, enforce best practices, and improve maintainability.

What is ESLint?

ESLint is a powerful linter for JavaScript and TypeScript that analyzes your code, detects issues before they cause problems, and even highlights areas that can be improved to meet the best standards. It enforces rules based on predefined or custom configurations, helping teams maintain a clean and consistent codebase.

Why Use ESLint?

βœ… Catches errors early
Detects common mistakes such as unused variables, incorrect imports, and potential runtime errors before they break your application.

βœ… Enforces best practices
Encourages good coding habits by following recommended standards, such as using === instead of == this exemple is simple but give us a glimpse the intent of ESlint.

βœ… Improves readability and maintainability
A well-structured and consistent codebase makes it easier for developers to collaborate and understand the code. It also saves time in the future when adding new features or onboarding new developers.

βœ… Boosts development speed
By automating code analysis, developers spend less time debugging and more time building features.

βœ… Compatible with Prettier
ESLint can work alongside Prettier to format and lint your code, ensuring both style and quality are maintained.

πŸ“Œ If you don't know what Prettier is, check out my post:
πŸ’… Why Use Prettier in Your Project?
πŸ”— https://dev.to/jean_lucas/why-use-prettier-in-your-project-58dp

Let's see a simple example

Take a look at the code below. We can identify two common issues that might lead to unexpected behavior in an application:

1️⃣ Variable typing is missing.
2️⃣ Comparison is done using == instead of ===.

ESLint alerts us with messages explaining these issues and prevents the project from building until they are fixed. This guarantees better code quality for both local development and deployment.

Without ESLint:

function compareValues(firstValue, secondValue) {
  if (firstValue == secondValue) {
    return 'They are equal';
  }
  return 'They are different';
}
Enter fullscreen mode Exit fullscreen mode

With ESLint:

Now, all issues are fixed, making the code safer and more reliable:

function compareValues(firstValue: number, secondValue: number) {
  if (firstValue === secondValue) {
    return 'They are equal';
  }
  return 'They are different';
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

Using ESLint is a game changer for modern development, ensuring clean, error-free, and maintainable code. Combined with tools like Prettier, it helps you and your team focus on building great software instead of fixing code issues.

πŸ“Œ Do you already use ESLint in your projects? How has it helped you? Let’s discuss in the comments! πŸ’¬

Top comments (3)

Collapse
 
teqvunguyen profile image
teq-vunguyen
  "rules": {
    "no-var": "error",
    "semi": [2, "always"],
    "quotes": [2, "double"],
    "prettier/prettier": [
      "error",
      {
        "endOfLine": "auto",
        "trailingComma": "es5"
      }
    ],
    "@typescript-eslint/indent": 0,
    "@typescript-eslint/no-explicit-any": 2,
    "@typescript-eslint/explicit-module-boundary-types": 0,
    "no-return-await": "off",
    "@typescript-eslint/return-await": "error",
    "no-use-before-define": "off",
    "@typescript-eslint/no-use-before-define": "off",
    "react/display-name": "off",
    "react/jsx-filename-extension": "off",
    "import/extensions": "off",
    "import/no-extraneous-dependencies": "off",
    "react-hooks/rules-of-hooks": "error",
    "react-hooks/exhaustive-deps": "warn",
    "dot-notation": "off",
    "@typescript-eslint/dot-notation": "off",
    "react-refresh/only-export-components": "off",
    "@typescript-eslint/consistent-type-imports": "error",
    "simple-import-sort/imports": "error",
    "simple-import-sort/exports": "error",
    "import/first": "error",
    "import/newline-after-import": "error",
    "import/no-duplicates": "error"
  },
Enter fullscreen mode Exit fullscreen mode

my rules :D

Collapse
 
hardikgohilhlr profile image
Hardik Gohil

ESLint is an absolute lifesaver for keeping code clean and consistent! πŸ”₯ Pair it with Prettier, and you’ve got a smooth workflow.

Speaking of clean code, I’ve built Logar, an open-source log management tool with Next.js, Tailwind, ShadCN, Clerk, and Supabase – and, of course, I’ve set up a solid ESLint + Prettier config for it!

πŸš€ Check it out here:
πŸ”— Live: logar-app.netlify.app
πŸ’» GitHub: github.com/HardikGohilHLR/logar

Would love to hear your thoughts! πŸ˜ƒ

Collapse
 
aaronmccollum profile image
Aaron McCollum

This was a great description. I haven't used any linter yet, but I've heard people talk about ESLint a lot. This is helpful to know what they are talking about.