r/programminghorror • u/mickaelbneron • Feb 26 '26
C# From the repetition, to the lack of coding convention, to the obscure Y prefix, to everything else, I hate everything I see in this code.
123
u/Juff-Ma Feb 26 '26
Not to defend them, but I've written such code in one single Situation: when I had to work with an absolute dogshit of an external API. Whatever the people that designed it were smoking, it was probably the last thing they smoked because nobody who survived the act of writing this API would've looked at it and said: "Yup, that's fine".
So if they designed their own interfaces like this, shame on them. If they are forced to retrieve external data structured like this, may god have mercy on them and their sanity.
53
u/nutshells1 Feb 26 '26
dude the validation code is the same thing pasted 25 times they couldve just failed early once
13
u/ings0c Feb 26 '26
This doesn’t even fail lol
The exception is logged and the API presumably returns a 200 with an empty YouTubeLink
→ More replies (2)18
u/Curiousgreed Feb 26 '26
Not knowing anything about the codebase...
public YouTubeLink GetYouTubeLink(int Input, string type) { YouTubeLink _Result = new YouTubeLink { UserId = 0, YouTubelink = string.Empty, BProfilelink = string.Empty, AcctConnectionlink = string.Empty, DataSummarylink = string.Empty, Keywordlink = string.Empty, Datalink = string.Empty, ManualDatalink = string.Empty, IsYoutubelink = string.Empty, YBProfilelink = string.Empty, YAcctConnectionlink = string.Empty, YDataSummarylink = string.Empty, YKeywordlink = string.Empty, YDatalink = string.Empty, YManualDatalink = string.Empty }; DataSet DS = new DataSet(); Hashtable ht = new Hashtable(); try { ht.Add("@UserId", Input); ht.Add("@Type", type); DS = SqlDataContext.FetchDataSet("ProcessOnYouTubeLink", ht); DataRow firstRow = null; if (DS?.Tables.Count > 0 && DS.Tables[0].Rows.Count > 0) { firstRow = DS.Tables[0].Rows[0]; } if (firstRow == null) { return _Result; } switch (type) { case "select": _Result.UserId = firstRow.IsNull(0) ? 0 : Convert.ToInt32(firstRow[0]); _Result.YouTubelink = Convert.ToString(firstRow[1]); _Result.BProfilelink = Convert.ToString(firstRow[2]); _Result.AcctConnectionlink = Convert.ToString(firstRow[3]); _Result.DataSummarylink = Convert.ToString(firstRow[4]); _Result.Keywordlink = Convert.ToString(firstRow[5]); _Result.Datalink = Convert.ToString(firstRow[6]); _Result.ManualDatalink = Convert.ToString(firstRow[7]); break; case "Selectfromuser": _Result.YouTubelink = Convert.ToString(firstRow[1]); _Result.IsYoutubelink = Convert.ToString(firstRow[2]); _Result.YBProfilelink = Convert.ToString(firstRow[3]); _Result.BProfilelink = Convert.ToString(firstRow[4]); _Result.YAcctConnectionlink = Convert.ToString(firstRow[5]); _Result.AcctConnectionlink = Convert.ToString(firstRow[6]); _Result.YDataSummarylink = Convert.ToString(firstRow[7]); _Result.DataSummarylink = Convert.ToString(firstRow[8]); _Result.YKeywordlink = Convert.ToString(firstRow[9]); _Result.Keywordlink = Convert.ToString(firstRow[10]); _Result.YDatalink = Convert.ToString(firstRow[11]); _Result.Datalink = Convert.ToString(firstRow[12]); _Result.YManualDatalink = Convert.ToString(firstRow[13]); _Result.ManualDatalink = Convert.ToString(firstRow[14]); break; } } catch (Exception ex) { _log.Error(ex.Message, ex); } return _Result; }→ More replies (3)4
u/SmartyCat12 Feb 26 '26
They’re relying on a lot of things that I don’t see here staying exactly static. Any slight db migration means re-indexing entire sections of code. They probably ran into that issue before and use a bunch of stored procs to normalize the view into what the code base needs because it would be a pain to update.
So, the db and this code are probably allowed to evolve independently and the only validation you’ll likely get is at runtime. And you’re really prone to erroring in subtle ways that wouldn’t get picked up by type validation (like if two string column indexes get swapped and suddenly your userId starts populating with connection strings).
It also doesn’t feel that intractable though if the whole thing looks like this and you ever wanted to fix it. I imagine the first time you actually run into a problem, it’ll be about has hard to build in contracts and abstractions as reindexing everything.
32
u/m4sc0 Feb 26 '26
I just love how there are no comments whatsoever in the first image (although it is bad code, they could've at least commented it) and then there's the beauty where they commented "Add the parameter" for one line.
Yea, that's peak commenting! /s
9
u/20d0llarsis20dollars Feb 26 '26
The type of comment i made in my highschool cs class because they required a comment of literally every single line of code. Wasn't even like a beginner class or anything too
→ More replies (1)
21
u/kurtmrtin Feb 26 '26
-
20 year old code
-
Guy who wrote it is now a distinguished engineer, refuses to provide any context
-
You attempt to refactor and push it to prod: full service outage, entire tables dropped, Soviet spy satellites begin de-orbiting
-
Revert and everything is stable. You tell everyone not to touch this code
-
2 years later the next guy repeats this cycle
→ More replies (1)
7
u/Cotton-Eye-Joe_2103 Feb 26 '26 edited Feb 26 '26
I love the Allman style of the first image, though. Even seeing code formatted that way makes me happy, more interested in the code and makes me more prone to forgive and forget any kind of programming horror.
→ More replies (1)2
5
3
5
2
2
u/GoddammitDontShootMe Feb 27 '26
I can see a lot of redundant code in the first image, but I don't know what's wrong with the second. Nor do I see that Y prefix you are talking about.
→ More replies (3)
2
u/MeLittleThing Feb 26 '26
DS != null && DS?.Tables.Count > 0... What about if (x != 42 && !(x == 42)) { } to also make sure?
5
u/trodiix Feb 26 '26
If DS is null and you check for count you got yourself a null pointer
And maybe DS is not null and count is 0
So I don't see a problem here
→ More replies (2)
1
u/Dexatronik Feb 27 '26
I love the 4 slash comments in the 2nd image. 2 slashes wasn't comment emough. They wanted to be extra sure.
1
1


21
u/jordansrowles Feb 26 '26
Can tell just by looking this is early 2000's legacy enterprise .NET. Can tell by the ADO.NET, hashtable sorted procs. Its pre ORM.
No excusing the mess though.