Write a method range that accepts an ArrayList of integers as a parameter and that returns the range of values contained in the list, which is defined as 1 more than the difference between the largest and smallest elements.I feel like I'm beginning to understand conditionals, but for some reason this just makes me cringe. Is it good practice to have these conditionals as catch-alls in the beginning? Should I be using else ifs instead?public static int range(ArrayList<Integer> list){    if(list.size() == 0){        return 0;    }    if(list.size() == 1){        return 1;    }    int firstElement = list.get(1);    int max = firstElement;    int min = firstElement;    for(int i = 0; i < list.size(); i++){        int elementValue = list.get(i);        if(max < elementValue){            max = elementValue;        }        if(elementValue < min){        min = elementValue;        }    }return (max - min) + 1;}
Calculate range of ArrayList
java;beginner
To answer your question, yes, early returns are very good.  Often, you will check all error conditions at the beginning of the method, then do your work knowing the input data is OK.  You can read more about this here.if(list.size() == 0){    return 0;}if(list.size() == 1){    return 1;}You can combine these into a single statement.  Look at what you are returning - you are returning the value of list.size() in both of these conditions.if(list.size() == 0 || list.size() == 1){    return list.size();}if(elementValue < min){min = elementValue;}For the most part, your indentation is very good.  Just try to be a bit more consistent.Other than these little things, this looks like very good code.