Comments (5)
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.
That's a great summary, Jessie. I agree with all of your points.
from guides.
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.
@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.
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)
- Add accessibility resources to guides HOT 8
- YouTube Video Unavailable HOT 5
- Should we delete empty (seemingly duplicate) graphql guide? HOT 1
- Document SVG best practices HOT 5
- Update Backbone guide? HOT 2
- Update Ember guide? HOT 1
- Update how-to guide on feature-testing a Rails app’s Javascript? HOT 2
- Document Go Best Practices HOT 2
- Replace Ruby style guide with standard HOT 2
- Combine Best Practices and Style guides HOT 3
- Our testing guides are ruby/rails-centric HOT 2
- What layout should we suggest for test suite files in JavaScript / TypeScript projects?
- Audit guides for potential improvements
- Guides have inconsistent formatting
- Data guide should use description lists HOT 4
- Change master to main in thoughtbot/guides HOT 4
- Inquiring about JSONB column issues HOT 2
- rebase workflow link missing from Git guide HOT 1
- 404 link in relational-databases
- A11y template HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from guides.