r/programminghorror • u/SUPERNOOB_20 • Nov 24 '25
How to put DRY into practice here?
A lot of times, I struggle trying to "not repeat myself" when coding (DRY).
There are some memes about it: like checking number parity by testing them one by one up to "infinity", instead of simply using the modulo operator, or writing some simple algorithm with a loop.
However, memes are memes, and in practice, in real life, it is rarely that simple for me to fix...
Here is one of the scenarios where I have encountered this problem, and couldn't solve it:
Context: This is for a card game I'm making. The script that I will discuss here is written in Python.
There's 28 cards in total, and each card's image is loaded from storage, then converted into bytecode, and then rendered onto the screen as a texture on top of a GL_POLYGON (upon being prompted to).


My questions are the following:
- How would you improve this code? I have a few ideas, but they're a bit complicated (making a C/C++ library or rewriting everything in C/C++, using metavariables,... ugh)
- Do you think this code is... bad? Is it bad repeating yourself like this, even if it's pretty much a one-off thing for my whole project?
- ...maybe there's some "enum" equivalent in Python that would shorten this code?
- Do you reckon that rewriting this whole script in C/C++ would fix the DRY issue (by using arrays with pointers for example)? Because, if so, I might try to go for it! "^^
19
u/Jmcgee1125 Nov 24 '25 edited Nov 24 '25
You are using an object-oriented language. Use objects. Those cards in the first picture all have the same set of variables, right? So:
For
draw_card, I see the same issue: you're setting the same set of variables, so why not just store that info in the card? Then you can just do:To make it easier to keep track of your cards with the card IDs, make a dictionary. You can use the card ID as the key and the
Cardobject as the value, which will make lookups convenient. So addressing the comment in the first code block:To question 3: there is an enum equivalent in python -
from enum import Enum. The docs will give you a good overview of how that works. That'll let you replace the"kosuzu"strings or whatever withCards.KOSUZU.At some point you do have to make a giant list of each card to put them in the dictionary. It's fine if that part looks very similar, the point is you'll only have one list like it.
(Though, if there's absolutely nothing different between each card besides the filename, you could just make an array of filenames and have a for loop build the dictionary. But I see you have some color differences and possibly other stuff, so even though the unrolled version is more repetitive, it's more expressive).
22
u/garblesnarky Nov 24 '25 edited Nov 24 '25
Not sure if this is the right sub but...
Are you familiar with dicts? AKA maps in other languages? I'm not sure what you you would do with enums or "metavariables" here, but with a dict you can reduce all the duplication, e.g.
If you think rewriting from python to C/C++ will improve duplication issues, that seems like a sign to me that you are not very familiar with python.
7
u/clesiemo3 Nov 24 '25
There's also a bug on line 145 where you reassign Aya height
4
u/luxiphr Nov 24 '25
ever heard of maps/dicts or even classes?
that said... kiss > dry... don't @ me
3
u/Cylian91460 Nov 24 '25
Sounds like you need to make a class that you can easily initialize with the right value, that would reduce the number of variable
3
u/tehtris Nov 24 '25
each one of those blocks in the first pic should probably be represented as a single object that you create like `kyoko_card = Card("kyoko.png")` that has properties like "image_obj" which would be the `pygame.image` object you assigned to `kyoko_card_raw`.
The second image with the case statements looks like all you are doing is populating hardcoded data into some place so glBindTexture can use it.
I would do something like this with a dictionary:
```
lookup = { "kyoko": {"card": kyoko_card, "texture": 69}
...}
``` to store all of that data (because you already should have height/width from the Card object we created up there) So then in the end it would be something like
but this is super broad examples. Generally when trying to DRY, if you find yourself writing anything, like your giant wall of repeated text, you want to see what is common in all of them, and pull that out, and either objectify if you need to keep it around, or functionalize it to process something quickly.
5
u/ironykarl Nov 24 '25 edited Nov 24 '25
I do think it's bad, in the sense that you essentially have structured data, but instead of encapsulating that in a type, you're relying on naming convention.
That makes things very un-reusable, and un-extensible.
Just think about actually using this data (say in other functions, or in a loop) or extending some of your interfaces (e.g. to do some type of error detection/handling).
If you do a very little bit of abstracting out, you can construct all of the data you want here with nothing more than the path to the PNG, and you can construct something that contains references to all of the relevant data in a single identifier (of the type you define).
--
To point out a specific example of what your preponderance of redundant code is doing: look at lines 133–141. You've done the same thing twice and simply not noticed, cuz your code is basically an eye-glazing wall of text
0
u/toyBeaver Nov 24 '25
Hashmap
1
u/tazzadar1337 Nov 24 '25
Inject configuration instead of using
switchstatements. This way you only have 1 class/implementation to test, instead of this monstrosity. Separating code from configuration.A quick Google search suggested this article: https://medium.com/miragon/use-interfaces-and-dependency-injection-instead-of-switch-case-63f54eac77c4
-1
u/ztaffa Nov 24 '25
I mean your basically defining resources so there's no way not to repeat.
Your case statements looks like you just need to make a class and make each one into objects. I'd probably move the definitions of each card into a separate file and load them in iteratively to create each as an object of a custom class, separating resources from code.