What parts of these should i put in functions?
Im a beginner so i dont know where to begin
45 Replies
you should break code into functions based on some logical task that is getting done
for example, attacking or defending could be an Attack or Defend function
they could make a single class called Human that has the stats defined as properties, and create 2 instances of that class for player and enemy
follow the DRY principle
put all defense logic in a function? some of the attacking code needs the defense code
could you show an example
First, do u know how to make a class?
no
There are 2 easy rules:
1. Group related data into classes or structs
2. Make functions out of repeating code
Is that a school/uni project?
no
But im planning to add more to it
Also, send data as parameters, don't use globals
first you should learn what classes are
Then there are more considerations:
1. Conditions or checks getting too involved or too nested -> pull out a function
2. Same code done on multiple pieces of data -> use an array and a loop
3. Higher and lower level operations mixed up in a single function -> pull out more functions so it's easier to reason about
These are the basic things you should master
dont think fortnite that has 100 players declare their variables like this
they use the power of object orientation, and C# is an object oriented level
Note: don't actually get too deep into OOP as a beginner, it's going to be overwhelming unless you know the basics I mentioned above
classes atleast should be beginner friendly
its not like theyre gonna use interface or abstract classes
Well that's how you structure data
public fields
As a beginner, that's pretty much what you should go for by default
you could start checking out the links below $helloworld to start learning the basics of C#
I did in codeacademy
it didnt teach you what
class
is?its teaching abstraction next
without teaching what a class is first?
learn that
it's like one of the most important things
what an algorithm is
and what an abstraction is
ngl I dont really understand algorithims at all
they're related in many ways
yes
algorithm means input data, steps to transform it, and output data
abstraction basically means giving names to a group of operations or data
a variable is an abstraction for accessing a piece of memory
it has a name, and it lets you do something indirectly — interact with computer memory through the name
it seems to be much more complex that that
it is just this
interface + implementation = algorithm
that's me rephrasing the same sentence
people may start talking about classes and OOP when they hear "abstraction"
but classes are not required for it
so its just making steps to solve a problem and transfering the steps to code?
That's called solving a problem by writing a program. Algorithm is more or less the product of this
It's a bit more general
Like, it doesn't have to exist in code
It's more the idea of a sequence of steps than a concrete program
so an algorithm is basically just the steps
im going to put teh attack and defend in there own functions but i feel like its going to cause errors
how so?
this code attacks in else if because I gave them a defense cooldown so if it it selects defense again it will still attack so should i still put that in the defense fuction?
then do I put the player attack and the enemy attack in the same function?
how will the ennemy defend when its in antoher function and idk if the turns will work
honestly I have no idea what you said
you need to structure your code in a way that the information the function needs is accessible
Yes, start from pulling every variable that represents a piece of game state into a single class, this sounds fair
public fields
your functions can take in the game state as a parameter
I dont know how to do that
that's part of software development, taking a problem and turning it into smaller pieces that can connect together
you just have to practice it
so how do you do that
you see related data, you group it.
you see code that repeats, you make a function that takes in what it needs to do the thing.
there's more, like recognizing when an abstraction does multiple separate things and restructuring it into multiple abstractions that communicate
but start from those two
ok im going to tdo that and see if i get errors
Ok when do you use classes
whenever you have data and behavior that can be meaningfully treated as one "thing"
when you see related data
breaks code since i dont really know how to implement classes
though i know how to make them
in main you now do
and replace playerHealth for state.PlayerHealth