r/badcode • u/Dependent_Upstairs79 • Apr 07 '23
c# Unused variables?!?!? One of DOZENS of examples I'm finding in this legacy nonsense. Stuff passed around and never used, things assigned to intermediate variable for no reason. Were they TRYING to make this hard to read? And those Writelines?!
302
Upvotes
169
u/redheness Apr 07 '23
This is 100% a code copied from an internet tutorial or stack overflow example. All these myCommand, myWatch and the myQuery assignment as the final proof;
90
u/d0nny2600 Apr 07 '23
Unfortunately you are going to have to get used to it. Probably was being debugged at one point and stuck a conditional breakpoint on it and then it became part of the code base. This crap is everywhere. People change functions, remove the usage of a variable and then never actually remove the variable. Sucks but happens a lot
36
u/IBJON Apr 07 '23
This kind of stuff happens a lot with older or frequently updated codebases. Things get refactored or have to be rewritten because of new requirements, and things like this fall through the cracks.
This is especially true for legacy codebases because older IDEs weren't great about highlighting things like unused variables.
This likely isn't an example of bad code that resulted from some conscious decision.
18
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Apr 07 '23
Image Transcription: Code
[In the following code, both appearances of
strSPNameare highlighted. The first is highlighted green, the second gold. The code reads:]I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
8
u/scatmanFATMAN Apr 07 '23
I won't repeat what others have said. But I do have a question: What's wrong with the WriteLine(s)?
6
u/cyclewanderist Apr 07 '23
This either was VB.NET code and someone used the wizard to change it to C#, or this is a Visual Basic developer typing in C#.
Edit: Also, on a second re-read of the code, the complete lack of using statements or finally around the closing is just gross. I would expect this to be pretty resource leaky code should there be exceptions.
4
u/LightShadow Apr 07 '23
If you're doing a big refactor or just trying to grok a terrible code base my favorite tool is a plugin called MultiHighlight for JetBrains IDEs.
It's basically Ctrl+F but it persists with colors so you can easily see all the places a variable might have been used in the whole file. You can toggle each instance on and off with a hot key until there's only one usage left and <delete>
screenshot
2
u/jkconno Apr 08 '23
I think the wall of text without any spacing between lines bothers me the most
1
u/WallstreetChump Apr 07 '23
And all of those console.writelines just makes it obvious that whoever wrote this was lazy
1
u/[deleted] Apr 08 '23
catch Exception…their comment makes me smile with joy.
1
u/RedLibra Apr 08 '23
Does c# have no linter that will throw error if u have something like this? Like eslint in javascript...
1
u/fiddz0r Apr 08 '23
We have a lot of stopwatch in production in the project in working with. I tried using somethin else called something like "miniprofiler" and it was around 2 seconds faster. So I think stopwatch makes performance even worse
1
u/noxylliero Apr 08 '23
used by something which imports it ?
1
u/Beggarstuner Apr 10 '23
What the hell is type “string”?! That won’t compile.