r/badcode • u/the96jesterrace • May 08 '23
c# Better treat this number as a string to make sure that .. make sure that .. that it’s sure .. ehhrrmm.
… I don’t know. Maybe its just me being stupid but what might be this authors intention?
346
Upvotes
27
u/josephblade May 08 '23
I don't know about intentions, this doesn't look like live code (given 'someNumber, someList, SomeMethod)
whatever number is given, add 1000 to it. then for every item in some list add another 100
it's absolute insanity that they keep switching to and from string, just in order to re-use the original variable. It's like great, you saved 4 bytes on the stack but you pointlessly are using up cpu cycles. all the int to string implicit casts followed by formatted casts.
at the very least assume you can do:
I mean it's awful to keep the total as a string. But why first write it in default format and then write it in another format the very next line.
Also if in comments they are admitting that SomeMethod needs the number as an int, and all operations inside this method need number as an int.
then why for the love of lovelace are they converting it to string every time.
It's for this sort of reason I tend to suggest: if you get user input, your outside layer is responsible for converting user input into machine friendly numbers (and other things). then inside you use nothing but machine friendly. then when you are ready to pass something back to the user, the outer layer is again going to convert from int to string. People take 'what the user entered' way too literally.
I may have to have a lie down now, this one gave me a headache. I hope this isn't your code and whatever junior you have there that wrote this code is getting a senior to talk things through.
there isn't enough coaching done in workplaces and this would be a great place to start
4
u/SaylorMan1496 May 08 '23
It is bad code since I’m pretty sure it wouldn’t even compile
2
u/[deleted] May 09 '23
68
u/00PT May 08 '23
It seems like they want to format the integer in a specific way, possibly for displaying later, and are just doing some unnecessary conversions during calculation.
35
u/MurdoMaclachlan public boolean isInt(int i) { return true; } May 08 '23
Image Transcription: Code
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!
2
u/poope_lord May 09 '23
Something is going on in here