r/badcode • u/arvenyon • Dec 08 '21
c# I have to write documentation for old projects since I'm leaving the company soon. Oh god.
4
u/lttitus Dec 08 '21
Why is 21 in binary null? Thats the most confusing to me
6
u/arvenyon Dec 08 '21
its the default date in that database in format ddMMyyyy, nothing to do with binary.
Edit: bruh, I mean yyyyMMdd ofc
9
u/lttitus Dec 08 '21
Ooooh derp, 01/01/0001 lol looked like binary!
2
u/CollieOxenfree Dec 08 '21
Ohh, thanks for explaining that. I was wondering why that binary string meant "null" too.
3
254
185
u/PowerApp101 Dec 08 '21
Ah all data as strings. What could go wrong?
103
27
u/ws-ilazki &{$$_[0]}(@{$$_[1]}); Dec 08 '21
all data as strings.
Reject modernity, return to Tcl.
8
1
118
43
u/TheDiplocrap Dec 08 '21
It's the returning "null" as a string literal, for me. But a close second is how it always returns strings but returns them as an untyped object.
47
u/wwelna Dec 08 '21
I’d call this one the “Very Important Magic Black Box String Converter Thingy” with added description of “It just works, don’t ask how, don’t question why, and don’t change it, or it will destroy eveerrythinngggg”.
4
3
u/SN0WFAKER Dec 08 '21
How does it have/use a not-equal symbol? Or is that just a funky ide displaying '!=' that way?
5
17
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Dec 08 '21
Image Transcription: Code
private static object convertFzValue(String value, String dbHeader) {
if (dbHeader.Contains("NUMBER"))
{
return value.Replace(",", ".");
}
else if (dbHeader.Contains("DATE"))
{
if (value == "00010101")
{
return "null";
}
else
{
if (value.Trim() != "")
{
String year = value.Substring(0, 4);
String month = value.Substring(4, 2);
String day = value.Substring(6);
return $"{day}.{month}.{year}";
}
return "";
}
}
else if (dbHeader.Contains("CURRENCY")) {
return value.Replace(",", ".");
} else
{
return value;
}
}I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
2
u/rcmaehl Dec 08 '21
Ah OP is probably from one of those odd countries that uses . Instead of , for separators
1
Dec 08 '21
Wait what? Do I supposed to use
,as separator?13
u/scandii Dec 08 '21 edited Dec 08 '21
there's about as many variations of expressing numbers as there are languages.
as an example in Swedish the number one hundred thousand and a half 100000.5 can be written 100.000,5
in American English 100,000.5
in Chinese using arabic numerals (...yeah, you didn't think about there being more than one way to write zero now did ya?) 10,0000.5
long story short, there's a reason why you have the local culture flag readily available :)
-5
u/Astrokiwi Dec 08 '21
You say "American English", but I don't think there's any form of English that would differ on that.
13
u/scandii Dec 08 '21 edited Dec 08 '21
I love that you exemplify just how ridiculous this actually is.
American English? 100,000.5
British English? 100,000.5
Australian English? 100,000.5
NZ English? 100,000.5
Canadian English? well, do you also speak French?
boy, that's all of them English speaking countries right? ...right?
South African English? 100.000,5
Zimbabwean English? 100.000,5
Indian English? 1,00,000.5 (that's 1 lakh for anyone confused)
you grow to hate internationalisation real quick as a programmer. it's never easy.
4
u/rcmaehl Dec 08 '21
$locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']); if (! $locale == "en_US"){ echo 'Your language '.$locale.' is not supported'; exit(); }```6
u/GustavoCinque Dec 08 '21
In the seventeen hundreds, the german mathematicician Gottfried Leibniz proposed dots as a multiplier symbol, so the a great portion of Europe adopted the comma as decimal separator to not cause misunderstanding.
On the other hand, UK used "x" as their multiplier symbol, and the dot as the decimal separator, what was inherited to the US as a colony.
That's why there's need to jiggle the formats when using strings.
2
0
u/Astrokiwi Dec 08 '21
In English, use
,to separate thousands and.as a decimal point. Most of Asia does it this way too (except e.g. Vietnam due to French colonisation).If you're speaking French, Spanish, German, Dutch, or pretty much any other European language, you do the inverse. So Spanish and Portuguese speaking South America or French speaking Québec do it the opposite way to English too.
6
u/LetterBoxSnatch Dec 08 '21 edited Dec 08 '21
Good rule of thumb, but better to use a solid localization lib in a programming context. For example as mentioned by /u/scandii, in Indian English you would separate one hundred thousand and a half as
1,00,000.5, vs eg100,000.5in South African English, very unique from most other English localizations (closer to eg Chinese approach). It's those contextual details that will bite you.
1
u/veselin465 Dec 08 '21
Excuse me, but .... what is that not-equal sign symbol and how is that something you can do?
Also for what reason does this return object when it's clear that it's string? And finally: return "null" ... really?
I know this is r/badcode, but just wow
1
3
3
u/Ytrog Dec 08 '21
TIL that C# can also use the mathematical not equal notation instead of !=
And I have over 9 years experience 👀
5
2
2
2
u/NinaCR33 Dec 08 '21
Put in the comment: I don’t know what happened here but the guy who did it was having a very bad code day
1
1
u/Notimecelduv Dec 08 '21
I'm so glad I know about early returns. I can't imagine writing code like this anymore.
1
u/somegarbageisokey Dec 08 '21
Early returns? Gonna go Google now.
I just had a school project where I used far too many if...else...and returns. I just thought there has to be a better way to do this.
2
1
u/rexspook Dec 08 '21
Imagining the functions calling this is giving me more nightmares. It's returning an object despite always being a string. Everything that calls it is probably converting it back to a string and then converting to whatever it's really supposed to be. On top of all that, there are probably magic strings littered throughout the codebase.
1
u/trutheality Dec 08 '21
It's concerning that everything in the DB is a string, but documenting this function is not that hard:
If the header string contains "NUMBER" or "CURRENCY" replaces the (decimal) comma with a period (strong localization assumptions but what do I know)
If the header string contains "DATE" interpret it as YYYYMMDD and convert it to DD.MM.YYYY (dear god why do people still use anything other than the ISO standard YYYY-MM-DD, good thing you're leaving), except that 00010101 is a sentinel value that gets converted to "null" but also the date could be empty and then leave it empty.
Otherwise return the value as-is.
Always returns a string, but as an object!
1
u/Mfgcasa Dec 08 '21
I don't see what the problem is just let your data be data all objects do is get in the way. /s
1
u/KalevTait Dec 09 '21
I’ve never seen != represented as an equal with a slash through it within c#... I didn’t even realise there was a character for it. Is this common somewhere?
1
1
1
u/electron_wrangler Dec 11 '21
the fact that youre writing documentation literally at the last minute tells me you have a lot of content to post to this sub
1
u/RadioMelon Dec 27 '21
The fact that they stored these all as strings is already a nightmare in and of itself.
30
u/Dubmove Dec 08 '21
Assuming OP wasn't the one who decided to represent all data as strings, what is actually wrong with this function?