Skip to content

Instantly share code, notes, and snippets.

@josephwoodward
Last active December 14, 2016 14:43
Show Gist options
  • Save josephwoodward/0db5f2d4116260a6e9b33a7cb16ce60c to your computer and use it in GitHub Desktop.
Save josephwoodward/0db5f2d4116260a6e9b33a7cb16ce60c to your computer and use it in GitHub Desktop.
// 1: Object Initializer //////////////////////////////////
public class User {
public bool IsActive { get; set; }
public string Name { get; set; }
}
var user = new User {
IsActive = true,
Name = "John Doe"
};
// 2: Constructor //////////////////////////////////
public class User {
public User(string name, bool isActive){
Name = name;
IsActive = isActive;
}
public bool IsActive { get; private set; }
public string Name { get; private set; }
}
var user = new User("John Doe", true); // Boolean is confusing at this point?
// 3: Factory method //////////////////////////////////
public class User {
public bool IsActive { get; private set; }
public string Name { get; private set; }
public static User CreateActiveUser(string name) => new User {
Name = name,
IsActive = true
};
public static User CreateInactiveUser(string name) => new User {
Name = name,
IsActive = false
}
}
var user = User.CreateActiveUser("John Doe");
@danbarua
Copy link

danbarua commented Dec 14, 2016

var user = new User("John Doe", true); // Boolean is confusing at this point?

Make the implicit explicit.

var user = new ActiveUser("John Doe");

Or just use F#

match user with
  ActiveUser -> printfn "User is active."
  InactiveUser -> printfn "User is not active"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment