Self-WTF: Identity crysis


  • BINNED

    I was profiling my code and noticed that some of the setter functions seem to be called too many times. After some poking around I found this (representative?) line:

    this->m_calls[call->getUniqueID()]->setPropertiesFromMap(call->toVariantMap());
    

    toVariantMap() method goes through a predefined list of properties and dumps them in a QMap<QString, QVariant>, basically it's just a map with a string as a key and property value stored in Qt's generic QVariant container.

    setPropertiesFromMap(QMap<QString, QVariant>) takes a map and sets the calling class' properties to those found in map. There are a few edge cases where this kind of shallow copy is useful so it's not a WTF in itself.

    What is a WTF is that this call will always get the data from one instance of the class, and then pass them back to the same instance, which will then set it's data... to it's own data.

    I don't know if I was drunk or stupid when I wrote that. Most likely both.


    Filed under: The most convoluted way to write x = x; ever



  • It's very enterprisey OO.


  • ♿ (Parody)

    I've done similar things. I imagine the original use case was some sort of poor man's ORM. But that use is probably lost to time, and somehow you started using it incorrectly and now it's just a pile of WTF.

    Sometimes I think bits of cruft are sentient and working against me.



  • The best part about writing cruft is the feeling you get when you realize your mistake later on and get to tear it all out, though, to "fix" the problem. In a sense, writing bad code is just sending endorphins forward into the future.

    Plus if it's not you that eventually finds it, then screw that other guy.



  • At least you didn't have event handlers on the setters that eventually ended up firing the setters.


  • BINNED

    @boomzilla said:

    I've done similar things. I imagine the original use case was some sort of poor man's ORM.

    Actually, no. The service has to provide some real-time data to a web frontend using JSON. Qt has a function that will take a map and convert it to JSON, so that's why the toVariantMap method exists.

    Later on I realized I need a shallow copy of, coincidently, the same set of data, so I just added a method that can do the opposite. It's a relatively rare situation so the overhead doesn't seem to be a problem. But since I found my own abuse of it, yeah, I think it's high time for that method to die and gets replaced by something better.



  • @Onyx said:

    I don't know if I was drunk or stupid when I wrote that. Most likely both.

    Things like that happen for various reasons. I tend to write even worse crap when I'm tired.

    And while we're at self-WTFs, I had to finish an 8-page report on a student project by today - involving a lot of bullshit art. So, it's 2AM, I'm blasting rap on full volume on my headphones to stay awake, and drinking Red Bull by the gallons. Today in the morning, I take one last look before printing, and see this:

    An interesting aspect of the presentation is the location of an annotation.


    Filed under: haven't you heard of the Emancipation Proclamation?


  • Discourse touched me in a no-no place

    @Maciejasjmj said:

    An interesting aspect of the presentation is the localization of an annotation.

    If that's the interesting thing going, you're in deep trouble. (Hey, I deal with annotations where localizing them is at least somewhat meaningful. But not something you'd want to start singing from the rooftops about.)



  • Just realised I fucked that up (it should've been "location" - the Polish word is a false friend). Corrected.

    @dkf said:

    If that's the interesting thing going, you're in deep trouble.

    @Maciejasjmj said:

    involving a lot of bullshit art.

    The project in question is basically a picture viewer with zooming, brightness and contrast controls and the ability to put a tag on the picture with some text. That's pretty much the whole spec. The only twist is that it views DICOM files, which translated to one more line in the code than with JPEGs.

    And we had to write an 8-to-15 page report on the concept, the implementation, the future directions of development, etc. The actual code is half that.

    (oh, and "the interesting aspect" is that the tooltip is moved if it wouldn't fit in the viewport when rendered in the place of clicking. Yeah.)


  • Discourse touched me in a no-no place

    @Maciejasjmj said:

    The project in question is basically a picture viewer with zooming, brightness and contrast controls and the ability to put a tag on the picture with some text. That's pretty much the whole spec. The only twist is that it views DICOM files, which translated to one more line in the code than with JPEGs.

    And we had to write an 8-to-15 page report on the concept, the implementation, the future directions of development, etc. The actual code is half that.

    (oh, and "the interesting aspect" is that the tooltip is moved if it wouldn't fit in the viewport when rendered in the place of clicking. Yeah.)

    I think my comment still stands. (Except we really do support localization of annotations. Because. And it probably only works with English anyway.)



  • Profiling works magic for some code.


Log in to reply