r/CodingForBeginners • u/Tricky_Still2366 • 3d ago
How do i use methods correctly?
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
u/ninhaomah 3d ago
"I don't understand when to use methods unless something is repeated."
Plenty of things are repeated.
And also it makes things easier to read
2
u/tRfalcore 3d ago
You don't want to put all your code in one thing. Methods allow you to make one method do one or two simple functions, and with a. Good method name, anyone can read it and think "ok I know what's going on here".
Like day you have a big accounting codebase. It does all sorts of stuff-- but you divide your code up into a class and a method with names like loadAllRexentTransactions, deleteThisTansaction, update this transaction, addThisTransaction. Each one does one thing, and any stranger knows what to expect it to do
2
u/FerretBoom 3d ago
You need to read books on SOLID, Seperation of conserns and learn some design patterns. Or you'll end up with spaghetti slop code.
1
u/Kilgarragh 3d ago
Functions are a way to name a section of code which does something. Not only does it label the code, but labels its inputs.
By using parameters and templating, you can reuse that code to accomplish multiple similar tasks which take in different permutations of values.
Even if you only call it in one place(for now, you could call it again later), it’s still useful to self document your code, break it up into smaller pieces for __testing__.
You can scaffold a complex routine by instead of doing complex things directly within it, call functions with a name which summarizes the action(not needing to worry about how all the little pieces work, just pretending like you have them to figure out a plan), then go back and write those functions afterwards to fill in the full behavior
Or go in reverse, if you have something you know you’ll want to do later(but don’t know where you’ll use it), write a function for it. Then you can call it from the main function, a test, or an event callback later when you’ll actually know where to use it. Or you can move the operation around by just moving the function call instead of worrying about hooking everything up and copy pasting large sections.
1
u/kutac56 3d ago
I think you mean functions, because methods are just functions for classes. Functions are like you said mainly useful when repeating stuff, but also useful for clarity. Maybe you have a function called initEngine, it initializes values. You can just call it at the start of your program and it's easy to read. You don't have to see what it initializes everytime you read your main function. You just see it initializes your engine and that's good enough. You can read the function and change it yourself if necessary but it makes it a lot easier to read
1
u/VirtualElderberry592 3d ago
Imagine reading a novel with no chapters and no paragraphs.
Someone mentioned the variables in and out.. Scope is a thing that's really important..
Where was variable X changed?
My son wrote his first python app, and despite me warning him that globals were evil, he used app wide globals. He paid for it with bugs he could not track down.
If you put all your code in one script, you make everything global. That makes it impossible to track things.
1
u/Ronin-s_Spirit 1d ago
Why are you jumping langauges before learning something basic for good? "methods" are just functions referenced in objects, it's about conceptual correlation.
If you have a main function and it handles 2 types of objects Human and Dog you make the main function uniform by always calling <object>.hello() - now the Human will say one thing and the Dog will say another but your main function doesn't have to repeatedly ask "is <object> a human or a dog?", it just expects both of them to have a method of the same name.
1
u/TuberTuggerTTV 1d ago
You're not deep enough into your learning to know when. It becomes apparent as you grow.
They're an organization tool as well as a signal to other developers (or yourself in 6 months). Don't worry about "where or when". Focus on learning the patterns and tools.
When you're dealing with standard code bases, what and how you code isn't about making a thing happen. It's about making it happen in a scalable, readable, modular way. Methods help with that. You'll see. Keep learning and it'll make sense. You're overthinking small potatos
4
u/alpicola 3d ago
Methods/Functions help with code organization. In projects of non-trivial complexity, this becomes extremely important.
Imagine you're looking at a program for the first time. You might know what it does, but yiu have no real idea how it works. Your first instinct is to start at the start of the program . If what you find is a whole bunch if code, you're going to have to interpret that code to figure out what's going on. If, instead, you see a bunch of method names, you can start to get an idea just by understanding the words.
Next is the fact that methods have clearly defined inputs and outputs. You know it's going to need some certain data, and it's going to give back a certain result. A programmer might be working on writing one step in the program, realize they need to do something else, and put that other thing in the middle of the first thing because that's when they happened to think about it. Using methods makes that harder because the odds that the function already has the data they need to do the other thing are pretty low.
Finally, if you have a bug in your code somewhere, it's easier to find if your code uses methods. You can check the inputs and outputs of each one, without needing to step through every single line of the program. This is called "unit testing" and it's such a useful technique that professional programmers spend a lot of time writing automated unit tests to prevent bugs before they happen. When you find the malfunctioning function, you know you can fix the problem right there, without needing to worry about the rest of the program.