Skip to main content
Commonmark migration
Source Link

###Usage of both

Usage of both

###Usage of both

Usage of both

deleted 56 characters in body
Source Link
public class YesNoAnswer
{
    public YesNoAnswer(string state) 
    {
        this.SetState(state);
    }

    public bool Yes { get; private set; }

    public void SetState(string state)
    {
        var uppedState = state.ToUpper();

        if (uppedState == "YES") 
        {
            this.Yes = true;
            return;
        }
        if (uppedState == "NO") 
        {
            this.NoYes = true;false;
            return;
        }

        throw new ArgumentOutOfRangeException(nameof(state), "Yes or No strings not found in argument");
    }
}
public static class StringExtensions
{
    public static bool GetYesNoState(this string state) 
    {
        var uppedState = state.ToUpper();
    
        if (uppedState == "YES") 
        {
            this.Yes =return true;
            return;
        }
        if (uppedState == "NO") 
        {
            this.No = true;
           return return;false;
        }
    
        throw new ArgumentOutOfRangeException(nameof(state), "Yes or No strings not found in argument");
    }
}
public class YesNoAnswer
{
    public YesNoAnswer(string state) 
    {
        this.SetState(state);
    }

    public bool Yes { get; private set; }

    public void SetState(string state)
    {
        var uppedState = state.ToUpper();

        if (uppedState == "YES") 
        {
            this.Yes = true;
            return;
        }
        if (uppedState == "NO") 
        {
            this.No = true;
            return;
        }

        throw new ArgumentOutOfRangeException(nameof(state), "Yes or No strings not found in argument");
    }
}
public static class StringExtensions
{
    public static bool GetYesNoState(this string state) 
    {
        var uppedState = state.ToUpper();
    
        if (uppedState == "YES") 
        {
            this.Yes = true;
            return;
        }
        if (uppedState == "NO") 
        {
            this.No = true;
            return;
        }
    
        throw new ArgumentOutOfRangeException(nameof(state), "Yes or No strings not found in argument");
    }
}
public class YesNoAnswer
{
    public YesNoAnswer(string state) 
    {
        this.SetState(state);
    }

    public bool Yes { get; private set; }

    public void SetState(string state)
    {
        var uppedState = state.ToUpper();

        if (uppedState == "YES") 
        {
            this.Yes = true;
            return;
        }
        if (uppedState == "NO") 
        {
            this.Yes = false;
            return;
        }

        throw new ArgumentOutOfRangeException(nameof(state), "Yes or No strings not found in argument");
    }
}
public static class StringExtensions
{
    public static bool GetYesNoState(this string state) 
    {
        var uppedState = state.ToUpper();
    
        if (uppedState == "YES") 
        {
            return true;
        }
        if (uppedState == "NO") 
        {
            return false;
        }
    
        throw new ArgumentOutOfRangeException(nameof(state), "Yes or No strings not found in argument");
    }
}
Source Link

Domain Model for Simple Yes/No

Working from a C# and EntityFramework background. Jimmy Bogard makes the point of modelling your domain model correctly, such that an Email Address is not a simple string but should be of type EmailAddress.

Also known as Primitive Obsession.

I've come to this part of a piece of software where a Application form is to be filled out.

For the user a Yes/No question where they can answer Yes or No is a very easy thing, you can even create really go user experiences around yes/no.

So I have quickly thought of two ways to handle Yes/No.

A complex object:

public class YesNoAnswer
{
    public YesNoAnswer(string state) 
    {
        this.SetState(state);
    }

    public bool Yes { get; private set; }

    public void SetState(string state)
    {
        var uppedState = state.ToUpper();

        if (uppedState == "YES") 
        {
            this.Yes = true;
            return;
        }
        if (uppedState == "NO") 
        {
            this.No = true;
            return;
        }

        throw new ArgumentOutOfRangeException(nameof(state), "Yes or No strings not found in argument");
    }
}

Or a simple extension method:

public static class StringExtensions
{
    public static bool GetYesNoState(this string state) 
    {
        var uppedState = state.ToUpper();
    
        if (uppedState == "YES") 
        {
            this.Yes = true;
            return;
        }
        if (uppedState == "NO") 
        {
            this.No = true;
            return;
        }
    
        throw new ArgumentOutOfRangeException(nameof(state), "Yes or No strings not found in argument");
    }
}

Obviously the validation should be done earlier in the pipeline (or at least it is in our application).

Which is better? They both achieve the same thing, the first is much clearer, neater. The second is well, slightly obfuscated - not a great deal but takes more than a second to think about what it is doing.

###Usage of both

var application = new ApplicationForm { IsOkayToWork = new YesNoAnswer(state) };

or

var application = new ApplicationForm { IsOkayToWork = state.GetYesNoState() };