naildrivin5.com

Website of David Bryant Copeland

Why underscores might be better than camel case

Get my weekly newsletter

So, the “Ruby way” is to use underscores to delimit most identifiers, e.g. “add_months_to_date”, as opposed to the Java camel-case way of “addMonthsToDate”. This was initially something that irked me about Ruby, mostly because typing an underscore is kindof a pain (shift with the left hand and pinky with the other).

Now that I’ve started working, I’ve been reading a lot of code and realizing that code is more often read than written. Ultimately, camel case is a just lot harder to read (especially if you create meaningful method names like myself and my co-workers seem to do).

It’s pretty hard to defend:

Date calculatePersonDataUsageHistoryStartDate() {}

as more readable than:

def calculate_person_data_usage_history_start_date()
end

The underscores are like spaces, making the identifier a lot more readable. Of course, both are more readable than:

// Calculates the start date of the
// person's data usage history
time_ prsn_dt_uhst_st_dt(){}

This would never fly with Java (and, honestly, look a bit weird), but I’m no longer gonna curse the Ruby convention.