Is using condition within condition a good practice?

Is the code below good(I know that you can use else but I’m talking about this particular case)?

local something=1

if something==1 then
  print("A")
  if not something==1 then
    print("B")
    if something==2 then
      print("C")
end

The example you showed isn’t a good method. It can be good in some cases but yours isn’t one of them.

Here are a few examples.

Your method’s correct way:

local number = 1
if number == 1 then
	print("Number is 1!")
elseif number == 2
	print("Number is 2!")
else
	print("Number is neither 1 or 2.")
end

A correct method:

local value = false
local number = 1

if number == 1 then
	if value then
		print("Number and value is 1 and true respectively!")
	else
		print("Number is 1 but value is false.")
	end
else
	print("Number is not 1.")
end

Another way to do this could be this one but it’s longer.

local value = false
local number = 1

if number == 1 and value then
	print("Number and value is 1 and true respectively!")
elseif number == 1 and not value then
	print("Number is 1 but value is false.")
else
	print("Number is not 1.")
end

By longer I mean it doesn’t look too good and is longer width wise.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.