Matrix 'conditional logic can be removed' checkstyle

public static boolean isCompatibleForMultiplcation(final Matrix a, final Matrix b)  
    {
        if (a == null)
        {
            throw new IllegalArgumentException("a cannot be null");
        }
        if (b == null)
        {
            throw new IllegalArgumentException("b cannot be null");
        }

        if(!(a.getNumberofColumns()== b.getNumberOfRows()))
        {
            return false;
        }
        else
        {
            return true;
        }

    }

      

I am getting "Conditional Logic", it is possible to remove the argument in the checkstyle for the next method. I can't figure out why ... Can anyone give me a pointer?

+3


source to share


1 answer


He complains about this part right here:

    if(a.getNumberofColumns() != b.getNumberOfRows())
    {
        return false;
    }
    else
    {
        return true;
    }

      

Whenever you see yourself writing code like this, you can easily replace it with a single line by simply returning the condition from the if statement:



return a.getNumberofColumns() == b.getNumberOfRows();

      

This operator returns true

if the number of columns for a and rows for b is equal, false

otherwise.

+8


source







All Articles