BlackJack method
im a beginner and i just learned about methods. i was wondering is this too many methods?
https://paste.mod.gg/jsggvgkilcew/0
BlazeBin - jsggvgkilcew
A tool for sharing your source code with the world!
42 Replies
your Main is about80 lines long. You dont have enough methods
i dont see where i can extract more methods, besides from 1 or 2 places max
potentially there is a lot you can do
like separating interface (console) from business logic (the card game)
in a different class or method?
classes
ill try ty
also any other tips are greatly appreciated
these can easily be extracted.
It can be taken much further as well. Basically big
if
blocks are obvious candidatesalso in general every state could be a class in itself
i cant convert if statements into classes with ctrl + .
idk if doing it manually would fix it
also can i use methods in methods?
@dont
you can use methods in methods but i wouldn't advice it at this time
the point of extracting methods and classes is isolating concepts (and responsibilities (and states))
i don't know if it's too abstract
probably it is
so id need to unextract my other methods and then extract a bigger method?
maybe we could try with an example
i hope it's alright
let's pick the random generator for example
it's fine, it works, BUT
what if let's say you have a crash somewhere at a certain point and you don't know why
you get what sequence of cards would break the program, so you have to 'inject' that fixed sequence
how do you do it?
you isolate the random generator in advance so that you can use something else
or you can plug the random generator again to play normally
or maybe you plug another client which is played by another human
yeah that makes sense
i extracted the red box
but cant the blue
/purple
i cant "ctrl + ." it
wait, let's make another example, the strings
you have strings because
1) you are using a console
2) they're only english
what if there was a language selection
you have to put the strings somewhere so that you can pass an option to select the language
so you would have a layer of abstraction between the strings and their meaning
obvisouly if you are never going to do it probably you wouldn't care
but just to give an example
this is one of the most common things to work on
do you expect an answer from me?
well this is your program, like you can do what you want
i probably would expect code...?
another not so brilliant thing i wouldn't use is the
while (true)
that i would changei use that every time for when i want to make a program repeat itself
like play as many times as you want
although i couldnt figure out how to make it so the player CHOOSES whether to play or not
i was thinking of making a bool
and making something like while (wantsToPlayAgain = true)
idk if thats the right syntax for a bool tho
yeah but reading that i wouldn't know how it exits
i would need to search for the break in the code
for example
also you could have forgot to write the break
again i would have to search for it
so what would i need to replace the while true with?
you should make a clean exit from inside there
this could mean refactoring the flow of the program
it only exits when you run out of money
otherwise you play again infinitely
so obviosuly
while(HasMoney())
or similarsomething that gives the reader the intent
it's the same for the extraction of the methods in the colore squares
but my main objective is to give the player the choice to continue or not, running out of money is just a side scenario
take the red for example, the first
i need to read the code to understand what it does
do i need to? not really
then add that in...
while 'not (out of money or player quits)'
BlazeBin - jsggvgkilcew
A tool for sharing your source code with the world!
and what this would translate in?
a state of the game
and a method that checks whether this action is allowed by the state
i just did it
mmmm this seems to need to be considered too
if (bet <= playerMoney)
is there a minimal bet? because if it does then it doesn't make sense to get there if the player is low on moneyyeah there isnt
but thats easy to implement
i think
done lol
i tweaked it so it gives a different error when you dont have enough money to bet and when your bet isnt valid at all
like its -50 or 0
what about giving random numbers in the code a name
put them in constants somewhere
if youre talking about the mins and maxes in the generators, i think its more than obvious that they are for cards, since the program is blackjack
i can do that though
Kristijan Kralj
MethodPoet
Do Magic Numbers Hurt Your Code?
Magic numbers are often seen as bad in the programming community. Why is this? Let's explore the reasoning behind it.
thank you, ill fix them tomorrow
you dont need
outOfMoney == true
, could just be outOfMoney
Great case for a feature release of variable pattern matches. If only 🥺
wdym?
could be