While writing my ValidForm library (http://phrogz.net/RubyLibs/) I
realized that I was mixing camelCase [which I love] with
whatever_you_call_this_case [which I don't, but I see that Ruby uses a
lot of]. (BTW...what *do* you call that naming style? snake_case? That's
what I'll call it until someone corrects me.)
I tried starting to eradicate camelCase, but found that I just couldn't
do it completely[1], and came up with the following idiom for that
particular library[2]:
* 'Verb' methods (named like they do something, and generally called
with parameters, or where the action they produce is more important than
their return value) I named using snake_case.
* 'Noun' methods (aka 'property' methods...methods which behave like
getters and setters of an internal instance variable [whether or not
they actually do] which are called explicitly to get a return value or
with a foo= assignment method to set a value) I named using camelCase.
I was very proud of my semi-rational rationale, in finding a way that I
felt was both Ruby-esque, which actually sort of conveyed additional
information with the camelCase vs. snake_case usage. But I realize it's
an idiom I made up completely on my own.
So, to the Question: is the above just a Bad Idea? Is camelCase *ever*
considered appropriate in Ruby?
- Gavin
[1] The problem for me (and this is not to start a flame war, just a
personal exposition) is that camelCase is just so much easier to type,
and (to me) LOOKS like a property.
[2] Whether or not I succeeded in religiously adhering to my proposed
idiom I'm not sure...I got disheartened after a while of global
find/replaces, upon how many pretty camelCases were disappearing.
--
(-, /\ \/ / /\/
In message "Appropriate use of camelCase"
on 04/02/23, Gavin Kistner <ga...@refinery.com> writes:
|So, to the Question: is the above just a Bad Idea? Is camelCase *ever*
|considered appropriate in Ruby?
We don't force you. You are completely free. But I prefer under_score
names, and never use CamelCase except for class/module names in Ruby.
matz.
For what it's worth, this is the opposite of how I do it, and here's why:
* If it looks and acts like a variable, I name it like a variable, for
which I use 'snake_case'.
* If it is a verb method that does something, that's where camelCase
makes sense to me.
I think it's important to visually distinguish the different types of
things in a program. To do this, I use ALL_CAPS_WITH_UNDERSCORES for
constants, all_lowercase_with_underscores for variables, and
variable-like attribute accessors. I use CamelCaseWithLeadingCap for
class like things, and finally I useCamelCaseWithLeadingLowercase for
methods that do something.
That makes it easy to glance at a 'word' and see what it is and what it
does.
On top of that, I'm careful in how I name things. Constants, variables
and classes are nouns, methods are active verbs. The reason I think
camelCase is useful to distinguish methods from variables/accessors is
when it comes to things named 'read_number'. Is that a variable that
stores the number of times something was read? Is it a method that
instructs something to read a number? The only way I know of to make
that unambiguous is camelCase. readNumber is the method, read_number is
the variable. Voila.
Maybe that's just me tho.
Ben