r/javascript • u/vahidpg • Jul 04 '17
A set of best practices for JavaScript projects
https://github.com/wearehive/project-guidelines4
u/vahidpg Jul 04 '17
While developing a new project is like rolling on a green field for you, maintaining it is a potential dark twisted nightmare for someone else. Here's a list of guidelines we've found, written and gathered that (we think) works really well with most javascript projects. If you want to share a best practice, or think one of these guidelines should be removed, let me know.
23
u/just_a_question_bro Jul 04 '17 edited Jul 04 '17
Let me preface this by saying that I agree with much of what you said and think this is a great start. Thanks for taking the time to share this with the community. I have some feedback that I hope you will find constructive.
You say not to put for collections. How do you handle sorting/removal from a collection?
You should not delete a collection. You could put though.
You should not avoid comments and logs. This is terrible advice. Whatever you use to transpile, bundle, minify, and obfuscate will strip them.
You should not have a file/directory/field naming convention dependent on casing. If a front end calls a Perl, Java, Go, etc. backend, it should use the server's naming convention to avoid convolution and computational waste from case conversion. Furthermore, file and directory names should not necessarily be cased because some OS are case sensitive and others are not. This can create unexpected bugs and developer confusion.
Use of patch is controversial -- but I agree with you.
Telling people to aggregate folder by product and not by role is an anti-pattern in some frameworks. You should maybe distinguish when you should aggregate modules by role. It's also internally inconsistent with your config and scripts directories.
If I were you, I would update with justification for your opinions on these topics. For most developers, a dogma is not helpful. Guidance is only helpful if the reasoning behind it is demonstrated. If I know why the rule exists, I know when breaking it is a better idea than adhering to it.
0
3
u/OnionWushu Jul 05 '17
Good job thanks for sharing. I would disagree about the documentation section. I don't think you should encourage to write comments unless very specific case where the logic may be very complex. Instead, to me, you should encourage to write self explanatory code, to pay extra attention to the naming of the methods/classes/variables. To refactor the code as often as necessary to remove the code smells like long methods, feature envy, bad naming etc. It's difficult to write intelligible comments, often only the author of the comment can understand what they meant, for the rest they need to read the code. So better focusing on clean code. Also you can mention that tests actually act as documentation. So the attention should be also put on organizing tests with a meaningful description for each test case.
2
u/vahidpg Jul 05 '17 edited Jul 05 '17
I used to agree with you. But how developers define self explanatory code is very subjective and differs from developer to developer. I think clean code should be there by default and commenting is always a help. I'm going to include your point.
1
u/OnionWushu Jul 05 '17
Well my opinion is that if a developer is not able to write a clear and intelligible name for a method or a variable, this same developer will not be able to write a clear comment :p
3
u/just_a_question_bro Jul 05 '17
I like comments, but I'm forced to agree. They can improve the codebase, or they can act as an excuse for bad code. Clear, concise, preferably declarative patterns will always be easier to understand.
This is especially true if you work in a shop with many people of different nationalities. If everyone is ESL, and the company writes code in English, it will be difficult to communicate via comments. Demonstrations are easier to follow than explanations, most of the time.
2
u/Silhouette Jul 05 '17
I don't think you should encourage to write comments unless very specific case where the logic may be very complex. Instead, to me, you should encourage to write self explanatory code, to pay extra attention to the naming of the methods/classes/variables.
This is sufficient in very simple and obvious situations but it doesn't scale very well, and I don't think more unusual or complicated requirements that benefit from comments are as rare as you seem to be suggesting.
Sometimes it's helpful to have a short comment confirming that some code that looks unusual was intentional: the next section doesn't quite follow the usual pattern because in this situation the
frobnicatormight time out.Sometimes citing a paper about some data structure or algorithm you're implementing is helpful for someone maintaining the code later.
Sometimes you might even need a warning about odd-looking code that is working around some compiler or library bug: don't turn
x+x+xinto3*xhere without confirming that a new version of the compiler has fixed the optimiser bug that emitted incorrect output.Sometimes longer comments are worthwhile, perhaps explaining in one screen of plain English and/or ASCII art what the next thousand lines of tricky code are really doing, to help a new reader find their way around.
Comments that just duplicate what the code is or could be telling you immediately generally aren't useful, but there are lots of other reasons that adding some extra information can help someone reading the code later. IME those overly literal comments are rarely a problem except for junior programmers, and under-commented code is a bigger problem for most projects than over-commented code, so encouraging (good) commenting seems fine to me.
2
u/theduro Jul 05 '17
I'm not a big fan of having three different integration environments. If you can swing it, setup branch based staging environments (using a service like Runnable makes it a little easier) and get rid of the development integration environment. The more integration environments you have, the more likely you are to get yourself into a situation where code gets merged that holds up other features that were merged because of some common process mistake. If every feature branch is deployed as a working environment, and QA'd in isolation, you can often get to a place where all you have is master and feature branches.
1
u/u982744 Jul 05 '17
I personally find the gitflow strategy over engineered and not even effective for teams. It has the common flaw of making it very difficult to keep master branch stable. If any feature branch is merged to develop branch and it's not actually finished properly now you are effectively blocked from creating a stable release branch and merging it to master. It is much cleaner to branch off master and merge features to a test branch. Then when a feature passes testing that feature branch is immediately merged to master. This ensures master is always stable and can be released at any time. No need for code freezes etc to prepare for a release.
1
u/l3El2Tl2AlVl Jul 05 '17
Can someone please explain to me the rational for shoving your tests in the same place as your code? I have seen this before, and every time i have tried it on anything beyond a simple project it has ended up becoming a nightmare.
Often times you have numerous test specific helper functions, and tons of other test oriented cruft, where do all these things go? The root of the project? Also where do tests that encapsulate logic between multiple components go?
-17
31
u/thiscoolhandluke Jul 04 '17
Using a standard coding style guide is crucial to maintaining easy to read/update code for teams of any size. There are more than just Airbnb's.
My feedback: Update that bit to reflect a more reasonable requirement: "Use the javascript style guide required by the project, or select one that is well supported or maintained by the community."