Skip to main content

Instead of returning an intint, return a value object that has the validation hard-coded. This is a case of primitive obsession and it'sits fix.

// should be class, not struct as struct can be created without calling a constructor public class ValidNumber { public int Number { get; } public ValidNumber(int number) { if (number <= 5) throw new ArgumentOutOfRangeException("Number must be greater than 5.") Number = number; } } public class Implementation : ISomeInterface { public ValidNumber SomeMethod(string a) { return new ValidNumber(int.Parse(a)); } } 

This way, the validation error would happen inside the implementation, so it should show up when developer tests this implementation. And havingHaving the method return a specific object, makes it obvious that there might be more to it than just returning a plain value.

Instead of returning an int, return a value object that has the validation hard-coded. This is a case of primitive obsession and it's fix.

// should be class, not struct as struct can be created without calling a constructor public class ValidNumber { public int Number { get; } public ValidNumber(int number) { if (number <= 5) throw new ArgumentOutOfRangeException("Number must be greater than 5.") Number = number; } } public class Implementation : ISomeInterface { public ValidNumber SomeMethod(string a) { return new ValidNumber(int.Parse(a)); } } 

This way, the validation error would happen inside the implementation, so it should show up when developer tests this implementation. And having the method return a specific object, makes it obvious that there might be more to it than just returning a plain value.

Instead of returning an int, return a value object that has the validation hard-coded. This is a case of primitive obsession and its fix.

// should be class, not struct as struct can be created without calling a constructor public class ValidNumber { public int Number { get; } public ValidNumber(int number) { if (number <= 5) throw new ArgumentOutOfRangeException("Number must be greater than 5.") Number = number; } } public class Implementation : ISomeInterface { public ValidNumber SomeMethod(string a) { return new ValidNumber(int.Parse(a)); } } 

This way, the validation error would happen inside the implementation, so it should show up when developer tests this implementation. Having the method return a specific object makes it obvious that there might be more to it than just returning a plain value.

added 18 characters in body
Source Link
Euphoric
  • 38.2k
  • 6
  • 82
  • 120

Instead of returning an int, return a value object that has the validation hard-coded. This is a case of primitive obsession and it's fix.

// should be class, not struct as struct can be created without calling a constructor public class ValidNumber { public int Number { get; } public ValidNumber(int number) { if (number <= 5) throw new ExceptionArgumentOutOfRangeException("Number must be greater than 5.") Number = number; } } public class Implementation : ISomeInterface { public ValidNumber SomeMethod(string a) { return new ValidNumber(int.Parse(a)); } } 

This way, the validation error would happen inside the implementation, so it should show up when developer tests this implementation. And having the method return a specific object, makes it obvious that there might be more to it than just returning a plain value.

Instead of returning an int, return a value object that has the validation hard-coded. This is a case of primitive obsession and it's fix.

// should be class, not struct as struct can be created without calling a constructor public class ValidNumber { public int Number { get; } public ValidNumber(int number) { if (number <= 5) throw new Exception("Number must be greater than 5.") Number = number; } } public class Implementation : ISomeInterface { public ValidNumber SomeMethod(string a) { return new ValidNumber(int.Parse(a)); } } 

This way, the validation error would happen inside the implementation, so it should show up when developer tests this implementation. And having the method return a specific object, makes it obvious that there might be more to it than just returning a plain value.

Instead of returning an int, return a value object that has the validation hard-coded. This is a case of primitive obsession and it's fix.

// should be class, not struct as struct can be created without calling a constructor public class ValidNumber { public int Number { get; } public ValidNumber(int number) { if (number <= 5) throw new ArgumentOutOfRangeException("Number must be greater than 5.") Number = number; } } public class Implementation : ISomeInterface { public ValidNumber SomeMethod(string a) { return new ValidNumber(int.Parse(a)); } } 

This way, the validation error would happen inside the implementation, so it should show up when developer tests this implementation. And having the method return a specific object, makes it obvious that there might be more to it than just returning a plain value.

Source Link
Euphoric
  • 38.2k
  • 6
  • 82
  • 120

Instead of returning an int, return a value object that has the validation hard-coded. This is a case of primitive obsession and it's fix.

// should be class, not struct as struct can be created without calling a constructor public class ValidNumber { public int Number { get; } public ValidNumber(int number) { if (number <= 5) throw new Exception("Number must be greater than 5.") Number = number; } } public class Implementation : ISomeInterface { public ValidNumber SomeMethod(string a) { return new ValidNumber(int.Parse(a)); } } 

This way, the validation error would happen inside the implementation, so it should show up when developer tests this implementation. And having the method return a specific object, makes it obvious that there might be more to it than just returning a plain value.