2

Situation is that I'd like to use descriptive variable names as member variables so they are well understandable in headers (for example: min_distance_to_polygon). Yet in complex algorithms I'd find it smoother to have much shorter variable names because the context is clear (for example min_dist in that case).

So in the definition of a method I'd just write:

int & min_dist = min_distance_to_polygon;

Does this cause an overhead after compilation and would this be acceptable coding style?

EDIT: Would this be better (as it prevents a possible copy)?

int & min_dist{min_distance_to_polygon};
AlexGeorg
  • 967
  • 1
  • 7
  • 16
  • Possible duplicate of [What exactly is the "as-if" rule?](https://stackoverflow.com/questions/15718262/what-exactly-is-the-as-if-rule) <- here you can find explanation – Marek R Nov 13 '19 at 15:47
  • related: https://stackoverflow.com/questions/1179937/how-does-a-c-reference-look-memory-wise – NathanOliver Nov 13 '19 at 15:48
  • for an `int` you probably wouldnt notice any difference even if you make a copy even without compiler optimization. When you have to choose between readability and premature optimization then better go for the first – 463035818_is_not_an_ai Nov 13 '19 at 15:48
  • @formerlyknownas_463035818 Hmm? A copy wouldn't even be made. A memory address would be stored if the reference weren't elided. – cdhowie Nov 13 '19 at 15:49
  • 3
    Regardless of whether compilers optimise this I don’t think I like your strategy: as a reader of the code I’d be left wondering what the heck is going on here, and why you’re seemingly needlessly aliasing a variable. Instead of renaming it via assignment (and drop the unnecessary reference! copy it), I’d try restructuring the algorithm to reduce references to verbose names. – Konrad Rudolph Nov 13 '19 at 15:50

2 Answers2

3

Does this cause an overhead after compilation

Not with an optimizing compiler, no. This is bread-and-butter optimization for a compiler. In fact, even copying the value would likely not cause any overhead (assuming that it remains unchanged) due to the compiler's value tracking and/or how CPU registers actually work behind the scenes (see Register Renaming).

and would this be acceptable coding style?

That's opinion-based and debatable. I posit that there exists code where this is a reasonable choice, but that such code is rare. In the end it's up to you to judge whether future readers will find either version easier to read and comprehend.

Would this be better (as it prevents a possible copy)?

The two code snippets you show are exactly identical in their semantics - both are initialization. No operator= is invoked (even conceptually) in X x = y;.

Max Langhof
  • 23,383
  • 5
  • 39
  • 72
0

Will a compiler remove effectless references?

It depends. It may, if it can. The language doesn't mandate optimisation, it is permitted.

Does this cause an overhead after compilation and would this be acceptable coding style?

Overhead would be highly unlikely in this case, assuming the compiler optimises the program.

In general, you can verify that a reference is optimised away by comparing the generated assembly with or without the reference. If the generated assembly is identical, then there will not be any overhead.

More generally, you can verify whether any change has significant overhead by measuring.

Would this be better

int & min_dist{min_distance_to_polygon};

It would be effectively identical.

Community
  • 1
  • 1
eerorika
  • 232,697
  • 12
  • 197
  • 326