23

What is the advantage of having this/self/me pointer mandatory explicit?

According to OOP theory a method is supposed to operate mainly (only?) on member variables and method's arguments. Following this, it should be easier to refer to member variables than to external (from the object's side of view) variables... Explicit this makes it more verbose thus harder to refer to member variables than to external ones. This seems counter intuitive to me.

Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366

5 Answers5

25

In addition to member variables and method parameters you also have local variables. One of the most important things about the object is its internal state. Explicit member variable dereferencing makes it very clear where you are referencing that state and where you are modifying that state.

For instance, if you have code like:

someMethod(some, parameters) {
    ... a segment of code
    foo = 42;
    ... another segment of code
}

when quickly browsing through it, you have to have a mental model of the variables defined in the preceding segment to know if it's just a temporary variable or does it mutate the objects state. Whereas this.foo = 42 makes it obvious that the objects state is mutated. And if explicit dereferencing is exclusively used, you can be sure that the variable is temporary in the opposite case.

Shorter, well factored methods make it a bit less important, but still, long term understandability trumps a little convenience while writing the code.

Ants Aasma
  • 53,288
  • 15
  • 90
  • 97
  • It's useful when a scoping issue arises (a locally scoped foo will hide the classes member variable foo), but considering comments by OP elsewhere, it's not strictly mandatory, just convenient. – Robert Paulson May 26 '09 at 11:16
  • I also need a 'mental model' to know if I'm within an `if` or a `while`; why don't we prefix all such commands with that information? – einpoklum Apr 09 '15 at 19:23
  • @RobertPaulson: This problem could be addressed by requiring explicit shadowing of variables from some external scope. – einpoklum Apr 09 '15 at 19:24
  • This is very useful. But it should be optional. I came here from a discussion about the mandatory "self." scope modifiers from Python. Excessive use of such modifiers, as seen in Python, litters the code and reduces readability overall. – MickeyDickey Sep 22 '21 at 17:05
3

What if the arguments to a method have the same name as the member variables? Then you can use this.x = x for example. Where this.x is the member variable and x is the method argument. That's just one (trivial) example.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
PowerApp101
  • 1,798
  • 1
  • 18
  • 25
3

You need it to pass the pointer/reference to the current object elsewhere or to protect against self-assignment in an assignment operator.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
2

I generally use this (in C++) only when I am writing the assignment operator or the copy constructor as it helps in clearly identifying the variables. Other place where I can think of using it is if your function parameter variable names are same as your member variable names or I want to kill my object using delete this.

Naveen
  • 74,600
  • 47
  • 176
  • 233
  • Is it just me or does calling `delete this` seem really wrong? – Robert S. Barnes May 26 '09 at 10:42
  • generally it is bad idea..but you may want to do in some cases like a reference counted object. – Naveen May 26 '09 at 10:43
  • You have no other choice in case of a reference-counted object if you want the action of "decrement the counter, check the value and delete if needed" to be a single method. – sharptooth May 26 '09 at 10:46
  • The reference counted object should not delete itself, it should delete an object it refers to. –  May 26 '09 at 11:09
1

Eg would be where member names are same as those passed to method

public void SetScreenTemplate(long screenTemplateID, string screenTemplateName, bool isDefault)
        {
            this.screenTemplateID = screenTemplateID;
            this.screenTemplateName = screenTemplateName;
            this.isDefault = isDefault;
        }
Rashmi Pandit
  • 23,230
  • 17
  • 71
  • 111