Git Product home page Git Product logo

Comments (5)

jessieay avatar jessieay commented on July 18, 2024

HI @petrachi - thank you for this discussion on public / private method organization!

To answer the question "how does thoughtbot do this" - we separate public and private methods like in your first example:

class MyClass
  def a_public_method
  end

  private

  def a_private_method
  end
end

I think the other ways of separating public and private methods that you show are interesting, but I think the reasoning behind them is flawed. You say "The problem is as the class becomes bigger, the split between public and private becomes less and less visible. (And sometimes you have big, enormous classes, and no time to refactor)"

While this is certainly the case, I think it is a mistake to develop a coding style that makes it easier to write big fat Ruby classes. I know what you mean about private methods getting buried when classes are long. But, if you do your best to break out smaller classes (Sandi Metz suggests classes be no more than 100 lines) then the problem will be solved.

So I guess my perspective on this is that the public / private divide is not a problem. And if a developer does not have time to refactor large classes, that is going to lead to even bigger problems down the road.

Thanks again for this thoughtful explanation. I will leave this issue open for others to weigh in on.

from guides.

gabebw avatar gabebw commented on July 18, 2024

That's a great summary, Jessie. I agree with all of your points.

from guides.

petrachi avatar petrachi commented on July 18, 2024

Hi, thanks for your response.

I understand now that class should be small, and that code guidelines should prevent against creating big classes.

For me, the problem is that the application I'm working on is seven years old, and it is just recently that we learn about programmation design (like the Sandi Metz recommandations about class length). So we have a lot of legacy code, not time or ressources to refactor them all today, and they are a pain to work with.

So the thing I'm looking for is probably not to include into a guideline, but a temporary solution to know how to deal with public interface visibility in classes that are 1500 lines long. How would you do it, and do you have better ideas than what I exposed earlier ?

from guides.

dideler avatar dideler commented on July 18, 2024

@petrachi prepending an underscore to the private method names doesn't hurt readability that much IMO (especially if you have a Python background). If I was in your situation, I would combine the best practice of private below public with the naming convention.

class MyClass
  def public_method
  end

  private

  def _private_method
  end

  def _another_private_method
  end
end

Under normal circumstances I would follow @jessieay's advice.

from guides.

jferris avatar jferris commented on July 18, 2024

I'm going to close this, as it doesn't seem like we're going to make a change to the guides based on this discussion.

from guides.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.