• (nodebb)

    Trying to remember some of the weird, but historically legal.... what if this code may be used in a case where there is a global variable of that name....

    Too early to think this much...

  • (nodebb) in reply to TheCPUWizard

    If that's the problem here, the code has much worse problems than one apparently-but-in-fact-not pointless lint-suppression comment.

    Y'know, like passing defaultable parameters by global variables...

    Addendum 2022-09-12 07:12: EDIT: specifically, by optionally-present global variables.

  • (nodebb)

    That reminds me... our current ReSharper InspectCode Warnings Report in Jenkins shows 11,900 issues.

  • Smithers (unregistered) in reply to TheCPUWizard

    That's my best guess at what they were trying to achieve here - if there is a global variable, use it, otherwise declare and use a local.

    Of course, that's not what this code actually does, because the presence of var loadingText hides the global even on earlier lines, but that's probably what they were trying.

  • Sole Purpose Of Visit (unregistered) in reply to BernieTheBernie

    Always fun.

    Our new product requires me to download node.js and various frameworks (including the VIsual Studio thing that is not actually Visual Studio).

    I was mildly alarmed to find that the download (official) reports about 500 "security issues." Do you want to fix these, says the download. Well, yes.

    Now I only have about 100 security issues.

  • Sole Purpose Of Visit (unregistered) in reply to Smithers

    Which would be cretinous anyway. If you have a global variable, it will have side-effects (by definition) everywhere.

    What you actually want (and I use this word with trepidation) is a local override for that global variable. Which means you set the local to the global and then look to see if you're being called with an override.

  • Sauron (unregistered)

    Let's imagine the rest of the story.

    The website, riddled with bugs, gets hacked. So the dev who wrote that lint bypass - let's call him Jimmy - gets promoted, while innocent people get fired. Jimmy then rules with an iron grasp on the IT department, and bad code spreads until the company collapses.

    Now Jimmy found another job, at a company making planes. One day, several planes crash because of a date-handling bug in the bad code produced by Jimmy's underlings, a team of overworked, underpaid and whipped junior devs. So Jimmy gets promoted again, whereas a few scapegoat devs are fired. It goes on until that company collapses as well.

    So Jimmy finds yet another company ready to hire him. That company makes code for nuclear power plants. And the world will hate what comes next.

  • dpm (unregistered)

    The first problem I saw was if (typeof loadingText === "undefined") when I expected if (typeof loadingText === typeof undefined)

  • Sole Purpose Of Visit (unregistered) in reply to Sauron

    I think you're confusing writing crappy JavaScript with writing crappy military software.

    You can get away with zero review or testing on the first, but (crosses fingers) not so much on the latter.

  • DrPepper (unregistered)

    To be fair, this is an asp.net mvc app. Which means that the dev is probably a c# dev, not a javascript dev. In c#, var is block-scoped, which it is not in javascript. If you're using visual studio to write javascript code, then resharper will flag this as an error; but as a c# dev, you may not understand why it's flagged.

  • Loren Pechtel (unregistered)

    I have yet to see Resharper complain that something is meaningless without it being right. Listen to it! It's suggestions must always be treated with considerable skepticism (I've seen it propose a lot of optimizations that would break the code), but if it says something's wrong it's wrong.

  • Chris (unregistered)

    We used to tell off juniors for ignoring compiler warnings, but now Visual Studio comes up with mountains of warnings for little things like adding two 32-bit integers together and storing the result in a 64-bit integer, because it might have overflowed first (https://docs.microsoft.com/en-us/cpp/code-quality/c26451?view=msvc-170). I mean, yes, sure, but what's next? Warnings on all code that it may still have bugs even if it is syntactically correct?

  • (nodebb) in reply to Sauron

    Clearly, I want to work at Jimmy's nuclear power plant so I can gain superpowers!

    Like the power of cancer. It must be great, it's so popular!

  • Yikes (unregistered)

    It looks like you can just use

    if (loadingtext === undefined) ...

    So that means using typeof and "undefined"enters the world of stringly-typed variables, which is also WTFery.

  • (nodebb)

    There are a couple of problems that some people are stumbling across:

    1. You used to be able to set undefined to whatever you wanted. It was just a global variable that the browser exposed. So, to be on the safe side, it was recommended to compare against the string "undefined" using typeof. Browsers do seem to have fixed this.
    2. Using typeof prevents the browser from complaining if the variable hasn't been declared yet. Try it in your browser console! Of course, this shouldn't be a problem in real life, but here we are.
  • Foo AKA Fooo (unregistered) in reply to Chris

    "what's next? Warnings on all code that [...] may still have bugs even if it is syntactically correct?" That would be great, but unfortunately AI is still busy trying to recognize traffic lights (reCAPTCHA, I'm cursing at you).

  • Dave (unregistered) in reply to PotatoEngineer
    Comment held for moderation.
  • Loren Pechtel (unregistered) in reply to Chris

    int64 = int32 + int32 should produce a warning! You used an int64 for a reason. Conceivably it's because you needed it for later operations but that line is likely an error. It's easy enough to write int64 = (int64)int32 + int32 and remove the problem. One should be highly paranoid about edge cases in code.

    I'm a firm believer in the development environment being as paranoid as possible. Yes, it will flag some stuff that doesn't matter but overall it saves time because it's so much easier to address when it gets flagged than hunting the bugs that slip through otherwise.

Leave a comment on “Undefined Variable”

Log In or post as a guest

Replying to comment #:

« Return to Article