code_junkie

Enforcing Input Parameters to a Method without a bunch of if's at the beginning of the method?

Nov 12th, 2011
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. public string SomeFunction(string param1, int param2)
  2. {
  3. if(string.IsNullOrEmpty(param1)){
  4. throw new ArgumentException("bla", "param1");
  5. }
  6. if(param2 < 0 || param2 > 100 || param2 == 53)
  7. {
  8. throw new ArgumentOutOfRangeException("eek", "param2");
  9. }
  10. }
  11.  
  12. public string SomeFunction(string param1, int param2)
  13. {
  14. CheckParameterNotNull(param1);
  15. CheckParameterRange(param2, 0, 100, 53);
  16. ...
  17. }
  18.  
  19. private string InternalSomeFunction(string param1, int param2)
  20. {
  21. /* implementation goes here */
  22. }
  23.  
  24. public string SomeFunction(string param1, int param2)
  25. {
  26. if (string.IsNullOrEmpty(param1)) {
  27. throw new ArgumentException("bla", "param1");
  28. }
  29. if (param2 < 0 || param2 > 100 || param2 == 53) {
  30. throw new ArgumentOutOfRangeException("eek", "param2");
  31. }
  32. return InternalSomeFunction(param1, param2);
  33. }
  34.  
  35. if (IsFormValid)
  36. {
  37. // Processing Magic Goes here.
  38. }
Add Comment
Please, Sign In to add comment