Enhancing the Architecture

In Development by PaulLeave a Comment

In my experience, one difference between a “junior” developer and a “senior” developer is how quickly (if ever) they realize that the architecture needs to be enhanced. Let me give you a simple example…

I was working on a project that stored & manipulated file paths. Sometimes the code worked on a full path like this:

k:\Test\t.txt

Other times it worked on a relative path like this:

Test\t.txt

And sometimes it just worked on a filename:

t.txt

Sometimes the paths were combined or split apart. For instance, “k:\Test\” might be combined with “t.txt” to form “k:\Test\t.txt”.

When I first started working on this project, I stored all the paths as strings. That usually works fine. But this project was different — there was a lot more path manipulation than usual. And I started finding some bugs. In some situations, I had forgotten to include a trailing slash. In other situations, I concatenated two pieces together that both had slashes. I started fixing those bugs one-by-one. But then I realized something: I really needed to enhance the architecture. So I created the following classes:

  • FileName
  • DirectoryName
  • FullFilePath
  • RelativeFilePath
  • FullDirectoryPath
  • RelativeDirectoryPath

Each class had a constructor that accepted a string. Each constructor did three things:

  1. It verified that the correct type of string was passed in. For instance, the FileName constructor would throw an exception if a full path was passed in.
  2. It “fixed up” the paths in certain ways. For instance, the RelativeDirectoryPath constructor added a trailing slash if needed. It also converted everything to lower case (to make path comparisons in other parts of the app easier).
  3. It saved the path so it could be returned from ToString() later.

I also defined some operators and methods to help me convert between the different classes. For instance, I could concatenate a FullDirectoryPath and RelativeFilePath into a FullFilePath using the “+” operator. But there was no such operator to concatenate a FullDirectoryPath and FullFilePath — that made no sense.

Once I started using these classes instead of strings, the bugs quickly disappeared.

I’ve worked with some junior developers that would just continue fixing the bugs one-by-one and never make the leap to creating these classes. But a more senior developer would notice what is happening and realize that the architecture needs to change. The final result? A better architecture that also prevents future bugs. And a useful set of classes that can be re-used in future projects.

Share:

Leave a Comment