And yet you have people clamoring that it writing your tests first causes your code to melt, and others who say that if you write your tests last, you might as well do surgery without washing your hands.
Seriously, all this, over when you write your tests?
Granted, one of those people is probably right (and by the end of this article, you'll know which one I agree with). But both of them are wasting their time, because all they're really doing is standing on either side of an isle calling each other extremists while sounding fairly extremist themselves.
I guess I'll ruin the suspense. I love TDD. I've practiced it for a little over a year, and I've found that I have a much easier time producing code faster and of higher quality when I practice TDD. And instead of shaming you for not following TDD, or melting down a building or something, I'm going to use this series of posts to show you, my valued reader, exactly what my TDD-style process looks like.
And if you don't like it and decide that it's not for you, then that's probably fine.
And if you don't like it and decide that it's not for you, then that's probably fine.
Moving on, have you ever heard of JSON? Probably, but just in case you haven't here's the Wikipedia article on it. The summary is that it's a nice human-readable, but also computer-readable, format that's often used for transmitting generic data over various protocols. For instance, a lot of HTTP payloads are JSON strings unless they need to be HTML for some reason.
A JSON string might look like this:
{
"blog":"The Box of Sand",
"article":{
"title":"JSON Parsing, Step 0: Concept",
"length":158941
}
}
At its core, it's just a map of string keys to JSON objects, which can themselves be numbers, strings, maps, and so on. For reasons I won't go into right now, I tend to dislike using third-party libraries, and prefer to re-invent my own wheels when I can, especially when it can be a learning or teaching experience. So what could be more useful than building my own JSON-parsing library! (Don't actually answer that question)
Moving on, have you ever heard of the Anti-If campaign? If not, here's their official website.
Sounds crazy, right? Until you realize that you spent the entire week fixing a bug that would have taken three minutes to solve if it hadn't been hidden behind seventeen if-statements, some of which don't need to exist and the rest of which could be moved out to named methods or something like that.
My understanding is that it's possible to write code with no if-statements - that an if is a sign that you should be using polymorphism instead. I've never followed a rule like that before, but something as user-inputty and process-y as JSON parsing seems like an interesting place to find out just how feasible it really is to avoid if's.
And that brings us to the mission:
- Build a JSON Parser
- Using TDD
- With no external libraries
- And wave GIANT red flags if I ever see an if statement
I'll keep an ongoing table of contents here, and you can look at the code and the commit history at https://github.com/joshlarson/json-parse.
Posts So Far
- Step 0: Concept
- Step 1: Setup
- Step 2: Requirements
No comments:
Post a Comment