Page 1 of 1

Martin Westin :: 12.10.2008 05:17:04
THis is funny enough exactly the conclusion I have come to. It is nice to see I am not just crazy... or at least that I am not alone in my madness :)

It was when I started having a few methods with a lot of ifs that this really hit home for me. I do have one criticism. This kind of "vertical" style works great for controller logic, but often not so great for validation-type code.

When you have a method that is validating you would probably want the result to reflect ALL problems with the data validated, not just the first error encountered. Forms that only give me one error at a time really irritate me.
Your name is missing!... ok. Now your email is formatted wrong... ok then. you need a zip-code... bloddy **"#&!!

But when it does not affect things like this it is much easier to read.
lecterror :: 27.10.2008 10:35:36
I'm glad you agree Martin, and as you pointed out well, sometimes this style would basically drive the end user mad. And since we always have to think about the end user, sometimes this is a big no-no. :-)

On the other hand, validation is a different kind of animal so maybe another article is due.. ;-)
klaar :: 28.04.2016 16:45:27
But what if you'd left the conditions to negative ones in the nested if clauses, you'd still end on a positive note. Granted, the note would be wrapped inside some curly braces, but the note would still be at the end of your function.

I wonder if you ever came to this conclusion yourself!

I am currently searching for legitimate reasons to hate guard clauses, but I'm finding more and more that's it's a matter of personal preference.