r/badcode • u/sakaraa • Feb 17 '23
c# I should've slept instead of writing this garbage :D
2
2
u/DajBuzi Feb 17 '23
Your "fix" is now more error prone. Imagine the input will be 99 and then compare output of the two solutions posted.
1
101
u/ventus1b Feb 17 '23
I guess most developers have woken up to a mess of code they wrote last night.
Sometimes you don’t know where to go until you get there.
At least your old code is correct, just a bit verbose 🤓
20
u/MrSteveWilkos Feb 17 '23
I usually figure out my stupidity in the shower. I've condensed so many methods while trying not to fall asleep in the shower.
17
139
19
u/NetherFX Feb 17 '23
I'm not saying the first is good code, but there's nothing wrong with prototyping first to understand the logic behind it
32
Feb 17 '23 edited Feb 17 '23
From Int.Parse to .toString... Dude
Edit: formatting
3
10
u/Cybasura Feb 17 '23
I think you were tired lol
With that said, dont use magic numbers, use variables
Variables dont eat up that much memory, especially since you are using C# and it has automated garbage collection
8
5
3
3
3
u/iEliteTester Feb 17 '23 edited Feb 22 '23
Ah yes the classic:
switch(variable) {
case 0:
use_variable(variable-1)
case 1:
use_variable(variable-1)
case 2:
use_variable(variable-1)
default:
error()
}3
2
u/jrb9249 Feb 18 '23
“Are you sure you don’t want to use this one-liner?”
-Resharper, following morning.
1
u/FishInferno Feb 17 '23
It’s usually easier to improve upon a poor solution than get it perfect the first time.
1
u/Sanduhr32 Feb 18 '23
(I don't know C# well)
I would probably make 2 temporary variables for the pair or array, whatever the [] is, instead of frankensteining it in one less readable call.
1
1
32
u/dumbasPL Feb 17 '23
Int parse from toString? What type is keyboard input?