Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 a 'Conditional Logic can be removed argument in checkstyle for the following method. I cannot seem to figure out why... Can someone give me a pointer?

like image 263
matic Avatar asked Jan 26 '13 00:01

matic


1 Answers

It is complaining 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 just returning the condition from the if statement:

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

This statement will return true if the the number of columns for a and rows for b are equal, and false otherwise.

like image 126
Hunter McMillen Avatar answered Nov 07 '22 21:11

Hunter McMillen