Loomio

Styleguides

JH Jonne Haß Public Seen by 69

So looks like we want to establish coding styleguides and actually enforce them. Let's try to find one that we all like.

Tools

We recently enabled Hound for Javascript. That should settle JSHint as linter for Javascript. Hound supports Ruby and SCSS linting too, through Rubocop and and scss-lint respectively. Those seems to be flexible tools, so let's settle on these too and enable Hound for Ruby and SCSS too.

Ruby

Rubocop defaults to the bbatsov styleguide plus some additional checks not specified there. Hound changes some of Rubocops defaults, unless you specify your own config, to match the thoughtbot styleguide. I can not fully agree to either, however bbatsov's provides a good base, especially since Rubocops defaults are modeled after it, which makes it less work to configure. Therefore I started a Rubocop config that we can use as a base for discussion. I tried to add some reasoning to most entries. However let me point out the main choices and deviations from bbatsov here:

  • Line length: 80 is a bit too short to enforce, especially since we have bigger screens these days. I'm working with 120 for years now and so far nobody complained.
  • Different spacing rules for { ... } blocks and hash literals, in order to differentiate them better
  • Using fail requires you to know that it's just an alias for raise with no benefit, in other places the styleguide already discourages using aliases. Having more than one semantic meaning for exceptions is wrong in the first place, it suggests that you're using them for control flow.
  • No safe assignment, that is, do not allow if (foo = bar) while disallowing if foo = bar. We should either allow assignments in if statements or not, this is just inconsistent.
  • Do not enforce block argument names for inject. Enforcing variable names, especially single letter ones is just plain silly.

Additional rules and deviations that I couldn't make out a cop for:

  • Define exceptions as class Foo < RuntimeError; end, not Foo = Class.new(RuntimeError). The later requires more knowledge about Rubys meta model and what the argument to Class.new does for basically no benefit.
  • Disallow spaces inside string interpolation that is allow "a #{b} c" but not "a #{ b } c". This is to settle to one style and keep it consistent.
  • Disallow doing control flow with && and || outside conditionals. The only valid uses of && and || are in a condition and to compute a predicate that's returned from a method.

Please read bbatsov's styleguide and then my Rubocop config if you want to know more.

Javascript

(last edit by Steffen)

The proposed deviations for our settings are: (follow the links for more information about the options)

  • camelcase : true
  • latedef: true
  • maxlen: 120
  • nonew: false. nonew would prohibit the use of constructor functions for side-effects. While testing that setting to me it looked like we need those side-effects for our backbone code. Does anyone have more information about that? (I am no javascript/backbone.js expert)
  • quotmark: 'single'
  • eqeqeq: true
  • freeze: true
  • indent: 2
  • devel: true. We use alert and confirm in some places.
  • eqnull: true.
  • lastsemic: true
  • supernew: true. We use that in some places. I don't know if we should get rid of it but because we are currently using it this is set to true.
  • I also added some global variables we are using.

SCSS

(edited by Pablo Cúbico)

I'm suggesting using the styleguide from Bootstrap, which is a little leaner than things like BEM and OOCSS.

http://codeguide.co/#css

Some ideas from its maker:
http://markdotto.com/2014/07/23/githubs-css/

I'll try to match the configuration of SCSS Lint to it and post it here.

Porting the codebase

We won't do it. As reasoned excellently in Hound's faq, the way to adopt a styleguide is to gradually port all code you write and touch to it, not to do it in one go.

JH

Jonne Haß Sun 22 Feb 2015 11:35PM

In both of your examples I see 40-60% of the comments as superfluous. I mean things like /** Some constants */? Or /** My bar id */, bid: null,, here the time would've been better spend by using a self-explanatory name, like bar_id. Most of the comments of your Java example are for the API docs, not to explain the actual code or even the classes structure.

I think, from now, at least every new JS contribution has to be commented.

Java styleguides often have such a requirement, and I know enough developers who don't like it. Therefore I highly disagree with such a requirement. Giving an overview at the top of your class / file about its purpose can be good if it's non-obvious, but it should be applied as necessary, not as necessity. The later only leads to superfluous noise in the code, like in the examples I pointed out (which I didn't even have to scroll down a page for).

DU

Deleted account Mon 23 Feb 2015 7:37AM

In both of your examples I see 40-60% of the comments as superfluous.

That's what I say : you see. But you don't code for yourself, you code for every other developer of diaspora*.

A code is never self-explanatory. It might be for you, but I'm sorry to say that, given a complex function, a name with two or three words is never enough. Especially in JS.

and I know enough developers who don’t like it

Oh ! This is an actual study from the institute of the wet finger !?

At work, I'm responsible for the documentation of all the projects. I see every day devs who were convinced their code was self-documented, no need for comments or actual document. And see, 5 years later, they are unable to say what their code do, what they did it for. For some project, we are even unable to say what they are used for and if they are stil up-to-date ! Documentation and comments are not luxury.

Giving an overview at the top of your class / file about its purpose can be good if it’s non-obvious

Who determines it is non-obvious ? We have to be clear : wether we do, wether we don't. Where you see superfluous comments, I see a sane way of coding where comment helps to see the logic and the structure of the code.

F

Faldrian Mon 23 Feb 2015 7:58AM

When you want to know how something specific works, you will always read the code.

But what we really would need (and I think this is more important than inline-comments or class-comments) is a top-down overview of the whole project, maybe with 2-3 different zoom-levels, explaining how everything works together (graphically with annotations). That would not be part of this styleguide-thing, but a new piece of works.

@augier I don't think we have to introduce mandatory comments here. If you think they are useful and want to add some - you can, but you don't have to.

JH

Jonne Haß Mon 23 Feb 2015 12:21PM

I guess we have to put that point to vote then, let me answer to one more of your points though:

given a complex function, a name with two or three words is never enough.

The problem in that case is not that you have no comment on it, the problem is that you didn't break up your function. I only have a Ruby example at hand, but let's take it. This method calls a bunch of other methods that aren't used anywhere else, they're extracted not for reusability but for readability and to reduce the size of the method.

I can recommend this book, it has an excellent chapter on comments too.

DU

Deleted account Mon 23 Feb 2015 2:58PM

But what we really would need (and I think this is more important than inline-comments or class-comments) is a top-down overview of the whole project, maybe with 2-3 different zoom-levels, explaining how everything works together (graphically with annotations). That would not be part of this styleguide-thing, but a new piece of works.

Agree.

@faldrian : I don't have strong opinion about Ruby, but concerning JS, and given how this language is unnecessarily verbose, I really think we should have document comments for each function more than 4 lines. And there is a lot of them.

The problem in that case is not that you have no comment on it, the problem is that you didn’t break up your function.

This is also a problem, for sure. The there are many complex function of that kind in the JS part. And we all know that they will never be refactored.

Your Ruby example is not fully relevant as Ruby is a very concise and easy to read, which is not the case of JS.
Please also notice that it has many doc comments and is not as difficult to read as you say.

Also, I think we should do an inventory of the SCSS files during the port to BS3.

F

Faldrian Mon 23 Feb 2015 3:05PM

as Ruby is a very concise and easy to read, which is not the case of JS.

I think your milage may vary. I find ruby very disturbing to read and feel like in wonderland when I can read JS. So this does not depend on the language.

JH

Jonne Haß Mon 23 Feb 2015 3:11PM

Also the comment in my example is for the API docs, the extracted private API has no comments at all.

DU

Deleted account Mon 23 Feb 2015 4:07PM

About quotmark, I've adopted the styleguide of Ruby mine for both JS and Ruby, i.e : ' ' by default and " " only when necessary.

JH

Jonne Haß Mon 23 Feb 2015 6:12PM

In Ruby, I found myself repeatedly changing 's to "s since I wanted to use string interpolation or have an interpreted \n and so on, therefore I adopted to default to "s and only use ' if I need to their semantics. Any such reason for preferring ' in JS?

F

Faldrian Mon 23 Feb 2015 6:25PM

Of course! :D https://twitter.com/mathias/status/247708186696634368

(No, there is not even a performance difference ... http://jsperf.com/single-quote-vs-double-quote/3 )

DU

Deleted account Mon 23 Feb 2015 6:41PM

Any such reason for preferring ' in JS?

None, it is just personnal preference. And I prefer using " " for class or id when I define inline HTML, so this prevent quotes collision and usage of \".

Edit : yeah, just like @faldrian :p

Also, about JS, I like JSON rather that function() for object definitions :

JSON :

var object = {
    var1: 'var',
    var2: 'var also'
}

Function definition :

var object = function(){
    var1 = 'var';
    var2 = 'var also;
}

Prettier and easier to read.

DU

Deleted account Tue 24 Feb 2015 3:55PM

Would be nice to set option multistr to true, Milou bothers me with that >.<

SVB

Steffen van Bergerem Tue 24 Feb 2015 7:18PM

The JSHint docs say

Multi-line strings can be dangerous in JavaScript because all hell breaks loose if you accidentally put a whitespace in between the escape character () and a new line.

DU

Deleted account Tue 24 Feb 2015 8:49PM

I didn't know that. Can't wait for ES6...

JH

Jonne Haß Thu 26 Feb 2015 3:50PM

So, shall we put mandatory comments to a vote?

F

Faldrian Thu 26 Feb 2015 3:55PM

Since we exchanged arguments with pretty much no effect, this might be a good idea.

DU

Deleted account Thu 26 Feb 2015 4:40PM

I think so.

To me, proposition should at least include these elements :

  • mandatory comments required for eavery PR
  • at least for JS code
  • at least for functions that are above 3 lines.
JH

Jonne Haß Thu 26 Feb 2015 6:13PM

mandatory comments required for eavery PR
at least for functions that are above 3 lines.

That's contradictory, please clarify.

at least for JS code

All or nothing, doing it for part of the code base and for another part not is just worse.

DU

Deleted account Thu 26 Feb 2015 6:35PM

I say at least, now if we vote fore mandatory comments and you think it's better to

That’s contradictory, please clarify.

Every new function has to be documented unless it is obvious (less than 3 instructions) eg. this is obvious and this too.

I consider as an instruction any call of method. So, if there's chained methods call, each call is an instruction. Eg :

var.call1().call2().call3().call4()

is 4 instructions. But I'd like to limit a too big number of chained call (not more than 4, maybe ?) if possible.

Also, about SCSS is nesting limit relevant ?

JH

Jonne Haß Thu 26 Feb 2015 9:07PM

That's way too vague, you can't measure complexity in LoC or calls.

DU

Deleted account Thu 26 Feb 2015 10:16PM

It's just a proposition, I don't see any other easy way to measure it. It's not useful to put mandatory comments for very obvious case, but we need a clear rule to determine what is ovious and what is not.

F

Faldrian Thu 26 Feb 2015 10:38PM

I don't like rules and hard limits where none are necessary. It is already possible to look through new pull requests and add comments like "hm... big block of code, care to comment some of that?" - if the developer not already has done so.
We all want to write good and understandable code - if there are people who have hints how to make code better understandable, they can say so in the PRs.

I think the solution to documentation issues will more likely be solved by communication and external documentation than by enforcing arbitrary rules on the structure of new code.

Let's focus more on gettings things done here than setting up a ruleset to play by... (that won't be used if people don't see the purpose of it).

JH

Jonne Haß Fri 27 Feb 2015 2:25AM

I'm totally with Faldrian. Since there obviously are no simple rules to determine when a mandatory comment should be placed, do you still hold to wanting them?

DU

Deleted account Fri 27 Feb 2015 8:17AM

We have to find how to better document the code. This is the solution I found. If I'm the only one on this, of course it will never be accepted.

But the problem is still here : the code is still abstruse. So, no matter what, we need a solution on this.

DU

Deleted account Fri 27 Feb 2015 9:58AM

When the styleguide is set, what if I set up a RubyMine inspection file for the official repo ?

JH

Jonne Haß Fri 27 Feb 2015 10:29AM

But the problem is still here : the code is still abstruse. So, no matter what, we need a solution on this.

The way forward is to make it less abstruse, not to bury the clear parts in comments.

When the styleguide is set, what if I set up a RubyMine inspection file for the official repo ?

I don't use RubyMine, so maybe. We will most likely add the above mentioned linters with configs that match the styleguide as good as possible and enable Hound.

DU

Deleted account Fri 27 Feb 2015 1:43PM

I don’t use RubyMine, so maybe. We will most likely add the above mentioned linters with configs that match the styleguide as good as possible and enable Hound.

BTW, RM supports JSHinter and JSLinter.

DU

Dumitru Ursu Tue 3 Mar 2015 10:57AM

JSLint is a bunch of cr*p. It's too harsh for any sane person. JSHint is the way to go, I guess. I think comments should be optional, but each file should at least have a comment, explaining what it's trying to do in the system, why it's needed there. But this can be enforced when accepting PR's, there's no need for a linter to check it.

Regarding SCSS - It's a mess right now. I've been thinking a lot about it, and it's a difficult problem.

I propose to use the SMACSS approach and use these rules to separate the selectors:
https://smacss.com/book/categorizing

We should have 5 folders and a file:

  • base

    • _base.scss (only for including this folder files)
    • random .scss files
  • layout

    • _layout.scss
    • the same rules as before
  • module

  • state

  • themes

    • default (a folder)
    • _main.scss (to include the other files) (this should be nested under default folder, it's something weird with loomio markdown)
    • whatever files the theme creator wanted, it may again follow the SMACSS structure
  • application.scss

    @import 'base/base', 'layout/layout', 'module/module', 'state/state';
    @import 'themes/cerulean/_main'; (changed the theme to cerulean)

SVB

Steffen van Bergerem Tue 3 Mar 2015 1:23PM

@dumitruursu We already use JSHint.

JH

Jonne Haß Thu 5 Mar 2015 2:47PM

Since there's no discussion around it, I will start the vote for the Ruby styleguide tomorrow, unless discussion emerges until then.

JH

Poll Created Fri 6 Mar 2015 1:25PM

Ruby styleguide Closed Mon 16 Mar 2015 1:04PM

Outcome
by Jonne Haß Tue 25 Apr 2017 5:15AM

The proposal passed.

Adopt bbatsovs styleguide with the derivations enforced by this Rubocop config and the additional derivations mentioned in the description of this discussion.

Results

Results Option % of points Voters
Agree 57.1% 4 FS JH DU DU
Abstain 42.9% 3 JR F A
Disagree 0.0% 0  
Block 0.0% 0  
Undecided 0% 47 ST MS TS AA S CB HF BO DM GC JH M EG G AX PP BB T DY SH

7 of 54 people have participated (12%)

DU

Deleted account
Agree
Fri 6 Mar 2015 2:11PM

In @jhass, I trust.

DU

Dumitru Ursu
Agree
Fri 6 Mar 2015 10:36PM

I would still like to have "fail" when the intention is to terminate the program and "raise" when you re-raise an exception, but I do not insist on it.

FS

Florian Staudacher
Agree
Sat 7 Mar 2015 11:05AM

looks good to me ;)

JR

Jason Robinson
Abstain
Sat 7 Mar 2015 7:32PM

Happy for those who care about ruby to decide ;)

JH

Jonne Haß Sat 7 Mar 2015 12:39AM

@dumitruursu I recently found Kernel#abort, which is the official way to abort the program with an error state.

DU

Dumitru Ursu Sat 7 Mar 2015 8:10AM

hmm, that is actually not bad.

SVB

Steffen van Bergerem Tue 10 Mar 2015 10:58PM

In the proposed settings for jshint I changed
camelcase : true: we already use camelcase for most backbone.js functions
quotmark: 'single': no need to escape double quotes in HTML
maxlen: 120

I will create a proposal for the javascript styleguide as soon as the ruby proposal closes.

DU

Deleted account Wed 11 Mar 2015 12:16PM

So there's no possibility to differentiate camelCase for class names and function and snake_case for local variables ? Well. I'll deal with it.

SVB

Poll Created Tue 17 Mar 2015 4:39PM

Javascript Styleguide Closed Fri 27 Mar 2015 10:04PM

Outcome
by Steffen van Bergerem Tue 25 Apr 2017 5:15AM

The proposal passed.

Enforce the javascript styleguide as described in the discussion and implemented by the following JSHint config: https://gist.github.com/svbergerem/83f5d267cc1897d64edd

Results

Results Option % of points Voters
Agree 77.8% 7 JR DS SVB F DU DL K
Abstain 22.2% 2 JH F
Disagree 0.0% 0  
Block 0.0% 0  
Undecided 0% 47 ST FS MS TS AA S CB HF BO DM GC JH M EG G AX PP BB T DY

9 of 56 people have participated (16%)

DS

Dennis Schubert
Agree
Tue 17 Mar 2015 4:57PM

I usually tend to using double quotes, but it looks good to me.

JH

Jonne Haß
Abstain
Tue 17 Mar 2015 5:25PM

No JS expert, but seems solid.

DU

Deleted account
Agree
Tue 17 Mar 2015 5:52PM

Ok to me.

DL

Dani'el Levity
Agree
Tue 17 Mar 2015 6:08PM

Makes life easy..

JR

Jason Robinson
Agree
Tue 17 Mar 2015 8:11PM

I really think double quotes are a saner choise for javascript - but won't fight for that.

DU

Deleted account Tue 17 Mar 2015 5:53PM

In addition, I'd propose to use JS Code Style to enforce some rules like no trailing spaces.

DS

Dennis Schubert Tue 17 Mar 2015 6:16PM

In addition, I’d propose to use JS Code Style to enforce some rules like no trailing spaces.

It is absolutely unnecessary to use two tools doing basically the same. JSHint does everything we need. Trailing whitespace should be removed anyway, but that's nothing JS specific. ;)

SVB

Steffen van Bergerem Tue 17 Mar 2015 6:25PM

In addition, I’d propose to use JS Code Style to enforce some rules like no trailing spaces.

I agree. The JSHint docs say that some options will be removed in the next major release and that you should use JSCS for those instead. Unfortunately Hound doesn't support JSCS yet and as far as I know there is no Ruby gem for JSCS.

DU

Deleted account Tue 17 Mar 2015 6:51PM

@dennisschubert : as @steffenvanbergerem said, the options related to code style are going to be deprecated in the next release of JSHint. They say that JSHint is only about code correctness and not code style. JS has a lot of ways to do some things and I saw very different ways of developing while working on the notifications system. We need to agree on things like using var that = this; or rather var self = this;, spaces usage (before and/or after brackets and curly braces?) and so on. Every big project has a code style. It improve the understandability of the code as it provides conventions.

SVB

Steffen van Bergerem Tue 17 Mar 2015 8:55PM

@dennisschubert @jasonrobinson If you want we can have another proposal after the current one to decide whether we prefer single or double quotes. In that case I would keep the current value (false, accept single and double quotes) until we have an outcome for the second proposal.

It would be a bad idea to enforce single quotes if the majority prefers double quotes.

SVB

Poll Created Sat 28 Mar 2015 2:18AM

Javascript Styleguide: Use double quotes Closed Tue 31 Mar 2015 9:04PM

Outcome
by Steffen van Bergerem Tue 25 Apr 2017 5:15AM

We will use double quotes.

Change the approved javascript styleguide by using double quotes instead of single quotes.

Results

Results Option % of points Voters
Agree 75.0% 3 JH JR SVB
Abstain 0.0% 0  
Disagree 25.0% 1 DU
Block 0.0% 0  
Undecided 0% 50 ST FS MS TS AA S CB HF BO DM GC JH F M EG G AX PP BB T

4 of 54 people have participated (7%)

JH

Jonne Haß
Agree
Sat 28 Mar 2015 9:58AM

Encourages templating and is consistent with the Ruby styleguide.

DU

Deleted account
Disagree
Sat 28 Mar 2015 2:30PM

It forces either to use single quotes in HTML, either to escape double quotes. Decreases readabilty.

JR

Jason Robinson
Agree
Sun 29 Mar 2015 7:11PM

Yes please, double quotes are so much nicer. I'd prefer not having to use either, but if one is needed to be chosen, double it should be :)

PC

Pablo Cúbico Sun 5 Apr 2015 5:43PM

For (S)CSS I suggest adopting the coding styles from Bootstrap, which are very reasonable, and will provide enough structure to make the code consistent.

Also, it will be very useful to have a similar notation on the bootstrap styles and our own, with no modular__classNaming--nonsense.

http://codeguide.co/#css

I think it will be pretty easy to adopt too.

This is only for the "measurable" styleguide, there are also some best practices we should adopt, but those will be our own, and we can add best practices gradually on the run as we find use cases. I have a few of my own to suggest.

@steffenvanbergerem if you guys want me to create a separate proposal, just let me know. I see this one is closed, but I guess the discussion is still alive.

JH

Jonne Haß Sun 5 Apr 2015 5:58PM

A discussion can totally have multiple proposals, we already used this one to decide Ruby and Javascript. Feel free to edit the top post accordingly too to reflect current discussion status.

I'd like to enforce the styleguide with HoundCI, at least as much of it as possible. It runs SCSS-Lint, do you happen to run across an existing config for the guide you propose?

PC

Pablo Cúbico Sun 5 Apr 2015 10:26PM

Nope, but I'll give it a try.

There are some further insights on the approach that gave birth to that styleguide here, the guys at Github use SCSS and the linter too:

http://markdotto.com/2014/07/23/githubs-css/#linting

PC

Pablo Cúbico Tue 7 Apr 2015 4:42AM

I still have to check some config values to see if they are fully compatible with the guide I posted, but for anyone interested in taking a look, here is the ANSI-colored output of SCSS Lint with Hound's config, which I think is pretty close to the guide:

http://www.vispress.com/scss-lint-hound.txt

And here are the differences between the default scss-lint config and Hound's:

https://www.diffchecker.com/n2wj07ys

I think maybe we should disable some rules, or apply some automatic styling, even using sed...

DU

Deleted account Sun 7 Jun 2015 12:44PM

As I pointed it here one of the things that gave me hard time whil porting to BS3 was the fact that classes where used both to define CSS rules and to select in JS.

As most of the CSS rules defined could be removed because they were alreay defined by BS, I nerver knew if the class ws safe to remove or if it was still used by any JS somewhere.

So, my idea would be, from now, to avoid using the same classes to define CSS rules and to select in JS. The diference could be pointed by the use of a prefix on the classes used by JS like js-. This will avoid to break logic or unit tests in the future when removing deprecated classes.

SVB

Steffen van Bergerem Sat 29 Aug 2015 11:37PM

For SCSS I think we could start with the default config of scss-lint. Any comments before I create a new proposal?

SVB

Poll Created Thu 3 Sep 2015 12:34AM

SCSS Styleguide Closed Thu 10 Sep 2015 9:07PM

Outcome
by Steffen van Bergerem Tue 25 Apr 2017 5:15AM

We will use scss-lint with the default config.

Results

Results Option % of points Voters
Agree 100.0% 6 JH JR F SVB F DU
Abstain 0.0% 0  
Disagree 0.0% 0  
Block 0.0% 0  
Undecided 0% 49 ST FS MS TS AA S CB HF BO DM GC JH M EG G AX PP BB T DY

6 of 55 people have participated (10%)

DU

Deleted account Mon 26 Oct 2015 11:02AM

Does someone knows a JSLinter to enforce some programmatic rules like do not use parseInt without base or do not use for(... in ...) loop?

I've discovered here that parseInt was used without giving a base, for instance.

F

Flaburgan Sun 22 Nov 2015 7:28PM

Hey, I'd like to discuss about the two rules:
- to not use ids selector
- to not have a depth of 4 or more

I know ids selector are bad for performance, but I'm not sure that we need to be that strict about it.
And I would like to know why it's bad to have a big depth? Scss is compiled anyway, I'm not sure why we want to avoid that, it's even useful to avoid side effect.

DU

Deleted account Sun 22 Nov 2015 7:34PM

And I would like to know why it’s bad to have a big depth? Scss is compiled anyway, I’m not sure why we want to avoid that, it’s even useful to avoid side effect.

As Steffen indicated me tonight, the given reason is that deep depth lead to a CSS tightly-coupled to your HTML structure.

Though, I tend to agree with you: I think the depth could be extended to 5.

Same about ID selectors. IMHO, there are legit use case.

SVB

Steffen van Bergerem Sun 22 Nov 2015 7:35PM

You can find reasons for these rules in the scss-lint documentation.

IdSelector
SelectorDepth