Blog Posts

Refactoring a wall of JavaScript from dev.to project

We refactor a wall of JavaScript code with Raddevon, in thisvideo. We take a function that's 5 screens high, and extract methods to uncover the underlying structure. We're using a method objectrefactoring towards the end of the video. Also, we're experimenting with the naming process, inspired byNaming as a process.This video was originally streamed on my Twitch channel. If you'd like to be notified, when I stream, you can subscribe on Twitch or on Twitter This video was originally

Continue Reading

A real-world example of technical debt

Creating technical debt can be as simple as writing a draft and not refactoring it. That's exactly what I've done here. Now, I want to go through my code and see if it does, in fact, have technical debt. I wrote a script to show me a quote from a light novel I'm a fan of. The script is called as overlord_quote , and it prints a random quote made of 6 sentences. First, it matches all sentences with

Continue Reading

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

I refactor acts_as_list Ruby gem again: watch as I choose better names, strip unnecessary variables, work with some ActiveRecord internals and make code intent clearer. 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 one method. You don't need to read part 2 and part 1 to understand this article. acts_as_list is a Rails gem. It allows you to treat Rails model

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
Web Analytics