Github user MarkDacek commented on a diff in the pull request:

    https://github.com/apache/commons-lang/pull/336#discussion_r200858239
  
    --- Diff: src/main/java/org/apache/commons/lang3/ArrayUtils.java ---
    @@ -8672,4 +8672,53 @@ public static void shuffle(final double[] array, 
final Random random) {
                 swap(array, i - 1, random.nextInt(i), 1);
             }
         }
    +
    +    /**
    +     * Gets an element from the array if the array is non-null and 
appropriately long, otherwise returns null
    +     * @param <T> the component type of the array
    +     * @param array   the array holding the desired element
    +     * @param index  the index of the element in the array
    +     * @return The element in the array at the index, or null if it is 
ill-formatted
    +     * @since 3.8
    +     */
    +    public static <T> T get(T[] array, int index){
    +        return get(array, index, null);
    +    }
    +
    +    /**
    +     * Gets an element from the array if the array is non-null and 
appropriately long, otherwise returns the specified value
    +     * @param <T> the component type of the array
    +     * @param array   the array holding the desired element
    +     * @param index  the index of the element in the array
    +     * @param defaultReturn the object to be returned if the array is null 
or shorter than the index
    +     * @return The element in the array at the specified index, or the 
given argument if it is ill-formatted
    --- End diff --
    
    The usefulness of these would be to reduce some clutter in everyday 
development. Checking for both null and then length can be a tedious situation 
for developers striving to get high unit-test coverage. 
    
    isArrayIndexValid would reduce the number of branches in a typical use 
case, whereas get is probably a bit simpler. I'm can't say this with any degree 
of certainty, but a correctly-placed null element probably requires yet another 
check after the fact. Something like:
    
    if(isArrayIndexValid(array, 0) && array[0] != null)



---

Reply via email to