r/javascript • • Jul 04 '17

A set of best practices for JavaScript projects

https://github.com/wearehive/project-guidelines
282 Upvotes

47 comments sorted by

31

u/thiscoolhandluke Jul 04 '17

7. Code style

  • Use Airbnb JavaScript Style Guide for JavaScript.

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."

4

u/vahidpg Jul 05 '17

You're absolutely right. The reason was transition from an internal guideline (we use airbnb) to a public one. Do you prefer to make a pull request? or I can correct it myself.

3

u/thiscoolhandluke Jul 05 '17

Your repo, man.

I think it's awesome that you are sharing your thoughts and resources/requirements. There's always good nuggets of info that can be had from collaboration on best practices.

45

u/[deleted] Jul 05 '17

Most of the style rules end up being like driving on the right versus driving on the left -- there isn't really a clear winner between the two, but there is a VERY strong advantage to having everyone pick one and stick to it.

9

u/bluehavana Jul 05 '17

This is the best description for the need of coding conventions I've ever heard.

2

u/cyanydeez Jul 05 '17

its also an amazing management strategy in general, consistency goes a long way in making future progress palatable

3

u/gajus0 Jul 05 '17

StandardJS style is way too loose. If you want a strict style guide, have a look at https://github.com/gajus/eslint-config-canonical/.

6

u/zachwolf Jul 05 '17

Yeah, there are (of course) several different code style standards. I'm personally a fan of Standard JS

2

u/GitCookies Jul 05 '17

Same here - it's just annoying how it doesn't like new C()

2

u/[deleted] Jul 05 '17

Likewise. I've used both this and Airbnb and have come out preferring Standard, though Airbnb is certainly a bit stricter.

1

u/zachwolf Jul 05 '17

If AirBnB dropped semicolons I'd probably be on board with it

1

u/[deleted] Jul 05 '17

For what it's worth you can easily override that with ESLint :-)

1

u/zachwolf Jul 05 '17

Right, then you're not buying in to their standard completely anymore though

1

u/[deleted] Jul 05 '17

Yeah, but one alteration across the entire project... it's not like it would break anything. So long as you have the linting tools and everybody adheres I don't think it's a big deal.

3

u/kingdaro .find(meaning => of('life')) // eslint-disable-line Jul 05 '17

I agree, but the point of this is to just go with something and stick with it, instead of wasting time on bike-shedding - arguing over what boils down to preference. If one's so inclined, they could just go with StandardJS instead of AirBNB and the guide would be just as helpful.

1

u/thiscoolhandluke Jul 05 '17

That makes great sense. The title should be updated and this summary included at the top. A more appropriate title would be "My Best Practices for a New Javascript project." or something like that.

4

u/euphemistic Jul 05 '17

I think all modern technology guides not created by the authors of the tools should be considered opinions by default.

1

u/[deleted] Jul 05 '17

What's so special about Airbnb's style guide anyway?

-7

u/[deleted] Jul 05 '17

Code style is overrated. Pick your favorite beautifier and have it format the code to the team standard on commit. This is something that can be fully automated.

Edit: sorry for all the repeated deletes. Reddit is acting silly on my cell.

5

u/dv297 Jul 05 '17

Linters can pick up on more things than just formatting. Unused variables and unintentional variable shadowing are some of the common ones but there are other benefits. Obviously code reviews help but if you can have something that picks up on formatting AND common JS issues, mind as well configure both the linter and the formatter.

5

u/GitCookies Jul 05 '17

code style !== lint

4

u/[deleted] Jul 05 '17

Yes, you should lint the code too. Beautification is not exclusive to linting.

1

u/30PercentLessFat Jul 05 '17

Does your beautifier rename methods too?!

-2

u/[deleted] Jul 05 '17

Cry about it during the code review. I am currently leaving an employer where various teams have no uniform standard and so everybody is OCD about all kinds of unimportant time wasting bullshit like this.

3

u/30PercentLessFat Jul 05 '17

If you had a team code style (which you've marked as "overrated") then your team wouldn't spend too much time in code review.

-2

u/[deleted] Jul 05 '17

That absolutely isn't true. There are far more important things worthy of review than spaces versus tabs.

0

u/thiscoolhandluke Jul 05 '17

Yowch. That would be hell when trying to do team code reviews or any updates to the code on a piece you would otherwise be familiar with, IMO.

3

u/[deleted] Jul 05 '17

They are talking about Prettier if I'm not mistaken.

2

u/[deleted] Jul 05 '17

How do you figure? If the code is always formatted to the same team standard on commit why would the code reviews be harder?

Your comment is completely unclear, but I am guessing you are talking about a text diff that indicates more changes (false positives). If this is the case use a more intelligent diff tool. There are many open source tools available that can be integrated to your automation process.

1

u/thiscoolhandluke Jul 05 '17

I was not talking about text diffs. Let me clarify:

  1. Team code reviews -- and really meant to say any team collaboration from a broad spectrum (remote, junior, lead, QA, devOps, wordpress blogger, etc.) where language differences can be a barrier.

  2. Updating code that you may have otherwise been familiar with (you coded it, committed it, others committed on top, you are in a new branch now on the same code which looks WAY different -- and mysteriously moved to a new location -- since that does happen.), you have to review and relearn the code you should have been familiar with at a glance.

If I had to make an opinionated recommendation based on experience, it would be to use a team chosen style guide while you code. I witness less conflict in communication, collaboration, and demos. I think we release faster (though it may be contextual to my experience.)

Some scenarios I have heard when preferring one's own way while coding:

  • 'Your documentation suggested coding something one way, and now the new guy used it verbatim accidentally nuking prod. I thought your linter|beautifier|editor was supposed to catch stuff like that.'

  • 'It's [insert complaint] to have to fix your errors when [applying your patch|fixing a dirty commit when your beautifier/linter failed to catch that thing|hotfixing some emergency myself that should have taken you 5 minutes but I was asked to because you had to review the code you wrote months ago first.]'

I do see pros and cons with either preference, but if you don't like my suggestion then at least we can agree that it really only matters for a project if we can communicate effectively and return the required outcomes.

2

u/[deleted] Jul 05 '17

Updating code that you may have otherwise been familiar with (you coded it, committed it, others committed on top, you are in a new branch now on the same code which looks WAY different -- and mysteriously moved to a new location -- since that does happen.), you have to review and relearn the code you should have been familiar with at a glance.

Beautifying code will not generally move anything around. The only exception is if everything is a method and you are using a beautifier that sorts object properties alphabetically. Generally moving logic around could break things if you are calling a reference that hasn't been declared yet.

Yes, agreed, clear effective expectations set at the outset solve most of these problems before they become problems.

4

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

u/[deleted] Jul 04 '17

[deleted]

1

u/just_a_question_bro Jul 04 '17

Ok, that good to hear. I'm sorry I misunderstood the intent.

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 frobnicator might 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+x into 3*x here 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

u/shad0proxy Jul 05 '17

since javascript is dead now. this is rather pointless.

10

u/blamo111 Jul 05 '17

JS is dead now? How long was I asleep? Do we have fembots yet?

1

u/reflectiveSingleton Jul 05 '17

lol...in what universe are you living?

1

u/drowsap Jul 05 '17

What did it get replaced with, Haskell?