Loopback-next: Why does public exposed function start with underscore?

Created on 28 Nov 2018  路  6Comments  路  Source: strongloop/loopback-next

Out of curiosity, why does the _createHasManyRepositoryFactoryFor function start with an underscore even though it is publically exposed?

developer-experience help wanted refactor

Most helpful comment

@codejamninja Good question!

I think it was me who introduced the convention of using underscores. My problem with private/protected/public modifies is that they are not available nor enforced in JavaScript, at least AFAIK.

At the same time, I can see how protected methods with a name starting with underscore can look weird.

I am proposing to make the following changes. What do you think, @strongloop/loopback-maintainers?

  1. For current protected methods like _createHasManyRepositoryFactoryFor:

    • Rename the method - remove underscore (e.g. createHasManyRepositoryFactoryFor)
    • To preserve backward compatibility, add a thin wrapper with the old name (_createHasManyRepositoryFactoryFor), mark it as deprecated and implement it by calling the new method (createHasManyRepositoryFactoryFor)
    • Update documentation, examples to use the new method
    • Check CLI templates for any places using the old method (I don't expect any).
  2. Document the naming style: public and protected members don't start with underscore. Discuss what style to use for private members as part of the pull request.

    Unfortunately, we did not yet manage to update our style guide for TypeScript and move it into loopback-next. To keep things simple, I think it's best to add the new content to the current outdated style guide here: https://github.com/strongloop/loopback.io/blob/gh-pages/pages/en/contrib/style-guide.md

    Of course, if there is a volunteer willing to migrate that style guide to loopback-next and update it to our actual style, then we would gladly accept such contribution!

All 6 comments

I'm sorry, I guess it's protected. Is it convention to start protected methods with an underscore?

I don't think it's strictly enforced. IMO, with private/protected/public modifiers in TypeScript, we don't have to play such conventions. YMMV.

I find it slightly odd because users of the system will be accessing those methods a lot. That's just my opinion though.

It kinda makes you feel like you're not supposed to use them, or that you're monkey patching the code.

@codejamninja I tend to agree with you.

@codejamninja Good question!

I think it was me who introduced the convention of using underscores. My problem with private/protected/public modifies is that they are not available nor enforced in JavaScript, at least AFAIK.

At the same time, I can see how protected methods with a name starting with underscore can look weird.

I am proposing to make the following changes. What do you think, @strongloop/loopback-maintainers?

  1. For current protected methods like _createHasManyRepositoryFactoryFor:

    • Rename the method - remove underscore (e.g. createHasManyRepositoryFactoryFor)
    • To preserve backward compatibility, add a thin wrapper with the old name (_createHasManyRepositoryFactoryFor), mark it as deprecated and implement it by calling the new method (createHasManyRepositoryFactoryFor)
    • Update documentation, examples to use the new method
    • Check CLI templates for any places using the old method (I don't expect any).
  2. Document the naming style: public and protected members don't start with underscore. Discuss what style to use for private members as part of the pull request.

    Unfortunately, we did not yet manage to update our style guide for TypeScript and move it into loopback-next. To keep things simple, I think it's best to add the new content to the current outdated style guide here: https://github.com/strongloop/loopback.io/blob/gh-pages/pages/en/contrib/style-guide.md

    Of course, if there is a volunteer willing to migrate that style guide to loopback-next and update it to our actual style, then we would gladly accept such contribution!

I created a pull request to solve this

https://github.com/strongloop/loopback-next/pull/2112

Was this page helpful?
0 / 5 - 0 ratings

Related issues

marioestradarosa picture marioestradarosa  路  3Comments

mightytyphoon picture mightytyphoon  路  3Comments

shadyanwar picture shadyanwar  路  3Comments

rexliu0715 picture rexliu0715  路  3Comments

acrodrig picture acrodrig  路  3Comments