Title. Right now, I have several true/false values that I evaluate from ModuleScripts. Unless I'm going to be iterating over these, it seems like storing "false" as a value wastes space as I could just evaluate
if bar then -- do stuff end
instead of
if bar == true then -- do same stuff end
Am I correct in this optimization?
if bar then -- do stuff end
would 100 percent work better
They can essentially be the same thing.
For example, when checking a boolean value:
local bool = true
Both would check to see if bool == true
:
if bool then if bool == true then
By doing if bool then
, you're basically telling the conditional statement to check for bool == true
in this scenario. If you did this...
if not bool then
...then you telling the statement to check for not true
, which is essentially bool == false
. Efficiency is, therefore, almost entirely ignored when doing this.
When checking to see if a userdata exists, you can then feed the condition the object directly:
local inst = script.Parent:FindFirstChild("Humanoid") -- FindFirstChild() will return the Instance if it finds it, or nil if it can't find the Instance if inst then
In this scenario, if inst then
is equivalent to if inst ~= nil then
, which can also be viewed as if (inst ~= nil) == true then
since inst ~= nil
will evaluate to either true
or false
.
"Efficiency" is not a relevant factor here. It's just simpler to do either one of these things:
if inst then if not inst then if bool then if not bool then
As compared to:
if inst ~= nil or bool == true then if inst == nil or bool == false then