From the course: JavaScript: Best Practices for Functions and Classes

Understanding style guides - JavaScript Tutorial

From the course: JavaScript: Best Practices for Functions and Classes

Start my 1-month free trial

Understanding 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 style guides can be project specific, they may 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 in dense 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 const and 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, heep in mind that 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 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 these style guides as well as keeping up with other style guides that colleagues or developers you respect maybe referencing, can be helpful in ensuring that your code reflects up to date formatting practices.

Contents