Method uses object as return value, returns string "null". Didn't know about date formats. Number & Currencies are both stored as doubles, why the differentiation.
But worst of all, and I am aware that this is not evident when looking at the screenshot. There's a ORM in that solution, I wouldn't have had to worry about ANYTHING at all.
Thanks for mentioning the usage of String, I get really frustrated whenever I see someone use that instead of string (which in 99.9% of all cases is the intended type).
There is none(in most domains). Both are of type system.String however generally string is preferred becuase it cleaner.
Just like using int instead of system.int32. Int is cleaner.
It's best practice to use the alias string when creating an object and using the System.String class when you require a method on that class to modify your string object.
The reason it's best practice, boils down to thats what everyone else is doing. There are actual reasons, such as "string" will always be referring to System.String, but theoretically someone could be an idiot and create a custom String class that they are then using in a class, for example, that would then be of type custom.String and not System.String.
The problem is when you are maintaining someone else's code you have no idea how idiotic they were. By using "string" instead of "String" you can reassure future programmers that you weren't a compete dumbass.
They mean the same thing ... almost all of the time. The issue is that String can be re-directed, but string cannot.
string is a built-in keyword, which expands to the fully-qualified name System.String
See here https://stackoverflow.com/questions/7074/what-is-the-difference-between-string-and-string-in-c
String is just an unqualified class name, which gets a fully-qualified name found, using the same mechanisms as looking up any other class name e.g. CustomerRepo is matched to SomeApp.Respositories.CustomerRepo by looking through all the namespaces in scope. i.e. because you also have using SomeApp.Respositories;
For String, will using System; win the name resolution, and be the same thing as string? Almost always, yes.
But try and put this at the bottom of the same file, inside the namespace:
public class String
{
public override string ToString() => "Sucker!";
public override bool Equals(object obj) => false;
}
Yes this compiles. It's in a different namespace to string, e.g. SomeApp.Services
Now suddenly all the code nearby that uses String will resolve this local class with fully qualified name SomeApp.Services.String
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”.
$locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
if (! $locale == "en_US"){ echo 'Your language '.$locale.' is not supported'; exit(); }```
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.
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.
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 eg 100,000.5 in South African English, very unique from most other English localizations (closer to eg Chinese approach). It's those contextual details that will bite you.
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.
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.
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?
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?
4
u/lttitus Dec 08 '21
Why is 21 in binary null? Thats the most confusing to me
3
u/zombarista Dec 09 '21
Fira Code fist bump 👊
254
u/rebornfenix Dec 08 '21
I love stringly typed data
185
u/PowerApp101 Dec 08 '21
Ah all data as strings. What could go wrong?
118
u/[deleted] Dec 08 '21
Just comment it like:
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”.
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?
17
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Dec 08 '21
Image Transcription: Code
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!
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
3
u/GustavoCinque Dec 08 '21
Why?
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 👀
2
u/trevster344 depraved Dec 08 '21
I feel personally attacked.
2
u/JoeyBroths Dec 09 '21
OP has to add comments, not fix it.
It’s ugly, but very readable..
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
u/KuchDaddy Dec 08 '21
"Have to," lol.
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/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:
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
u/brainsto Dec 09 '21
My eyes. MY EYES!
You'll pay for this my pretty. And you're little dog too!
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.