Method Name: Straightforward, or Accurate?

I am working on a Ruby logger. (Yes, yet another logger! But this one is different. Really!).

For logging an array (or array-like object), the method could be either Log#put_array or Log#put_each_with_index.

The former seems more straightforward, but the latter describes more closely what the method actually does.

Similarly, for logging a hash-like object, the method could be either Log#put_hash or Log#put_each_pair.

What’s the right thing in these cases, do you think?

1 Like

The more descriptive methods make more sense.

1 Like

Ended up naming them more descriptively (log#put_each_with_Index), then aliasing as log#put_array.

Duh.

2 Likes

If you’d like you can put the printing behavior in a lambda and pass it to each.with_index.

put = -> item, index=nil { puts "#{item} #{index}" }

[1,2,3].each.with_index(&put)
# 1 0
# 2 1
# 3 2
# => [1, 2, 3]

[1,2,3].each(&put)
# 1 
# 2 
# 3 
# => [1, 2, 3]

Most interesting post, Daniel, for which Thanks!

1 Like

I prefer being accurate
If enough people request for an alias, then add it maybe

Remember the no. of times of code reading is usually more than code writing :004: