Master Code Poet
Wielding the pen almost as mightily as the curly brace!
Application Logic Layer
This is a follow-up to my post Rethinking Strategies for Testability.
Since I wrote that post I’ve done a ton of research on the Command patter. I really shouldn’t have done all that much reading on it, because it’s a rather simple design pattern, but I couldn’t shake the fact that what I did wasn’t quite right, but I wanted to make it right. Since then I’ve come to the conclusion that what I’ve done really isn’t a Command pattern at all and I need to rethink it.
What I created was an Application Logic Layer, but this time I did it in the right way. I tried to do it originally as this big static class that was a pass through for database access. So the view model called the application logic, which called the database code, and returned back up to the view model. Now I’m creating separate objects to fulfill each need of the view model to access the database. The view models create these objects, and populate them full of data and then execute all the database code they need and perform any business logic on them that needs to get done. Then all those results are stored inside the object for future use by the view model whenever they see fit. The greatest part is I built in multi-threading into these little application commands and since they don’t update anything besides themselves, they’re safe to use wherever for whatever.
This isn’t to say that I think the command pattern is useless, because I think it’s actually very powerful, and I’m going to look for a way to try to implement it in future projects. I’m just also going to stop fooling myself into thinking what I did was the Command pattern, when it clearly wasn’t.
Event Aggregator – The Right Way
I have a question on implementing the Event Aggregator pattern the “right” way. Right now I’m only using it to push a single type of event, so the answer is simple. I’m forced however to look at the larger picture and wonder who to use it for an area where I have a lot of different kinds of events.
Should each of my aggregators be written as single entities with no framework in place for the pattern at large? How do I write this model into a framework if I were to do so.
On the one hand, there should be an easy line of demarcation in most systems that would allow me to classify types of events and split these events into separate aggregator objects. I wouldn’t have to worry about god-like event bus corrupting my systems too much.
There isn’t too much code in this pattern, especially not in the aggregator object itself. What code there is, I wonder if I can write some sort of framework around to ensure that these objects abide by the rules, even when I get lazy and my project is past due. I’m thinking an abstract and generic parent type to inherit from. My concern then becomes how to handle multiple types of events that all fall under the same umbrella.
What if I want to have one aggregator be the bus for 3 different events? I don’t see a good way to make this situation generic, and I don’t think that having a completely separate bus for every single aggregated event is a good solution, especially when the events logically do go together.
For now I will keep the event aggregator in my repertoire, but I won’t try to build any framework around it. What do you think? Am I making a huge mistake?
Rethinking Strategies for testability
I’ve lost a ton of testability in my recent code. While I like the way it’s structured in that it’s very consistent and very clean so I can make changes quickly, without testability I begin to loose a grip on the effects my changes have on the system. This needs to be fixed, but I’ve been at a loss as to how to do that.
Yesterday I really started to flesh out a library of classes to use on my future projects. I’ve noticed certain classes needed to be imported to other projects and I have some free time so I am going to write out this library of classes to use since getting third party frameworks accepted is way too much work. In that process I took another look at my desire to implement some form of a Command pattern in my code, and I think I finally have something that really works.
Part of this comes from a multi-threading nightmare I got myself into and quickly hacked my way out of, but the fix wasn’t pretty and I knew it wasn’t. I think what I’ve settled on really is pretty.
Here’s my general concept. My Commands are objects that are specially written for a specific task, but all follow the same pattern. That pattern is to first go out and get all the information needed and store it within themselves, so their interaction with the rest of the framework is read only. They follow the BackgroundWorker event pattern (indeed they have an embedded BackgroundWorker object they use to do the work) so the calling object can be notified of progress changes and of completion. Each command has the option of being run on a separate thread or of being run on the calling thread. When they gather all the information they do any manipulations on it they need to and store the results in themselves and either return or notify listeners they are finished. When they are finished the calling objects can retrieve the data and make the necessary updates.
This to me follows the exact same pattern a JavaScript “prompt” or Visual Basic MsgBox or InputBox follows. They all block the thread as they are modal, but the example still holds I think.
This would allow me to test the command objects separately, and if I use an IOC approach and make them all follow an interface I can shim some mockups into my ViewModel classes and increase testability on the ViewModels as well.
Speaking of mock ups, does anyone have any great tools or links to in depth mock testing strategies? I really think I could improve there. Might be my next project.
Quarterly lessons learned
So I’ve completed a couple projects and I have a great method down. It’s been almost four months of code writing so I thought I’d take some time to write down lessons learned so far.
Not repeating myself is hard. I’ve identified some areas of code that I reuse through projects and I need to separate that out as a sort of framework.
Separating out business logic into a separate layer isn’t as straight forward as you would think. I keep ending up with near god classes and that’s not helpful.
Removing the business layer logic actually made my software a lot easier to manage but I lost a lot of testability.
In order to improve testability I think I need to come up with a good MVVM Command pattern implementation.
So far my concept for a command pattern would be abstracting my view-model layer out into interfaces and creating a layer of objects that represent commands and interact with those view models.
I’m convinced more and more that code generation is important an wish I had time to come up with a solution. Writing repetitive tests sucks the life out of me, but it’s important it’s especially important when using a language that isn’t strongly typed. My testing strategy could be improved some by creating a testing framework based on my development strategies and a strong use of reflection.
That’s all I have for now. Can’t wait to get my frameworks written and dart my next project.
POC Proved True!
Now that I’m wrapping up my “Best Code Ever” project, I’m faced with wondering what it did for me. My hope was that it would prove to me I can write great code, and show me some new and inventive ways of accomplishing that start to finish.
I just got a new project at work for a small little tiny teensie-weensie (get the idea yet) application. Simple concept, pick a file, it does some magic, and uploads that file to a predefined server for parsing by other applications.
I started on the project with reckless abandon, and with little to no thought whipped up a solution that was not only correct, but structurally sound.
While the feat isn’t impressive as it stands by itself, it does prove in my eyes that my effort on the last project was worth it. I did in fact learn something, and learning things is always a worthwhile endeavour.
Pre-Code Review Reflections
So I’m coming up on my first peer code review, of the “best code I’ve ever written” and I want to reflect some on how this project went before the review actually happens. Look for another post after my review.
I already know I’m doing too much for what they want me to do, and they want me to not work so detailed and work a little quicker. I don’t want this to change too much how I write my code. My biggest fear is I won’t be able to write unit tests moving forward (at least not for “small” projects), but I really believe in test-driven development and I hope they let me keep it.
In my last post I wrote about my nightly build work. This should be expandable so I can use it going forward. Having those statistics available to me every morning will help me so much in development. I spent way too much time fixing code that was out of control on this project. Plus watching the graph of SLOC over time will just plain be cool. Since this is going to come for free, I envision I’ll keep it. I hope to have better reflection on this after I execute a project with it working the whole time.
My documentation was overboard for this small project, but that was largely because I was attempting things I’ve never done before, and I wanted the documentation for myself. Now that I’m comfortable in the MVVM architecture and believe in it’s applicability to the work I’m doing, I think it will come more naturally to me. I’m hoping to be able to write the MVVM architecture into simple applications without having to document every layer like I did this time.
The other piece of documentation I did was a user requirements document. Honestly, I keep losing track of user expectations so I’m going to have to come up with something for this, but I do think I can cut down the document into something smaller. Hopefully this will be able to satisfy my needs and their desires.
I also spent copious amounts of time checking and double checking my unit tests. This is because I only half heartedly used test-driven development, and so I couldn’t trust my unit tests. Ideally unit tests should never have to be “audited” unless a bug is discovered in user testing. Any new features or changes to features should first be written into the tests, then be implemented into the code.
I have a coding standards document, that I’m going to keep to myself and use on all my projects. Now that I’ve done a personal code review with it, my hope is to just write the code that way the first time, and then if anything comes up in a peer code review I’ll address it then, which should cut down on the time for that. Furthermore, I’ve proved the efficacy of the design standards document, which means I can trust it, and any code I write in the future better have a damn good reason to need it to change.
Nightly Builds
I read a long time ago (in a galaxy far, far away) that having a nightly build was one of a few litmus tests that this person had which determined whether the software shop was any good or not. I always scoffed at that because I felt I was a good software developer, but nightly builds were only important for people who had complicated build procedures. All of my work was largely done by pressing the “Build” button in whatever IDE I happened to be working on. Even worse for web work, I almost never “built” anything for the web.
I just became familiar with MSBuild (skipping NAnt due to the Microsoft Acceptance Factor typical in .NET development places.) and setup my first nightly build for my current project. Again my project is only 10k lines of code, but I wish I had this place before I wrote a single line.
What does my nightly build do? First it executes the build for my project and my test library using the build file Visual Studio automatically generates. After that I have it run the NUnit tests from the test library, and those results are transformed using an xslt into something more usable. Then I run my Gendarme analysis, outputing xml and html files, and I run a SourceMonitor checkpoint, dumping the data from that checkpoint as both a kaviat image and an xml file.
This is going to be a god-send in my next project, I can tell. Having this information on-going every night will help me understand where my code base lies each morning, since usually I leave it at night in the midst of very low level implementation details.
There is room for improvement however. First, and this is a architectural issue larger than just myself, I wish my nightly build checked my project out of a repository somewhere. Ideally this would great if we used git because I could check it out from my local git repository. We use CVS and I do not commit anything that is broken or unfinished.
Next I wish it was a little more expandable. Currently I’m relying on command line tools provided by these products to get them to work. This leads to long and not immediately helpful XML build files with everything being a command line execution. This is possible because MSBuild supports custom tasks, but I haven’t had a chance to do that just yet.
I also need to have a seperate XML document to support my SourceMonitor exports. Rolling this into one file, or possibly supporting it with custom tasks would be ideal.
Beyond that, there’s some “prettying-up” desires I have for the XML transforms, but those are largely beyond the scope of a development post.
What other things should my nightly build be doing?
New Tool!
Continuing from my latest post on tools, I did find a free static source code analysis tool, that I’ve been using. It’s a little cumbersome to work with at first, but it doesn’t take much to get used to and definitely get the job done.
Check out SourceMonitor
One other thing I found to make Sandcastle slightly more manageable to work with: SandCastle Help File Builder. It let’s you build the documentation in a very Visual Studio.NET friendly way. The UI is built to be very similar so it’s comfortable at least.
Check these things out and let me know what you think.
Tools are Essential
Continuing my quest to write the best code I’ve written so far in my professional life I ran into some performance issues this week. To tackle this I did some searching around and found a pretty fantastic open source .NET profiler called SlimTune.
Download it here: http://code.google.com/p/slimtune/
This is a great little tool and has some really nice promise for future versions
NUnit has been my unit testing suite of choice. Remember I’m doing this in Visual Studio 2005 Pro, so I don’t get the Microsoft testing suite from the Team version. If I did I would probably use it, and if I transition this project to a platform where that is available I will probably move my tests to it.
NUnit is wonderful, and it does its job perfectly. I’d be moving it into the Visual Studio unit testing purely because integration is wonderful, and it would help me win over other people into test driven development.
Download NUnit here: http://www.nunit.org/
For static code analysis I am using Gendarme from the Mono project.
This is an awesome tool, and does just about everything I could ask of it. One nice feature, and maybe this exists somewhere else but I can’t find it, is just simple metrics for my code. Things like LOC counts, and complexity measures would be nice.
For testing possible areas of poor architecture, bad practice, or other impending doom it’s spot one. It suffers a little from VB specific nuances, but most of the .NET generic stuff do because C# is the gold standard.
Download Gendarme: http://www.mono-project.com/Gendarme
For my comments I use Sandcastle from Microsoft. This tool is actually a little tough to use. The GUI is a touch easier, but the command line being so tough makes it difficult to integrate into a building process.
Besides the clumsiness of getting it to run, the tool does what it is supposed to do perfectly. If you have any issues with the outputs from the software, you can edit the XSLT files or write your own and it will generate them to your specification.
Download Sandcastle: http://www.microsoft.com/downloads/en/details.aspx?FamilyId=E82EA71D-DA89-42EE-A715-696E3A4873B2&displaylang=en
For modeling I’ve been using ArgoUML. It’s really nice, and does everything I need it to do, but I have very limited use for it. I despise the modeler in Visual Studio because it makes you tie everything to actual code, which while you might think is useful, it isn’t. It’s frustrating and makes the models useless.
Download ArgoUML: http://argouml.tigris.org/
I’m always on the lookout for great tools that would help me get work done. I could really use a nice VB.NET code generation tool. As I mentioned above, getting software metrics like SLOC is something I am looking for. Currently I’m doing code reviews and wondering if there are any tools that could help me with it, although I think code review by nature has to be pretty manual and intensive.
Mission: Best Code Ever
Just got a new job, and I have my first project. Admittedly I suffer from a problem where I don’t complete all the projects I assign myself. I come up with an interesting problem, and work on it until I have a solution. Once I see the solution, I rarely complete the project. See, I’ve already solved the puzzle, so the rest is just boring grunt work.
Naturally, this isn’t an option at work. I have to finish the project. To keep myself interested throughout the project, my goal is to write the best code I have literally ever written.
Now I’m handicapped by some things. First and foremost I’m the new guy at an established place. They’ve been doing what they’ve been doing for a long time, so I’m not going to be able to change things over night. Second, I have to code in Visual Basic. The lack of a strongly typed language is the source of many headaches.
Here’s what I’m attempting to do.
1. Documentation. I want everything documented in an appropriate manner. This means not only code, but also proper documents outside of the code detailing requirements, source code standards, and other things. I enforce these on myself by doing personal code reviews where I take a step back and review my own code as inpartially as possible.
2. Enforce standards. I wrote myself a coding standards document (because to my knowledge we don’t have one currently anywhere so far) and will be forcing myself to stick to it throughout code reviews.
3. Testing. I’m actually unit testing everything and keeping track of all my unit tests. There are some units of code I have deemed as “unreachable” via unit tests. This amounts to less that 2% of my code base, and is due largely to existing protocols being written that I must adhere to. These don’t necessarily lend themselves to unit tests, so I’ve clearly marked them as out of reach so they are at least trackable.
4. Tools. I’m using a couple tools to help me in my endeavor. The first of which is NUnit. This project is written in Visual Studio 2005, so I don’t have access to the Microsoft Unit Test platform. NUnit has been invaluable to me, and I highly recommend it. I’m using Gendarme, a Mono sub-project, to analyze my code and help me adhere to best practices. Finally I’m using Sandcastle to transform my XML comments into HTML documents.
Currently I have about 90% of features implemented and it’s still exciting to me. I am hoping to impress the people I work with here, and this should include enough bells and whistles to do so.
What about you? Do you have any tools or methodologies I could use that would help me out or I might find fun to try?