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");
@josephwoodward
Copy link
Author

josephwoodward commented Dec 13, 2016

MS docs on Constructor usage https://msdn.microsoft.com/en-us/library/ms229060(v=vs.110).aspx

Consider using a static factory method instead of a constructor if the semantics of the desired operation do not map directly to the construction of a new instance, or if following the constructor design guidelines feels unnatural.

@NathanGloyn
Copy link

NathanGloyn commented Dec 14, 2016

Example one has the by product of creating a temporary object in memory before creating the actual object (http://haacked.com/archive/2013/01/11/hidden-pitfalls-with-object-initializers.aspx/).

Example two is vanilla constructor - the boolean flag is strange when looked at alone, you could always use the parameter name to make it more intuitive or set it seperately via a property or method. A thought though - how does this differ from a method call where the parameters aren't named?

Example three to mee seems to mix concerns, you have a class which represents a user but now also has multiple methods to create a user the only difference being the setting of a boolean flag.

You could of course create a factory class that takes the static methods out of the User object and use that:

public static class UserFactory
{
	public static User CreateActiveUser(string name) => new User(name, true);
	
	public static User CreateInactiveUser(string name) => new User(name, false);
}

@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