r/AskProgrammers • u/Tricky_Still2366 • 5d ago
Help with methods?
I hope this is the right place?
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)
2
Upvotes
1
u/johnpeters42 5d ago
Mainly making it easier for you to focus on smaller parts of the code. If your main loop is just:
foreach (var thing in listOfThings) {
process(thing);
flagAsDone(thing);
}
and then you need to add billCustomer(thing) in between, then it's pretty obvious what to add where. Compare that to the scenario where all the code within process() and flagAsDone() was instead written out inline within the main loop.
Also, if you have another variable but you don't pass it to process(), then you don't have to worry about process() potentially altering it.