*sigh*... Why do I even try? A novlet on StackOverflow


  • :belt_onion:

    So I was doing something that required me to look up a SO question.

    Found my answer, all was well, etc. Except I noticed a problem. There was a potential (classic, btw) race condition in the answer. Good Citizen Sloosecannon to the rescue, I edited the answer to catch the FileNotFoundException instead of the racey "check if file exists, read from it, ignore the error cause it can "never" happen" trick.

    This edit deviates from the original intent of the post. Even edits that must make drastic changes should strive to preserve the goals of the post's owner

    ­

    This edit was intended to address the author of the post and makes no sense as an edit. It should have been written as a comment or an answer.

    ­

    This edit deviates from the original intent of the post. Even edits that must make drastic changes should strive to preserve the goals of the post's owner

    Riiiiight. I suppose the unhandled exception bubbling up to the caller is perfectly inline with the original intent of the post, and helps everyone on SO, right?

    Why not write it as an answer? Because then my (correct) answer will be at the bottom.
    Why not write it as a comment? hah. Nobody reads the comments unless they have to.
    But whatever.

    Guys, this is why we can't have nice things. This is why people don't feel like answering questions and crap like that. Because this shit happens.

    I don't even care. But I probably won't contribute to the site the next time I see a mistake like that. Because it'll just get shot down again. (Yes, 1 edit accepted, 1 rejected. I'm clearly not rep-whoring here...)

    Pic:

    Link: https://stackoverflow.com/questions/7463414/what-s-the-best-way-to-load-a-jsonobject-from-a-json-text-file/20090084#20090084


  • Grade A Premium Asshole

    What else do you expect? The spirit of Jeff is still strong over there.


  • :belt_onion:

    Eh, not much tbh. I figured maybe people would actually appreciate someone helping with their content? Clearly not...


  • Grade A Premium Asshole

    Pffffbt. That place is worse than Wikipedia.


  • :belt_onion:

    Hey, my last edit went through, and it was basically the same thing!!!!111!

    Perhaps Jeff pushed some discoursistancy upstream to the SO folks?



  • @sloosecannon said:

    Riiiiight. I suppose the unhandled exception bubbling up to the caller is perfectly inline with the original intent of the post, and helps everyone on SO, right?

    It's a static main. It just gives some examples of calling JSONObject functions. There is no race condition. There is no need for exception handling. If the file doesn't exist, stderr will say so.

    So, to be the devil's advocate: your change adds unnecessary complexity to some simple sample code, and is indeed not in the spirit of the original answer.



  • @Hanzo said:

    There is no race condition.

    There is, and it's pretty much the classical time-of-check vs time-of-use. Essentially you
    (a) check if the file exists
    (b) open it for reading

    There's no guarantee that between (a) and (b) somebody didn't go ahead and mess with that file. Most of the times it probably doesn't matter, but there have been various exploits based on such code. So, getting the simple example right is IMO important. After all, it's supposed to show how you should do this (and you should be at the very least aware that things can go haywire if you do it like the original suggestion shows).



  • This was a perfectly valid edit. These reviewers are obviously unqualified to judge someone else's code.

    Wisdom of the crowds doesn't work if crowd is self-selecting for rep whore idiots.



  • @cvi said:

    There is, and it's pretty much the classical time-of-check vs time-of-use.

    Exploits? Adding an exception handler doesn't make it less vulnerable to someone messing with the file in the mean time. It would still exist. The only problem with the code is someone deleting the file after f.exists(). Then FileInputStream would throw an exception, which ends up on stderr, instead of having an exception which ends up on stderr.

    After all, it's supposed to show how you should do this
    It's supposed to show how to load a json file in a JSONObject. That's what the thread starter wants to know. Examples should be kept simple. File handling is not part of the problem.

    Edit: I do agree that the edit is absolutely not drastic. At all.



  • @Hanzo said:

    It's supposed to show how to load a json file in a JSONObject

    I would argue that handling the situation where the file goes walkabouts while you are attempting to read it is part of how you should be doing it. I know enough people who will copy verbatim from stackoverflow that the code samples should show best practice. Otherwise, you end up with an undefined config doing weird shit because the config file was deleted while you tried to read it and your code didn't notice.


  • Banned

    @Hanzo said:

    Adding an exception handler doesn't make it less vulnerable to someone messing with the file in the mean time.

    But it prevents crash.


  • area_pol

    Not to criticize your work, just to understand why it was rejected:

    Maybe your improvements were not understood?
    @sloosecannon said:

    Comment: Removed possibility of race condition in file exists check and added exception handling

    Was this the only comment? - the bottom of the post is outside of the image bounds, cannot see.
    If there is no more explanation, some readers would think "what race condition?" "is exception handling added because you prefer doing control flow with exceptions?" etc.
    Surely many people know these, but the question "how to load a file?" does not attract the experienced developers.

    If this:
    @sloosecannon said:

    I edited the answer to catch the FileNotFoundException instead of the racey "check if file exists, read from it, ignore the error cause it can "never" happen" trick.

    and

    File opening and reading can fail in many ways (for example file exists, but you do not have permission to read it - which is not checked by File.exists), let us handle the possible exceptions here.
    to the commit message - or even better to the answer text for educational purpose - maybe the users would better understand your intention and accept the edit.


  • @Hanzo said:

    Exploits?

    Yes, you're of course right that there's no exploit in that sample (relating to the file check). But the code is going to end up somewhere (otherwise the OP presumably wouldn't have asked), and more importantly, people are going to see that example and think that doing that kind of useless check is good practice (and problem-free).

    @Hanzo said:

    Examples should be kept simple. File handling is not part of the problem.

    The exception handling trades two useless lines for four useful ones (of which most are quite short), so I'm not sure how it's that much more complicated. (I do think that the example would have been perfectly fine without that check, after all, as you say, file handling is not part of the problem.)



  • @cvi said:

    so I'm not sure how it's that much more complicated

    No, you're right, it isn't. Even the original reply should not have needed to add if (f.exists()). It's just that OP's arguments were not as good as he thought they were.



  • BTW, I would suggest that both versions are equally wrong.

    The original is wrong because it claims to load a file into a JSON object, but if the file doesn't exist, it silently does nothing.

    The edited version is wrong because, based on the job the code has, there is no more sensible behavior when the file doesn't exist than to throw a FileNotFoundException.

    If you had a sensible implementation of the exception handler, then it would have been an improvement.



  • I think we boil down into two camps: Those who care about answers that are correct, and those who understand the reality of SO.

    SO these days is definitely a mixed bag, and any useful answer you can find is usually incorrect in some details. Since I would estimate that questions have answers that are useful and to the point about 15% of the time: getting an actual answer to your question, much less a correct answer, is almost a miracle.

    SO seems to like it that way; Wikipedia they are not. Wikipedia at least tries.

    So I understand your desire to improve SO, but SO definitely is not inclined to want your help improving anything.



  • After my first 4 attempts at using StackOverflow to get some sort of benefit or value from it failed, I never even bothered trying again. I have one question with an answer that's blatantly wrong, and two with no answers at all. Whee. What a useful website.



  • @Hanzo said:

    There is no race condition.

    Yeah there is. Except on, like, MacOS 6.0, where multitasking is cooperative.

    Now if you're trying to argue the race condition doesn't matter... that's a different argument. But I still agree with the edit, since even example code should be as correct as possible.

    You can either delude yourself with, "it's just example code! It's not like anybody will copy-and-paste that into an income tax handling product!" Or cope with the reality that that code will be placed, verbatim, in a production codebase and it should be as correct as humanly possible.



  • @Adynathos said:

    Was this the only comment? - the bottom of the post is outside of the image bounds, cannot see.If there is no more explanation, some readers would think "what race condition?" "is exception handling added because you prefer doing control flow with exceptions?" etc.Surely many people know these, but the question "how to load a file?" does not attract the experienced developers.

    If they don't know, then they aren't qualified to judge and they should have not rejected the edit. If they were suspicious of intentions, they could have called in someone more knowledgeable. We're talking "Brain Usage 101" here.



  • Where is my dislike button?



  • @blakeyrat said:

    Now if you're trying to argue the race condition doesn't matter... that's a different argument. But I still agree with the edit, since even example code should be as correct as possible.

    You can either delude yourself with, "it's just example code! It's not like anybody will copy-and-paste that into an income tax handling product!" Or cope with the reality that that code will be placed, verbatim, in a production codebase and it should be as correct as humanly possible.


    If you're going to be your pedantic self, you should appreciate the difference in effect: if the file disappears after .exist(), FileInputStream will throw the exact same exception in both cases. The difference is that in the original code, that exception will end up logged on stderr, whereas in OP's code it will end up in the catch, which will most likely be implemented as logging to stderr, since it's a simple demo main().

    Should you wish to argue that .exist() is an abomination and should be removed from the face of the planet, by all means join Google vs Oracle over the API. If you want the code to be as correct as possible, you should argue against exception handling in that function, or add a return value indicating success.



  • @JazzyJosh said:

    Where is my dislike button?

    Right next to the button that generates all of your contentless 7 word responses.



  • Gee, it's really unfortunate that you don't understand how to count.

    You should set up a GoFundMe so you can hire a tutor to help you.


  • area_pol

    I used to help people with C++ there but was successfully driven away. Why? Because of some strange need for people to be as much dicks as they can. If you make a question which does not hold someone's standard of asking questions, you will be downvoted and shit upon. The same goes for answers - not good enough? Downvotes from some nazi. If you fix it, you'll get downvoted and commented on by some other nazi, who feels the previous version was better. For some reason the longer someone is there, the more assholish he becomes. Like if there were some points for begin a dick.


  • :belt_onion:

    @CoyneTheDup said:

    So I understand your desire to improve SO, but SO definitely is not inclined to want your help improving anything.

    Yeah, apparently not. I don't really have a desire, it's more of a "Oh look, I've seen that bug before and I know it can be a pain so I can just go and fix it"...

    But I've been taught "the way", I shall never be so foolish. Or something like that :)


  • :belt_onion:

    @NeighborhoodButcher said:

    I used to help people with C++ there but was successfully driven away. Why? Because of some strange need for people to be as much dicks as they can. If you make a question which does not hold someone's standard of asking questions, you will be downvoted and shit upon. The same goes for answers - not good enough? Downvotes from some nazi. If you fix it, you'll get downvoted and commented on by some other nazi, who feels the previous version was better. For some reason the longer someone is there, the more assholish he becomes. Like if there were some points for begin a dick.

    QFT.

    It does appear to be community-related too. I've had very few issues on the Android community, but this is my second contribution to SO itself.

    Honestly, I just use it as an (awesome) resource most of the time anyways. You can find an answer for almost anything on there. Just don't ask or contribute, cause if you do, you'll get burned.


  • :belt_onion:

    @JazzyJosh said:

    set up a GoFundMe

    Count you. Give me money


  • :belt_onion:

    @Hanzo said:

    @blakeyrat said:
    Now if you're trying to argue the race condition doesn't matter... that's a different argument. But I still agree with the edit, since even example code should be as correct as possible.

    You can either delude yourself with, "it's just example code! It's not like anybody will copy-and-paste that into an income tax handling product!" Or cope with the reality that that code will be placed, verbatim, in a production codebase and it should be as correct as humanly possible.


    If you're going to be your pedantic self, you should appreciate the difference in effect: if the file disappears after .exist(), FileInputStream will throw the exact same exception in both cases. The difference is that in the original code, that exception will end up logged on stderr, whereas in OP's code it will end up in the catch, which will most likely be implemented as logging to stderr, since it's a simple demo main().

    Should you wish to argue that .exist() is an abomination and should be removed from the face of the planet, by all means join Google vs Oracle over the API. If you want the code to be as correct as possible, you should argue against exception handling in that function, or add a return value indicating success.

    Correct, there is a difference in effect. In one, main() will error out, while in the other, you get a chance to handle the problem. Of course, since you ran the .exists() check, you're not expecting main() to error at all, because you "checked if the file exists". In other words, in one option, there's unexpected behavior, while in the other, you're explicitly stating what to do if the file doesn't exist.

    .exists() shouldn't be used to see if a file exists prior to accessing it - or if it is, you shouldn't make any assumptions about it actually existing. It's fine for other uses - looking for a flag file for example. The (only) correct way to ensure the program doesn't have problems reading a file is to catch and handle the FileNotFoundException.



  • @Hanzo said:

    If you're going to be your pedantic self, you should appreciate the difference in effect: if the file disappears after .exist(), FileInputStream will throw the exact same exception in both cases.

    In the code on SO now, you have no choice. The program will crash and dump the stack trace to stderr. In @sloosecannon's example, you can:

    • choose to display a nice message to the user telling them that the file wasn't found
    • log it somewhere
    • Do like now and just let the program crash
    • All of the above

  • Trolleybus Mechanic

    @JazzyJosh said:

    Where is my dislike button?

    Here:
    http://i.imgur.com/kCbmaz0.png




  • BINNED

    @cartman82 said:

    Wisdom of the crowds doesn't work if crowd is self-selecting for rep whore idiots hivemind parrots.

    FTFY



  • @powerlord said:

    In the code on SO now, you have no choice. The program will crash and dump the stack trace to stderr. In @sloosecannon's example, you can:

    You can do exactly the same in the original code. If you are operating under the idea that people copy and paste the code as is, sloosecannon's code is not good either, because it swallows FileNotFound exceptions without logging them, but throws exception for other errors, such as SecurityException and IOException (from the deprecated toString()). Apart from the fact that it's a static main, and the file must be called "file.json".

    So either people copy and paste, and can't use it, or they notice the logic and adapt it to their own needs, at which point both examples are fine. If you really want to prevent people from making dumb copy-paste errors, the code should be stripped to just

    InputStream is = new FileInputStream(...);
    ...
    System.out.println(a);


  • In this case, the moderators are correct. The intent of the post you edited was very clearly: "This is how I solved the problem" and not "this is how you should solve the problem".


  • :belt_onion:

    So, should SO be about keeping the original intent of the post, or giving good advise?


  • :belt_onion:

    @Hanzo said:

    static main

    You keep using this phrase. What is a non-static main?



  • The two arn't mutually exclusive.



  • @sloosecannon said:

    @Hanzo said:
    static main

    You keep using this phrase. What is a non-static main?

    The entry point to my C projects don't use the static keyword. 🚎


  • BINNED

    @sloosecannon said:

    So, should SO be about keeping the original intent of the post, or giving good advise?

    It would be nice if it actually did either. 🚎



  • @sloosecannon said:

    You keep using this phrase. What is a non-static main?

    A class instance function called main?


  • I survived the hour long Uno hand

    @sloosecannon said:

    I've had very few issues on the Android community

    Not surprised.



  • I think that's my one badge on StackOverflow, because with all the gamification people would rather answer eleventy-billion noobs wondering which JQuery plugin to use for their homework assignment rather than answer a question about a vague gcc error message.


  • Considered Harmful

    @Hanzo said:

    A class instance function called main?

    There are kids on the internet. No one should have to put up with this filth.


  • ♿ (Parody)

    :wtf: Somehow I have this:

    Which I supposedly earned on September 2, 2015. There's no way I was going there every day. Oh, wait, I have my user RSS feed in my reader. Must be that. 😆

    LOL...looking at rep history, one of my answers (from 2009) is marked as the answer, and is currently (well, since 2013) rated -1.



  • I have a bunch of those.

    What's great is not only is StackOverflow useless for helping you with your problem, but it actually mocks you for it.




  • :belt_onion:

    +1

    I have never seen a non-static main. Perhaps he is thinking of a different language?

    That doesn't even make any sense.


  • Notification Spam Recipient

    Unless it's the main() of a Super_Main() that actually is static. Just ma'in.


    Filed under: That rhyme really doesn't work too well



  • So, just like DC?

    Filed under: I'll show myself out


  • Notification Spam Recipient

    @JazzyJosh said:

    Where is my dislike button?
    I think that should of been the obvious failure when moving to discourse. We don't like anything. It should of been a nice fuck you symbol.

    @NeighborhoodButcher said:

    I used to help people with C++ there but was successfully driven away. Why? Because of some strange need for people to be as much dicks as they can. If you make a question which does not hold someone's standard of asking questions, you will be downvoted and shit upon. The same goes for answers - not good enough? Downvotes from some nazi. If you fix it, you'll get downvoted and commented on by some other nazi, who feels the previous version was better. For some reason the longer someone is there, the more assholish he becomes. Like if there were some points for begin a dick.
    The html questions embody this to a tee. The question, unless absolutely trivial, is rarely answered but rather a debate about the elegance of the solution. The person who spews the most shit usually gets the tick. I saw an amazing one about div vs tables and how superior divs were despite all he needed was to display data from a csv and had a typo in the header.


Log in to reply