graham.reeds/

Improving Legacy Code

February 16th, 2011 :: graham.reeds
Categories: C++ ::Programming ::Work

My suggestions to a client on improving their legacy code.

Initially:

  1. Move all source code from VSS into TFS.
  2. Ensure all code needed to compile is within the workspace.
  3. Ensure all dependencies are correct so compilation of any one module will automatically compile needed dependencies for that module.
  4. Add UnitTest++ (or CppUnit – whatever floats your boat) to project.
  5. Add Log4Cxx to project – more man-hours have gone into writing this than your own system.

As you develop new code or extend older code:

  1. Move the logical projects into folders. Create another physical project alongside this project. Usually you will call this Test or Tests.
  2. For each module you are editing in add a c++ source file in the test project. Format for this will be Test.cpp.
  3. Write the test for the part you are changing, this should be as concise as possible. See Feathers “Working Effectively with Legacy Code” and Kerivsky “Refactoring to Patterns” books for tips.

And eventually:

  1. Use Msbuild for automated building of source. This will build all code, every time, and warn of errors and can even bundle things into installers, etc.
  2. Turn on treat warnings as errors and fix.
  3. Get a static analysis tool and slowly fix the errors (only about 20 are relevant – the rest are pedantic).
  4. Make everyone use “Convert tabs to spaces” and set to 4. (HICPP)
  5. All if and else statements must be enclosed in braces, even if a single line. (HICPP)
  6. Apply SOLID principles to code (Single responsibility, Open/Closed, Liskov Substitution, Interface, Dependency Inversion).
  7. Evangelise to others in team.

Others:

Did I miss anything?

1 comment.

Long time…

December 30th, 2010 :: graham.reeds
Categories: Life ::Misc

It has been a long time since I posted. Mainly due to work and a lack of excitement to write about.

I am writing this on my HTC Desire phone which less than 24 hours ago deleted all my text messages, some 6,000 in total. Most were boring ‘What time will you be home?’ type messages but some were important (pictures of my cat that passed away earlier this year, addresses, etc.).

This fault has been around since 2009 and if you do a Google search for ‘android deleted all text messages’ the amount of results are frightening. The issue on the Android issues forum has 760 (at the moment) posts complaining about this problem but it has only been given Medium priority. Part of the problem is that people don’t have the sdk installed to get the debug info once it occurs and since it seems entirely random it is nigh impossible to reproduce.

So I now have a backup plan in place (shouldn’t the message system have that to begin with?) but no way of recovering those lost memories.

I think I have been burnt too many times by Android now. Apple have had a few problems but nothing as major as deleting all a users texts and when Apple do have publicized issues the are fixed fairly quickly.

2 comments.

Update

October 19th, 2010 :: graham.reeds
Categories: Life ::Misc

Updates are supposed to bring about improvements but the latest Android updates have been a definite step backwards.

0 comments.

Birthday

September 5th, 2010 :: graham.reeds
Categories: Life ::Misc

Would of been Gizmos birthday if she was still with us.

Still miss the little one.

0 comments.

Crowd source pavements

September 1st, 2010 :: graham.reeds
Categories: Leisure ::Life ::Misc

Just looking at where Google would have me walk to get to a local dry cleaners. It’s literally across the bridge an turn right and that isn’t taking into account turning off early and heading across the park. Surely some enterprising Googler could add a feature where pavements and cycle paths could be manually added and voted on for reliability?

Employ me and I would do it for you!

0 comments.

Object of desire

May 16th, 2010 :: graham.reeds
Categories: Life ::Misc

This post and the one that came previously, were written on my HTC Desire.

The WordPress app will allow me to write posts as and when they occur to me instead of what happens now which is they occur to me but are forgotten as I am not near a keyboard at the time.

A big hurrah for modern technology!

0 comments.

C++ and Maven sitting in a tree

May 8th, 2010 :: graham.reeds
Categories: C++ ::Life ::Programming ::Work

…but they definitely were not K-I-S-S-I-N-G. In fact they weren’t even holding hands.

As a test at work for a new project I was asked to look at using Maven, which is currently being used to great effect by the Java programmers, with C++. A new project has arisen that requires proof that there is 100% code coverage, has various code metrics and need to be presented at the end of it.

As usual the first thing I did was canvas the ‘Net for other peoples experiences. There is no point in reinventing the wheel if you can help it. Unfortunately peoples experiences weren’t good. On the plus side they weren’t recent.

So I set off looking at the Mojo Native Plugin. One of the first things that concerned me was the lack of direct Unit Testing support. I had noticed that several other language plugins had direct support for unit testing. However I can work around that. The other major concern was running the code metric tools that were being evaluated concurrently.

Eventually, after nearly whole frustrating week, I gave up with the Mojo tool. It was simply unworkable. Didn’t help that I am not use to paying the angle bracket tax. I then shifted my focus to the Jade Native Plugins. This is actually a fork of the Maven plugin but has moved along and supports unit testing and static analysers.

This on the face of it seems like a good thing but was again unworkable, but this is probably me more than them – I am not used to editing the pom.xml and getting the relevant info from provided examples is not easy especially when the underlying code seemingly doesn’t work with the examples.

Feeling increasingly like a failure, I turned to Hudson which several posts had pointed to. This was a breeze in comparison. A massive relief as I had a build up and running quickly and a couple of days later after overcoming a few niggardly problems I had a build that had unit tests and code coverage.

I presented my findings and guess what we picked? Option C: Maven2 execing batch files. Did I mention the PM is/was a Java programmer?

0 comments.

Creating directories

April 8th, 2010 :: graham.reeds
Categories: C++ ::Life ::Work

I recently had a problem at work where an application failed if the user tried to use UNC paths in a application that was ran as a service.

This service was written in C# that called into C DLL. Not only that, but the DLL was written in VC6.

So after confirming I could recreate the bug I removed the service and installed the debug version. I tested again and it was still there. I then added System.Diagnostics.Debugger.Break to the code and stepped in and through until I got to the calls to the DLL. The call stepped straight over. Hmm.

So what next. Maybe it wasn’t picking up the debug DLL.  So I went around and deleted every other version of the DLL. Didn’t fix it and broke most of the support apps.  Maybe it was a weird VS2K5/VC6 interaction.  So I rebuilt the library in Visual Studio but it still failed.

I could see the problematic code, but I couldn’t step in to see what the parameters were when it was there. So I created a small console app and transplanted in the problem code and called it. Worked as expected. Which brought up the idea that something was changing the parameters – but why, where and again why?! So the simple console app was extended to call the dll with every single call from the service in it with the same parameters, finally ending where the problem happened.

However it didn’t happen where I expected it to – the breakpoint I placed was never hit.  I thought that it wasn’t debugging properly still but it broke successfully if I placed a breakpoint at the start of the code. But if the code wasn’t being reached where it called CreateDirectory how comes there is a directory in the root of C:\ with the name of unc machine?

It turns out that it called a function earlier in the code than expected that uses mkdir – and the code that uses CreateDirectory never gets used due to the parameter tests that are done.

I looked at this code and thought about fixing it but there was over a hundred lines of strcat, strcpy, strtok and moving pointers around.  I decided that it was horribly broken and rewrite was in order.  So I looked on the net because this is a good case of reinventing the wheel.  Most sites, including my beloved StackOverflow, suggest using Boost.FileSystem which in my case is a non-starter. So I had a think about it and this is what I came up with after one false start:

BOOL CreateDirectories(char* path)
{
	BOOL status = TRUE;
	char* p;
	char temp_path[256];
	strcpy(temp_path, path);
	do {
		p = strrchr(temp_path, '\\');
		*p = '\0';
	} while (!SetCurrentDirectory(temp_path));
	do {
		p = strchr(temp_path, '\0');
		*p = '\\';
		status = CreateDirectory(temp_path, 0);// do not check because this will fail on 'too\\many_slashes'
	} while (strlen(temp_path) < strlen(path));
	return status;
}

Now you can get away with removing the temp_path but I copied the original string to be safe, and I should of used MAX_PATH instead of hard-coding 256 but I think the code is quite elegant (though the WordPress formatting doesn’t really do it justice).

There’s a warning there – not to compare against the status in the while clause as having two slashes instead of one will prematurely cause the function to exit. Ignore the error that the function returns (ERROR_ALREADY_EXISTS) and the following time through it will continue to create the directories.

0 comments.

Resize a VirtualPC disk

October 28th, 2009 :: graham.reeds
Categories: Life ::Misc ::Programming

My 10GB disk on my VirtualPC was nearly full to overflowing so I went looking on how to resize the virtual disk. There’s a free product that does this for you, but to quote an old song “I waved that thing all over the place, my boomrang won’t come back”. In fact I was using the app correctly but not understanding what the app actually did. This guide showed me the way.

0 comments.

Update

September 13th, 2009 :: graham.reeds
Categories: Galaxy ::Leisure ::Life ::Misc ::Programming ::Work

It’s been a while since I posted, so what has been keeping me busy?

Work, it’s getting easier – I think I am getting into the VB6 mindset, but I still hate VBA with a passion. When is the nice .NET extension becoming available? Is it already, and could Reporting Services save our souls (not to mention our sanity)?

Space is coming along very slowly. Any time I sit down to work on it I have to spend about an hour refreshing my mind on what I have done – and I only have about 40 mins to do it in.

Had my birthday: One of my presents was an iPod from Mrs Bear – which gave me a lovely opportunity to hate iTunes all over again. This time I accidently deleted my play list, there is no undelete and then it synced it – wiping it. I’ve installed iTunes 9 but I don’t hold out any hope for them fixing all the problems with it. Another present is an ant farm – so along with my Sea Monkeys you could say I have Surf’n'Turf.

Another side effect of my birthday was my the choice of venues for a meal.  Since I’ve always wanted to eat there I picked Kaminaki and had some of the nicest fish I have ever eaten, and definately the nicest pitta bread ever. All this and despite the grumblings of some of my friends.

1 comment.

Older » Next Page »