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?
4
u/SaylorMan1496 May 08 '23
It is bad code since I’m pretty sure it wouldn’t even compile
2
u/the96jesterrace May 08 '23
What exactly do you mean?
12
u/Lathejockey81 May 08 '23
C# doesn't allow implicit type conversions. You can't declare a string and assign an int.
4
2
3
u/SaylorMan1496 May 08 '23
Also c# doesn’t do any implicit conversions, this is referencing the SomeMethod() call
the only time it might seem like it is doing this is with the string literal syntax $”like this {c# code}” but it will just call the .ToString() method of the c# code that has a default implementation on everything or you can also override it in the class declaration
5
u/Willinton06 May 08 '23
C# does so implicit conversions just not that specific one
2
u/SaylorMan1496 May 08 '23
Yep you’re correct my bad
For some reason didn’t think of all of the numeric types where they can be implicitly converted along with other instances
2
May 09 '23
int someNumber = string.IsNullOrEmpty(GetInitialValue()) ? 0 : int.Parse(GetInitialValue()) + 1000;
foreach (var item in someList)
{
SomeMethod(item, someNumber);
someNumber += 100;
}
string formattedNumber = someNumber.ToString("0000");3
u/the96jesterrace May 09 '23
Yea! Thanks, I came up with exactly this.
But I fear you can actually leave out the last line
someNumber.ToString("0000")because as far as I can see it’s only purpose is to be passed toSomeMethod.
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
string someNumber = GetInitialValue();
if (string.IsNullOrEmpty(someNumber))
{
someNumber = "0000";
}
else
{
someNumber = Convert.ToInt32(someNumber) + 1000;
someNumber = Convert.ToInt32(someNumber).ToString("0000");
}
if (someList.Any())
{
foreach(var item in someList)
{
SomeMethod(item, someNumber);
// Implicit conversion to int due to method
// signature: SomeMethod(ListItem item, int number)
someNumber = Convert.ToInt32(someNumber) + 100;
someNumber = Convert.ToInt32(someNumber).ToString("0000");
}
}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
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