From the course: JavaScript: Best Practices for Code Formatting

Style guides - JavaScript Tutorial

From the course: JavaScript: Best Practices for Code Formatting

Start my 1-month free trial

Style guides

- [Instructor] If you've already watched one of my other courses on JavaScript best practices, you may already have the background under your belt. If so, feel free to skip ahead to Chapter Two. If this is your first time exploring best practices, it's useful to understand where they come from, and that's style guides. A style guide is a set of agreements on how to write code for everyone working on a common code base, whether that's on an organizational level, a project level, or even a specific repo. A style guide often combines approaches that developers have found useful in working with that code base, with choices that developers have made about how to build and organize that code. Although guidelines can be project-specific, they may also be drawn from broader, industry-wide understandings about the best approach to certain aspects of building code, known as best practices. Some best practices may be nearly universal. For instance, most every developer agrees that using consistent indents is helpful, both in building and reading code. However, many best practices come with trade offs. As a result, managers, project leaders, or groups of developers may need to choose which approach is best for a specific code base. For instance, modern JavaScript provides an almost overwhelming range of options for creating functions. When working on code collaboratively, does it make more sense to use a function declaration, or an expression with let or with const? Maybe an arrow function, but with let or with const? And if you're using a function expression, should it include a lexical function name, to help with debugging? A style guide, based on best practices, does a couple things. First, it keeps you coding, rather than needing to break your flow while you try to balance the options and choose a syntax. It also ensures that the code you write interacts with the rest of the code base, in a particular way. For instance, defining a function using const ensures that it can't be erroneously redefined. As you build your knowledge of best practices, keep in mind there's often no right answer. So it can be useful to understand the trade offs. Different projects may make different choices that work best for a specific team, architecture, or goal. The Airbnb Style Guide, is one of the most widely referenced style guides among JavaScript developers. Another influential JavaScript style guide, is the Google Style Guide. Many best practices can and do change over time. This is especially true in the modern JavaScript landscape, where new features are regularly introduced as optimized replacements for previous practices. So staying on top of changes in popular style guides, as well as keeping up with other style guides that colleagues or developers you respect may be referencing, can be helpful in ensuring that your code reflects up-to-date formatting practices.

Contents