To be fair to you, if I were given this to solve at work, and the deadline was horrible, I'd probably do it like this just to get it done. It'll work fine, and it'll only be an issue if you add more cards to the game in the future. It's dirty code, but I don't think it's that bad.
Fair point. Stuff like this is dotted around everywhere I've ever worked (and I'm definitely responsible for some of it!), and it's always the sort of thing you want to come back to and tidy up at some point, but because it basically works, you never really do.
the sort of thing you want to come back to and tidy up at some point, but because it basically works, you never really do.
The issue is that when the next dev comes in and sees a mess, they add more mess. Having a tech lead on your team that sets rigorous standards for clean code is IMO quite important for teams with inexperienced devs, so they don't create a horrible mess.
They don't really teach how to write proper, clean code for enterprise-sized applications in uni.
Looks like there's a bug in the code to me. I can't explain otherwise why on the last line there's ccard3 == ccard4 and ncard2 == ncard4 while everywhere else the ccard and ncard numbers correspond.
It makes no difference, but range(len is a bit of a pet peeve for me. It has its uses, but here for in ncards does it just fine and is more readable. sorry for the little rant lmao
How would you use in ncards here though? I think because you need to check uniqueness, you need to know the index to check up to - you're also checking ccards in the and condition so you'd have to use zip (if that's a thing in python?) to iterate through them anyway?
In Scala, you could just do ncards.indices, however a quick google came up with nothing outside of enumerate, which makes it weird when we address ccards by index but not ncards
well tbh, I think the most pythonic way to wtite this would be
for i, ncard in enumerate(ncards):
for j, ccard in enumerate(ccards):
…
and now you can access their indexes if needed or their actual values, not sure what the best approach for the rest of this would be though as its waay too early and I cant think lol
Hash set usually has an insertion time of O(1), and there is only need to do it once for each element in an array, so I'm not sure where log(N) comes from.
The nature of programming, the thing that we were proud of in the past (often very recently) often causes us self loathing and horror in the present day.
73
u/Chris_H-219 Feb 17 '22
It was for a card game and this code was meant to ensure that there are no duplicate cards
63
u/StruffBunstridge Feb 17 '22
To be fair to you, if I were given this to solve at work, and the deadline was horrible, I'd probably do it like this just to get it done. It'll work fine, and it'll only be an issue if you add more cards to the game in the future. It's dirty code, but I don't think it's that bad.
23
u/[deleted] Feb 17 '22
Looks like there's a bug in the code to me. I can't explain otherwise why on the last line there's ccard3 == ccard4 and ncard2 == ncard4 while everywhere else the ccard and ncard numbers correspond.
41
u/DGAssassin1 Feb 17 '22
What's the best way to write this?
29
u/[deleted] Feb 17 '22
The nature of programming, the thing that we were proud of in the past (often very recently) often causes us self loathing and horror in the present day.
26
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Feb 17 '22
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
23
u/fullSpecFullStack Feb 17 '22
This actually looks a lot like the kind of program an instructor would use to convey the usefulness of arrays.
5
u/[deleted] Feb 17 '22
Thank god it wasn’t more than a handful or cards. I’ve done shit like this too, loop within loop should work well here
2
u/RandomAccount4821 Feb 18 '22
I got you beat, I made this “simple” blackjack program around 3-4 weeks into my first computer science class Freshman year of high school
https://replit.com/@JasonRabinowitz/blackjackjava#Main.java
1
u/stevenr4 Feb 18 '22
That last if-statement, shouldn't the "ncard" be 3 instead of 2?
1
u/Brelee2222 Feb 21 '22
To infinity and beyond.