Appropriate use of camelCase

Discussion in 'Ruby' started by Gavin Kistner, Feb 23, 2004.

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

    --
    (-, /\ \/ / /\/
     
    Gavin Kistner, Feb 23, 2004
    #1
    1. Advertising

  2. Hi,

    In message "Appropriate use of camelCase"
    on 04/02/23, Gavin Kistner <> 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.
     
    Yukihiro Matsumoto, Feb 23, 2004
    #2
    1. Advertising

  3. Gavin Kistner

    Ben Giddings Guest

    Gavin Kistner wrote:
    > * '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
     
    Ben Giddings, Feb 23, 2004
    #3
  4. Gavin Kistner

    Guest

    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.

    --
    Nobu Nakada
     
    , Feb 24, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Benson

    entering the lists against CamelCase

    John Benson, Dec 7, 2003, in forum: Python
    Replies:
    2
    Views:
    318
    John Roth
    Dec 8, 2003
  2. Mark Hahn
    Replies:
    97
    Views:
    2,227
    Martin Maney
    Apr 29, 2004
  3. Jeff Schwab
    Replies:
    8
    Views:
    816
    James Kanze
    Mar 24, 2008
  4. Michael Neumann

    CamelCase issues

    Michael Neumann, Oct 21, 2004, in forum: Ruby
    Replies:
    28
    Views:
    258
    Stefan Schmiedl
    Oct 23, 2004
  5. Bil Kleb
    Replies:
    3
    Views:
    122
    Cameron McBride
    Dec 10, 2004
Loading...

Share This Page