# CamelCase and Underscores

**URL:** <https://www.ruby-forum.com/t/camelcase-and-underscores/191349>\
**Category:** Rails\
**Created:** [July 10, 2010, 1:00am UTC](https://www.ruby-forum.com/t/camelcase-and-underscores/191349 "2010-07-10T01:00:48Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [July 10, 2010, 1:00am UTC](https://www.ruby-forum.com/t/camelcase-and-underscores/191349/1 "2010-07-10T01:00:48Z")

</div>

Is there a document anywhere that indicates when to use camel casing and  
when to use underscores between words?

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [July 10, 2010, 1:04am UTC](https://www.ruby-forum.com/t/camelcase-and-underscores/191349/2 "2010-07-10T01:04:27Z")

</div>

> Is there a document anywhere that indicates when to use camel casing and  
> when to use underscores between words?

Huh? Do you mean for things like variables, methods, classes,  
constants? Or something else?

In general…

variable\_name  
method\_name  
ClassName  
CONSTANTNAME

-philip

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [July 10, 2010, 2:01am UTC](https://www.ruby-forum.com/t/camelcase-and-underscores/191349/3 "2010-07-10T02:01:47Z")

</div>

Philip H. wrote:

> > Is there a document anywhere that indicates when to use camel casing and  
> > when to use underscores between words?
> 
> Huh? Do you mean for things like variables, methods, classes,  
> constants? Or something else?
> 
> In general…
> 
> variable\_name  
> method\_name  
> ClassName  
> CONSTANTNAME
> 
> -philip

That is exactly what I meant. Thank you.

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [July 10, 2010, 2:33am UTC](https://www.ruby-forum.com/t/camelcase-and-underscores/191349/4 "2010-07-10T02:33:27Z")

</div>

> variable\_name  
> method\_name

But why?! Since methods don’t need (), how are we supposed to  
instinctively know what’s a method and what isn’t when reading someone  
elses code? (Or our own when we’ve forgotten what we wrote!)

Matt.

PS. Anyone play spot the Ruby N., score 10 points!

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [July 10, 2010, 3:24am UTC](https://www.ruby-forum.com/t/camelcase-and-underscores/191349/5 "2010-07-10T03:24:24Z")

</div>

> You shouldn’t need to.

Because… (?)

(Not being facetious).

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [July 10, 2010, 8:08am UTC](https://www.ruby-forum.com/t/camelcase-and-underscores/191349/6 "2010-07-10T08:08:09Z")

</div>

MattB wrote:

> > variable\_name  
> > method\_name
> 
> But why?! Since methods don’t need (), how are we supposed to  
> instinctively know what’s a method and what isn’t when reading someone  
> elses code? (Or our own when we’ve forgotten what we wrote!)
> 
> Matt.
> 
> PS. Anyone play spot the Ruby N., score 10 points!

variable\_name = something  
Object.method\_name

If you are inside a class

def instance\_method  
self.method\_name = something  
variable\_name = method\_name  
end

^^

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [July 10, 2010, 3:11am UTC](https://www.ruby-forum.com/t/camelcase-and-underscores/191349/7 "2010-07-10T03:11:40Z")

</div>

MattB wrote:

> > variable\_name  
> > method\_name
> 
> But why?! Since methods don’t need (), how are we supposed to  
> instinctively know what’s a method and what isn’t when reading someone  
> elses code? (Or our own when we’ve forgotten what we wrote!)

You shouldn’t need to. The similarity is deliberate.

(BTW, I would do CONSTANT\_NAME, not CONSTANTNAME.)

> Matt.
> 
> PS. Anyone play spot the Ruby N., score 10 points!

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [July 10, 2010, 7:55pm UTC](https://www.ruby-forum.com/t/camelcase-and-underscores/191349/8 "2010-07-10T19:55:49Z")

</div>

On Jul 9, 2010, at 6:21 PM, MattB wrote:

> > You shouldn’t need to.
> 
> Because… (?)
> 
> (Not being facetious).

Because for all practical purposes they are the same thing. They aren’t  
of course, but only the class itself should know or care about that.

Look at ActiveRecord… say I’ve got a Widget class with a title  
attribute…

W = Widget.new  
w.title = “Foo”  
puts w.title

_both_ the 2nd and 3rd lines are actually methods. You’re not accessing  
a variable of the instance of the class.

Does it matter to you? No.

In the first case the method is “def title=()” and in the second it’s  
“def title()”. Maybe. Rails probably abstracts even that. But that’s  
the general idea.

-philip

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [July 10, 2010, 8:00pm UTC](https://www.ruby-forum.com/t/camelcase-and-underscores/191349/9 "2010-07-10T20:00:31Z")

</div>

Should have mentioned this before, but if you’re really curious about  
this sort of thing, the “Well Grounded Rubyist” is excellent.

> **[The Well-Grounded Rubyist](https://www.manning.com/books/the-well-grounded-rubyist?new=true&experiment=A)**
>
> The Well-Grounded Rubyist takes you from interested novice to proficient practitioner. It's a beautifully written tutorial that begins with the basic steps to get your first Ruby program up and running and goes on to explore sophisticated topics like...

(just a happy owner of said book)
