C
C#15mo ago
vl4do

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
phaseshift
phaseshift15mo ago
your Main is about80 lines long. You dont have enough methods
vl4do
vl4do15mo ago
i dont see where i can extract more methods, besides from 1 or 2 places max
boiled goose
boiled goose15mo ago
potentially there is a lot you can do like separating interface (console) from business logic (the card game)
vl4do
vl4do15mo ago
in a different class or method?
boiled goose
boiled goose15mo ago
classes
vl4do
vl4do15mo ago
ill try ty also any other tips are greatly appreciated
phaseshift
phaseshift15mo ago
these can easily be extracted.
phaseshift
phaseshift15mo ago
It can be taken much further as well. Basically big if blocks are obvious candidates
boiled goose
boiled goose15mo ago
also in general every state could be a class in itself
vl4do
vl4do15mo ago
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
boiled goose
boiled goose15mo ago
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
vl4do
vl4do15mo ago
so id need to unextract my other methods and then extract a bigger method?
boiled goose
boiled goose15mo ago
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
vl4do
vl4do15mo ago
yeah that makes sense i extracted the red box but cant the blue /purple i cant "ctrl + ." it
boiled goose
boiled goose15mo ago
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
vl4do
vl4do15mo ago
do you expect an answer from me?
boiled goose
boiled goose15mo ago
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 change
vl4do
vl4do15mo ago
i 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
boiled goose
boiled goose15mo ago
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
vl4do
vl4do15mo ago
so what would i need to replace the while true with?
boiled goose
boiled goose15mo ago
you should make a clean exit from inside there this could mean refactoring the flow of the program
vl4do
vl4do15mo ago
it only exits when you run out of money otherwise you play again infinitely
phaseshift
phaseshift15mo ago
so obviosuly while(HasMoney()) or similar
boiled goose
boiled goose15mo ago
something that gives the reader the intent it's the same for the extraction of the methods in the colore squares
vl4do
vl4do15mo ago
but my main objective is to give the player the choice to continue or not, running out of money is just a side scenario
boiled goose
boiled goose15mo ago
take the red for example, the first i need to read the code to understand what it does do i need to? not really
phaseshift
phaseshift15mo ago
then add that in... while 'not (out of money or player quits)'
vl4do
vl4do15mo ago
BlazeBin - jsggvgkilcew
A tool for sharing your source code with the world!
boiled goose
boiled goose15mo ago
and what this would translate in? a state of the game and a method that checks whether this action is allowed by the state
vl4do
vl4do15mo ago
i just did it
boiled goose
boiled goose15mo ago
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 money
vl4do
vl4do15mo ago
yeah there isnt but thats easy to implement i think
vl4do
vl4do15mo ago
vl4do
vl4do15mo ago
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
boiled goose
boiled goose15mo ago
what about giving random numbers in the code a name put them in constants somewhere
vl4do
vl4do15mo ago
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
boiled goose
boiled goose15mo ago
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.
vl4do
vl4do15mo ago
thank you, ill fix them tomorrow
Haze.
Haze.15mo ago
you dont need outOfMoney == true, could just be outOfMoney
Landan
Landan14mo ago
Great case for a feature release of variable pattern matches. If only 🥺
vl4do
vl4do14mo ago
wdym?
Landan
Landan14mo ago
switch(variableToCheck)
{
case {Card1: "A", Card2:"F"} or {Card1: "F", Card2: "A"}: break;
default: //check other scenarios...
break;
}
switch(variableToCheck)
{
case {Card1: "A", Card2:"F"} or {Card1: "F", Card2: "A"}: break;
default: //check other scenarios...
break;
}
could be
case Pattern<TypeArg> BlackJackCase { Card1: "A", Card2: "F" } or {Card1: "F", Card2: "A"};

switch(variableToCheck)
{
case BlackJackCase: break;
default : break;
}
case Pattern<TypeArg> BlackJackCase { Card1: "A", Card2: "F" } or {Card1: "F", Card2: "A"};

switch(variableToCheck)
{
case BlackJackCase: break;
default : break;
}