I could do that, but that would be premature abstraction.

The rule of thumb that I try to live by is to not attempt to abstract something until I'm doing it for the third time. Why do I do that?

Well the first time I guarantee that I don't know how similar examples will vary and won't be able to pick a good abstraction. So attempting to abstract at that point will result in more code that is less to the point (and therefore less clear), but I'm very unlikely to find an abstraction that is useful for my next time. Which means that my abstraction has lots of real costs and no real benefits.

The second time I now have a point of comparison. I could abstract and it might work. But in my experience I still don't know enough for the cost/benefit to work out. Also at this point I am consciously fighting the second system effect.

The third time I know enough about the problem and have enough experience that I am confident that I can come up with the right abstractions. Abstractions that pay off enough in future maintenance to justify the cognitive overhead of having to learn them, and the initial effort of writing them.

Moving back to this specific example, you are perfectly right that if I had several variations of HTML format that I had to deal with, an OO layer would be a great way to go. However if I don't, adding an OO layer would add code and make it more difficult to see what is going on. It would also make other kinds of changes more difficult to make.

Let's make this concrete. In the several years since I wrote this I have never once wanted to have several similar markup languages that just varied in the syntax of the tag. (Which is what your proposed OO layer would give me.) However I have encountered other needs, such as emoticons and the need to have tags that are only allowed to open when another tag (eg a table tag) is open. Those changes would be complicated by your proposed OO interface since they cut across that interface and require changes to both sides.


In reply to Re^7: replace conditionals with polymorphism by tilly
in thread replace conditionals with polymorphism by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.