Style question

Hi all,
Quick (I hope) question about preferred style for variable naming. I
see reference in various books to a preferred Ruby style of underscores
in variable names in preference to camel case.
(my_variable_has_this_name vs myVariableHasThisName).
Why?
Two reasons I ask, a better one and a poorer one. The poorer one is
that I come from Smalltalk where camel case is standard and expected.
The better one is that camel case seems easier to type, the shift key
being both easier to reach and ‘more familiar’ to the hands than the
underscore.

Thanks!

Bill

hi Bill -

i use camelCase too, and if i’m not mistaken this is strictly a
question of style preference. from what i understand most rubyfolk use
underscores, but i don’t believe it really matters.
couldn’t agree more that the shift key is quicker, and i just like the
way camelCase looks better…
all_those_underscores_get_funny_looking_to_me.

-jk

I guess Matz could speak to this better than I can, but I think that
underscores are an aesthetic choice. They aren’t necessarily a pragmatic
one, re: strokes involved. Because the underscores are an aesthetic
choice, there is no rational way of demonstrating that they’re better.

However, consider that:

  1. Underscores give variable names a flow - they don’t disrupt the code
    around them
  2. Underscores mirror standard English prose: it’s easy to read
    underscored variables “without the underscores”:

Consider:

def render(results)

render results

end

resultsNeedRendering = true
render results if resultsNeedRendering

results_need_rendering = true
render results if results_need_rendering

I can tell you anecdotally that 6 years ago, moving from Java, I was
very opposed to snake_case. Now, I can’t imagine going back to
camelCase.

David
On Friday, 25 February 2011 at 7:28 am, Bill F. wrote:
Hi all,

On Feb 25, 3:28pm, Bill F. [email protected]
wrote:

Hi all,
Quick (I hope) question about preferred style for variable naming. I see
reference in various books to a preferred Ruby style of underscores in variable
names in preference to camel case. (my_variable_has_this_name vs
myVariableHasThisName).
Why?
Two reasons I ask, a better one and a poorer one. The poorer one is that I come
from Smalltalk where camel case is standard and expected.
The better one is that camel case seems easier to type, the shift key being both
easier to reach and ‘more familiar’ to the hands than the underscore.

Thanks!

Bill

Rails standard uses underscore. As for my preference I like it too
especially after migrating from qwerty to azerty layout since the
latter does not require shift for underscore.

Also remember that “standard Ruby Style” has class names with multiple
worlds in CapCamelCase, so using underscores_for_variables helps
distinguish
the two.