Last month, news came over the wireless that Steven Moffat is taking over “Doctor Who” when its fifth season starts in 2010. I didn’t think much about the announcement, since I haven’t been paying much attention to the series. It’s turned into kind of a shrill, nonsensical mess with increasingly overwrought finales. Even though it has the occasional brilliant, best-thing-on-television episode, that hasn’t been enough to keep me interested.

Of course, what the brillaint, best-thing-on-television episodes have in common (with a couple of exceptions) is that they were written by Steven Moffat. I just finished watching his two-parter for season four, “Silence in the Library” and “Forest of the Dead”, and it looks like this hasn’t been just a coincidence. The guy is just crazy good. And he seems to understand the series on a gut level, and he knows how to turn it from goofy kids’ sci-fi programming into astoundingly good television.

What impresses me the most is how he manages to nail the formula of the series, without its feeling formulaic. The two-parter is a straight-up “Doctor Who” formula story: time travel, aliens, a little bit of horror, with new characters getting picked off one by one and a thrilling conclusion where the Doctor suddenly figures out a deus ex machina to fix everything. Not only that, but this episode is something of a mash-up of Moffat’s other episodes, with time paradoxes, a love story, a little bit of self-referential storytelling, and scary monsters from unlikely sources (in “Blink” it was statues, here it’s shadows) shambling around repeatedly saying creepy catch-phrases (in “The Empty Child” it was, “Are you my mummy?” here it’s “Hey, who turned out the lights?”)

So it’s amazing that it all mixes together to make something that works so well. I think the last time I’ve been genuinely creeped out by a TV show was when I saw “Blink,” and the last time I’ve been so genuinely happy at a happy ending was when I saw “The Doctor Dances.” Lesser writers are afraid to save a character because they think it’ll look like a cop-out, but Moffat really earns his happy endings. And earns his scares, as well — the monster in these episodes is basically the haunted spaceman from “Scooby Doo.”

If we can expect a whole season of episodes as good as these two, then the next full season of “Doctor Who” could be amazing. Of course, it’s over a year away, but it’ll be worth the wait.