- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
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...
Admin
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.
Admin
That reminds me... our current ReSharper InspectCode Warnings Report in Jenkins shows 11,900 issues.
Admin
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.Admin
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.
Admin
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.
Admin
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.
Admin
The first problem I saw was if (typeof loadingText === "undefined") when I expected if (typeof loadingText === typeof undefined)
Admin
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.
Admin
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.
Admin
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.
Admin
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?
Admin
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!
Admin
It looks like you can just use
So that means using typeof and "undefined"enters the world of stringly-typed variables, which is also WTFery.
Admin
There are a couple of problems that some people are stumbling across:
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.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.Admin
"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).
Admin
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.