3 ms·
Interestingly enough, some of the entries in this list are the conventions I tend to use for "short block level params." If you're not used to Ruby, here's an
by bphogan 15y ago
Interestingly enough, some of the entries in this list are the conventions I tend to use for "short block level params."
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.
- randomdata 15y agoAs far as I am concerned, the full name is more readable. A few keystrokes mean nothing in the grand scheme of things. Readable code does. hash.each { |key,value| puts "#{key} #{value}" }
- bphogan 15y agoIt's all about style. Which is what makes it really interesting. In your implementation you used string interpolation, a different kind of shortcut. And in my example, I used + to make it more readable for the parent poster who said they weren't familiar with Ruby. Interesting how "more readable" is somewhat objective, isn't it?
- randomdata 15y agoI chose interpolation in this example because I found the lack of any break character between the method and the variable to not read well. 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.
- bphogan 15y agoI've done some of that in my teaching - people get the string addition right away since that's how people write in JS and other languages they already know. I immediately show how string interpolation works (by showing how it automatically calls .to_s to handle type issues.) 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.