C ++: using '.' operator for expressions and function calls

I was wondering if using a member statement is correct . in the following way:

 someVector = (segment.getFirst() - segment.getSecond()).normalize().normalCCW(); 

I just did it to show two different things that I was interested in, namely, if using (expressions).member/function() and foo.getBar().getmoreBar() corresponded to the spirit of readability and maintainability. In all the C ++ codes and books that I learned about, I have never seen it that way, but it's intoxicatingly easy to use it as such. You do not want to develop any bad habits, though.

Probably more (or less) important than that, I was also wondering if there would be any gains or losses in performance using it that way, or unforeseen errors that could lead to program errors.

Thank you in advance!

+2
source share
8 answers

or unforeseen errors that would introduce errors into the program

Well, possible traps will be

  • Itโ€™s harder to debug. You wonโ€™t be able to see the results of each function call, so if one of them returns something unexpected, you need to break it into smaller segments to see what happens. In addition, any call in the chain may fail, so again you will have to break it to find out which call fails.

  • Harder to read (sometimes). Function call chains can make code more difficult to read. It depends on the situation, there is no hard and fast rule. If the expression is even somewhat complicated, this can complicate the task. I have no problem reading your specific example.

Ultimately, it comes down to personal preference. I do not try to fit on one line as much as possible, and I was bitten enough, clinging to where I should not, so that I would stagger a little. However, for simple expressions that are unlikely to crash, the chain is fine.

+4
source

Yes, thatโ€™s quite acceptable and actually would be completely unreadable in many contexts if you would NOT.

He called up a chain of methods.

There may be some increase in performance since you are not creating temporary variables. But any competent compiler will optimize it anyway.

+3
source

Itโ€™s absolutely right to use it as you showed. It is used in the idiom named parameter described in C ++ faq lite, for example.

One of the reasons why it is not always used is when you should store the intermediate result for performance reasons (if normalization is expensive and you have to use it more than once, it is better to store the result in a variable) or readability.

my2c

+1
source
 someVector = (segment.getFirst() - segment.getSecond()).normalize().normalCCW(); 

Not the answer to your question, but I have to tell you that the behavior of the expression (segment.getFirst() - segment.getSecond()) not defined according to the C ++ standard. The order in which each operand is evaluated is an unspecified standard!

Also see this topic: Is this code correct?

+1
source

Using a variable for intermediate results can sometimes increase readability, especially if you use good variable names. Excessive chaining may make it difficult to understand what is happening. You must use your decision to decide whether to break the chains using variables. The above example is not excessive for me. Performance should not vary much anyway if you enable optimization.

+1
source

I suppose what you are doing is less readable, however, on the other hand, too many temporary variables can also become unreadable.

As far as I know, there is a bit of overhead when creating temporary variables, but the compiler can optimize this.

0
source

There is no big problem with using it this way - some APIs greatly benefit from the chain of methods. In addition, it is misleading to create a variable and then use it only once. When someone reads the next line, they donโ€™t need to think about all the variables that you have not saved now.

0
source

It depends on what you do.

For readability, you should try using intermediate variables.
Assign calculation results to pointers , and then use them.

0
source

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


All Articles