Skip to main content
Commonmark migration
Source Link

###ValidateNotExistImproved

ValidateNotExistImproved

###ValidateNotExistImproved

ValidateNotExistImproved

Source Link
Heslacher
  • 51k
  • 5
  • 83
  • 177

public static class ExistsHelper
{
    public static bool Country(int id)
    {
        return (id > 10) ? true : false;
    }

    public static bool State(int id)
    {
        return (id > 10) ? true : false;
    }

    public static bool City(int id)
    {
        return (id > 10) ? true : false;
    }
}  

The usage of a ternary here is senseless, you can just return the evaluated condition like so

public static class ExistsHelper
{
    public static bool Country(int id)
    {
        return (id > 10);
    }

    public static bool State(int id)
    {
        return (id > 10);
    }

    public static bool City(int id)
    {
        return (id > 10);
    }
}  

but looking at it, it seems it doesn't matter what "type" you are validating because they all have the same condition. You could simply use only one method and extract the magic number 10 into a meaningful constant.


###ValidateNotExistImproved

Here you are using a ternary as well which isn't needed because a simple if statement would be enough and more readable like so

public static string Validate(EntityTypeEnum type, int id)
{
    switch (type)
    {
        case EntityTypeEnum.Country:
            if (!ExistsHelper.Country(id))
            {
                return NotExistErrorCodeConfig.Country;
            }
            break;

        case EntityTypeEnum.State:
            if (!ExistsHelper.State(id))
            {
                return NotExistErrorCodeConfig.State;
            }
            break;

        case EntityTypeEnum.City:
            if (!ExistsHelper.City(id))
            {
                return NotExistErrorCodeConfig.City ;
            }
            break;
    }

    return string.Empty;
}