03.10.08
Posted in Uncategorized at 1:03 pm by Kyoryu
So, reading the last post, it might seem like I’m advocating a lot of extra work to do anything. Having each class create its own interfaces for everything probably seems a bit insane. Wouldn’t it be better to define a single, universal interface for, say, a database?
Short answer: no. By doing so, you’ll inevitably end up intermingling aspects of the dependency with your core code. In the User example in the previous post, even if you abstract the DBConnection to an IDatabase, you still have to deal with the fact that you’re now tied to a database, and have intermingled knowledge of the database layout and SQL variant you’re using into your User class.
The problem is that each class has its “natural” language. The “natural” language of the User class is “save the user with this id and this name” (and whatever other information is appropriate). The natural language of the database is “run this sql command.”
Just as it would be inappropriate to put code specific to the User object in the database, it’s also inappropriate to put database specific code in the user.
So, instead, what we can do is create small classes that take the output of one class (the User, in this case), and turn it into the input expected by the other class (the database, file system, etc.). There’s no real extra code required to do this, as all of the code would have to be written ayway. By doing this, you remove the knowledge of this glue code out of either of the two classes, and allow it to live in its own classes, possibly even its own assemblies.
There are times, though, that you do want to maintain a common interface between classes. But only do this if the classes in question naturally end up with the same interface, or it’s a “natural” dependency – if you’re writing something that retrieves data and writes to a disk, then a mocked file system is appropriate. An FTP client would be a good example of this, as there wouldn’t really be a point to writing an FTP client that saved to a database.
Additionally, if the “glue” classes are more than simple pass-through classes, it may be worthwhile to test them more thoroughly, and so mocking their underlying data store may be appropriate.
Permalink
03.06.08
Posted in Uncategorized at 1:52 am by Kyoryu
There’s two things you frequently hear when talking to developers about TDD. It’s either "our code is too complicated, you can’t separate it" or "how can you test a database" (or graphics system, or insert other complicated system).
Here’s an example of code that someone might have in mind.
public class User
{
private DBConnection connection;
public string Name
{
set { SetName(value); }
}
private void SetName(string name)
{
this.name = name;
// other stuff...
connection.ExecuteSQL("update users set name = '" + name + "' where id = " + id);
}
}
So, usually someone will want to create a mock object for the database for testing purposes. That’s one way to do it. But, I’m not sure it’s the best way. After all, you want to test your class, not your understanding of the database!
There’s another issue here. The User class knows waaaaay too much about the database for its own good. I mean, what if we wanted to save to a file system? We can’t, at least without serious rework. While we could replace the database connection with a different type, we’re still tightly coupling to the idea of a database.
What we really want to do is save the user info. So let’s just make a method that does that.
private void SetName(string name)
{
this.name = name;
// other stuff...
SaveUser();
}
private void SaveUser()
{
connection.ExecuteSQL("update users set name = '" + name + "' where id = " + id);
}
Okay, that’s a little better. Now we’ve removed the implementation details of saving the user from the SetName method. It now pretty much says what, conceptually, we want to do. All the database stuff is in its own method.
But, being in its own method doesn’t really allow us to change anything. So, let’s extract an interface.
public interface IUserPersistence
{
void SaveUser(int id, string name);
}
IUserPersistence persistence;
private void SetName(string name)
{
this.name = name;
// other stuff...
persistence.SaveUser(id, name);
}
And now, our user class doesn’t even have to know about a database at all, or maintain a reference to its connection. Instead of giving it a DBConnection, we just give it a IUserPersistence object. We can then test away to our heart’s content, throw whatever exceptions we want, without having to worry about trying to mock anything as heavyweight as a whole database. We also remove all database specific code from our user class, allowing it to be reused elsewhere.
It’s also conceptually cleaner – the user class now only has to know about the idea of saving itself, not the specific details.
The drawback of this is that it does potentially pollute the API space a bit. One way to get around this is to set the instance of the interface to protected, and not expose them on the default object. Then, you can write a test object which inherits from the normal class which exposes a setter.
So, the answer to "how do I mock a database" is simple – you don’t.
Permalink
12.20.07
Posted in General development, Uncategorized at 1:46 pm by Kyoryu
“Plans are nothing. Planning is everything.” – Dwight D. Eisenhower
One of the common criticisms I see of any kind of agile development (note: agile with a little a, not a capital A) is that people seem to believe that agile development means that you don’t plan, don’t design, and don’t document.
In my opinion, this couldn’t be further from the truth.
In fact, I’d go so far as to say that agile development involves more planning, more design, and more useful documentation than other methodologies. The key difference is when you do the planning, to what extent, and what information you derive from it.
I’ve worked on projects that planned to the extreme. There was a gantt chart which specified what you would be doing at 2pm on Thursday. Now, that’s somewhat okay, except that the Thursday in question was two years into the future.
Let’s come up with a hypothetical example. Let’s say you’re going to start a brand new Generic Fantasy MMO from scratch. Do you just start diving in and writing code and making art? I’d hope not, and I doubt anybody with a brain would suggest that.
You start with planning. You know you’re going to need art, some levels, some amount of design documentation, and code.
You’ll know you’ll need some areas for people to walk around and swing their swords. How many? Well, that’s a function of how large they are, but we’ll say 100 distinct “areas,” where each area is more-or-less self contained. How will we make them? In Max or Maya? A custom tool? Who will make them? If designers are doing the layout flow, we probably don’t want to have them use Max, so we’ll need a custom tool. How will that tool work? Will it plug together pieces Lego-style? While it give more freedom, and then allow artists to perform a beautification pass? How many levels will be inside vs. outside, and how does that impact what tools are needed? What are the plans for future expansion after release?
Once we have some of these answers, we can start putting together a plan. Let’s say we want a 50/50 mix of outdoors and indoor areas, so we’ll need relatively strong tools for both. We want designers to do at least initial layout, and we’re pretty sure we can use a “Lego-brick” approach. We’ll still need artists to create the bricks. But, we can start using this knowledge to make a plan for what resource we’ll need, when we’ll need them, and how we can go about putting this thing together. In this case, we’ll probably need at least one programmer pretty quick, with at least some art time not too long after that to create some sample “bricks” (once we get past the programmer art stage). We can make a rough guess as to when at least some functionality will be done, and start making sure we have more level designers and artists for when that happens.
And the plan will be wrong. Period, end of story, no matter what. Your estimates will be off. Things will change, your knowledge of the problem will increase, and you will have to adapt.
So, in that case, what’s the point of planning? In this case, the simple act of planning gave us a good amount of the information that we needed at that moment to make the decisions we needed to, and could make, right then. It gave us enough information to start making staffing decisions, and helped us define the scope of what we were doing. It made sure that we thought the problem of “how do we make levels” through, and didn’t miss any major pieces of that particular puzzle.
We didn’t try to nail down too many specific details. We didn’t say exactly what each dungeon should be, or how we were connecting areas, or anything at all of that nature. Those are details that are unimportant at this time. They don’t impact a single thing that we’d do for the next month or so. So, instead of wasting time arguing about them now, do another planning session in a month (or 2-3 weeks, or whenever), revise the plan as your knowledge has increased, and work from there.
People using agile development (I refuse to use “agile” as a noun or to capitalize it) do plan. They plan a lot. They just see the value of planning in the information that comes out of the exercise, not the specific set of dates and requirements.
This seems directly counter to what I’ve seen in BDUF-type organizations. Plans, in those orgs, tend to focus excessively on the detail level, and actually focus less on the high-level decisions. In my experience, those are the wrong things to worry about at the beginning of any project. They’re almost always easy to change, require knowledge you don’t have anyway, and subject to iteration. Yet, somehow, I see planning documents that focus extensively on these minor details, and merely imply the larger decisions via these details.
Permalink
12.02.07
Posted in General development, Uncategorized at 3:43 am by Kyoryu
Everyone’s had to deal with it. The big, nasty project, where everything links to everything, and anything you do can have seemingly random effects in apparently unrelated areas.
So, let’s look at some ways to avoid that.
But first, let’s define what we mean by “coupled.” Two pieces of code (classes, modules, whatever) are coupled if they have to know how each other work in order to function. Couplings can exist one-way, or two-way. Some coupling is good – programs probably wouldn’t do much without them.
Where the sinister evil starts to sneak in is unnecessary coupling, and unnecessary intimacy.
To start with, let’s look at a piece of code:
public class SomeClass
{
public void DoSomethingToCharacter(Character c)
{
string username = c.Connection.User.Name;
}
}
Okay, so it doesn’t do much at this point. But it’s a pretty good start as to where there might be a problem.
And what problem is that?
Well, it’s pretty apparent that this method needs to do something with the character’s name. But to get that information, we have to go through both the connection and user properties.
This opens us up to problems. First, it means we have a physical dependency on those classes, which can create compilation time issues.
Secondly, it means that to get the data we want, we have to know quite a bit about the class we’re using. We have to know that it has a connection object, and that that object has a user object, and that object has a name. I don’t want to know that much. I just want the user name from the character!
The worst problem is that we open ourselves up to build breaks every time we use this access pattern. If any of those classes change, we break. And probably in lots and lots of places.
Let’s look at an alternate:
public class SomeClass
{
public void DoSomethingToCharacter(Character c)
{
string username = c.UserName;
}
}
Ah, yes. Isn’t that pretty? Now our code only cares about the character we class. We don’t know how many layers of ugliness are going on underneath, and we don’t need to. Even better, if one of them breaks, the damage is going to be limited to inside the character class itself – which means an obvious place to fix, instead of hunting down those chains of objects all over your codebase.
Oh, and for what it’s worth – those chains of objects have a name. They’re called “train wrecks,” after all of those objects smashed into each other. It’s not a term of endearment.
And what I’ve been describing has a name as well – it’s called the Law of Demeter. It basically states that a given method can only use methods internal to objects it owns, on objects that are passed in, or on objects it directly creates.
Permalink
09.12.07
Posted in Uncategorized at 12:57 am by Kyoryu
No, really.
The usual analogy that we here is that software development is like building bridges. This makes for a nice picture, with lots of little programmers carrying around bricks made of ones and zeroes.
But, it doesn’t seem to fit reality very often. And attempts to build software in that way tend to have certain, repeated issues.
So, maybe, programmers are more like artists? Sounds good. But, what does an artist do? How do they sit down and create a masterpiece painting?
The first thing that an artist will do is do some sketches – studies of individual parts of the final painting, some sketches giving the overall composition. At this point, they’re trying out techniques, working through tricky areas, and deciding how they want the painting as a whole to look.
Some of these sketches may end up relatively detailed, but they’re all ultimately designed for one purpose: To be discarded and thrown away. None will be the ultimate painting. They’re nothing more than preparation.
So then, the artist will begin by stretching his canvas, putting on a layer of primer/gesso (or not), and then generally doing a sketch right on the canvas. This sketch is extremely rough, and is only meant to show the general size, shape, proportion and position of the final visual elements. As the artist puts down these lines, he puts them down very lightly, until he has more confidence in the final placement.
Next comes the underpainting. The painter will then work with (generally) a single color of paint at various dilution levels, to block in the large areas of light and shadow for the painting. The painting will still have a loose, unfinished look about it, but the major forms will start to be visible.
As the painter continues, they will add in other colors, begin to add large details, and refine the painting. Normally, they will work more or less uniformly over most of the painting – they won’t heavily favor one area over another, unless doing so is part of the composition.
To me, this is much more a description of the software development process than any engineering analogy. When we write software, we first might make prototypes to test out individual concepts or systems, knowing full well that most of them will be thrown away.
When we start the program for real, it is generally best to start with only bare functionality in the basic systems of the application. Not doing too much too fast leaves us in a better position to change our minds if we find we made a mistake.
As the application grows, we start more rigidly defining the areas, always taking care to look at the whole of the application instead of just the one part we’re working one at the moment.
Permalink
08.29.07
Posted in Uncategorized at 2:08 pm by Kyoryu
Just wiped out every trace of the old site. I didn’t like some of the writing, and wanted to start fresh.
With the wipe comes a commitment to post at least weekly, and to try to use a more structured writing style. More to come…
Permalink
« Previous Page « Previous Page Next entries »