question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

New rule request: naming conventions of methods

See original GitHub issue

I looked for a rule for enforcing naming conventions on methods of a class but I couldn’t find any.

As an example, the AirBnb style guide for React forbids prefixing methods with an underscore, but there is no way in ESLint to enforce it.

The rule I’m proposing would be an equivalent of no-underscore-dangle but for methods.

Valid examples

class Foo {
  bar() { }
}

Invalid examples

class Foo {
  _bar() { }
}

The rule has two purposes: enforcing a common style and preventing errors. The rationale behind the error prevention is that prefixing with an underscore may give a false sense of privacy, which in turn can cause mistakes, e.g. breaking changes to a method which was thought to be private.

I have already a working implementation of a more generic version of this rule https://github.com/buildo/eslint-plugin-method-names, which allows specifying a regex to which method names should confirm.

I think at least the underscore-specific version should be in the core rules, to match the no-underscore-dangle rule.

Open questions (if the proposed rule is valid):

  • should the rule scope be limited at the underscore case or should it be generic and allow a regex to be specified?

    • should the rule scope include class properties defining a function? (my plugin currently does). E.g.

      class Foo {
        bar() { } // method, invalid
        _baz = () => {} // class property, method
      }
      

    would be both invalid. This is a fairly common use case when using React components, in order to bind handlers.


Updated proposal, after the discussion below

no-underscore-dangle will have one additional option:

  • enforceInMethodNames (default: false)

enforceInMethodNames

Examples of correct code for this rule with the { "enforceInMethodNames": true } option:

class Foo {
  foo() {}
}

const o = {
  foo() {}
}

Examples of incorrect code for this rule with the { "enforceInMethodNames": true } option:

class Foo {
  _foo() {}
}

class Foo {
  foo_() {}
}

const o = {
  _foo() {}
}

const o = {
  foo_() {}
}

The rationale between the additional option and its default value is to make this change non-breaking.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:5
  • Comments:42 (42 by maintainers)

github_iconTop GitHub Comments

3reactions
ljharbcommented, Sep 7, 2016

It’s unfortunate to have options where absence is not the same as false 😕 could we invert the option names so that the defaults can be false?

3reactions
platinumazurecommented, Sep 6, 2016

Okay, we’ve gone off track. That’s my fault, I apologize, but let’s please get back on track.

If we can get a full proposal for extending no-underscore-dangle to optionally support method names and class properties, then the team can evaluate.

Sound good? 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

Naming conventions | Cloud APIs
Method names should not include prepositions (e.g. "For", "With", "At", "To"). Generally, method names with prepositions indicate that a new method is being ......
Read more >
Naming Convention — 9 Basic Rules for any Piece of Code
Rule #1 — Consistency​​ All 3 names of the above methods are the same and can be interpreted as the same operation. It...
Read more >
Set up request naming | Dynatrace Docs
On the Service settings page, go to the Request naming rules tab and select Add rule. Here you can define a set of...
Read more >
REST Resource Naming Guide
Below are a few tips to get you going when creating the resource URIs for your new API. 2. Best Practices. 2.1. Use...
Read more >
9. Naming Conventions - Oracle
Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found