Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

'if x ~= nil then' or 'if x then'? Is my dad right?

Asked by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

My dad is a professional programmer and has been for many years. He's a team leader for the company he works for. Yesterday, he gave me some advice after seeing my code.

When I code, I almost always check if something exists just by putting the name, e.g.

if x then

as opposed to

if x ~= nil then

but he said not to do this. Now, they are both obviously functional, but that's not the point, because readability is arguably more important than functionality. My dad told me that it's better to write out the comparison to help destroy any chance of another human misunderstanding.

So now I want one of you -- preferably someone who has some kind of professional programming experience -- to give me your opinion. x or x ~= nil?

0
if x is false, (x ~= nil) won't work as (if x then) FieryEvent 185 — 9y
0
That's totally irrelevant to the question... Perci1 4988 — 9y
0
No it is not, @Perci1. Oioiamigos has a point. The logic of your conditional completely changes what will be executed. If x is false, the underlying scope will still be ran with a comparison to nil vs the first comparison that will not run it. DigitalVeer 1473 — 9y
0
I'm checking if something "exists*, not if it's false. Will no one read the question? The same thing applies, though. 'if x ~= false' or 'if x then'? Perci1 4988 — 9y
0
I use if x then but im far from professional ZeptixBlade 215 — 9y

2 answers

Log in to vote
3
Answered by
Unclear 1776 Moderation Voter
9 years ago

I'm not a professional programmer, but I'll write down my opinion anyway and include some credentials, I guess.

Credentials..?

I'm going into the University of California at San Diego this fall to study Computer Science and graduated from Thomas Jefferson High School for Science and Technology. Some of my relevant courses that I took over high school include (aside from the lowly AP Computer Science course) Principles of Imperative Computation at Carnegie Mellon University, Parallel Computing, and Artificial Intelligence. I've been programming for five years now in a variety of languages. My strongest language is Python, and my second strongest language is C. I graduated after presenting my research on the entire pipeline of computer vision techniques to interpret variable LaTeX symbols and expressions in real world multi-line document images. I just recently requested a mock senior software engineer interview with a Microsoft interviewer when I was staying at his house in Washington state complete with a practical examination and passed.

I know how Scrum and Agile environments work from my father's work. Code review is an aspect of software development that I heavily emphasize.

Point is, I think I'm somewhat qualified to answer this question.

My Opinion

What to do pretty much boils down to what language you are using and code review.

What language you are using will matter. In some languages,

if object then

is not guaranteed to evaluate to true if the object is not equivalent to nothing.

My best example of this would be Python. In Python, a variety of constructs are evaluated as false in certain scenarios, which brings me to my next point: the language may matter because each language typically has an overarching style.

In Python, this is known as being Pythonic. The goal is to emulate English as much as possible by making your code readable and understandable.

For example, an empty List in Python is evaluated as false when it is forced to be evaluated as a Boolean. The output for this snippet when evaluated is: is empty.

object = [ ]
if not object:
    print("list is empty") 

However, notice that because of this some misunderstandings may occur. The output for this snippet when evaluated is: is empty.

object = None
if not object:
    print("list is empty")

This brings me to my next point: while some languages cast objects as certain values when evaluated as a Boolean, there is both an advantage and disadvantage.

You have already identified the advantage: it is far, far more readable.

The disadvantage is at that point in the code, you know next to nothing about the input type.

This may not concern you, but for someone working in a large software company this is a problem. Most software companies produce APIs and libraries; sets of tools that other people will use. As Microsoft likes to say, while readability is important your code must be "ready for production". That is your primary priority. This means that your code must be safe, error-prone, and absolute. No misconceptions are allowed; a single misconception can propagate into a system breaking error. Even worse, it may end up silently evaluating and producing unexpected behavior that you will then have to comb through with a debugger, outputs, and maybe a bit of intuition.

The point is, you're both right and it really depends on the scenario.

Now, what do I do? I actually typically do what your dad suggested. This is because when I work with objects and table structures, I never want to run into issues where false is evaluated the same way as nil.

object = false
if object then

end

object = nil
if object then

end

Notice how both variations in the code above produce the same result. However, they are not the same because one is where object is nil, representing nothing and one is where object is false, a Boolean. There may be scenarios where you need to explicitly tell (especially if you are constructing a very abstract library).

Just be careful.

0
Makes sense, thanks. Perci1 4988 — 9y
Ad
Log in to vote
1
Answered by
LuaQuest 450 Moderation Voter
9 years ago

I don't see any relevant difference in either, but if you want a personal preference, I typically just use the variable in the if statement without comparing it to nil.

Answer this question