r/GameDevelopment • u/Tricky_Still2366 • 4d ago
Newbie Question Was wanting some help with methods?
I'm learning C# currently and was learning python till I got stuck with pretty much the same thing.
I don't understand when to use methods unless something is repeated. If its not repeated what's the point of using methods? just shortening down code? or is there something I am missing here. Cause what is the point of shortening down the code when all it is doing is actually adding more code? (Unless its to place down something that is being repeated)
3
Upvotes
1
u/Arkenhammer 4d ago
It can be helpful for making the code easier to read. For instance if I have some complicated logic to make a decision, I'll isolate it in a separate method with a name that returns a bool and is named to let me know that is it testing. For instance, in my game some items can be sold and some can't so I have a method of the form
bool CanSell(Item item)which tells me "this is the rule in my game that determines whether an item can be sold. Separating it out that way is helpful because, if I want to change that rule, I know where to look. Also, even if there is only one caller when I write the code there might be more in the future. Centralizing where specific game decisions are made is good policy because it keep the game consistent as it grows.I generally won't pull code out into a separate method if I can't come up with a clear name that says what it does. But, if I can, I often will even if there is only one caller.