- Feature Articles
- CodeSOD
- Error'd
-
Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
It does however only have one exit point. That is soooo much more important to proper code structure. /s
Admin
So this code checks the object properties properly populated?
Admin
Sometimes it's necessary to have a lot of validation rules applied in sequence. If the object doesn't naturally decompose, you might end up with a lot of properties to validate in which case this isn't really a AWF. However I would normally have a single boolean function with early return statements if anything is invalid, so at least you save a level of nesting
Admin
WTFGuy brings the hobgoblin to the arena. Is this STILL dogma somewhere? Edsger, gods love him, has a few things to answer for.
For this to work as an actual validator, the first clause must evaluate true to indicate the desired (valid) condition, and every clause thereafter must evaluate false to indicate the desired (valid) condition.
IOW, every elseif must execute and validity is assured only in the penultimate code block here. Mind-bending.
Admin
Tell me if I'm wrong, because I object strenuously to the claim that this validator "confirms that object is properly populated". When I write this function, there are no "else"s: I check every property and append error messages to an array as necessary, and the array is returned --- success is an empty array. This code bails at the first problem, so any remaining issues are ignored.
Admin
OK, if you insist. You are wrong.
"is the object properly populated?" is a boolean condition: it is true (the object is properly populated) or false (the object is not properly populated).
It is not "tell me everything that's wrong with the object", but "tell me if the object is properly populated", and that can, indeed, be done by this function.
Addendum 2026-04-07 09:22: It might be more useful to have the full list of problems, but that's not necessary to answer the basic question.
Admin
Precisely.
Admin
Won't this just iterate though until it find the first property that is 'valid' (ie, resolving to to True) and then break out of the loop?
As in it'll search through each property until it find the first one with, for example, 'Dave' as the value and then break.
Admin
I'm guessing the
ifconditions look for invalid values, not valid. So it stops at the first invalid property and reports failure.Admin
There seems to be a lot assuming going on here. All I see is an
ifstatement containing anotherifwith lots of nested (or chained, f you like)elseclauses.We are told only
and that the function is a validator.
We can't assume anything about each condition and we can't assume anything about what is done in each compound statement. We also can't assume that this is the whole function. If there's stuff that is supposed to happen after the
ifstatement, you can't justreturnin the conditional body you execute.I fail to see how this structure is a WTF for the context as described.
Admin
Maybe I'm wrong, but if the code for your constructor is sane you shouldn't have an object that is **im**properly populated because creating objects with empty properties that must have values or creating objects with bad values should throw an exception rather than instantiate a bad object.
Can someone who is a better developer than I am validate that logic? Happy to learn something if I'm off base.
Admin
You're generally correct, but the object might be valid when it's created, and the invalid state is caused by some change to the object later. If the validation rules are complex enough, it's sometimes necessary (or at least easier) to check all of the rules with every change.
It's also possible that this validation function is called by the constructor, so it would be just like you said.
Admin
Thanks for validating, I appreciate that. I was thinking there might be something outside the constructor might ~~mutate~~ mutilate the object's properties, but even in this case, shouldn't the set() do rule checking? I mean, I'm far from a developer. I write some simple tools to do my job more easily, and even I have sanity checks in my set() methods. This just makes sense to me.
Admin
Just sitting here imagining what that code would look like in python.
Admin
@Bananafish.
That might be a wise idea for immutable objects. But for any mutable object validated perfection at construction time cannot be guaranteed after that instant. And you cannot necessarily simply validate each property as it's being mutated because you may have transient invalid states along the way when multiple property updates are allowed.
Addendum 2026-04-07 11:46: Particularly in web UIs you often end up with an object which represents a compendium of all the input fields. And need to apply validation to some or all individually and some of them collectively locally before wasting time sending bad data to the server. So you get a "do-ll" function which validates all 27 textboxes, 13 checkboxes, 3 sets of radio buttons, and 5 dependent dropdowns.
Admin
Yes, you would do the checks in the
setmethods as well. If the rules are simple enough, eachsetmethod could do only the checks related to that single property, but the rules might be too complex to make that easier than just having single validation function that gets called by all thesetmethods.If the checks are just copy/pasted everywhere instead of being in a single function, that would be yet another WTF.
Admin
Well, at least something good can be said of this code: its author chose the sanest style of elseifs and indenting. That said, I don't think we have enough context to call this a WTF. Whatever object this is, it is HUGE; and that itself facilitates the appearance of WTFs, but it is not necessarily a given.
@bananafish: If this were in (or being called by) a constructor I'd agree with you that it would be a big WTF. Since we don't know that. It's up in the air.
@WTFGuy: 100% agree with what you said about not wasting time and processor cycles validating an incomplete set of changes.
Admin
This is a failure test, not a success test.
Each condition is testing for something that should not be.
Check it in setters? Not always an option--what happens when you need to do a two-step change?
I can easily see this being the best solution. Make the change, test the validity, if it fails rollback the change. Might be overkill, but rarely does an extra layer of safety harm and putting everything in the validator provides one source of truth for what constitutes valid. Maybe the object is too complex, but I do not consider that proven. I have dealt with some nightmares in enforcing business rules--sure, the real problem was the rules but I had to live with that.
Admin
I count 38 'else if' statements (I may have miscounted, but there are at least 30 of them). Based on that alone, I am supposing this to be some sort of 'god-object', which could (should?) be broken into smaller, relevant, self-contained pieces. Even if you then create an object which is only a collection of those smaller objects, it's still better than what this code is suggesting.
Although, as @WTFGuy suggests, it could very well be the contents of a form, needing validation. It can still be done better. Probably by making the form better, and making smaller objects. Each of which can be validated.
@Bananafish - it's possible that it is initialised correctly (e.g. 30 blank strings and some check-boxes and drop-downs), but some things are invalid, so it can't be committed to a database. Validate, highlight in red anything which needs attention, and turn control back to the human filling the form.
Admin
Programmer Peter Pikul projected properly populated property properties to propelling priority objects; if if if if if if he properly picked pepper protocol properties for promulgation people, he would have returned in a pack of proper provost properties with proverbial pronouns, or else.
Admin
When did python 2 get curly braces?
Admin
Where WTF?
I prefer a longer function over logic exploded all over the place with 30 functions called by far. It's not the prettiest, and depending on the complexity of some checks some if's could be made into functions for readability, but other than that it's perfectly fine. Not a WTF.
Admin
Same here. As a general rule functions are for stuff that is intended to be called from more than one location. If a piece of code is not intended for reuse it should not have its own function.
Admin
Well, Python has else-if, short "elif". Also, in contrast to the writer of that code, Python knows a cool keyword "and" that can be used to join multiple conditions that all need to be True in order to evaluate the big thing to True.
Admin
When it comes to validation - particularly of a form, it boils down to the 2 opposing UXs:
Don't know about you - but a load of "else if" statements is indicative of the first UX option, which I would find far more frustrating as a user.