DEV Community

Cover image for Methods for Grandmas
coding4grandmas
coding4grandmas

Posted on • Updated on

Methods for Grandmas

Today we will be covering a very interesting topic - methods.

Methods primarily achieve two things:

  1. Allows the code to be reusable
  2. Keeps your code organised

Implementing methods achieves a very important objective - it keeps your code DRY (No grandma, we are not suggesting that you drink less water). DRY stands for Don't Repeat Yourself. In programming, you should always seek to keep your code simple, elegant and easy to understand, hence the need to apply the DRY principle. When possible the programmer should always aim to avoid re-writing code.

Methods allows you to be much more efficient with your programming. Instead of writing the same lines of code a number of times, you can implement a method that will execute a pre-defined code.

For example:

def my_name_is(name)
    puts "Hello my #{name} is!"
end
Enter fullscreen mode Exit fullscreen mode

By implementing this simple method, we can call it when we need it anywhere in our code. Thus we are following the DRY principle and minimising unnecessary lines of code in our program.

Now, let's break it down how a method actually works.
A method is written like this.

def method_name(argument(s))
 executable code 
end 
Enter fullscreen mode Exit fullscreen mode

Three points to consider:

Method Names

Similarly, to variables method can be named. We do advise naming the methods in easily understood names that clarify what that method is supposed to do.

Argument

Next is the argument(s). When you want to use a method, most of the time, you pass an argument. In the previous example, we passed a name. What is being passed can be various data types (strings, integers, arrays). You can even pass more than one argument - the possibilities are endless (until your program crashes).

Executable Code

Lastly, this is where the magic happens. As a programmer, you can implement anything you like. Do you want the method to calculate the square area of the house or convert a string to an integer or maybe calculate the age of you grandma. It can be done.

Let's implement an example to understand how a method works fully. For the example below, we are looking to calculate the age of our grandma.

grandma_birth_year = 1930

def calculate_age(year)
    current_age = 2021 - year 
    puts "Grandma is #{current_age} years old"
end

calculate_age(grandma_birth_year)
# => 91
Enter fullscreen mode Exit fullscreen mode

It might seem like there is a lot, but it is not - let's break it down by line.

  1. We assign a birth year which is 1930.
grandma_birth_year = 1930
Enter fullscreen mode Exit fullscreen mode
  1. We create a method which accepts a year. Year in the parenthesis is a parameter which receives an argument, when the method is called.
 def calculate_age(year)
Enter fullscreen mode Exit fullscreen mode
  1. We implement our logic. We take the current year minus the year we passed as an argument (1930).
current_age = 2021 - year 
Enter fullscreen mode Exit fullscreen mode
  1. We write some logic which will print out the age of our grandma
puts "Grandma is #{current_age} years old."

Enter fullscreen mode Exit fullscreen mode
  1. The last part we have not covered, which is calling the method.
calculate_age(grandma_birth_year)
Enter fullscreen mode Exit fullscreen mode

When the method is called, it tells the computer not to run any other lines and go to the called method and execute the code inside that method. Once the code has been executed, Ruby resumes from where the method was called.

The outcome, we get is a print statement
"Grandma is 91 years old."

Before we wrap up this blog, it is important to touch on parameters and arguments.
In the example above, the parameter was (year). The parameter is what the method accepts. A point to note the parameter could be named anything we want (however, we also advise following a good naming convention).
The argument in the above is (grandma_birth_year). The code calls the method, and the argument is sent to the parameter in the method.

Summary

What's been covered is very simple in nature (hopefully our grandmas think the same). One thing to consider is that, once the program becomes complex as more logic is implemented, employing methods will achieve two things:

  1. The code will be reusable
  2. The code will be organised

This ties up very well with the DRY principle. The concept is to minimise how many lines are written and not to repeat your self - hence, using method statement achieves both. In the professional environment, methods are used often and we believe every programmer should have a good mastery over them.

Oldest comments (0)