views:

253

answers:

3

Prefixing variable and method names with an underscore is a common convention for marking things as private. Why does all the methods on the page tracker class in the Google Analytics tracking code (ga.js) start with an underscore, even the ones that are clearly public, like _getTracker and _trackPageView?

+1  A: 

Just in case you have a getTracker() function in your own code, or similar.

In other words, to avoid naming conflicts with the page's javascript code, probably.

@Theo: Didn't realize (ie, not read carefully enough) they were methods. Then maybe to encourage caution or discourage use? Dunno, really.

Vinko Vrsalovic
Those are methods, there's no possibility of naming conflicts unless I start declaring methods on the page tracker's prototype.
Theo
A: 

I've always read this like so:

If the property/method is prefixed with an underscore, it is for some "internal" workings. Therefore if you are about to use/call/alter this property/method, you had better darn well know what you are doing, and or expect it to possibly be renamed/removed in a future release.

scunliffe
I take it you haven't used Google Analytics? *All* methods on the page tracker object are prefixed with an underscore, even those that explicitly stated as public in the documentation.
Theo
+1  A: 

Because Google can't be bothered to follow the Module Pattern and therefore they don't want accidental collisions in the global namespace?

Hank Gay