3 ms·
This is one of my periodic pain points with Rails, as compared to Java. At the day job I work with multiple code bases with more than X00,000 lines of code eac
by patio11 17y ago
This is one of my periodic pain points with Rails, as compared to Java. At the day job I work with multiple code bases with more than X00,000 lines of code each. My Rails site has 5k lines as reported by rake stats, and due to a combination of poor IDE support compared to Java and the toss-it-all-in-one-folder Rails conventions, that sometimes results in headaches.
My suggestions:
1) Refactor like it is going out of style. Rails classes (particularly controllers and models in my experience) have a tendency to accumulate cruft over time. Periodically evaluate whether something should be broken into two classes, or at least two files. Mixins and require are your friend here. (Breaking a controller into two changes URLs by default. I think letting Rails assign publicly visible URLs is a mistake, but most people do it. Consider carefully whether you can accept URLs changing as a result of code changes.)
2) Encourage internal code reuse, and better separation of responsibilities, by breaking things into plugins when appropriate. I extracted code from my own classes to make A/Bingo for example. It keeps my own classes readable and makes it much easier to publish and reuse A/Bingo.
3) Coding conventions are your friend. Since the IDE lacks a reliable jump-to-definition-of-this-function feature, I tend to try to put predictable things in predictable places. For example, model classes always have a consistent ordering of filters, validations, and associations at the top of the file prior to business logic. (Those could be externalized if they got realllly messy, via the magic of require.)
4) Put partials into thematically coherent subdirectories. This saves you from having to find the right form partial from a directory with 20 views and 50 partials.
- kylebragger 17y agoI couldn't have said it better myself. The ordering of validations/associations/filters/etc. especially - that stuff can get really messy really quickly.
- steveklabnik 17y agoOnly thing left of this list is good test coverage, so that when you refactor, you know you're not breaking stuff. Luckily, Rails has really good support for this...
- j_baker 17y agoI slightly disagree with point 2. The problem is that sometimes making something be adaptable to multiple use-cases makes it more complex than simply having two very similar pieces of code.
- bguthrie 17y agoIt's true that you lose some time by doing this. But my experience has been that it inevitably improves the quality of your code if you and your team are forced to think about interfaces and common pain points by moving towards reuse. Your team members are code consumers too. Plus, you end up in a position where you can give back to the community, which is nice.
- cabalamat 17y agoI agree particularly with (1). If your project is 5k lines and you wrote all of it, it ought to still be small enough for you to navigate round it easily (at least, that's my experience based on my projects of that size or bigger). My other suggestion, regarding point (3) would be to not rely on the IDE to heklp you navigate the code. If it's your code you should really have laid out the classes and source files so things are in a logical (to you) order and easy to find. (I'm currently working on a 12 kline project and my IDE consists of multiple kwrite windows plus the command line -- I find this enough to navigate the code).
- jamesjyu 17y ago+1 to (4) Also, another good thing to keep in mind is to avoid partialitis. Many people tend to start making partials for every single widget on the page. Don't do that. Only use partials when it DRYs up your code, and sometimes, it's better to encapsulate UI elements into helpers. Also, partial rendering can get expensive. The worst is when you're faced with a directory of dozens of partials that are ambiguously named.