Skip to main content
added 426 characters in body
Source Link
juanchopanza
  • 228.2k
  • 35
  • 421
  • 493

You could say

if (theString.size()) { .... } 

Whether that is more readable is a different matter. Here you are calling a method whose primary purpose is not to tell you if the thing is empty, and relying on an implicit conversion to bool. I would prefer the !s.empty() version. I might use not instead for fun:

if (not theString.empty()) { .... } 

It might be interesting to see the correlation between people who find the ! and not versions confusing.

You could say

if (theString.size()) { .... } 

Whether that is more readable is a different matter.

You could say

if (theString.size()) { .... } 

Whether that is more readable is a different matter. Here you are calling a method whose primary purpose is not to tell you if the thing is empty, and relying on an implicit conversion to bool. I would prefer the !s.empty() version. I might use not instead for fun:

if (not theString.empty()) { .... } 

It might be interesting to see the correlation between people who find the ! and not versions confusing.

Source Link
juanchopanza
  • 228.2k
  • 35
  • 421
  • 493

You could say

if (theString.size()) { .... } 

Whether that is more readable is a different matter.