A Little Background
As software developers we solve problems for people. A client comes to us and asks us to create solutions. It is our job to take our knowledge and experience to deliver the best product for their needs. Like a carpenter, a painter, a surgeon, and yes even like an engineer we have to study and practice and understand our craft in order to deliver.
I recently watched a talk by Patrick Lioi titled Keeping Your Inner Architect On A Leash in which he goes through a sample program with life-like pressures that require the program to change. These pressures include bug reports, features requests, feature changes, etc. It is a great walk through of only changing the code according to the amount of pressure you receive and not letting yourself get carried away.
After watching this video I have decided I need some practice in controlling my inner architect. I sometimes have the tendency to refactor everything. I think about all the possible problems and obsess over design patterns and clean code and making sure it is perfect. Now don’t get me wrong. All of these things have their merit, but not at the expense of delivering what is needed when it is needed. It can especially be a problem in a customer’s code. They probably don’t want you to change their entire code base and they certainly don’t want you to waste their time or money.
With that in mind I am going to start with a sample client project similar to Patrick’s but with different requirements and see how it evolves. My goal is to practice controlling my inner architect as well as honing my skills and knowledge of design principles and patterns, refactoring, and delivering what the client needs.
So here it goes.
We have a customer who has a problem to solve. A file containing ten random numbers separated by commas is placed in a folder on a computer at different times. Unfortunately their Do-What-Ever-It-Does-With-The-File machine cannot do anything with 10 random numbers. It needs those numbers sorted from least to greatest. They have hired you to create a program that an employee can run that will do the number sorting. They can’t leave the task of sorting to a human because mistakes in sorting would be costly.
The client has outlined the requirements in a succinct paragraph.
The program should sort the 10 random numbers in the file from least to greatest and write them back to the same file. The file will always be in the same place and there will ever only be 10 numbers.
This seems like a pretty straight forward problem and one that can easily be solved. The customer requirements are clear, so let’s get to work. We don’t want to over engineer this thing. We only want to build to the requirements.
Here is the algorithm I think will work for the requirements.
- Read in all the text of the file
- Split up the text into individual numbers
- Sort the numbers
- Overwrite the file with the sorted number sequence
Now that we have our requirements and a plan to move forward, let’s move forward with writing the code. According to our defined algorithm the first thing we need to do is read in all the text of the file. No problem. First we read in all of the contents of the file at once. This is standard stuff and .NET has this built in for us.
Next we split the contents of the file into ints using the built in Split method of string. We need to convert all of this new strings into integers. We can do this with a standard for loop, parse the string using Int32.Parse, and add it to an array of integers.
Sorting is easy enough. Again the .NET Framework provides what we need: Array.Sort.
Now we are getting somewhere and closer to completing the requirements of the project. All we have to do now is write the new sorted array of numbers back to the file. First we create the new text string out of the sorted array of ints by looping through the array with a standard foreach loop adding each int to a string. This will become the new content of the file.
Finally we write the new content to the same file. Again, .NET has us covered.
And that finishes up the solution. We have satisfied the requirements of the client, but before we deliver we need to take a step back and see what, if any thing, we can do to this code to follow best practices and deliver a solid product.
In the meantime you can get the project on Github.