This is Part 2! If you like this, I would appreciate it that you also check out Part 1! Also if you want to see more tips and talk to me, go to my ...
For further actions, you may consider blocking this person and/or reporting abuse
Nice post. I would like to add that comments should usually be avoided, especially when few people work on the same codebase.
There are multiple reasons, but the most obvious maybe:
This could lead to misunderstanding in the future.
Having this in mind, I agree with "Mistake 4", but it would be even better to put the code in a separate function with a descriptive name. For example "prepareDataForSending()"
I totally agree here.
This can be a problem especially if you are using JSDoc and suddenly your function changes but people forget to update the JSDoc.
If you have a function that is like 200 lines of code you should exactly do what you are proposing. you should split it up into smaller functions. I would even go that far that if your function is longer then 30 lines you can split it up.
Thanks for the comment and its a really good point!
I have seen too much code whose purpose is never explained, and been told too many times to "read the code", as if by some process of clairvoyancy that will tell me what was in the mind of the coder.
If a method is called "setMessage()" and you change its functionality so it appends instead of replacing, you don't leave the name unchanged, do you? So why are comments any different?
It's the responsibility of all programmers to ensure their code remains readable and its purpose is clear. As with method names, without appropriate comments the purpose is often far from clear. Don't duck the responsibility.
It's true that all programmers need to ensure that their code remains readable, and again, this could also be the one more reason to use descriptive function names instead of comments.
These are some reasons why I think that descriptive names are better than comments:
Comments inside a function used to describe what some block of code does can lower readability.
If we need a comment to describe a block of code, then this block of code can be moved to a separate function.
A function should do only the one main thing which should be easily described by its name.
A large number of comments also lower the readability.
There is more chance to see that some function has a bad name than that comment is bad.
Functions are recognized by modern tools and IDEs, while comments are not. This can help us to recognize that name is bad even outside of a file the function belongs.
Additionally, functions should be short enough that comments are not needed.
However, sometimes comments could be our friends:
public functions should be commented (documented) especially in public libraries/packages - this could help us to understand what those are doing without the need of looking at source code.
todo's - but please, do not push it to the repository or rely on someone else to fix it or to see it (I used to see some todo's more than 2 years old). It is better to create a new ticket/task/issue.
It's not easy to make fixed rules about these things, but I do believe that too little documentation is the enemy of maintainable code. In the end we have to rely on the professionalism and common sense of the programmer. If (s)he doesn't have any we're doomed anyway.
I think I'm generally in agreement with what you say. I advocate the implementation of 'user stories' as high-level code, if only as a set of single-use functions, to match (where possible) the project brief supplied by the client. Then it's quite clear what the intention of the program is without the need for excessive documentation. That tends to answer the "why?" question that the code itself rarely reveals.
Definitely agree about comments. Just occasionally they are required, but if you have to describe something using comments then that's probably a smell since it should be easy enough to follow the intent of the code. Code should always follow DAMP principles, which ties in with one of the other points about not using single letter variable names. There's no excuse for that.
I see the SOLID force in you.
Clean code FTW
That's it. Good guess :) and I totally agree with him.
Very informative post, thanks a lot and Captain Obvious!!! Ahahaha good one....
Your welcome. How you could get some useful internation.
If tests are specified and you have written them (some people don't and that's an instant fail), then for the love of all that is good, make sure they actually run. I have reviewed code tests in which there are failing tests and that test was instantly rejected as a result.
Also, don't write tests for the sake of it: Your tests should be usefully exercising your code, and test cases should be things such as boundaries and edge cases not just the same test run a few hundred times with slightly differing values that would never cause a different outcome.
I've seen this kind of thing in code tests submitted for mid-senior positions.
Great points!
Always comment your intent; don't just restate the code.
Yes true!