Clean Code Principles
clean code does n’t rely on language-specific rules. rather, it relies on language-agnostic principles agreed upon by the developer community. As such, even though the initial wonder on our Slack groove was about how to keep your JavaScript / TypeScript code clean, X-Teamers replied with some of the general design principles of clean code. KISS : Keep It childlike Stupid. A invention rationale originating from the U.S. Navy that goes back to 1960 already. It states that most systems should be kept american samoa elementary as possible ( but not dim-witted, as Einstein would have said ). unnecessary complexity should be avoided. The question to ask when you ‘re writing code is “ can this be written in a simple room ? ”
Reading: A Few Principles of Clean Code
DRY : Do n’t Repeat Yourself. closely refer to KISS and the minimalist purpose philosophy. It states that every piece of cognition ( code, in this case ) must have a single, unambiguous, authoritative representation within a system ( codebase ). Violations of DRY are referred to as WET : We Enjoy Typing, Write Everything Twice, Waste Everyone ‘s Time. YAGNI : You Are n’t Gon na Need It. A developer should not add functionality unless deemed necessary. YAGNI is part of the Extreme Programming ( XP ) methodology, which wants to improve software quality and increase responsiveness to customer requirements. YAGNI should be used in conjunction with continuous refactoring, unit of measurement test, and integration. Composition over inheritance : not an acronym, sadly. It ‘s a principle where you design your types over what they do rather of over what they are. It ‘s explained in more detail in this television. One of the ways to implement this principle is with the Object.assign()
method acting in ES6.
composing is favored over inheritance by many developers, because inheritance forces you to build a taxonomy of objects early on in a visualize, making your code inflexible for changes late on. Favor readability: It ‘s not because a machine can read your code that another homo can. Particularly when working with multiple people on a project, always favor legibility over conciseness. There ‘s no point in having concise code if people do n’t understand it. There are many ways to make your code more clear. Two examples are placing coarse numbers into well-named constants ( e.g. const CACHE_TIME = 200;
) and creating long diagnose rather of shorter ones ( e.g. userHasFormAccess
over canAccess
, which does n’t tell deoxyadenosine monophosphate much ).
Read more: The 36 Best (Old) Books We Read in 2021
Practice consistency: This is arguably the overarching principle of all clean code principles. If you decide to do something a certain way, pin to it throughout the entire project. If you have no choice but to move away from your original choice, explain why in the comments. Of course, this is by no means a comprehensive number. There ‘s indeed much more to clean code. In fact, if you want an excellent ledger on uninfected code, we recommend The Art of Readable Code by D. Boswell and T. Foucher. Want more ? Read about programming best practices to improve the way you write code .