Today’s Java class was interesting. One of the assignments was to parse lines of something that was expected to be Java code, and strip out all comments that begin and end on a single line. That is to say, block comments that don’t end on the same line should be left as-is. String and character literals shouldn’t count as starting a comment block, and so forth.
I read the description, and had two near-simultaneous thoughts:
– Despite the instructions that hint at writing a method to find a comment and another to delete it, this clearly calls for a state machine, and
– the two programming courses available so far don’t equip you to deal with a problem like this. At all.
I wrote my comment remover test-first. I’ve written state machines before, but it’s been a while, and I was fairly sure I’d make a mess of it if I did it any other way.
There was another person there who had written a state machine, but not tested it, and I can fairly safely assume it didn’t handle its corner cases as well as mine (given it was entirely untested). Aside from him, we saw one implementation that clearly didn’t and couldn’t work — kudos for trying though — and also demonstrated a severe lack of understanding of the fundamentals. Which is in line with what I’ve seen so far — the course steers clear of actually trying to teach anything.
Another exercise was to describe what to do when you have a constructor, MyClass(MyClass other) that should copy instance values from “other”, and “other” is null. Exceptions were clearly expected (no pun intended), but the guy from before — the one who thought inheritance was there to allow access to member variables — was convinced that just letting a NPE happen when dereferencing would spare us from doing anything ourselves, and provide a “clear and helpful error message.”
Afterwards, a fellow student asked for clarification on variable scoping. He thought that if he introduced a variable in a block and then called a method, the variable would be visible in the body of the called method too.
I’m going to sing the doom song now.