Skip to main content

Debugging? Write more tests!

I see a lot of people on the internet talking about writing unit tests.  While they can provide tons of advice on when you need unit tests, I don't see a lot of people talking about how to start.  I mean, you're not suddenly unit testing on a brand new project.  You're going to likely start with a few thousand lines of spaghetti code.

So where do you start with that?  How do you test the untestable, extremely tightly coupled code that throws errors on every page?  Do you even bother?  Shouldn't you fix it, refactor it, then test it? ABSOLUTELY NOT!*

Unit Test First!
I cannot emphasize enough how important it is to test the code before you change it.  How are you supposed to ensure that existing functionality is kept if you don't find out what the existing functionality is?  Sure, it threw a bug on every page, but they were expected bugs.  They might be accounted for elsewhere in the code, and your change could break everything.

OK, that's out of the way.  You can read lots of websites that try to define what a good unit test is.  The truth though, is that if you're not writing a test in the first place, you're problem isn't defining a good test.  When I get a user story, my first task is to write a test that would verify the functionality expected in the story.  Usually, this test ends up being an integration test, though it's technically called an acceptance test, but it also helps me to define the parameters of my unit tests.

So if you decide to heed my advice, to Unit Test before you refactor, and you start with a massive pile of spaghetti code, the only place you really can start is with some integration tests.  Then you find the units you can test, then you slowly start to decouple, making sure all the tests pass as you do.  This is how you start testing code.

As a wrap up, let's give a realistic coding example.  This is our mess of code in a very generic high-level diagram.
You have an ugly database, with an ugly data access layer, all intermingled with ugly code.  Your code probably has lines in it like this:

var sql = "SELECT * FROM codeRepo INTO #tempCode WHERE codeName = '" + fixName + "'";


And half of the company's software passes over that line of code at some point.  This code is not only not unit testable, it's dangerous to test because of the nasty side-effects it could have.  So hop on over to Dev, and build some integration tests.  You'll start with some standard use cases like: CanSaveFiles() or CanBookClients() and this may take a while (a long while).  But eventually, you'll get to the point where you can create a test called ClientCodeRepoFixTest() and you might not even know what the outcome is going to be.  That's OK!  That's what a trusty debugger is for!.  You will just have to figure out what this ugly bit of undocumented code does.  Not only will your job security go up enormously after you do, but you'll be able to make the code work, decouple it, refactor it, and make the whole company make more money.  Then you have a reason to go talk to your boss about that bonus he forgot to give you.

*This is just my opinion, please disregard it if you're a troll

Comments

Popular posts from this blog

How to identify a skilled programmer during an interview

How does one identify a skilled programmer?  No company that has interviewed me could tell the difference between myself and other programmers they'd interview.  The interview process is truly a game of luck in this industry--on both sides.  Both the programmer and the company are basing their actions entirely on luck.

Companies have come up with numerous methods to attempt to discern a good programmer from a bad one.  The best tricks they have include a series of math problems, algorithms, problem solving technique tests, and even obscure programming questions, some without definitive answers.  As an example: Is there an authoritative source of information on the core principles that define object oriented programming?  I've heard everywhere from 3 to 7.  In a field of research about a synthetic concept, an authoritative answer is almost impossible to obtain.

Programmers were then forced to study to the interview.  Careercup is one of my favorite sites for this.  This almost …

Dependency Injection - You're doing it wrong!

So I have worked at a lot of places and seen a lot of different styles of programming.  Early on in my career, I became acquainted with the concept of dependency injection.  It was a hard topic to grasp, but I have learned understand it deeply.  Now, when I step into a new application that uses it, I can very quickly see the flaws in the implementation, and there's a common one I want to talk about today: global singletons.  But we'll get to that in a minute.

What is Dependency Injection?
Dependency Injection is exactly what it sounds like.  You use it to inject your dependencies. The unique part about Dependency Injection though, is that you can do this at runtime.  Now this sounds fancier than it is.  By inject we don't mean they're downloaded for you.  You still have to have all of the parts installed where you want to run your app.

Dependency Injection is somewhat of a complicated topic to a newbie.  Let's start with defining the word dependency here.  Specific…

Managing Programmers

Working with other programmers is tricky.  That said, it's nothing compared to the job of managing programmers.  One of my favorite quotes about Perl is that (paraphrased) "a Perl developer is like a rockstar.  Now imaging having a bunch of rockstars in one room together and you will understand why you don't want an entire team of Perl developers."  It's not about Perl here though. What's important to understand is that any developer worth his salt is going to be like a rockstar.  And yes, there are a lot of professional developers out there who aren't worth their salt, but that's for another post another day.  Rockstar may not be the right term here, but think of it this way.  These guys are smart.  They may not be geniuses, but there's going to be things that they know that you don't and probably never will.

I've seen it more than once and it's not going to make some Product Managers happy, but I'm going to state a fact, an eleph…