Blog Posts

Is it always a good idea to split long methods into smaller ones? An experiment.

There’s a long discussion on Reddit named Is it OK to split long functions and methods into smaller ones even though they won’t be called by anything else?. Some people in that discussion hold an opinion that it’s not always the best idea to split a long method into smaller methods. Well, of course, you can refactor code in many different ways, including ways that read worse than the original long method. But I think that in most cases

Continue Reading

acts_as_list refactoring part 3

In this article I’m continuing refactoring acts_as_list gem I started in part 2 and part 1. You don’t need to read either part to understand this article. acts_as_list is a Rails gem. It allows you to treat Rails model records as part of an ordered list and offers methods like #move_to_bottom and #move_higher. In this refactoring adventure I’m going to focus on just one 11-line method, and surprisingly, there’s a lot of things that can be improved in just

Continue Reading

acts_as_list refactoring part 2

In this post I’m continuing refactoring of acts_as_list gem I started in part 1. As you might remember, I’ve split .acts_as_list method into several modules, each module dedicated to an option passed to the method. E.g. ColumnMethodDefiner module defines methods related to the column option (the option defines column name for storing record’s list position). This post is dedicated to refactoring of the ColumnMethodDefiner module. Improving ColumnMethodDefiner module So, I’ve extracted code related to column option of .acts_as_list to

Continue Reading

acts_as_list refactoring part 1

Today I’m going to refactor acts_as_list Rails library. It allows to treat Rails model records as part of an ordered list and offers methods like #move_to_bottom and #move_higher. Step 1: .acts_as_list introduction .acts_as_list is available as a class method in ActiveRecord::Base when acts_as_list gem is loaded. Here’s an excerpt from .acts_as_list definition: Using ClassMethods module is customary in Rails, but it’s not a requirement to be familiar with it to read this article. All you need to know is

Continue Reading

CreateSend refactoring part 2

Today I’m continuing to refactor a library called CreateSend. The first part is here. In part 1 I’ve finished all class methods of Base classs and now I’m going to refactor instance methods. Step 1: initialize method initialize (see above ↑) chooses not to use named arguments, and treats method arguments as an array of many arguments. And yet, it only processes one argument from the whole args array. It’s misleading to accept arguments and throw them away. It

Continue Reading

Be lazy and don’t keep context in your head

Sometimes, code we read makes us to remember context. Consider the following code (it sends an invite email to a user): Lines 2-8 (see above ↑) deal with the case of existing user, and the rest deal with the case of new user. When reading lines 3-5 you have to remember that you’re dealing with an existing user, same with lines 12-22, only there you deal with a new user. After all, both existing user and new user cases

Continue Reading

CreateSend refactoring part 1

Today I’m going to refactor a part of Ruby library called CreateSend. I’ll work on lib/createsend/createsend.rb, which contains CreateSend::CreateSend class and some other stuff. Introduction CreateSend is a base class for accessing CampaignMonitor API. It provies .user_agent to set HTTP user agent and .exchange_token to get OAuth access token. In summary, it’s a Ruby wrapper for accessing API. Classes like Campaign inherit from CreateSend to add specific methods to work with campaigns, etc. First look The first things declared

Continue Reading

PaginatedResource refactoring

Today I’m going to refactor a class from Digital Ocean Droplet Kit, a library to control droplets (that’s what Digital Ocean calls virtual machines). The class is called PaginatedResource and you can find the original source here. The idea behind the class is that it fetches elements from an external source on demand, and you just call #each and don’t worry about fetching. How PaginatedResource works Take a look: Elements are fetched from external source one page at a

Continue Reading

Ninefold CLI refactoring

Today I’m going to review and refactor some code from Ninefold CLI. I picked a method I could make better. Ninefold is a hosting company and pick_app is apparently responsible for selecting an app for CLI to work with. What’s good about this code: Long method names like app_from_dot_ninefold_file. Easy to understand what the method does.

Continue Reading

Site Footer

Web Analytics