- 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
Edit Admin
No. It's a whole parade ground of red flags, all at once.
Regardless of whether you're relying on duck typing, Python style, or you're using a more disciplined polymorphism approach, C++/Java/C♯ style, you ask the object whether it can do the thing, so you can reuse this code later when you get new modules that do or do not do the thing.
Or you ask it do do the thing, with the interface defined so that "I can't do that thing" is just a no-op.
Edit Admin
I am completely unfamiliar with Python, so when I saw the "try: raise X except X: pass" it was so stupid-looking that I assumed it had to be due to my ignorance. Apparently we need an extension to Hanlon's Razor: "Never attribute to ignorance that which is adequately explained by stupidity."
Edit Admin
I fully agree. I can see asking the object what it is in a case of a method called updateAddress, because the way to update the address for an employee is going to be different than updating the address for a vendor or a contact. But I'm not sure "reuse this code later" is the goal...
I think TRWTF is a developer who calls any method isinstance. It suggests the method is checking to see if the passed object is an instance of any object type or, possibly, to return the object type (why not ask the object directly?). In this case, it doesn't validate the object type. It <koff> checks for valid values.
I'm not a Python wizard, but it looks to me that if the passed object o does not instantiate o.requirement or o.title - because its type does not contain those variables - the result is the same as it would be if object o did contain those variables and their values were None. I could be wrong there, because I'm not a Python guy (never used it, tbh). I'm just going on what I think the exception handling will do. In the more disciplined styles you mention, asking an Employee object to tell you its PartNumber value would throw an exception (presuming, of course, that employees in your company are not assigned part numbers). And it looks to me like exceptions are ignored here.
If I'm wrong about this, I'm happy to hear why.
Edit Admin
isinstance
is a built-in function in Python, it tells you if a value is an instance of the given type (or types).And the rest of the code is checking whether those attributes contain the value
None
. If the attributes don't exist at all, you'll get an error trying to access them (it's not like JavaScript where nonexistent properties return a default value). Those accesses should perhaps be in the try/except to catch missing attributes.Edit Admin
Which, handling that error is the "Pythonic" answer:
Though, in a situation like this, I can see an argument for another red flag method-
getattr
, which would allow you to writegetattr(obj, "property", None)
, whereNone
is the default value if the property doesn't exist, and thus condense the "doesn't exist" and "null" into a single case. But I don't love it, and honestly, you should just avoid getting in this situation in the first place. When you're inspecting objects for properties and methods you're in desperate need for a good refactoring.Admin
Looking at the weirdo in the article photo, all I can think is the Developer probably did that on purpose, as a means of escaping having to work with / for someone, who just looks like they are so annoying that you almost can't resist punching them in the face,just on general principle.
Admin
Ehm, that photo is of editor Remy Porter and has nothing to do with submitter of WTF...
Edit Admin
It's exactly equivalent to this in C++ (meaning that it's exactly as stupid as it seems):
Edit Admin
As I said, I don't know Python. I just Google'd the function and see it's Python's TypeOf() so this will only execute if the object is a Test type object. I didn't know that. :embarrassed_face:
In that case, the constructor should verify all required attributes are present when the object is instantiated. This looks like an after-the-fact test. If this is true, it's extra WTFy, imho.