mardi 6 septembre 2016

Better way to return debug information instead of using lots of if-else

So I have a boolean method that is used to verify if a command is valid. This is used inside of an engine in which it verifies that the process can continue or not. This is the validation method:

private bool CommandIsValid(WC command)
        {
            if (command.Address == null ||
                command.UserId < 0 ||
                String.IsNullOrEmpty(command.CurrencyCode) ||
                command.Amount < .01m ||
                command.Address.PrimitiveAddress == null ||
                String.IsNullOrEmpty(command.Address.Source) ||
                String.IsNullOrEmpty(command.Address.PrimitiveAddress.City) ||
                String.IsNullOrEmpty(command.Address.PrimitiveAddress.Country) ||
                String.IsNullOrEmpty(command.Address.PrimitiveAddress.FirstName) ||
                String.IsNullOrEmpty(command.Address.PrimitiveAddress.LastName) ||
                String.IsNullOrEmpty(command.Address.PrimitiveAddress.Region) ||
                command.Address.Created <= DateTime.MinValue)
            {
                return false;
            }
            return true;
        }

And is called here inside of my method here:

if (!CommandIsValid(cmd))
{
    _logger.Debug("Invalid command);
}

The issue is that I want to have some type of information regarding what failed validation. However, having a bunch of if else statements seems very poor style and I was wondering if there is any way in c# or in general I can do to avoid this.

Aucun commentaire:

Enregistrer un commentaire