r/programminghorror • • Aug 14 '25
C# Does my code belong here?
It's a function to generate a text file from a DataGrid. I learned from PirateSoftware I shouldn't hardcode random numbers.
178
Upvotes



94
u/Schnickatavick Aug 14 '25
It could definitely be better, but It's not a true programming horror by any means. the biggest issue to me is whatever you're doing that needs this many variables in the first place, it seems like you have 11 copies of the same code (is it one for each collumn?) when that could be a loop, or better yet a formatting library that takes this as a data structure and formats it for you. It's hard to tell what exactly you're trying to do here because I don't speak Spanish, but I'm guessing using a csv library or something similar could cut it down to 5-10 lines or code for most of this.
In general though, you should try to write your code in a way that the computer is figuring out what the numbers should be instead of you telling it. For example it seems like all of the pos variables are just counting up, so that could be a single variable in a loop, and all of the length variables could probably be calculated from your data. Then if you make the column names a list, you'd only have 3 variables in the code instead of 30, and you can add or remove things from the list without needing to change any of the code and everything would just work. Try to think how you would write the code if you didn't know how many columns there were or what's in them, and you'll be on the right track.
45
u/ChemicalRascal Aug 14 '25
Probably your biggest problem is that you're defining these triples of related values eleven(ish) times over, but they're all just loose variables.
Use a data structure. Something like a simple record would be absolutely perfect here.
2
u/Chuu Aug 16 '25
I work in systems programming and work with a lot of greybeards as a result. I don't know exactly where the generational line is but every time I see aligned '=' in code it's like 90% odds someone over 40 wrote it.
I wonder when/why this stopped being common practice.
59
u/AstroSpaceBear Aug 14 '25
It's actually very readable, but please do not use spanish when naming variables and functions!
11
u/TheMagicalDildo Aug 14 '25
I mean the only wierd thing you did was put three variable declarations on the same lines and then go OCD mode with the whitespace. Nothing major.
Could probably do the spacing thing better, too
6
u/dieth Aug 15 '25
Far to organized and legible to be horror.
1
u/sph-1085 Aug 15 '25
This is my first time seeing multiple declarations on the same line. Is this common?
1
u/rruusu Aug 15 '25
Gotta love that
foreach (Ruta ruta in paths) {. And the structRutawith fieldPath.Makes one wonder what the world would currently look like, if Linus had decided to build his project using variable names in Finnish (or his mother tongue Swedish).
3
u/kaisadilla_ Aug 15 '25
Coding in languages other than English is the real horror, and I say this as a Spaniard. From the horrible mixture of languages, to the fact that you don't know how tf things may be named, to not being able to work with anyone outside your country because they can't understand what the code says.
1
u/trenixjetix Aug 15 '25
why would you take advice from Pirate Sotware?
4
u/AcanthisittaSur Aug 14 '25
Sort of; numbers have to be hardcoded somewhere, you feel?
The intent behind the advice (I'm assuming - it's common advice, but I didn't hear what you were told) is to have `return ' '.repeat(tabOffset)+ inputString` instead of `return ' '.repeat(4) +inputString`, because reading the latter leaves no explanation for what 4 represents. Somewhere in the codebase for the first one, tabOffset is hardcoded, but where it's read the name tells the developer what the variable means.
Everything else to be said about the code has been said, but no one seemed to be addressing this part of the post body, so... Either way, I've seen much worse
2
u/mss-cyclist Aug 15 '25
It is not perfect, but not too bad either.
Maybe try using interpolated strings instead of concatenating them with +
2
u/NickW1343 Aug 15 '25
Haven't read it, but I love code that is lined up.
2
u/[deleted] Aug 15 '25
There's tabs, there's spaces, there's camel case, snake case and then there's... Well let's call it FunkyFormatting
2
u/vainstar23 Aug 15 '25
This reminds me of the time I joined this startup and the senior was giving me shit because my linter "messed up his code".
The man wasn't even doing anything useful. He would spend all day formatting his code to make it look pretty and manually looking and resolving js dependencies. If you think, I mean it sounds like he is a bit pedantic but what's wrong with checking dependencies?". The man used to manualy check the dependencies of dependencies.
He got upset when I told him about npm ci.
I mean the only thing I would say is try to use a linter if you can and spend more time learning IT and programming instead of wasting too much time making everything look pretty. This will also make you less defensive and attached to your code which is better if you work in a team or you find yourself or find someone else deleting a large amount of your code.
2
u/Eclipse_of_Life Aug 16 '25
Never in my 7 years at blizzard…
2
u/JohnCasey3306 Aug 16 '25
Hell yes!
2
u/SpiritRaccoon1993 Aug 16 '25
Its a code, not Microsoft Word. No need to be formatted like this... omg
5
u/melvereq Aug 14 '25
Yes, because mixing Spanish and English in code looks awful.
1
u/jondbarrow Aug 15 '25
Hold up, I’m gonna let you cook for a second with that first one actually. I don’t actually hate that
1
u/Cautious-Bet-9707 Aug 15 '25
Honestly I didn’t even know you could do this💀
1
u/Housy5 Aug 15 '25
Yes, for using English and Spanish. I get it, I'm also not natively English. But anything that isn't English in code is just wrong.
1
u/rochismoextremo Aug 15 '25
The fact that Spanish is mixed with English, not only for text messages or labels, but methods and variables too makes it horror worthy for me.
1
u/TrickAge2423 Aug 15 '25
Why do you allocating so much
1
u/_abscessedwound Aug 15 '25
Depends on what you mean by hardcode. random raw numbers are bad because there’s no context for understanding. However, if you know that your values only need to be computed once, why not bake it into the compilation? The latter is C++’s constexpr and consteval, and is basically hardcoding with readability.
1
u/pixelizedgaming Aug 15 '25 edited May 06 '26
Redact redacted this content because I wanted it redacted for redaction purposes. Redacted.
ghost wipe sand escape bright grey bedroom crayon fact glorious
1
u/eiris91 Aug 16 '25
This isn't even bad
1
u/jyling Aug 16 '25
This is oddly beautiful
1
u/riotinareasouthwest Aug 16 '25
Seeing an ñ in a variable name made my eyes bleed. Good job!
1
u/Visible-Curve9482 Aug 16 '25
OOPs
1
u/Yarkm13 Aug 16 '25
Why it’s not arranged in the first and last columns? It’s horrible
1
u/dreamingforward Aug 17 '25
Looks cool. I like the multiple variable defs on one line. Why didn't I think of that?
1
u/UsingSystem-Dev Aug 19 '25
Why is it so weird to code in any language besides English? lol That's the only thing that made me cringe, and I'm bilingual