r/learnjava • u/imgonnabethebest • Nov 21 '14
learning about methods, question
it's not covered in the text so i figured i'd ask. so basically methods = a way to make your text nice and clean and readable. so if u did something like checking if a number is like odd or even or something that involves if statements or else statements, you put it in a method then call on that so your "main method (am i using the correct terminology)" is readable and cool.
also, do all methods u make need to be after the main method? can it be before the main method like you have something called checkEvenOrOdd() before the main method, then in the main method u call on that. is that wrong ? thx bros
0
Upvotes
1
u/get_username Nov 21 '14 edited Nov 21 '14
Yes.
But there is more power than just readability here.
There are two misunderstood powers of methods that aren't used often enough:
Abstraction - expressing your code as ideas
Encapsulation - insulating your code from changes made to other parts
I am going to take a simple function now. Using abstraction I will express it as ideas (to show you how much easier it is to understand). Then using encapsulation I will play with it until it resembles something kind of crazy.
Follow me down the rabbit hole now.
Disclaimer: If you become confused, that is fine. Stop reading whenever you'd like. I go overboard at the end :)
Let me give you an example: we have some method
sqrtthat calculates the square root of a given number.This isn't so bad... maybe you know what its doing, maybe you don't.
If you don't it can be confusing:
Why are we using
guess * guess - n > 0.0001as you while loop check?Why are we using
(guess * guess + x ) / (2 * guess)in the while loop?The problem is I've been very descriptive in the method. The process is very clear (to a computer), but the ideas of the method don't really come through. Why am I doing these things?
Lets "push" them into a couple methods:
Now we've created these sub (private) methods. They can be called at any time. They represent the ideas of checking if a number is good enough, and updating a guess.
Putting them into our previous method makes things more transparent.
We can see how the
sqrtmethod is actually functioning. It has become a member of the realm of ideas now.This is what I mean by abstraction. You can make your intention alot clearer.
By making your intention in your code clearer it is easier for you and others to analyze if you're performing the correct actions.
If an error occurs you can isolate it down to either
sqrt,notGoodEnough, orupdateGuess.Isolating the problems like this helps everyone involved craft better code.
Because I isolated the methods, I don't even need to tell you what they are. You just know the ideas of these methods:
notGoodEnough- checks if a number is not good enoughupdateGuess- updates our guessSo for the next section I will simply speak about single methods at any given time (because now you're more experienced in abstraction).
Encapsulation:
Now that its abstracted, lets play around with the ideas of the method. We will see how encapsulation has allowed us to play with the ideas without effecting the logic.
Previous I used abstraction which also encapsulated the
notGoodEnoughandupdateGuessmethods from change. When we change the code forsqrtnow, we will have no effect on the other two methods!Now lets really play with it and see what we can do:
I want to change the sqrt method. Well lets just assume we have a method named
sqrtUpdateAndCheck. Performing that method would perform a single iteration of the update and check. We would want to call that method.Okay. Now last time I had to set a variable.. well this time I am just going to use a "helper" method and pass the guess through it. So I call the method I want. What method do I call? one I haven't created yet.. but I will now:
How do I find the sqrt? Well I can do my while loop before, but I named the method to imply it is a single run.. so lets do a single run:
So we are using ideas again to express what we want. In the end we want to return a result.. we know that if we hit a return statement that terminates the method.. so that last else is redundant
Now.. when its not good enough I need to return the update and check.
If only I had a method that could update and check the sqrt..
oh wait.. I do.. THE METHOD I AM WRITING.
In a bold move called recursion, we call ourselves:
Now since the methods
notGoodEnoughandupdateGuesswere encapsulated, the previous changes do not effect them. They run exactly like they did before.Even though we've changed the code drastically.. it should still work as expected!
Really going off the deep end:
Well.. now I noticed we can play more with this method. Lets just do it and see what we get.. for fun.
Notice how we are calling
updateGuesseverytime we enter the method. That tells me we can push it into the call before.. like so:But now I notice something too. We only have a single if, with two conditions that execute in different ways. Perhaps we could in line this with ternary:
Oh my.. we've created some kind of Frankensteinian monster. Lets use abstraction and GO DEEPER!!!
Really REALLY going off the deep end:
Lets use abstraction again to push the original into the state of the class. Then I set all the members to non-static so that they can access this state:
The whole point here was just to show you how you can use abstraction and encapsulation to play with how methods work. To really play with the ideas of a method on a high level.
I really like how it ended up though. The
updateSqrtmethod almost reads like a sentence (if you read:as "otherwise")notGoodEnough(guess) ? updateSqrt(updateGuess(guess)) : guess;Is the guess not good enough? update the sqrt with the updated guess, otherwise its your guess