Appropriate use of camelCase

G

Gavin Kistner

Following the 'instance variable capitalization' thread, I'm convinced
that I should be trying to learn the Ruby idioms. Now I just need to
learn what they are.

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.
 
Y

Yukihiro Matsumoto

Hi,

In message "Appropriate use of camelCase"

|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.
 
B

Ben Giddings

Gavin said:
* '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.

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
 
N

nobu.nokada

Hi,

At Tue, 24 Feb 2004 09:51:19 +0900,
David A. Black wrote in [ruby-talk:93516]:
You can get at least a good rough idea from the source. From
/usr/local/lib/ruby/1.8:

$ ruby -ne 'print if /\b[a-z]+[A-Z]/.match($_)' `find -name "*.rb"` | wc -l
862

They all aren't method names; regexps, string literals, local
variables, or comments.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top