Skip to content

Instantly share code, notes, and snippets.

tute / email_interceptor.rb
Last active Aug 29, 2015
View email_interceptor.rb
class NonProductionMailInterceptor
def self.delivering_email(message)
message.subject = "#{}: #{message.subject}" = ',' = nil
message.bcc = nil
unless Rails.env.production?
tute /
Last active Aug 31, 2021
Notes/Patterns for Refactoring Code

Intention Revealing Methods

Intention revealing method is simple and yet I see it frequently slip through programmers' code. Developers don't like lengthy methods, or find it inconvenient to read through chubby if-else branches, and if they are nice enough they'll leave comments like those.

If we change spaces by underscores in the comments, delete the comment characters, and define the resulting methods in the same file (as private helpers for example), we get code that explains itself, instead of through verbose long methods, or human code comments which get stale.

Intention revealing methods is the most basic, no brain-teaser, easiest rule that I know. Combine it with Sandi Metz's rule of a maximum of 5 lines per method and you'll get simple code that explains itself, that is a pleasure to read, improving communication and productivity of the team (even when it's only yourself).

Sample Code

View sortable_controller_methods.rb
# lib/sortable_controller_methods.rb (must then load lib files manually)
# Add in "sortable" controllers:
# include SortableControllerMethods
# As in
module SortableControllerMethods
def sort
klass.all.each do |object|
object.position = params[object.class.to_s.downcase].index( + 1