Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does java/javascript/python force the use of () after a method name, even if it takes no arguments?

One of my most common bugs is that I can never remember whether something is a method or a property, so I'm constantly adding or removing parentheses.

So I was wondering if there was good logic behind making the difference between calling on an object's properties and methods explicit.

Obviously, it allows you to have properties and methods that share the same name, but I don't think that comes up much.

The only big benefit I can come up with is readability. Sometimes you might want to know whether something is a method or a property while you're looking at code, but I'm having trouble coming up with specific examples when that would be really helpful. But I am a n00b, so I probably just haven't encountered such a situation yet. I'd appreciate examples of such a situation.

Also, are there other languages where the difference isn't explicit?

Anyways, if you could answer, it will help me be less annoyed every time I make this mistake ^-^.

UPDATE: Thanks everyone for the awesome answers so far! I only have about a week's worth of js, and 1 day of python, so I had no idea you could reference functions without calling them. That's awesome. I have a little more experience with java, so that's where I was mostly coming from... can anyone come up with an equally compelling argument for that to be the case in java, where you can't reference functions? Aside from it being a very explicit language, with all the benefits that entails :).

like image 282
araneae Avatar asked Sep 19 '10 01:09

araneae


1 Answers

All modern languages require this because referencing a function and calling a function are separate actions.

For example,

def func():
    print "hello"
    return 10
a = func
a()

Clearly, a = func and a = func() have very different meanings.

Ruby--the most likely language you're thinking of in contrast--doesn't require the parentheses; it can do this because it doesn't support taking references to functions.

like image 56
Glenn Maynard Avatar answered Oct 25 '22 06:10

Glenn Maynard