News for hackers. Stuff that matters.
Re:True hacker resume: CV as Python unit test by iron_ball on Thursday July 15, 2010 @04:52PM (#1519145)
Ugh, he uses Yoda conditions. If two, the value is less than, Jedi you will become.
Re:True hacker resume: CV as Python unit test by subwindow on Thursday July 15, 2010 @05:10PM (#1519207)
Oh, I didn't realize that had a name! My boss does that, and it is incredibly irritating.
Re:True hacker resume: CV as Python unit test by acangiano on Thursday July 15, 2010 @05:52PM (#1519355)
Not in this case, but Yoda conditions can be useful. The most common case is preventing accidental assignment when you are trying to check for the equality of values.

Say that you mean to do:

    if (something == 2)
If you forget an equal sign, the resulting code can lead to subtile bugs:

    if (something = 2) // Not what we meant
If you are using a Yoda condition and forget an equal sign, you'll get an error right away (so that you can correct it):

    if (2 = something) // Error
There are other use cases, particularly related to nil values, but this should show you the gist of it.
Re:True hacker resume: CV as Python unit test by masklinn on Thursday July 15, 2010 @06:01PM (#1519394)
> Not in this case, but Yoda conditions can be useful.

Not in Python. Assignments as anything but stand-alone (potentially chained) statements are syntax errors.

The parser will not let you write

    if something = 2:
in the first place.
Re:True hacker resume: CV as Python unit test by acangiano on Thursday July 15, 2010 @06:13PM (#1519435)
That's why I said "not in this case". There are languages affected by this though (typically C-based ones). Ideally, their compiler will give you a warning about it, but that's not always the case.
Re:True hacker resume: CV as Python unit test by moe on Thursday July 15, 2010 @05:55PM (#1519366)
May be irritating for some but good practice regardless.

Putting the constant left in a conditional means you will never miss an accidental = instead of ==.

Re:True hacker resume: CV as Python unit test by masklinn on Thursday July 15, 2010 @06:00PM (#1519386)
> Putting the constant left in a conditional means you will never miss an accidental = instead of ==.

That's only useful for dumb languages

    >>> a = 5
    >>> if a = 3: print 'ok'; else: print 'nok'
      File "<stdin>", line 1
        if a = 3: print 'ok'; else: print 'nok'
             ^
    SyntaxError: invalid syntax
Python simply does not allow assignments in conditional contexts.
Re:True hacker resume: CV as Python unit test by moe on Thursday July 15, 2010 @06:12PM (#1519427)
Dumb is a strong word, young padawan.

The difference between theory and practice is smaller in theory than in practice.

Re:True hacker resume: CV as Python unit test by masklinn on Friday July 16, 2010 @01:49AM (#1520404)
> Dumb is a strong word, young padawan.

I carefully weighted my words, and I rest my case.

Re:True hacker resume: CV as Python unit test by 146 on Thursday July 15, 2010 @09:12PM (#1519850)
> That's only useful for dumb languages

The compilers/interpreters I know that don't do this are PHP and Perl (C/C++ will do it if you use -Wall, which you should be using anyway).

So yeah, you're pretty much right. /snark

Re:True hacker resume: CV as Python unit test by chromatic on Thursday July 15, 2010 @10:40PM (#1520029)
Perl does indeed warn about the use of = instead of == in a conditional.
Re:True hacker resume: CV as Python unit test by 146 on Friday July 16, 2010 @01:57PM (#1521894)
You're right; I didn't know about the -w setting. I guess that just leaves PHP.
Re:True hacker resume: CV as Python unit test by subwindow on Friday July 16, 2010 @11:13AM (#1521368)
That's like wearing a helmet while walking down the street because you might accidentally fall and hit your head.

Don't be an idiot in the first place and you won't have to do idiotic things to prevent damage from your idiocy.

Re:True hacker resume: CV as Python unit test by moe on Friday July 16, 2010 @12:29PM (#1521604)

    Good judgement is the result of experience.
    Experience is the result of bad judgement.  <-- You are here
Re:True hacker resume: CV as Python unit test by leftnode on Thursday July 15, 2010 @05:51PM (#1519352)
Until you're system is crippled by a bug as a result of not using them.
Re:True hacker resume: CV as Python unit test by masklinn on Thursday July 15, 2010 @06:02PM (#1519396)
This is something that cannot happen in Python. Yoda conditions are entirely pointless in that language.
Re:True hacker resume: CV as Python unit test by moe on Thursday July 15, 2010 @06:19PM (#1519450)
A good programmer looks both ways before crossing a one-way street.
Re:True hacker resume: CV as Python unit test by 146 on Thursday July 15, 2010 @09:11PM (#1519846)
A better programmer uses -Wall and lets the compiler check for him.
Re:True hacker resume: CV as Python unit test by moe on Friday July 16, 2010 @02:22AM (#1520458)
Confidence, n.: The feeling you have before you understand the situation
Re:True hacker resume: CV as Python unit test by Timothee on Thursday July 15, 2010 @05:19PM (#1519238)
I didn't know it had a name either. Because of that I read the line wrong the first time. (even though I knew what to expect…)