10 Rules of Thumb to radically improve your Ruby on Rails code
Ruby on Rails has generated a lot of hype over the last couple of years.
This is a good thing - Rails is well-supported and well-understood.
But it's also a bad thing.
Lots of people read the "Agile Web Development" book and then charge a fortune to develop RoR applications. Badly.
I have seen some shocking projects, chock full of the type of spaghetti code that the Rails hype claims you cannot produce (of course this is nonsense - you can write shite in any language).
Over the years I have developed a whole number of "rules of thumb" that I use to evaluate a project's code. These are subjective, inaccurate and open to interpretation. But taken as a whole they give you a pretty decent idea of the state of the code.
And here they are:
rake stats
Look at the ratio of code to tests.
- More code than tests? Lose 10 points
- Between 1:1 and 1:1.5? No points
- Above 1:1.5? Gain 5 points
Dependencies
Is Rails and any required plugins installed into the vendor folder?
- No? Lose 10 points
- Yes? No points
rcov
Is rcov pre-installed?
- Yes? Gain 5 points
What is the test coverage?
- rcov won't work? Why not? Lose 10 points
- More than 10% red? Lose 20 points
- Less than 10% red? Lose 10 points
- All green? Gain 10 points
Migrations
Can the database be built completely from migrations?
- Yes? Gain 10 points
- No? Why not? Lose 10 points
Logic in all the right places?
Is there any application logic in the controllers?
- Yes? Lose 10 points
- No? Gain 10 points
Is there any application logic in the views?
- Yes? Lose 10 points
- No? Gain 5 points
Warnings and Deprecations
Are there any deprecation notices when running the tests or application?
- Yes? Lose 5 points
Are there any warnings when running the tests or application?
- Yes? Lose 20 points
RDoc
Do all the models have RDoc comments - a header describing the class and comments for each method?
- Header comments? Gain 10 points
- All method comments? Gain 10 points
- Some method comments? Gain 5 points
- No? Lose 10 points
- Yes? Gain 5 points
- No? Lose 5 points
- Yes? Gain 10 points
- No? Lose 10 points
Repeated Code
Is there any repeated code? Are there similar pieces of code in multiple places that could be consolidated into a single parametrised method?
- Yes? Lose 10 points
Readability
How readable is the code? Can you tell, at a glance, what the most complicated pieces of code do?
- No? Lose 20 points
- Yes? Gain 20 points
Idioms
Does the code use symbols as hash keys and (the equivalent of) enumerations?
- Yes? Gain 10 points
- No? Lose 10 points
Does the code pass objects to generate links?
- Yes? Gain 5 points
- No? Lose 5 points
Writing this stuff down is strange. It takes things that often comes to me as a "gut feel" and documents and codifies it. Necessarily this means that there are things that I notice that I haven't written down here. Apologies - I will update the article as and when I think of them.
What use is this check-list?
If you are worried about the state of your Rails project 3hv is planning a service where your code is evaluated against these criteria and you receive a detailed report with recommendations for improvement. Excellent for managers who are worried about progress, even better for developers just starting out on Rails. Details are still being finalised but contact me if you are interested.
No comments:
Post a Comment