vendredi 5 octobre 2018

More Variables for More Readable IF/Conditional?

Question: Is it better to have more variables to allow a conditional to be "more readable" or is it cleaner to have less variables but a harder to read conditional?

I read this which says less variables. I think their solutions look LESS readable ( https://techbeacon.com/why-unnecessary-variables-are-bad-your-code ) This SO sounds slightly different though: Introducing variables only for readability?

Example:

I have the following statement:

if ((memberIDandLNIsValid) || (memberIDandDOBIsValid && memberDOBFormatIsValid))

To get memberIDandLNIsValid I have:

if (!string.IsNullOrEmpty(Search_MemberID) && !string.IsNullOrEmpty(Search_MemberLastName))

Notice that Search_MemberID and Search_MemberLastName are two separate variables. I thought it was easier to read to have a combined variable. But I have been told there are too many variables in my code.

I'm guessing the solution is to just leave it as :

if( (Search_MemberID && Search_MemberLastName) || ((Search_MemberID && Search_DOB) && memberDOBFormatIsValid) )

To me, this conditional is MUCH less readable now.

Thoughts?

Aucun commentaire:

Enregistrer un commentaire