Why some people use "this->" when there is no need?

Pages: 123
I seem to be in the minority but I use comments to help readers follow along. Yes, comments can grow stale, but this is a coder problem, misuse of the tool. And overdoing it is a mess for sure, but a few here and there go a long way.
I don't think you're in the minority. Comments aren't bad. If you have complicated code, then comments are good to help the reader understand what's happening. But everything has a cost. I've run into comments time and time again that don't match what the function is doing because the function grows into something more than it used to be, or the requirements for the function change. The best comments also explain why the code is doing what its doing, so that the next reader understands the intention of the code. Comments aren't an excuse for bad variable/function names.
regarding comments going out of date because functions grow...

It's worth saying that this happens not only because codebase is under development but also because functions are often not designed to be explicit and short.

for example, it's not the same thing to write:

1
2
3
4
5
6
7
/*
 out of date comment because logic not split
*/
void Control::AutoSize()
{
      /* Do everything here */
}


Or splitting this code into:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// autosize control - end of story
float Control::CalculateFontSize();

// calculate content pixel size
float Control::CalculateContentSize();

// calculate font pixel size
void Control::AutoSize()
{
      CalculateFontSize();
      CalculateContentSize();

      // ... the rest is short and only about what comment says
}


See? if you write comment in first case, your comment will surely go out of date soon.
In second case it's much less likely for comments to go out of date because things are explicit, functions become shorter and comments more understandable even if out of date.

This is rather stupid example, but there are many such stupid examples and one reason why comments can't explain the logic behind single "AutoSize" function.

Sometimes you figure this out and split your code, but then forget to update comments.
My point is that writing comments should be followed by good design as well, to minimize unncessary confusion.
Last edited on
Topic archived. No new replies allowed.
Pages: 123