In C ++, is it good practice to use 'this'?

I was wondering if it is good to use 'this' in C ++. At first I thought it should be, because in this way you make it obvious that the thing you are talking about is a member of the current class, but sometimes you end up with the following code:

Document::Document(QWidget *parent) : QWidget(parent) { this->file = 0; this->layout = new QGridLayout(this); this->layout->setSpacing(2); this->layout->setMargin(0); this->setLayout(this.layout); this->textArea = new QTextEdit(this); this->textArea->setLineWrapMode(QTextEdit::NoWrap); this->textArea->setAcceptDrops(true); this->textArea->setAcceptRichText(true); this->textArea->setUndoRedoEnabled(true); this->textArea->setFont(QFont("Mono" , 11)); this->layout->addWidget(this->textArea); this->textArea->show(); this->textArea->setFocus(); } 

It seems to me that it will be much better if you do not use 'this' when it is used as this->layout->addWidget(this.textArea); . And I think that in most cases the code should use the same style to make it easier to read, so you should use 'this' only when necessary, or it is good practice to use it to clearly indicate that you are referring to member of the same class.

+6
source share
7 answers

There is no single answer to this question. It depends on the context, and it depends on who you ask.

Some people use this all the time. Some never use it. Some instead rename all member variables with a special prefix ( mFile instead of file ).

I prefer to use the this prefix when you need to avoid ambiguity.

In well-written code, it is usually obvious whether you are referencing a local or a member variable, and therefore this actually does little. But sometimes it can be difficult for the reader to determine if the variable is a member of the class or not. Then this is a good way to clarify.

+5
source

If using this makes reading easier (it’s easier to understand, that is), then additional typing is worth the effort.

In a complex program, each line of code will be written once, but it will be read many times (and by different programmers), so this is a good investment.

One of the most widely read languages ​​I know, Python, makes you write self.x (the equivalent of this->x C ++) every time, and that is not a problem at all. The problem is that in Python, when you write slef.x instead of self.x , the error gets to the runtime , but this is another unrelated problem ...

+4
source

This does not matter to the compiler (in your example).
Its a matter of user preference, some find it more intuitive, while some find it an untrained extra typing.
So just use it if you adhere to the coding rules that you use.

Please note that in some cases you need to use this because there is no other way around this, but that is not your question as I see it.

+2
source

Personally, I don't like using this .
But I think this is a personal preference. Until you agree, it does not matter.

Note. Also I have to compile with -WShadow -WError

+2
source

In my opinion, this is not often used for members. IMHO it looks pretty dirty. I prefer to use the prefix "m_" for all members, so it clears the member and what is a local variable. In any case, there is more important good practice, for example, more efficient C ++ written by Scott Myers.

+2
source

I usually prefer code that uses this for all members.

In your example, this seems redundant because you are not referencing any local variables. However, in code that uses a mixture of local and instance (or even classes), it is much better to find out where to look.

0
source

IMO, it’s really bad practice to use it, unless it’s really necessary.

The code should include what you need - nothing more. This does not mean that you cannot (for example) implement convenient functions that are not strictly necessary for using the software, but that the code should include exactly what is needed to implement the functions.

I will add that, in my opinion, the use of prefixes, such as m or m_ for member variables, belongs to the same category: simple noise, which contributes nothing to real understanding.

In any case, the sense of need for such added noise tends to indicate that the code is simply not very well designed. There are several cases (for example, parameters passed to the constructor) where it is reasonable to have some confusion as to what the internal value is and not the value coming from outside, but this is an exception, not a rule. Since they are an exception, emergency measures to prevent / avoid ambiguity should also be limited to them, for example, the init prefix, which will be systematic, but still meaningful:

 class X { int x, y, z; public: X(int init_x, int init_y, int init_z) : x(init_x), y(init_y), z(init_z) {} }; 
0
source

Source: https://habr.com/ru/post/914093/


All Articles