Hello, I’d just like to know different type of ways to write this:
if value_one < 400 and value_two < 400 then
return 3
elseif value_one < 400 then
return 1
elseif value_two < 400 then
return 2
end
Hello, I’d just like to know different type of ways to write this:
if value_one < 400 and value_two < 400 then
return 3
elseif value_one < 400 then
return 1
elseif value_two < 400 then
return 2
end
Since the code itself is very simple, I don’t see a cleaner way of typing it.
This is a way i suppose:
function Method(value_one, value_two)
return value_one < 400 and value_two < 400 and 3 or value_one < 400 and 1 or value_two < 400 and 2 or nil
end
But it’s barely readable lol
Your code is pretty short so there isn’t much to really change, it’s clean enough for what your trying to do. Try focusing on cleaning up large chunks of code, it’s a lot better to do.
As everyone else has said, this code snippet is really short and you really don’t need to change anything here.
Here’s one different type of way to write it:
if value_one < 400 then
if value_two < 400 then
return 3 -- v1 < 400, v2 < 400
else
return 2 -- v1 < 400, v2 > 400
end
elseif value_two < 400 then
if value_one < 400 then
return 3 -- v1 < 400, v2 < 400
else
return 1 -- v1 > 400, v2 < 400
end
end -- v1 > 400, v2 > 400 (implicitly returns nil)
This makes the code unnecessarily long, but will avoid evaluating if value_one/value_two < 400
twice when the first if statement is false. (only two conditions are ever evaluated, instead of 3 in the scenario where one value is < 400, and the other > 400)
Please note that this is a micro-optimisation, it’s unnecessary and won’t affect your code’s performance in any noticeable manner.