In 2017 I delievered a talk with the title “Boring Ruby Code” at two conferences – Brighton Ruby Conf and Southeast Ruby. After that I always wanted to write a blog post that summaries the approach, but I have never gotten around to do that. Today is the day so here we go.
This is just a short summary, not one-to-one transcription of the talk. You can watch the video from Brighton Ruby Conf here.
- Boring code is easier to understand. In your team you have (or at least should have) people at different stages of their career. Certain constructs in Ruby do not bring you much value, but make the code harder to understand for entry level programmers.
- Boring code is easier to read. Programmers spend most of their time reading the code so it makes sense to optimise for the reading time and not the writing time. Less common constructs increase the reading time even for people who understand how they work.
- Boring code is easier to delete. There is no “pride” associatied with boring code, so the decision to delete it comes easier.
- Iterating over an array of names to assign values or define methods is just lazy and brings no value.
sendis a code smell (
- Dynamically defining methods is a perfect way to prevent other programmers from discovering where the method is defined.
- Dynamically defining methods where method name is dynamically constructed is a no-go.
- Metaprogramming capabilities of Ruby can steer our attention away from standard refactoring practices. A lot of code duplication can (and should) be solved by extracting a method, not by metaprogramming.
method_missingis a code smell.
- “Smart code” can hide a lot of simple mistakes just by looking smart. Programmers assume that a piece of code is right, because it looks smart.
respond_to?is a code smell.
- Once you start using
respond_to?they tend to leak to multiple places in your codebase.