CamelCase and Underscores

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

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

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.

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!

You shouldn’t need to.

Because… (?)

(Not being facetious).

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

^^

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!

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

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

(just a happy owner of said book)