Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

What's the coding standard for naming a private method in a language which does not have the private modifier? Specifically, I am talking about Javascript. Below I've placed an underscore at the end of my private method's name, but a colleague told me that underscore is usually for class fields.

var MyClass = Class.create({
    initialize: function() {
        alert('constructor');
    },
    publicMethod: function() {
        alert('i am public');
    },
    privateMethod_: function() {
        alert('i am private');
    }
});
share|improve this question
    
Basically this. Python uses double underscore before the function name (it mangles the name, but you can still get to it). –  unholysampler Jan 26 '12 at 0:49
    
Double underscore, you say... What would a single underscore mean? –  JoJo Jan 26 '12 at 0:56
7  
More detail: Python uses _ for private and __ for Python internal names. You can use _ names, which are simply concealed in a few ways. You should not use __ names because the mangling makes debugging remarkably hard, and it exposes you to the possibility that a future release will interfere with your name. Use _ in Python for "more-or-less private". –  S.Lott Jan 26 '12 at 0:59
    
@JoJo: Take a look at this, it goes into the details. –  unholysampler Jan 26 '12 at 1:01
    
Be aware that in C and C++, identifiers starting with underscores are reserved to the implementation; what works in one language may not work in another. –  Keith Thompson Jan 26 '12 at 18:32
add comment

2 Answers

up vote 2 down vote accepted

As everyone told you in a comments, most people(and some languages) use single underscore.

share|improve this answer
add comment

EDIT: Per the comment below by @Raynor, the approach below is not a good way to achieve privacy in JavaScript. See: http://blog.jcoglan.com/2012/01/19/the-cost-of-privacy/


I use this convention:

var MyObject = (function() {
  var initFunction = function() { /* init some stuff */ };
  var privateFunction = function() { /* state secrets */ };

  return {
    init: initFunction
  };
})();

Only the public functions are returned. As you may guess I also tend to use only methods when using this style, no public properties.

share|improve this answer
    
This style also has the disadvantage of being significantly less efficient then the original style. Generally privacy is never worth run-time penalties. –  Raynos Jan 26 '12 at 9:23
    
@Raynos, I didn't realize it was so much less efficient. Can you link me to a doc showing this? I'm glad to update my own coding style but I need more data as to why. –  Ross Smith Jan 26 '12 at 17:04
1  
The cost of privacy mainly about increasing memory usage to O(n) (n === instances) rather then O(1). –  Raynos Jan 26 '12 at 17:08
    
That was a great read; thanks for sharing!! I'm definitely going to write my next project differently. –  Ross Smith Jan 26 '12 at 17:28
    
Works great for services or other classes that are only initialized once though. And really, most of your memory is not going to be sucked up by code, but instead data. –  Domenic Jan 26 '12 at 18:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.