Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blank lines conventions #1048

Closed
jmlopez-rod opened this issue Sep 2, 2016 · 2 comments
Closed

blank lines conventions #1048

jmlopez-rod opened this issue Sep 2, 2016 · 2 comments

Comments

@jmlopez-rod
Copy link

jmlopez-rod commented Sep 2, 2016

Do you guys have any conventions on when to use blank lines? I see in 18.8 it is specified that blocks should not be padded. That is this is bad:

// bad
function bar() {

  console.log(foo);

}

But, does this apply also for classes? My preference in style has been heavily influenced by python and thus when I do not see a convention being used from airbnb I default to whatever I can apply from the python style guide.

In particular I prefer to write like this:

import { foo } from 'mod';


const dummy = () => 'dummy';
const bar = () => 'bar';


class SomeClass {

  constructor() {
    this.someProperty = 'value';
  }

  callFoo() {
    foo();
  }

}


function auxilaryFunction() {
  ...
}


function anotherAuxilaryFunction() {
  ...
}


export {
  SomeClass,
};
export default SomeClass;

The convention is to surround methods by 1 blank line and functions by 2. In this way when we do add another method to the class we do not see a git difference in which we added a new empty line. Instead we can see clearly that a new method was added. For functions I prefer the 2 blank lines since I am treating them as if it were a class. I think of them to be a little bit less related (as opposed to methods).

Lastly, this is something that I also failed to see in the style guide: exports. Usually if my module consists of many functions I prefer to scroll to the bottom to see what I am exporting. For this reason I only write 1 single export {} and export default. In this way if ever decide to export another item now I just add it to the list instead of making git give me a line change on the function. That is, instead of seeing

- function foo() {
+ export function foo() {

now we have

 export {
   someVar,
+  foo,
 }

What are your preferences and why? Could we have some section in the style guide pointing them out for future reference? I'll be happy following them as long as there is consistency.

@ljharb
Copy link
Collaborator

ljharb commented Sep 2, 2016

Yes, it applies to classes as well. We always avoid extra padding blank lines. function and class should be self-consistent when possible. Our linter config already prevents two blank lines from ever appearing consecutively, in any context. I'd be happy to accept a PR that documents this more clearly!

We don't currently have a preference for exports in the way you describe - however, we do have a preference for only exporting one thing. In other words, files should ideally only have one default export, and zero named exports. A PR to add a section about this is welcome as well.

@Ensive
Copy link
Contributor

Ensive commented Feb 27, 2019

I've been thinking about how we could handle documenting this in a StyleGuide
@ljharb Please check out my PR when you have time, please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants