What's advantages of arguments?

When I learned arguments, I somewhat understand about concept but didn't understand "Why should I use arguments? So I organized my thought about advantages of arguments.


Advantages of arguments

By using arguments, programmers are able to re-use same code with different variables.


Example

Let's code the introduction of five siblings.


Using arguments

Same logic in the red area are used by different data. So programmers doesn't need to write similar code to write down introduction code.



// Main.java
public class Main{
    public static void main(String[] args) {
    
        Hello("Anna", 30);
        Hello("Edwin",25);
        Hello("Oliver", 20);
        Hello("Evelyn", 15);
        Hello("Emilio", 10);
    }
    
    public static void Hello(String name, int age){
        System.out.println("I'm" + " " + name + "." + " " + "I'm" + " " + age + "years old.");
    }
}


Without using arguments

So what's happen if programmers doesn't use arguments and write same code? Programmers are actually able to write the code. but it become so long.



// Main.java
public class Main{
    public static void main(String[] args) {

        Hello1();
        Hello2();
        Hello3();
        Hello4();
        Hello5();
    }

    public static void Hello1(){
        String name = "Anna";
        int age = 30;
        System.out.println("I'm" + " " + name + "." + " " + "I'm" + " " + age + "years old.");
    }
    
    public static void Hello2(){
        String name = "Edwin";
        int age = 25;
        System.out.println("I'm" + " " + name + "." + " " + "I'm" + " " + age + "years old.");
    }
    
    public static void Hello3(){
        String name = "Oliver";
        int age = 20;
        System.out.println("I'm" + " " + name + "." + " " + "I'm" + " " + age + "years old.");
    }
    
    public static void Hello4(){
        String name = "Evelyn";
        int age = 15;
        System.out.println("I'm" + " " + name + "." + " " + "I'm" + " " + age + "years old.");
    }
    
    public static void Hello5(){
        String name = "Emilio";
        int age = 10;
        System.out.println("I'm" + " " + name + "." + " " + "I'm" + " " + age + "years old.");
    }
}


This concept is useful when..

So how do we think this is useful? programmers actually can write same code both way. As beginner including me, it's hard to imagine how this concept is useful.

But wait, we can imagine how it's useful. for example, if we will work at Facebook or twitter or any services that has billions users? Engineers might edit billions of people's name or any profile info. Then this concept will be useful.

As Java language is good at large scale development, it might be useful to think "what if this concept is very useful when we are involving large scale development?".

Related posts

関連トピック