All good tips for newbies, but personally I wouldn't recommend the 1st. I think it's important to be explicit when defining variables so others can quickly scan the code.
That would then be a matter of code inexperience rather than writing clean code.
function example(backgrounds) {
let background;
let array;
let color = getUserBgColor();
backgrounds.forEach(color => {
if (color === 'red') {
array.push(color);
}
})
background = array[Math.floor(Math.random() * array.length)];
return background;
}
compared to this
function example(backgrounds) {
let background, array, color = getUserBgColor();
backgrounds.forEach(color => {
if (color !== 'red') {
array.push(color);
}
})
background = array[Math.floor(Math.random() * array.length)];
return background;
}
It would also reduce amount of lines which could be a great refactor for a big file. (In addition, the foreach loop would not need to have a space before it if the variable declares are set within the same line)
I'm not following your train of thought here PatricNox... or did you change your mind between comments?
"If the vars are of the same value...."
"let background, array, color = getUserBgColor();"
Also, If the purpose of refactoring is to reduce the cognitive load and error surface area, then your example would probably be best written as:
This assuming that I correctly guessed the behaviour and purpose for the "color" variable you define outside the forEach loop, and the other one inside it, as well as the magic string 'red'... which your colleagues should never have to do.
When you work with and review thousands of lines of code per day you want clean legible code. Scanning code vertically is more efficient. Writing a couple extra lines greatly helps the reviewers and maintainers.
Agreed, but I also want to point out that making extra lines of code that helps you reason better doesn't make the code more readable at all. In fact, it can make your code thousands of lines long when a a dozen might do the job with less bugs.
I think we are talking about different things. I replied to PatricNox's comment with an example that takes less LOCs and is imho less error prone. What were you replying to?
Agreed, but I also want to point out that making extra lines of code that helps you reason better doesn't make the code more readable at all. In fact, it can make your code thousands of lines long when a a dozen might do the job with less bugs.
Oh, that's not about variables at all, it's really about what happens inside a function and what should be outside it.
Both examples of uninitialised variable declarations let a, b, c = 1 or let a; let b; let c =1 or even let [a,b,c] = [,,1] are code smells imho, it just looks messy and error prone.
All good tips for newbies, but personally I wouldn't recommend the 1st. I think it's important to be explicit when defining variables so others can quickly scan the code.
If the vars are of the same value then I would only see it as a benefit as someone who reviews the code.
Why would the vars all be the same value? Unless you had them all undefined:
let x,y,z
.If you're assuming that
let x, y, z = "post"
assigns "post" to all, it doesn't, and so this reinforces my point.That would then be a matter of code inexperience rather than writing clean code.
compared to this
It would also reduce amount of lines which could be a great refactor for a big file.
(In addition, the foreach loop would not need to have a space before it if the variable declares are set within the same line)
I'm not following your train of thought here PatricNox... or did you change your mind between comments?
"If the vars are of the same value...."
"let background, array, color = getUserBgColor();"
Also, If the purpose of refactoring is to reduce the cognitive load and error surface area, then your example would probably be best written as:
This assuming that I correctly guessed the behaviour and purpose for the "color" variable you define outside the forEach loop, and the other one inside it, as well as the magic string 'red'... which your colleagues should never have to do.
When you work with and review thousands of lines of code per day you want clean legible code. Scanning code vertically is more efficient. Writing a couple extra lines greatly helps the reviewers and maintainers.
Agreed, but I also want to point out that making extra lines of code that helps you reason better doesn't make the code more readable at all. In fact, it can make your code thousands of lines long when a a dozen might do the job with less bugs.
Weβre only talking variables here. Besides, with destructuring and using modules your files should never be that big.
I think we are talking about different things. I replied to PatricNox's comment with an example that takes less LOCs and is imho less error prone. What were you replying to?
This
Oh, that's not about variables at all, it's really about what happens inside a function and what should be outside it.
Both examples of uninitialised variable declarations
let a, b, c = 1
orlet a; let b; let c =1
or evenlet [a,b,c] = [,,1]
are code smells imho, it just looks messy and error prone.Ah I see, then I think we are talking about different things. The max thread levels makes this confusing.
And I agree about undefined variable declarations.