0

I read that it's a good convention to name functions that return a bool like IsChecksumCorrect(Packet), but I also read that it's a good convention to name boolean variables like IsAvailable = True

But the two rules are incompatible: I can't write:

IsChecksumCorrect = IsChecksumCorrect(Packet)

So what's the best way to name vars that store boolean values returned by such functions?

PS: Extra points if you can think of a way that doesn't depend on changing the case (some languages--like Delphi--are case-insensitive).

DBedrenko
  • 4,871
  • 4
  • 38
  • 73
  • 1
    This is a bad fit for SO.com, but why not just drop the `Is` from the variable name? Then a subsequent `if(ChecksumCorrect)` reads "natural". – rubenvb Nov 18 '14 at 10:02
  • @rubenvb That's a good suggestion, thank you. But I contest that this is a bad question for SO; here are similar question which are successful and have not been closed: [#1](http://stackoverflow.com/questions/6950841/is-using-is-to-name-boolean-variables-bad-practice) [#2](http://stackoverflow.com/questions/1227998/naming-conventions-what-to-name-a-boolean-variable) [#3](http://stackoverflow.com/questions/2526886/naming-booleans) – DBedrenko Nov 18 '14 at 10:14
  • Old questions don't make new ones all right. This fits the "Primarily Opinion-based" close reason perfectly, as it will depend on a number of personalized and opinionated factors. The question would be a better fit if you were trying to adhere to some externally established convention (e.g. the standard library, Qt, or some other big framework you want to adopt a coding style from). But hey, that's just my opinion anyways, I mean no ill intent towards you `:-)`. – rubenvb Nov 18 '14 at 10:25
  • 1
    move the "Is" into the middle of the name so that the question "IsChecksumCorrect" becomes a statement/answer "checksumIsCorrect" which may be true or false – vlad_tepesch Nov 18 '14 at 13:02
  • @vlad_tepesch What an excellent suggestion; so far I'll be adopting this one. Thank you – DBedrenko Nov 19 '14 at 08:11

1 Answers1

0

First of all, there can be difficulties only with functions that don't require arguments, in your example instead the variable should just be called IsPacketChecksumCorrect.

Even with functions with no arguments I think you would only have problems if you were just caching the result of the function, for performance's sake, and you could safely replace all instances of the variables with calls to the function if it weren't for the performance. In all other cases I think that you could always come up with a more specific name for the variable.

If you were indeed just caching, why not just call the variable Functionname_cache? It seems quite clear to me.

If you needed to use a lot this "technique" in your project and _cache seemed too long or you did not like it you could well settle on a convention of your own; as long as you are consistent you can adopt whatever works best for you, people new to the project just need to be explained the convention once and they will easily recognize it ever after.


By the way, there are various opinions on the conventions for the naming of booleans. Personally I prefer to put the subject first, which makes the Ifs more readable, e.g. ChecksumIsCorrect, ChecksumCorrect or ChecksumCorrectness. I actually prefer not to put the Is altogether, the name usually remains clear even if you omit it.

gbr
  • 1,264
  • 8
  • 27
  • Hmm I have never seen or heard of any code that has `Functionname_cache`. In that sense, most vars that store what functions return are caches; if I was to make the function call everywhere it would be less readable than a well-named variable. – DBedrenko Nov 19 '14 at 08:14