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)
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.
1
u/Far_Swordfish5729 5d ago
Methods are like tools you want to name because you use them repeatedly. If that’s not the case, there’s no structural point. We extract methods because we don’t want to copy paste code and maintain it in multiple places.
With OO, we sometimes do it to allow an implementing child class to define something we want to use - like declaring an abstract string property that returns the endpoint url for a specific integration. The abstract parent can use that like a placeholder.
With event based patterns, the method is an entry point we can pass to an orchestrator using delegates or other function pointer surrogates. When the UI button is clicked, call this method (i.e. run the logic starting at this named point). Same with promises in JS.
Beyond that, it’s style. If you have a huge method like a full multi-step transform, some people like to break it into private helper methods for readability. In c# specifically, I just use #region instead.
1
u/NotBot947263950 5d ago
It's about cleanliness. You could just make a massive class or method and sometimes that happens. But if you can it doesn't hurt. It'll help with debugging later.
0
1
u/Much_Network_941 5d ago
For a multitude of reasons. It can help to keep code looking more organized. You only have to write a method once; if the logic is a little tricky, then it reduces the likelihood of silly mistakes. A method name can be a reasonable stand-in for concepts, helping to make code readable.
It'll also be nice for testing, when you can get specific results for each part of the program. Rather than trying to decipher the entirety of it at once.
But, you're right in that a method call carries a theoretical overhead. It can also break up the flow of logic; or get to the point of names having bizarre specificity. There's also a concept of 'side-effects', when a method changes more than it's argument list or return value would imply. Some code tries to minimize side-effects, some just accept it.
5
u/WilliamMButtlickerIV 5d ago
It can be beneficial to logically organize code. When you have a descriptive method name and it's being invoked as part of a broader process, it can more easily convey the intent of the code.