3

I have to write API validation functions where the rules can get hairy and have many outputs depending on the branch taken.

These validation functions end up being a nesting of lots of IF conditions here and there (I try to stick to simple condition IF for simplicity), where I have lots of comments to help what's happening, but it's a big spaghetti.

Also, as I validate, I do little steps towards constructing the object/result I'm interested on, so I have two problems with this code:

  • It's a huge spaghetti with lots of exit branches, which... does reflect the complicated real life situation.

  • Validation and result construction happen together, tricky to split validation separately, because if done then I end up with this complex tree twice, once in a validation function and another to build my result.

Are there strategies/patterns to look for these scenarios?

PS: I'm using Python in case any library or pattern helps with that.

asked Nov 3, 2017 at 1:26
3

2 Answers 2

1
  • Write simple boolean functions and combine them.

    bool ContainsUpperCase(string s) { }
    bool ContainsNumbers(string s) { }
    bool ContainsLowerCase(string s) { }
    bool ContainsSpecialCharacters(string s) { }
    bool IsValidPassword = ContainsUpperCase(password) && ContainsNumbers(password) ...etc
    
  • Use Early Exit

    if (parameter == null) return (or throw);
    if (!IsValidPassword) return (or throw);
    
  • Use default values to avoid null checks

    int parameter = 0;
    
  • Rewrite the logic so it requires less conditions.

  • Use a validation library

  • Write a Rules Engine

..etc

answered Nov 3, 2017 at 2:28
-2

Here's some snippet as suggestion.

class MyComplexObject:
 def __init__(self):
 #lots of properties in your complex object
def build_complex_result(complex_input):
 complex_object = MyComplexObject()
 #Validate person info
 assert(complex_input.age > 10 \
 and complex_input.age < 50 \
 and 'brazil' not in complex_input.country)
 add_person_info(complex_object, complex_input)
 #Validating address
 assert(complex_input.address is not None) #...and lots of other conditions...
 add_address_info(complex_object, complex_input)
 #... and so on ...
 #Also, you could place these assertions and 'add_xxxx_info' to other classes
 #if necessary
 return complex_object
answered Nov 3, 2017 at 12:00

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.