sandi-metz-rules

Review and refactor Ruby code against Sandi Metz's four maintainability rules.

Updated Nov 10, 2013
One-click install
npx skills add https://github.com/bnferguson/dotfiles --skill sandi-metz-rules-bnferguson
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: sandi-metz-rules
Source: https://github.com/bnferguson/dotfiles/tree/main/.agents/skills/sandi-metz-rules
Command: npx skills add https://github.com/bnferguson/dotfiles --skill sandi-metz-rules-bnferguson

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Ruby codebases often accumulate long classes, bloated methods, and fat controllers that are hard to test and maintain. This Skill applies Sandi Metz's four rules to systematically detect these violations and suggest concrete refactorings. ## Core Features & Use Cases - Rule-Based Code Review: Measures classes (100 lines max), methods (5 lines max), parameters (4 max), and controller instantiations (1 max) using precise counting rules. - Prioritized Violations: Ranks issues by severity so you fix the worst offenders first, with guidance on when breaking a rule is acceptable. - Concrete Refactoring Patterns: Suggests specific techniques like Extract Method, Parameter Object, Service Objects, and Composed Method with before/after examples. - Use Case: Point it at a legacy Rails controller with embedded business logic, and it will identify the violations and walk you through extracting a service object step by step. ## Quick Start Review the Ruby files in app/controllers against Sandi Metz's rules and suggest refactorings for any violations.

Frequently Asked Questions about sandi-metz-rules

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
What are Sandi Metz's four rules for Ruby code?▼

The four rules are: classes no longer than 100 lines, methods no longer than 5 lines, no more than 4 parameters per method, and controllers instantiating only one object. They come from Sandi Metz's talks and her book Practical Object-Oriented Design in Ruby.

How do I refactor a Ruby method that is too long?▼

Extract sub-methods with descriptive names, use guard clauses to reduce nesting, and apply the Composed Method pattern so each method stays at one level of abstraction. Replacing conditionals with polymorphism also helps for branching logic.

How are lines counted for the 100-line class rule?▼

Count only lines of actual code, excluding blank lines, comments, the class or method definition line, and end statements. Keyword arguments count as individual parameters, but block parameters do not.

When is it acceptable to break Sandi Metz's rules?▼

Breaking the rules is acceptable for configuration files, routes, database migrations, generated code, and DSL definitions. Even then, minimize the violation—a 6-line method is reasonable, a 20-line method is not.

Can RuboCop enforce Sandi Metz's rules automatically?▼

Yes, RuboCop can enforce them via Metrics/ClassLength, Metrics/MethodLength, and Metrics/ParameterLists with Max values of 100, 5, and 4. Reek, flog, and flay provide complementary smell and complexity detection.