1

In javascript, I have like.,

0. Current Way of Naming:

Cell = function(position){
   //Constructor.
   var pos = position;


   //Many Sub-functions using variable `pos` here through out this function Cell.
}

But in java/C++, I could write the same as.,

   Cell(int pos){
    this.pos = pos; 
   }

Proposed ways of naming :

1. About this.pos = pos
Here in javascript too I could do like this.pos = pos, it will work. But the problem is., then through out the program I have to access my position as this.pos., and it hazardous when assigning because when I assign like this.Pos = 7 (wrong case), then it wont show bug. So this method is not preferable.

2. About pos = _pos

Cell = function(_pos){
       //Constructor.
       var pos = _pos;

But generally, in C++/Java I will not use this way, so preferably, this is not my best way.

3. Using the parameter of constructor as variable through the function.

Cell = function(pos){

I could use this pos, through out my function, but I am in doubt whether I could do this. Because, I need to do this in a proper prototype-oriented way with constructors, setter, getter etc... Moreover in the John resign link , he used value and val two different names.

In this link., Best practice for parameter naming in Java constructors and simple setters I saw, there is no conventions for parameter naming.

My personal preference is 1, but I will be forced to use, this.pos and risky too.

So, Is 1 or 2 or 3 or 0 (my current way)., which one is best?

Community
  • 1
  • 1
Muthu Ganapathy Nathan
  • 3,199
  • 16
  • 47
  • 77
  • Which one is "best" is a matter of opinion. I don't understand what's your problem with option 3, since it's the cleanest one. – bfavaretto Jul 30 '13 at 19:02
  • While 2 and 3 are basically the same, 1 is not. With 1 you create an actual property on the object that is about to being created that can be accessed with `obj.pos` or with `obj["pos"]`. So it really depends on what you want. – basilikum Jul 30 '13 at 19:02
  • @basilikum what about my current way (0) ? – Muthu Ganapathy Nathan Jul 30 '13 at 19:04
  • Could you elaborate on what you mean by *I need to do this in a proper prototype-oriented way with constructors, setter, getter etc*? – bfavaretto Jul 30 '13 at 19:08
  • @EAGER_STUDENT I'm sorry, I completely missed that this is about naming conventions. If I had to decide between 0, 2 and 3, I would choose 3 because it's the cleanest way. I wouldn't use 2 because I personally don't see what kind of extra information the underscore should provide. But besides that, you are just adding useless clutter, it's perfectly fine to just use the variable that was passed to your function, it's a relativly normal local variable. – basilikum Jul 30 '13 at 19:21
  • Do not use abbreviations or acronyms. Spell out the full word, .e.g. position, not pos. – Fred Nov 03 '16 at 13:41

1 Answers1

0

Honestly, I would simply go for this.pos = pos; and rely on documentation to know that it's a private member that shouldn't be accessed. I've also seen some people use the _ prefix for that purpose, for instance this._pos = pos;. The problem with making all your instance variables "private" is that you are sacrificing all the benefits of prototypal inheritance since your functions will have to be declared inside the constructor function to create a closure over these. That means the functions will not be shared between instances through the prototype, but rather recreated everytime as a direct instance member.

Making all your members public doesn't prevent you from creating and using setters, but you need to rely on standards instead of enforcing the privacy.

function Cell(pos) {
    this.setPos(pos);
}

Cell.prototype.setPos = function (pos) {
    if (typeof pos !== 'number') {
         throw 'pos has to be a number';
    }

    this.pos = pos;
};

Let's compare with enforcing:

function Cell(pos) {
    //note that we use value to avoid shadowing the closed-over pos variable
    this.setPos = function (value) {
        if (typeof pos !== 'number') {
             throw 'pos has to be a number';
        }

        pos = value;
    };

    this.getPos = function () {
        return pos;
    };

    this.setPos(pos);
}

While this ensures the accessors cannot be bypassed, it leads to ugly and very inefficient code in my opinion. However, there are some valid cases to use a similar approach, like when writing modules or singletons.

plalx
  • 42,889
  • 6
  • 74
  • 90