Refactoring a Rock Paper Scissors Lizard Spock Game // Part 2

  Рет қаралды 21,644

ArjanCodes

ArjanCodes

Күн бұрын

Пікірлер: 71
@ArjanCodes
@ArjanCodes Жыл бұрын
💡 Get my FREE 7-step guide to help you consistently design great software: arjancodes.com/designguide.
@ProgressiveDeveloper
@ProgressiveDeveloper 9 ай бұрын
Hey, Arjan it's been a while now following your design guide videos. But the real problem is that, is it good to place more than once class in a single file, I have been coding in PHP where every class takes it's own file, the code is simple to refactor in that way. What are thoughts?
@asifiqbal21
@asifiqbal21 2 жыл бұрын
Thanks Arjan for part 2 of the refactor. I really enjoyed watching the refactoring of my code.
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Hi Asif, thank you for supplying the code and glad to hear you enjoyed it. It was a lot of fun working on your code!
@Stroid9
@Stroid9 2 жыл бұрын
Love the code roast videos! One thing I was thinking of is that instead of having one player entity and one CPU you could have two abstract players that you use dependency injection to choose the type of player. This allows you to pick one player and one CPU or two players or two CPU(if that even makes sense) Hope you understand what i mean!
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Yes! It will make the game logic more generic. I thought about doing this but in the end I decided to skip this because the video became already quite long.
@BLRMalcolm
@BLRMalcolm 2 жыл бұрын
@ArjanCodes I know you are using vim shortcuts now on vscode. I realized that at 5:37 your deleting pattern were always the same. Maybe it was easier to do it once, move your cursor to the next line and press (dot) which repeats the last command. This is something I forced myself to get used to and now it's a fundamental tool in my daily coding routine. (If this didn't work because it's a complex command, then it should be done by a simple macro (press enable macros, a letter to start recording and store it on that letter ( to store it on ) then do your thing once and move down and press to stop the macro recording. Now call the macro stored in a by doing and it will automatically do what you did before). This weird things are what make vim hotkeys amazing. Just try them in the wild and it will make your brain have goosebumps for a moment. Thank you for all your videos !!!
@claudiafischering901
@claudiafischering901 2 жыл бұрын
That is nice to know. Thx.
@nukedoom
@nukedoom Жыл бұрын
Thank you for that series. I’m not a coder, but code a lot for work. I’m self taught, and I always wonder how a pro coder would write the code I’m writing. This series helps a lot my self confidence as I can identify what I’m doing right and wrong. First I write a spartan code that just works (usually a giant script that does things step by step) and I refactor it adding classes and and separate functions. This is an excellent exercise for improving coding logic
@DegenOne
@DegenOne 2 жыл бұрын
These code roasts are fun and informative! Keep it up.
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Thank you, will do!
@dabusdruva
@dabusdruva Жыл бұрын
Hi Arjan, these videos is great. It was really difficult to find part 2 of this series after watching the first part. Could you add a link in part 1? I think it would help a lot of people. Great work, Sir.
@tiagovla
@tiagovla 2 жыл бұрын
It's a bit suspicious how he starts winning games after "cleaning up the code" Kappa
@tiagovla
@tiagovla 2 жыл бұрын
Also, enumerate(list,1) starts indexing at 1.
@smanzoli
@smanzoli 23 күн бұрын
The app is really simple, so my refactoring would change from OOP to Procedural and achieve the same result with 1/4 of the coding 😅 Thanks for the video, it was great btw!
@StevenMartinGuitar
@StevenMartinGuitar 2 жыл бұрын
Commenting before I've seen it, but it would be great if you could create a second visual version of this game using your new UI interface to show/prove how useful and easy setting up classes this way can be for making further developments
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Yes, sounds like a good follow-up, thanks!
@MScottBlake
@MScottBlake 8 ай бұрын
@@ArjanCodes I think there is a lot of value in showing how you maintain code over time. There are new elements in newer versions of python, there's still another UI class you could do, etc. I'd love to see you resurrect an older project such as this one for a new video to show common tasks to modernize.
@virtualraider
@virtualraider 2 жыл бұрын
I love that you use annotations extensively, they make code so much more readable. Around minute 10:52 you added `Optional`. While it's correct, I always advocate for the alternative ` | None`. I think it's more explicit to say ` | None`, because the word Optional is frequently misinterpreted as "the variable is optional", rather than "the VALUE is optional". I see that a lot at work. For instance, when we see a signature like this it should be obvious that the positional argument is required but it's value can be `None`, yet a lot of people read it as "the argument itself is optional" ``` def foo(bar: Optional[int]) -> Optional[bool]: ``` I think this is more explicit: ``` def foo(bar: int | None) -> bool | None: ```
@ArjanCodes
@ArjanCodes 2 жыл бұрын
That’s a good idea. I’ve used Optional until now because the ‘| None’ syntax was added to Python only recently, but I agree with you that it’s clearer.
@astronemir
@astronemir 2 жыл бұрын
Hey that’s a good idea. I actually dislike the optional syntax a lot so this is a good way around it.
@Ghost-Raccoon
@Ghost-Raccoon 2 жыл бұрын
13:40 I feel that conceptually a scoreboard is always part of the game and not an independent object. One could argue that the scoreboard could be used for different games but a typical ingame scoreboard is part of the game.
@alessandroferrari2166
@alessandroferrari2166 2 жыл бұрын
great video as usual, Arjan (I've to catch up a bit with all your latest videos!). A suggestion for the future. What about refactoring code by writing tests first? In this way you know for certain that the refactoring didn't break anything, instead of having to run manually the program at any change. Ofc, surely these videos would take longer to make. Just a suggestion from a stranger on the Internet :)
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Hi Alessandro, good suggestion that several commenters have also made. I didn't do this on purpose in these refactoring videos because I was afraid this is going to take too much time away from seeing the actual design changes, but I'll look into it.
@abdelghafourfid8216
@abdelghafourfid8216 2 жыл бұрын
I would love to see you refacoring some complex streamlit app since it's quite popular and simple to use, but thing can go out of control very easily when having multiple functionalities in the same app
@davidlakomski3919
@davidlakomski3919 2 жыл бұрын
Riker suits you very well that's right! 😂 A great character btw
@rahulprasad2318
@rahulprasad2318 2 жыл бұрын
I disagree with how you handled the scoreboard display. now scoreboard and UI are coupled. Now you will be restricted to always have a UI to use this. What if say you are training a ML model and need this to generate training data? The correct approach is to add a get_current_scores() which results a structure ( dict or 2d array) of player and score. Now UI must parse this and display however and whatever it wants. And if someone wants to generate training data, they can just call get_current_score
@xnick_uy
@xnick_uy 2 жыл бұрын
Good point. I think that a workaround for keeping the scoreboard as it is would be to implement a form of "null UI" that adheres to the protocol but actually does nothing. Then you can use it for the training model without much hassle.
@jessta314
@jessta314 2 жыл бұрын
In this case the UI protocol is a fairly general output and doesn't include anything that would force some kind of on screen display. If you were training an ML model then it would be interested in subscribing to that output (just like a human would) and could do that by implementing the UI protocol.
@marcotroster8247
@marcotroster8247 2 жыл бұрын
One thing: putting together the message in the rules module violates your UI abstraction. Moreover, I'd put the rules back into the game class, now that it's only 20 lines 😉 Then, you could even parameterize the rules in the Game class such that the game can be played with different sets of rules. And I'm not really convinced by the scoreboard class. It simply doesn't do much. Maybe call it GameState because it's basically bundling all the stateful stuff of your Game class. That way it becomes clearer 😉
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Good suggestions, thank you Marco! I might revisit this code for a do over and also introduce a GUI.
@marcotroster8247
@marcotroster8247 2 жыл бұрын
@@ArjanCodes You're welcome. All honor belongs to you because you taught me how to design software 😂 Nice to hear that you consider putting a GUI. I'm already excited!
@smalltimer666
@smalltimer666 Жыл бұрын
@@ArjanCodes YES!
@МихаилТерентьев-ц6в
@МихаилТерентьев-ц6в 2 жыл бұрын
Could you please recommend some free exercises resources covering that topic? (I mean refactoring and optimization of code) I just don't want to lose and forget all such nice techniques 10 minutes after watching your videos. Thanks
@MrAndyspickle
@MrAndyspickle 2 жыл бұрын
Hey Arjan, I was just wondering what some of your keyboard shortcuts are. Things like deleting all right with in the brackets?
@johnsonshek
@johnsonshek 2 жыл бұрын
I think Arjan uses vi mode, and then can delete using "di(" or "ci(" for change
@Shr11mp
@Shr11mp 2 жыл бұрын
I LOVE this series!
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Thank you, Ryan, glad to hear you like it.
@smalltimer666
@smalltimer666 Жыл бұрын
Hi, any ideas on how one can implement this with multiple players ? Especially if the players connect to the game from different terminals etc. ?
@gehbittedasgibtsned
@gehbittedasgibtsned 2 жыл бұрын
nice work, please add unit tests
@kelthazur
@kelthazur 2 жыл бұрын
Regarding the Enumeration at the end. Why not stick to auto() and refer to the enum.name.lower()?
@claudiafischering901
@claudiafischering901 2 жыл бұрын
Thanks for part 2. My solution for the if elif else problem is following: [...] winner, message = get_winner(player_entity, cpu_entity) if not winner: self.ui.display_tie() else: winner_entity, winner_name = [player_entity, self.player_name] if winner == player_entity else [cpu_entity, self.cpu_name] self.ui.display_round_winner(winner_name, winner_entity, message) self.scoreboard.win_round(winner_name) [...] It is short and I think readable enough or what do you think about it? I like this series. I am happy if it continues^^
@claudiafischering901
@claudiafischering901 2 жыл бұрын
Video Position was on 24:40 - 24:55. I missed too add these. sorry
@jeancerrien3016
@jeancerrien3016 2 жыл бұрын
The code @14:36 surprised me, line 4 in particular. Is the type hint really meaningful? If you changed user_name in line 2 to be an int, would something complain?
@metal571
@metal571 2 жыл бұрын
Only if using the mypy linter which I don't think he is in this video. Could be a good video topic all on its own
@sami3592
@sami3592 2 жыл бұрын
Thank you sir
@ch4sethe5un
@ch4sethe5un 2 жыл бұрын
Arjan, could you do a video on page object models (POM) in python?
@ackrman
@ackrman 2 жыл бұрын
Great content as always. I have a question. TL;DR VERSION - Need help in creating an api where uploads and downloads can be done via streaming mechanism for huge data sets for thousands of concurrent users. I have an application where people download and upload stuff But the issue is many people are hitting downloads and uploads and due to this 504 is becoming a common occurrence on the server. I tried increasing the specs of server but it didn't helped me much. Can you advise me how can I achieve streaming of data for downloading and uploading? So many people can do this concurrently and at the same time this operation won't horde memory. By the way I have huge datasets and I can't keep them in memory so need to do in chunks.
@Cookie-mv2hg
@Cookie-mv2hg 2 жыл бұрын
How can we submit codes for you to 'Roast'? I self-taught and made my very first larger game using pygame with the puzzle "untouchable"....annnd I think is quite a mess. I'd like to refactor the code with my own but I'd also like a review from professionals. Is there any chances to?
@ArjanCodes
@ArjanCodes 2 жыл бұрын
You can submit your code for a roast on my Discord channel (there’s a dedicated channel for this). The invitation to join is in the description of the video.
@Cookie-mv2hg
@Cookie-mv2hg 2 жыл бұрын
@@ArjanCodes Much appreciated!
@juanbetancourt5106
@juanbetancourt5106 2 жыл бұрын
This cut down the elif portion at get_winner() ordered_tuple = tuple(set((entity1, entity2))) if ordered_tuple in RULES: return (ordered_tuple[0], "some message")
@harshraj22_
@harshraj22_ 2 жыл бұрын
get_winner function at 7:56 isn't too readable anymore. any thoughts on this ?
@sthiag0
@sthiag0 2 жыл бұрын
"remove documentation because it's taking a lot of space" WRITE THAT DOWN WRITE THAT DOWN!
@AMIRKIZER
@AMIRKIZER 2 жыл бұрын
Any practical tips for starters?
@jereziah
@jereziah 2 жыл бұрын
what's the shortcut he was/is using for the renames? circa 11m15s
@ArjanCodes
@ArjanCodes 2 жыл бұрын
I use F2 to change a variable name. It also takes care of name changes across multiple files.
@Klej0aka0Klej
@Klej0aka0Klej 2 жыл бұрын
What's the shortcut in VSC to change names so easly? Just like in 11:05?
@_JNotJay
@_JNotJay 2 жыл бұрын
F2 to rename a variable. Works across multiples files too!
@padreigh
@padreigh 2 жыл бұрын
enumerate(Entity, 1) - will start at 1
@ArjanCodes
@ArjanCodes 2 жыл бұрын
Didn’t know that, thanks!
@endredimarci
@endredimarci 2 жыл бұрын
How do I submit code for this series? I'd love to be roasted and some professional input would be greatly appreciated :D
@brunosompreee
@brunosompreee 2 жыл бұрын
F2 or right click > rename symbol
@ArjanCodes
@ArjanCodes 2 жыл бұрын
If you join my Discord server (the invite is in the description of the video), there’s a channel where you can submit your code.
@alexandarjelenic7718
@alexandarjelenic7718 2 жыл бұрын
6:10 who thought this was funny? I did, even though it might not have been intended to be a joke
@coldhardwick
@coldhardwick 2 жыл бұрын
I can't stand the Star Trek episodes with Q. Anything with Q is just annoying. Like one long deus ex machina...
@dpgwalter
@dpgwalter 2 жыл бұрын
I don't think removing redundant comments is a useful thing. A lot of times having the documentation helps build a mental idea of the function, even if it does something very simple. If anything, comments should rather be simplified than completely deleted.
@aflous
@aflous Жыл бұрын
You almost never need comments
@corlevtec
@corlevtec 2 жыл бұрын
for the last refactor... if not winner: self.ui.display_tie() else: self.ui.display_round_winner(self.player_name if winner == player_entity else self.cpu_name, winner, message) self.scoreboard.win_round(self.player_name if winner == player_entity else self.cpu_name)
@Gregplex
@Gregplex 2 жыл бұрын
actually just else: self.ui.display_round_winner(winner, message) self.scoreboard.win_round(winner) I feel you could even pass None to scoreboard.win_round and ui.display_round_winner and not have any ifs in the play method.
@romulopontual6254
@romulopontual6254 Жыл бұрын
This reduces the code redundancy... I am not sure it is clearer! winner, message = get_winner(player_entity, cpu_entity) if not winner: self.ui.display_tie() winner_name = self.player_name if winner == player_entity else self.cpu_name self.ui.display_round_winner(winner_name, winner, message) self.scoreboard.win_round(winner_name)
Step-By-Step Chess Game Refactoring | Code Roast
32:55
ArjanCodes
Рет қаралды 49 М.
Refactoring A Tower Defense Game In Python // CODE ROAST
36:49
ArjanCodes
Рет қаралды 257 М.
🕊️Valera🕊️
00:34
DO$HIK
Рет қаралды 3,8 МЛН
Watermelon magic box! #shorts by Leisi Crazy
00:20
Leisi Crazy
Рет қаралды 109 МЛН
Зу-зу Күлпаш 2. Бригадир.
43:03
ASTANATV Movie
Рет қаралды 676 М.
How I animate 3Blue1Brown | A Manim demo with Ben Sparks
53:41
3Blue1Brown
Рет қаралды 418 М.
How To Reduce Coupling With Facade | Design Pattern Tutorial
28:29
Protocol Or ABC In Python - When to Use Which One?
23:45
ArjanCodes
Рет қаралды 203 М.
5 Tips To Achieve Low Coupling In Your Python Code
18:30
ArjanCodes
Рет қаралды 97 М.
The Art of Code - Dylan Beattie
1:00:49
NDC Conferences
Рет қаралды 4,7 МЛН
A Simple & Effective Way To Improve Python Class Performance
12:40
The Worst Programming Language Ever - Mark Rendle - NDC Oslo 2021
1:00:41
NDC Conferences
Рет қаралды 1,3 МЛН
Why the Plugin Architecture Gives You CRAZY Flexibility
24:06
ArjanCodes
Рет қаралды 113 М.
The biggest JETS from a BLACK HOLE ever found
16:03
Dr. Becky
Рет қаралды 103 М.
You Can Do Really Cool Things With Functions In Python
19:47
ArjanCodes
Рет қаралды 223 М.
🕊️Valera🕊️
00:34
DO$HIK
Рет қаралды 3,8 МЛН