0

I've found that this boilerplate code appeared frequently in my program:

if (ptr) {
  ptr->method();
}

and I've replaced it with a single-liner (like I saw people do it in Java):

ptr && (ptr->method(), true);

Is this a good practice from the standpoint of generated machine code or am I introducing additional constants into my binary? What possible issues might this "port" from Java cause in my program?

user1095108
  • 14,119
  • 9
  • 58
  • 116
  • 3
    Original looks better and more readable. – paper.plane May 12 '14 at 12:10
  • Yeah, but similar code is frequent in `Java` and `Java` is supposedly (according to some people) "better" than `C++`. – user1095108 May 12 '14 at 12:11
  • 2
    @user1095108 Other people disagree with some people. – danielschemmel May 12 '14 at 12:17
  • Before you change this, you should read abound undefined behavior and compiler optimizations!! – user743414 May 12 '14 at 12:19
  • 2
    @user743414 Why? This particular example is perfectly valid (if horribly unreadable) code. Unless someone was crazy enough to overload `operator ,` for whatever type `method()` returns, of course. – Angew is no longer proud of SO May 12 '14 at 12:20
  • It's just valid when you assume ptr was initialized with 0 like it is in a debug build. http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html – user743414 May 12 '14 at 12:21
  • @user743414 What? I mean, your comment would apply equally well to both cases. Their use of `ptr` is the same. – Angew is no longer proud of SO May 12 '14 at 12:23
  • 4
    @Angew It would also fail if `operator&&` is overloaded, since that [kills the lazy evaluation](http://stackoverflow.com/a/16031861/65678) ;) – danielschemmel May 12 '14 at 12:31
  • @user1095108: don't assume that a language being "better" than another means that all of its style guidance will be directly applicable to the other. – Dan Puzey May 12 '14 at 12:32
  • Is this legal in Java? I thought that lossy implicit conversions like this are one of the things Java did improve on with respect to C++. (In C++, of course, you would normally write `if ( ptr != nullptr ) { ptr->method(); }`, with an explicit comparison. The implicit conversion to `bool` is a historical left-over, which is avoided in well written code.) – James Kanze May 12 '14 at 12:58
  • @JamesKanze http://stackoverflow.com/questions/2369226/null-check-in-java – user1095108 May 12 '14 at 13:08
  • @user1095108 What does that have to do with `if (ptr)` (which is, as far as I know, illegal in Java)? – James Kanze May 12 '14 at 16:06
  • @JamesKanze You are right, but `if (null != ptr)` is ok and `(ptr != null) && ptr.bool_returning_method();` also. It is not vastly different syntax. – user1095108 May 12 '14 at 19:52

1 Answers1

1

From the standpoint of generating machine code, this is completely irrelevant. If one of these two forms is better and a compiler's optimiser is not capable of converting one to the other, it's not worth the name "optimiser," period.

This leaves just the question of which is more readable. The first one is immediately obvious. The second one will easily take a human more than a minute to parse. Not good.

Also note that the second case might introduce unwanted results in the presence of operator overloading. If operator , is overloaded for the type which method() returns, it will get called in the 2nd case, not in the first. If operator && is overloaded for the type of ptr, the 2nd case will simply fail since overloaded && is not lazily evaluated. However, both of these operators are among those whose overloading is strongly discouraged.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • 1
    One-liners also have an advantage over `if`, as they can appear in a `constexpr` function in `c++11`. – user1095108 May 12 '14 at 12:27
  • The generated machine code depends on the code preceeding this piece of code. – user743414 May 12 '14 at 12:27
  • @user743414 Of course. But that holds for virtually *any* code snippet. If we analyse the OP's snippet in isolation, my answer stands. If we don't, we'd need to know the context to say anything. But as far as the UB you keep referring to, if one of the OP's versions has UB, both have. – Angew is no longer proud of SO May 12 '14 at 12:30
  • I think your comment about the overloaded `,` operator might be worth including in your answer. – user1095108 May 12 '14 at 12:31
  • @user1095108 True, but that's a very specific case, for which specific handling applies. Just as if `ptr` was actually of a type which overloads `operator &&`. – Angew is no longer proud of SO May 12 '14 at 12:31