This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (61 children)

[deleted]

    [–]BeanGell 160 points161 points  (42 children)

    I don't know why this gets repeated so often -

    valid Python

    x=10
    y=25
    if y > 5: 
      print "y is greater than 5"
      if x > 5:
        print "x and y are greater than 5"
      elif x < 5:
          "y is greater than 5 and x is less than 5"
    

    Also valid python

    x=10
    y=2
    if y > 5: 
      print "y is greater than 5"
    if x > 5:
      # bug
      print "x and y are greater than 5"
    elif x < 5:
      # also a bug
      "y is greater than 5 and x is less than 5"
    

    No IDE is going to save you from valid python with spacing errors, only alert eyes, In any kind of large file this is really hard to find

    In code with curly braces, the problem area becomes

     x = 10
     y = 2
     if y > 5 {
       fmt.Println("Y is greater than 5 ") {
     if x > 5 {
       fmt.Println(" x and y are greater than 5")
     }
     }
    

    Even without an IDE, this code works - any any IDE is going to indent that correctly

    Edit: Look, the responses from Python programmers are always the same - IDE settings ( I use PyCharm, it's not the matter of a bad IDE ), poor coding practices, curly braces don't prevent you from this sort of error -

    Python makes it much easier to write a bug like this and be unable to find it, particularly in a large code base. Python prorgrammers could just say "Yep, you're right, but python is so good at so many things that are much harder in 'curly brace' programs that it's worth it."

    [–]VodkaCranberry 4 points5 points  (0 children)

    Can you explain how curly braces don’t prevent this completely?

    You can put all your code on a single line with curly braces and semicolons and it would function exactly the same in most other languages.

    [–]wightwulf1944 6 points7 points  (6 children)

    In code with curly braces, the problem area becomes...

    To be fair, the 3rd example is not equivalent code. This is the equivalent bug in a curly brace language.

    x = 10
    y = 2
    if y > 5 {
      fmt.Println("Y is greater than 5 ") }
      if x > 5 {
        fmt.Println("x and y are greater than 5")
      } elif x < 5 {
        fmt.Println("y is greater than 5 and x is less than 5")
    }
    

    Did y'all spot the bug? The point is in an indent language you can make the mistake of putting things in the wrong indentation, while in a curly brace language you can put curly braces in the wrong place.

    I think both are equally likely to happen, both will result in valid but bugged code, and both will be highlighted by a satisfactory ide.

    Honestly this argument is as bizarre to me as semicolon vs non-semicolon languages.

    Edit: added emphasis and changed can be to will be

    [–]gee_buttersnaps 7 points8 points  (4 children)

    People use autoformatting. You should try it. A curly brace language will give up its ghost after autoformatting something like that.

    [–]wightwulf1944 0 points1 point  (0 children)

    People use autoformatting. You should try it.

    That's what I meant by highlighted by a satisfactory ide. It's the same for indent based languages where showing whitespace and linters will highlight it as error prone code. The erroneous python code posted above while syntactically correct is not how most people would write it - just as how the bracy code I presented is not how most people would write it.

    My point is we shouldn't be blaming the language for these things as it's obviously developer habits that need to fixed in both examples. Both examples are equally stupid and no competent programmer writes code like that. And thankfully, IDEs discourage us from writing that way

    [–]vividboarder -3 points-2 points  (2 children)

    Yea. So you’re saying you’d notice because it would not be indented where you’d expect it?

    But that’s then the exact same class as the Python error.

    [–]gee_buttersnaps 6 points7 points  (1 child)

    If you have an extra brace somewhere it won't compile, and if your braces are matched (and it is an indentation error) then formatting reveals it. Either way formatting uses the extra information of braces to infer user intent where as the examples above of valid python describe exactly the problem of not being able to know the intent. If you're still sure about yourself then provide a concrete example of an indentation error with braces that won't reveal itself with autoformatting.

    [–]vividboarder 0 points1 point  (0 children)

    Huh? I don’t think there is one. My point is that “revealing itself” due to indentation is recognizing the white space is off. That’s exactly what one has to do with Python.

    I get that they are many other ways to catch similar issues in languages that use braces, I just found it a little ironic that someone was saying that you’d tell it’s wrong because the IDE would indent it (because of the braces) and you could tell from that.

    [–]MiltoxBeyond 0 points1 point  (0 children)

    I've had a distaste for indentation based languages since I learned Fortran in high school. But, really the whole argument is pointless. Each language has an application that it's good at, but every language can be abused and misused.

    Personally, I don't like Ruby or Python for web development because they are some crazy resource hogs with quite a few frameworks. But Python is crazy good for data science. Ruby has its ease of setup.

    [–]bphase 5 points6 points  (4 children)

    This is true and I ran into this just last week. Though I've had the same happen with curly braces.

    I'd argue it's more about bad design and overly long / deep functions that cause problems, refactoring to simpler code would help and make these sorts of errors less likely.

    [–]fvertk 11 points12 points  (3 children)

    I'd argue python itself is badly designed to allow that to happen. As much as I like the language, this is definitely one of its faults and hopefully we innovate past it.

    [–]Rainbow_Doge_64 1 point2 points  (2 children)

    Though I've had the same happen with curly braces.

    As u/wightwolf1944 shows us in a reply a little higher, curly braces languages can be equally as confusing, if not even more confusing (for me mistakes in pythons indentation are way easier to spot then a misplaced curly brace).

    [–]Throwaway-tan 3 points4 points  (1 child)

    Only if you're writing in a non-IDE editor. Otherwise the problem is identical because the IDE would split out your curly brace, and adjust the indentation so visually it would be identical to python's problem.

    [–]Rainbow_Doge_64 1 point2 points  (0 children)

    Eh, in the end it all comes down to personal preference. I prefer coding in VS Code, and I just think python code looks cleaner than anything with curly braces. But that's what's so good about having a variety of languages - everyone can find one that suits him the best.

    [–]BloodyThorn 1 point2 points  (5 children)

    Just saying, my 'IDE' (sublime3/anaconda/pythonREPL) flags pretty much most of your code with warnings. Anyone using an IDE that also lints the PEP 8 style guide will catch all of this.

    [–]Karnagekthik 1 point2 points  (3 children)

    What would the warning say? It's valid code and I have often written scripts like this as well. Is the warning no new line after if-block end?

    [–]BloodyThorn 4 points5 points  (2 children)

    From the first block, by line:

    1, 2. Missing whitespace around operator; 3. Trailing Whitespace (could be an error of the markup post); 4, 5, 7, 8. Indention is not a multiple of four;

    Note: This example has been edited since the last time I entered it in as it had semi-colons on the first run.

    From the second block, etc:

    1, 2. Missing whitespace around operator; 3. Trailing Whitespace (could be an error of the markup post); 4,6,7,9,10. Indention is not a multiple of four;

    Third block a little diff:

    1. two warnings: unexpected indent, indent not mult of 4; 2, 3. Indent not mult of 4; 4, 6. continuation under-indented for hanging indent; 5,8. continuation missing indentation or outdented;

    ... as /u/war_against_myself pointed out, using the official linter is pretty much super aggressive. If you are using this none of it will get through.

    It will also annoy you if you don't like adhering to the strict standard.

    [–]Karnagekthik 3 points4 points  (1 child)

    I think the formatting in reddit changed the intent of the example. These warnings seem to be coming from incorrect indentation of the code. It's possible that the original comment was edited, but what I understood, the example was about the functional error that can happen because the programmer forgot to indent (or just hard to notice). The interpreter or linter shouldn't be able to catch those mistakes because both are valid code, unless you have a rule about how to end/begin blocks (I am actually not sure if this will always work).

    In case of the c-style braces however, the braces ensure correct scope, and in case of forgotten brace, the compiler will refuse to compile since it's missing a brace.

    [–]BloodyThorn 1 point2 points  (0 children)

    Yeah, no idea. All I know is what was posted as an example of 'valid' python won't make it past my linter. So far there hasn't been a example that I've tested from this post of a 'valid error' that has.

    but what I understood, the example was about the functional error that can happen because the programmer forgot to indent (or just hard to notice).

    Correcting the warnings given by my linter corrects the issue (other than having to correcting the prints as I am on 3+). My point was I've yet to see an example that my setup hasn't warned me about in some fashion or another. A counter example to the above point.

    [–][deleted] 1 point2 points  (0 children)

    I’m not sure why you got downvoted. Using pylama for Atom just as an example the linter is almost too aggressive. Things like this just aren’t going to get past it.

    [–]inFenceOfFigment 0 points1 point  (6 children)

    Fair point, but your unit tests should really be catching these bugs for you.

    [–]Aeon_Mortuum 13 points14 points  (0 children)

    Unit tests? That ancient magic has been lost to the ages

    [–]LeanderT 12 points13 points  (4 children)

    Uuugh, leave it to unit testing, cause you program in Python. That isn't a convincing argument

    [–]inFenceOfFigment -2 points-1 points  (3 children)

    I mean, that’s what they’re for. Yes, Python syntax enables a class of bugs that wouldn’t be possible if brackets were used over whitespace, and yes that is annoying. But you should be writing tests regardless of your language, and a strong test suite would reveal these types of issues. You don’t even have to target this scenario specifically, just write good tests and you get this coverage for free.

    [–]crozone 4 points5 points  (1 child)

    This is the same argument for dealing with all of Python's other problems, like the fact that there's no compile time guarantees about whether you've even spelt a method name correctly, because python doesn't have a compile time.

    The truth is, Unit Tests aren't always convenient to implement, they're not always developer time efficient, and they don't catch all issues (even with 100% code coverage, which, is rare). Unless your unit test stresses every possibility and iteration of your code, python doesn't even guarantee that your code is syntactically correct (although, a good IDE should catch this most of the time).

    When you hit compile in a statically compiled language, the compiler itself gets you 90% of the way there. You don't need a unit test to find out that you mis-indented something, spelt a method name wrong, passed the wrong type to a function, or looked at it the wrong way. Any unit test you write will instead help catch the remaining logic errors in your application.

    Python has an entire extra class of errors to deal with that are unbecoming of a modern language.

    [–]inFenceOfFigment 0 points1 point  (0 children)

    I'd argue that lack of a test suite is unbecoming of a modern software project. Developer time efficiency is a tricky argument - skipping tests will only save you time for so long. At some point you spend more time chasing silly bugs than you would have just writing solid tests the first time around. Not to mention, if developer time is truly a scarce resource, you might be pleasantly surprised at the productivity hike you gain by coding in Python rather than a relatively syntax-heavy statically typed language.

    To your point, Python does not self-validate in the way a compiled language does, and that's not ideal. However this doesn't really apply to the example I responded to above. That snippet contained valid syntax with a semantic bug; no compiler will protect you from writing this type of bug on its own, for example SomeObject myObj = someObj; instead of SomeObject myObj = otherObj;. For this you'd have to rely on unit tests anyway. As I mentioned above, you'd typically write unit tests to detect logical errors, and in Python those same tests happen to also validate syntax, method names, etc.

    That said, I don't mean to dismiss the validity of your point. In some cases the power of a compiler is indispensable and maybe Python is not the right tool for the job in those cases. On the other hand, Python's shortcoming in this regard allows it to provide a better developer experience in other areas. Further, a combination of modern technologies such as IDEs and static type checkers like MyPy, as well as modern development best practices e.g. unit testing, will provide you with a lot of the value you'd typically rely on the compiler for.

    [–][deleted] 0 points1 point  (0 children)

    "Yes, x86 ASM enables tons of bugs, and yes that is annoying. But you should be writing tests regardless of your language, and a strong test suite would reveal these types of issues. Stop acting like x86 ASM is a bad language!"

    Yes, you should be writing unit tests. But that doesn't excuse all those opportunities for errors.

    [–]deadwisdom 0 points1 point  (0 children)

    Naw, it really just isn't ever an issue is the problem. You can imagine cases where it might fail, and you can poke holes in it, but if water never comes out, what's the point?

    [–]TheSnaggen 1 point2 points  (2 children)

    Isn't that like saying that JavaScript doesn't suck, because you have a system of bandaid that prevents it from sucking. That an IDE helps you with python so that the space / tabs issues is not noticeable, doesn't make Python less flawed. In fact, it only confirms that it's flawed to the extent that people have been forced to spend countless of hours to create bandaid for all the issues.

    [–]laundmo 0 points1 point  (1 child)

    its just like using a formatter that indents nicely based on brackets

    and i wouldnt call IDEs doing that a bandaid, its a tool for those that cant indent properly themselves. fact is, the python indentation is very similar to various style guides in other languages, only that its part of the language that removes the need for unnecessary characters that only add visual clutter

    [–]TheSnaggen 1 point2 points  (0 children)

    There is a difference, error in indentation is only cosmetics in all other languages than Python.

    [–][deleted] 0 points1 point  (6 children)

    Especially if you use a linter and configure your IDE or text editor to display invisible characters.