Is it better not to use embedded if statements?

I’ve been trying to learn scripting for about 3 weeks and I’m learning but a lot have told me that it is better to not use embedded if statements, is that true or it is essential to learning to script?

Ps Is nested if statements the same as an embedded if statement?

2 Likes

I think it is essential to learning how to script, but do whatever you think is right.

1 Like

Could you define what “embedded if” is? How is it different from “nested if”?

1 Like

First of all, embedded if statments are the same as nested if statments, from what I know at least.

When you say “it’s bad to use nested/embedded if statments”, if you mean that it’s bad in general, it’s bad in any case it’s applied in, that’s quite wrong, but if you mean it’s bad in certain cases, and can be easily replaced by something much cleaner, then yeah it’s correct.

Nested/embedded if statments are ESSENTIAL, and you’ll always find yourself using it without even noticing. There are cases where it’s redundant (meaning it can be easily replaced with something better) like for example, if I wanted to check if a part’s name was Bob and it’s brickcolour is "Really Red", you can check each property seperately.

if part.Name == "Bob" then
     if part.BrickColor == BrickColor.new("Really red") then
         print("yes")
     end
end

This is deffintely correct, but you can write something much cleaner

if part.Name == "Bob" and part.BrickColor == BrickColor.new("Really red") then
     print("yes")
end

It’s not bad to use an embedded/nested if statment in this case, but there is something better than doing so.

Sometimes embedded/nested if statments are obligatory. Like in this case (taken from another topic that you were also the one that made):

Embedded if statments are useful, when you need to check 2 conditions , and not at the same time, meaning without using the and operator, which I recommend you check out.
Like if you wanted to check if a part’s brickcolor was red, but what if the object you were checking was not a part, that will result into an error, meaning you also have to check if the object is a part, using :IsA() .

if object:IsA("Part") and object.BrickColor == BrickColor.new("Really Red") then --using and, to check for two things

end

But even if the object wasn’t a part, and the condition was not met, this would still error! Because we’re also checking at the same time for the colour, and we’re doing object.BrickColor , if the objetc wasn’t a part it wouldn’t have a brickcolor property, thus it errors. That’s why we have to check them seperately, so we check if it’s even a part first, then check if it’s brickcolor is red, and proceed

if object:IsA("Part") then
    if object.BrickColor == BrickColor.new("Really red") then
         --do something
    end
end

And here’s even a funny story that @Crazyman32 told talking about embedded if statments

2 Likes

Hi, thanks for responding I will take what you said in mind also I think I never said it’s bad to use nested/embedded if statements I said a lot have told me it is better not to use embedded if statements. but still thank you!

1 Like

Yeah no problem! I really just said that in general, to any person that said it