Monthly Archives: July 2008

Counter-Intiutive UML

It’s thirty minutes ’till the phone interview I’ve been discussing here, and I’ve once again returned to looking at the diagram I asked about in this post, and I’ve come to the conclusion that, well IMO, it’s flawed. Perhaps this is a fault in my character, but the reason why I’ve been struggling with it is because it’s counter to how I would design the program. Consider the relationships in the diagram given in the problem (and presented in that post) (oh and please correct me if I’m wrong here):

EmploymentContract can have either no Person object or 1 Person object
Person has exactly one EmploymentContract
EmploymentContract has >=0 Company objects
Company has exactly one EmploymentContract

Now, perhaps I’m reading too much into this, and perhaps this is a sign I hang out with too many lawyers (lol), but why would an EmploymentContract contain a Person if an EmploymentContract is a tangible object that a Person can have? An employment contract is an agreement between the employee and the company, and should contain information about the company (and shouldn’t it be ONE company per contract?), how much they’re going to get paid, benefits, whatever. And a person is able to work at one or more jobs, thus Person should be allowed to have >=0 EmploymentContract objects.

Employment contracts should have no knowledge of the people signing them, that should be in another class, perhaps called Employees, which exists for every Company and shows which Person has signed the EmploymentContract for that company. Thus every Company has an EmploymentContract for each type of employee they have (janitors and programmers may want different contracts, heh), so Company has >=1 EmploymentContract objects, and >=1 Person objects.

I’ll post a diagram when I have the time (if there’s even any interest lol), but yeah, all those two-way relationships where relationships shouldn’t exist (IMO)…unnecessarily complicated.

xkcd is selling a regular expressions skirt

Is the shirt difficult for you to read due to anatomy? Here’s a skirt with a regular expressions primer around the hem. It’s easily readable when you’re sitting and programming.

Haha. Yes, make sure you’re SITTING when you try to use the skirt as a reference, otherwise your office-mates will get a nice show (or a horrible show, if you’re that chick in Waiting). Too bad this is $35, because I think I’d buy one and wear it otherwise (yes, I would actually wear a skirt…if it were this…maybe).

Because it’s raining, I don’t have an umbrella, and my car is on the other side of the complex

Yeah, I could be looking at UML things (thanks for all the help BTW :D) or trying to get to my car so I can go to campus sometime today, but instead, I post this thing (courtesy of Billys):

Our xkcd fan video was picked up by Cory Doctorow

Ok, one last thing before I go to lunch lol. On the 4th of July, my friends and I made a video to go along with this xkcd comic which is a take on this Discovery Channel commercial (which I hadn’t seen or heard of until the 4th because I don’t own a television), and for some inexplicable reason, it found it’s way to professional blogger Cory Doctorow’s blog. For some even more mysterious reason, people seem to be into it, judging from the comments on his post. AND on top of that (no pun intended if you were involved in the making of the video lol), some individuals took our monochromatic song and made a video out of it with clips from Heroes!

People are crazy.

More coding exam thoughts: composition versus inheritance

Well, the reason why I needed an answer to that last question on UML has been posponed until Wednesday (it was a phone interview, following up on the exam), so now I get to take more time to look at that, and other interesting things. :)

One of the other questions that I left blank on the exam was, “What are the advantages of using composition as opposed to inheritance?”  I had no idea what they were talking about, as far as composition was concerned.  Actually no, I lied, I had a vague snippit of memory from Head First Design Patterns of some strange character saying “a MallardDuck is-a Duck”…no wait…more like “a Thing is composed of a Something”…yeah as you can see, that didn’t really help.

Anyway, the only resource I had at my disposal at the time (and the only one I felt ethically comfortable using lol) was THE Arnold/Gosling/Holmes Java book, which really did nothing for me here, surprisingly. So I left it blank and came back to it today (to prepare for that phone interview, remember?), and with a very small amount of Googling, I found this.

Apparently when people say “composition”, they mean something like

public class MallardDuck
{
    private Duck duck;
    // add methods with the same name 
    //as the corresponding ones in Duck
    // only call duck.whatever() in the body of this.whatever()
}

In contrasting to subclassing Duck (public class MallardDuck extends Duck or public class MallardDuck implements Duck). Arnold et al calls this “forwarding” because the Duck class has been “forwarded” into the MallardDuck class. So to speak.

So ok, now that we know what it is, why is it better than inheritance? According to the article I linked to above, composition is better for code reuse and updating, but that’s…really all I got out of it. So I broke out the aforementioned design patterns book (would I be a complete dork if I said that was one of the best books ever written?) and that REALLY cleared things up for me. It’s not so much an issue of subclassing Duck, it’s an issue of subclassing things like FlyingBehavior, which defines how a duck flies. Different types of ducks have different types of flying behaviors, so a Duck HAS-A FlyingBehavior. (For those following along at home, it’s page 22-23.)

With inheritance, the behavior of the subclass is set statically at compile time, which doesn’t allow for new things to be added to the subclass on-the-fly, unless you modify the superclass. With inheritance, all of the behaviors of the superclass MUST be inherited by the subclass; with composition, we can pick and choose (so we can pick certain FlyingBehaviors over others, giving a different one to MallardDuck, RubberDuck, etc.). With composition, you can add new things to your code without modifying what you already have, which you sometimes don’t have access to. (See page 85)

Anyway, I thought it was interesting. Composition is mysteriously left out of a 114/140/intro programming course, but it makes sense…it’s more of a technique than a property of the language (as inheritance is).

Maclean’s on Steven Page and Barenaked Ladies

Maclean’s, Canada’s equivalent of Time/Newsweek except more outlandish and slightly Hunter S. Thompson-esque, has quite the comprehensive article about what’s going on with my most favorite Canadian band of all time, Barenaked Ladies.  The issue is really with Steven Page, the co-frontman of the band, who was recently caught with cocaine in Fayetteville, outside of Syracuse (and awesome part is that I actually know people from that area LOL).  The guy’s been unstable for years, first with the alcohol problem (addressed on Stunt), then who knows…damn shame too, because he’s incredibly talented and awesome.  You probably wouldn’t think so, but BnL puts on an amazing live show (saw them in Binghamton of all places…even they were like “Why are we here?”).

Ok, now back to nerdy Java stuff (see below). :)

Curious UML

I’ve been working through a bunch of pre-interview coding exams/exercises, and I’ve decided to post some of the problems I’ve been given that I find interesting and/or challenging.  This first one, however, has some urgency to it.  Can anyone explain this diagram to me??

WTF are those numbers?!
WTF are those numbers?!

Hope I don’t get in trouble for posting this or something, LOL.