Despite typos here and there this is a nice article.
Good variable naming is hard but important and when done correctly there is no need for comments.
Function/method names are equally important.
For example when you do this:
// Compute roots of a quadratic equation.// This assumes that (b^2-4*a*c) is positive.lettemp=Math.sqrt(b^2-4*a*c);root[0]=(-b+temp)/(2*a);root[1]=(-b-temp)/(2*a);
You can get create a function called getQuadraticRoot and get rid of the first comment because we now know what the function does.
Same goes with the second one by adding an if:
functiongetQuadraticRoot(a,b){constdiscriminantSquare=b^2-4*a*c;if(discriminantSquare<=0){returnnull;// or whatever makes sense, I’m no quadratic root expert!}constdiscriminant=Math.sqrt(discriminantSquare);root[0]=(-b+discriminant)/(2*a);root[1]=(-b-discriminant)/(2*a);returnroot;
We could then argue about discriminant which could well be called d as it does not really help understand the code: it’s an official formula. E=mc2 won’t be easier to grasp with good variable names. ;)
One more thing. You mention convention but you list teamPointsMax, maxPoints as just right. Convention could be to prefix with max so choices should be maxTeamPoints and maxPoints.
Lastly, you do not need let when filling arrays in a loop.
Wow, thanks for the advice and additional knowledge. I need to learn more. After I reread it, I realized I was careless with lots of typos and wrong implementation. Thank you for being reminded. I have very little experience so I am very grateful to be able to listen to experienced people. Thank you so much for taking the time 🙏
Despite typos here and there this is a nice article.
Good variable naming is hard but important and when done correctly there is no need for comments.
Function/method names are equally important.
For example when you do this:
You can get create a function called
getQuadraticRoot
and get rid of the first comment because we now know what the function does.Same goes with the second one by adding an
if
:We could then argue about
discriminant
which could well be calledd
as it does not really help understand the code: it’s an official formula.E=mc2
won’t be easier to grasp with good variable names. ;)One more thing. You mention convention but you list
teamPointsMax
,maxPoints
as just right. Convention could be to prefix withmax
so choices should bemaxTeamPoints
andmaxPoints
.Lastly, you do not need
let
when filling arrays in a loop.Again, nice article!
Wow, thanks for the advice and additional knowledge. I need to learn more. After I reread it, I realized I was careless with lots of typos and wrong implementation. Thank you for being reminded. I have very little experience so I am very grateful to be able to listen to experienced people. Thank you so much for taking the time 🙏
Glad I can be of any help.
Good variable names is one thing but it's just one step.
If I take your example:
It's more readable but
I find it more readable.
Again, I really learned a lot from you, but here I can only say thank you very much to you and thank you for creating this discussion.
ha no problems. Keep on writing, it opens up discussions. 👌