The title might be a bit vague, so let me explain. Let's assume we have a function that does something (changes state of the program), for example a function that creates a file. That function returns True if the file is created and False if the file isn't created.
Now, we want to use that function in a conditional, for example:
if (createFile() == false)
// log: we cannot create file
And we can also do it the following way:
boolean fileCreated = createFile()
if (fileCreated == false)
// log: we cannot create file
The question is if the first case is worse than the second in terms of readability and clarity and which one is recommended to be used?
My reasoning is that because someone reading the code might not be familiar with the function's internals so in the first case he might assume that the function createFile() doesn't alter the state (since often these functions are predicate functions)?
== false
, rather than using!
. Aside from that, the first version looks better to me, but that's really just pure opinion.createFile()
will change state, but the first version doesn't.createFile()
would simply bevoid
and the 'if' would not be needed at all.