W
W
WannaCreative2016-07-17 16:18:10
Java
WannaCreative, 2016-07-17 16:18:10

Why write such a big code?

Hello everyone, dear friends!
Here I am slowly learning Java, and then one question arose.
Why write other methods if this results in longer code?
What are the differences, and is it still necessary to write with methods?

public class Main
{
    public static void main(String[] args) throws IOException{
        int[] array = new int[5];
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        for (int i = 0; i < array.length; i++){
            String s = reader.readLine();
            int value = Integer.parseInt(s);
            array[i] = value;
        }
        InitializeArray(array);
    }
    public static int[] InitializeArray(int[] array){
        int max = array[0];
        int min = array[0];
        for (int i = 0; i < array.length; i++){
            if (max < array[i])
                max = array[i];
            if (min > array[i])
                min = array[i];
        }
        WriteLine(min, max);
        return array;
    }
    private static void WriteLine(int minimum, int maximum){
        System.out.println("Minumum is: "+minimum);
        System.out.println("Maximum is: "+maximum);
    }
}

This is an example with methods, but without them
public class Solution
{
    public static void main(String[] args) throws IOException
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int[] array = new int[5];
        for (int i = 0; i < array.length; i++){
            String s = reader.readLine();
            int value = Integer.parseInt(s);
            array[i] = value;
        }

        int minimum = array[0];
        int maximum = array[0];

        for (int i = 0; i < array.length; i++){
            if (minimum > array[i])
                minimum = array[i];
            if (maximum < array[i])
                maximum = array[i];
        }
        System.out.println("Maximum is: "+maximum);
        System.out.println("Minimum is: "+minimum);
    }

}

PS | In the second case, the code is shorter and easier to understand, for me personally

Answer the question

In order to leave comments, you need to log in

5 answer(s)
A
Alexander, 2016-07-17
@WannaCreative

It all depends on the situation - the method solves some separate task, the division into methods is necessary in order not to repeat the same code, but simply call the ready-made method. Allows you to structure your code. Avoid mistakes - you decide to change the code a little and you don't have to go and search all over the project where you used it, just fix the method. In your case, if you are not going to call those methods from other functions, then you can combine everything in one.

R
Roman Rumyantsev, 2016-07-17
@Aleshonne

It is better to separate code blocks into separate methods that solve a specific task or are used repeatedly, so that later you yourself will be less confused and greatly simplify life for people trying to figure out your code.
But in this case, the programmer needs to be beaten with a big club for such a split: the method InitializeArraydoes not initialize the array, but searches for the minimum and maximum, prints them to the console, and for some reason returns its parameter, although it did not change it. The method WriteLineprints not one line, but two. The main method does not use the return value of the method in any way InitializeArray. In general, it is better not to split into methods at all than to split like this.

A
asd111, 2016-07-17
@asd111

As far as I understand, the InitializeArray method finds the minimum and maximum in the array and then prints them.
Therefore, it is desirable to name the InitializeArray method so that it is clear from the name what it does. For example, instead of InitializeArray, you can call the find_min_and_max_and_print_them method - i.e. Find the minimum and maximum and print them. You can get rid of the WriteLine method because it doesn't actually do anything.
You can also wrap in a function the place where the array is created and the data is entered.
And still it is necessary to make that the size of an array where that was specified.
In java, of course, CamelCase, but in my opinion, underscore is easier to read.
Pieces of code need to be wrapped in functions for encapsulation, i.e. to hide the details of the work behind the name of the function, so the functions need to be given names from which it is clear what this function does. For example InitializeArray is not a very clear name for a function that searches for and displays the maximum and minimum. And for example, if you call such a function Find_minimum_and_maximum_in_array_and_output_values, then everything is immediately clear.
The code did not check, maybe there are errors, but I think the approximate essence is clear.
Those. in general you can do this:

public class Main
{
    const int ARRAY_SIZE = 5;
    public static void main(String[] args) throws IOException{
        int[] working_array = create_array_and_fill_it_from_stdin();
        find_max_and_min_in_array_and_print_them(working_array);
    }
    public static int[] create_array_and_fill_it_from_stdin(){
        int[] array = new int[ARRAY_SIZE];
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        for (int i = 0; i < array.length; i++){
            String s = reader.readLine();
            int value = Integer.parseInt(s);
            array[i] = value;
        }
        return array;
    }
    public static void find_max_and_min_in_array_and_print_them(int[] array){
        int max = array[0];
        int min = array[0];
        for (int i = 0; i < array.length; i++){
            if (max < array[i])
                max = array[i];
            if (min > array[i])
                min = array[i];
        }
        System.out.println("Minumum is: "+min);
        System.out.println("Maximum is: "+max);        
    }
}

A
abcd0x00, 2016-07-18
@abcd0x00

Learn the top-down development method. If you don’t master it, you won’t write anything longer than a hundred lines.
wiki. structured programming

M
mikhail_404, 2016-07-18
@mikhail_404

And, of course, don't forget about SOLID !

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question