by bphogan · 15y ago · 0 comments
If you're not used to Ruby, here's an example of what he's referring to:
To work with the keys and values of a hash (dictionary / associative array) I use the .each method on the hash itself. The .each method gives me access to the key and value of each entry in the hash.
hash = {"name => "Homer", "age" => 42}
hash.each{|k, v| puts k + " " + v}
Where 'k' is key, and 'v' is value.I never really gave much thought to "standardizing" this.
hash.each { |key,value| puts "#{key} #{value}" }Interesting how "more readable" is somewhat objective, isn't it?
puts k + " " + v
Does that execute as puts(k) + " " + v, puts(k + " ") + v or any other combination? While those of us familiar with Ruby know the answer, I would still argue that it takes greater cognitive resources to evaluate over the single string, especially in the presence of a syntax highlighting editor who clearly defines the string boundaries in colour.You are right that it is definitely subjective. Coding is user interface design, but we don't usually have the luxury of processes like A/B testing to validate our work like other interface designers do. It would be interesting to put both of our code samples along with some other variations in front of an audience and see how they are received.
When I've tried to jump right into string interpolation, people don't quite get what's going on, which resulted in me having to backtrack and do addition anyway.
But that's just my experience.