Problems With Consistency

Today, I was refactoring a Ruby project that I had been working on. The main thing I was refactoring was consistency.

What was inconsistent?
Mainly the usage of operators and conditionals.

How was it inconsistent?
In Ruby, you have usual operators such as && and ||. They also allow you to use ‘and’ and ‘or’ rather then the symbols. I am used to this, as I started programming in Lua. When I realised Ruby could do this I thought “great, this will improve readability” and half way through the project started using them. Little did I know ‘and’ and ‘or’ behave differently to the symbol versions. Which lead to having different operators everywhere.

Also in Ruby you can do some cool things with conditionals, for example:

def find_will
@bloggers.each do |blogger|
if blogger.name == "Will"
return blogger
end
end
end

Can be turned into:

def find_will
@bloggers.each do |blogger|
return blogger if blogger.name == "Will"
end
end

This is a nice trick to shorten methods, but obviously don’t do this if it decreases readability. Again, I learned this half way through the project and started doing this… not changing the places where I hadn’t done it like this.

Both inconsistencies were caused by the same thing. I learnt new way of doing something, used this way and did not change the old ways.

What I will do next time?
Refactor more frequently. This is something that has caused other issues as well. Refactoring should never be pushed off or delayed, and I have done exactly this which has caused bigger inconsistencies and more. I should also focus more on consistency when refactoring as I overlook it a lot.