Brendan Enrick

Daily Software Development

Speaking at CodeMash 2015

I am very grateful to be speaking at my 6th CodeMash. I’ve again been selected to present a precompiler on Software Craftsmanship (a topic I am very passionate about). I hope that everyone can attend my workshops. I’ve got two of them again, so I’ll be spending a full day working with software craftsman coding. We’ll be focusing as always on practicing, pairing, testing, and applying all of these effectively. We’ll work through ways you can use practice to learn new concepts and patterns to improve your skills at building great software.

SpeakerSelection

Once again, Steve Smith and I will be presenting these Precompiler workshops together, so you’re sure to have a blast!

Our sessions are usually held back-to-back, so that you can attend the beginner session in the morning and follow it up with the intermediate session in the afternoon. Last year, our afternoon session had to have extra chairs and tables brought in for all of the extra people who showed up. My goal every year is to put on a workshop that will be the highpoint of your CodeMash! Join us for some great coding, learning, and practicing of your software skills.

If you’re in the Northeast Ohio area and want to learn more about software craftsmanship, you should check out HudsonSC. Our October meeting has been scheduled already. We meet on the third Wednesday of every month in Hudson, Ohio.

CodeMash 2014 Software Craftsmanship Precompiler Workshop

CodeMash 2.0.1.4 started as one of the coldest CodeMashes on record. On Monday, during the set up time for the event, the temperature in Sandusky dropped below negative ten degrees and the wind chill kept it feeling much colder. It was severely lacking in snow, but the cold was enough to cancel many of the Tuesday precompiler sessions whose speakers were not able to arrive at CodeMash on time to run the workshops.

The conference has grown to the point where it now has two initial days of workshops on Tuesday and Wednesday. Then the conference has two normal conference days filled with sessions. Considering that all of my favorite content is always the precompiler workshops on the first two days, I would love to see the conference have workshops through the entire event. I am usually one of the precompiler workshops speakers, so I might be a bit opinionated on that. From another viewpoint though, since I am one of the speakers during that time, I am often wishing I could attend workshops during the same time as mine! I love running the workshops though, so I gladly give up the other ones. I just need to figure out how to be in two plaes at once (future goal).

CodeMash Precompiler Workshop Details

For those who attended my and Steve’s Software Craftsmanship precompiler workshops on Wednesday, the weather was much more forgiving. Most people had arrived by that point, which meant that our attendance was also good. This was the fifth year that Steve and I had run this precompiler event at CodeMash. We adjust, change, and update the workshop every year. In the past, we had to teach a lot more about the basics of unit testing, but now more people have at least some knowledge of unit testing.

Over the years of doing this workshop here (and similar ones elsewhere), we've found a lot of good techniques and exercises for working with the attendees of the event. We try and experiement with lots of exercises and katas at conferences, events, and user groups, because we're looking for great ways to teach these types of events. When we run them, we are always trying to make sure that most of the event is spent coding. Through this, our goal is not to give an exercise to the attendees. Our goal is to teach them how they can continue their exercising and learning after they leave the event. That's our goal.

We use a few types of exercises: gateway exercises designed to teach you how to practice, guided exercises leading you down a path slowly with steps for freedom along the way, and open-ended exercises giving great freedom and possibilities in final implementations. As part of the open-ended exercise, we often attempt to use that as a plane up which to build a specific type of implementation. We will choose a principle or a pattern and use this plane as an area in which to practice following the principle or pattern. This shows the students how they can leverage programming exercises as places to learn new principles and patterns.

Beginning Software Craftsmanship Workshop

In the morning, we start out our session for beginners just learning software craftmanship. This course is for people familiar with programming (using any language) that want to learn more about software craftsmanship and work on programming exercises with their peers.

As we do every year, I started off the discussion by explaining software craftsmanship and giving an introduction to its concepts. I explain the software craftsmanship manifesto, what it means to developers, and how it relates to the agile manifest. Through this talk, I always make sure to emphasize the importance and reasons for deliberately practicing. As the entire audience will agree, when under a deadline they jump back to their poor practices they know aren't the best. Practice is how we drill ourselves to handle the situation in our own desired way. I make sure that the audience knows as well as discussing and working with others. If I can encourage more people to attend and start software craftsmanship groups, I would consider this talk a great success.

Introduction to Software Craftsmanship Slides

Once we had the reason for why we're all here, we start getting into some of the meat of the workshop. Steve then presented his well-honed talk on software principles. Most of them are designed for object oriented programming, but many are applicable to nearly any language or paradigm. Along the way he of course will cover SOLID, and the slides all contain images from our Software Craftsmanship Calendar. The 2014 edition of the Telerik Software Craftsmanship calendar is out, and everyone at CodeMash received a copy of the calendar.

Software Development Principles Slides

While Steve is wrapping up the end of his presentation, I start passing around sheets of paper (yes, real paper) containing a printing of the first exercise we'll be doing. For our first exercise, we were doing FizzBuzz an exercise that is simple enough that it gets people used to the idea of practicing. This is our gateway exercise. We use this to make sure that everyone is set up with their tools. As long as their setup allows them to write code and run tests, it should be good enough. We include some extra credit with these, so that most people can at least get close to finishing the first part of our exercises. Some people will finish the extra credit, which means that we can have nearly everyone reach a stage of accomplishment. 

We also ask that the entire group pair up, so that they can work on the exercise with others. This accomplishes quite a few things, including helping those who don't have a computer with them.

FizzBuzz Programming Exercise

Interrupting everyone's coding is always a challenge, but I make sure to do it nicely. A microphone helps with this! Now that everyone is set up, we take a break from the coding to talk about practices that we promote to the group. The first of these practices is testing. As is still the case, most people are testing or at least familiar with the practice. As a result, I cover this topic briefly making sure to emphasize the different types and their importance. There are still many groups forced to write integration tests instead of unit tests for example. We discuss how to avoid that situation in the afternoon when we discuss more good practices. 

The second practice that we discuss is continuous integration, however, since we don't have the time to set these up on everyone's computer, we only discuss this briefly and suggest that people try them.

The third practice is pairing, a practice that we've already had the group start in their first exercise. Not only do I cover the methods of pairing, but I point out all of the good pairing practices that everyone was using without thinking. I then point out the importance of changing partners and typists frequently. In their first programming exercise, very few people change who is typing. Since pairing is such a hard activity, we also make sure to cover all of the things that you can do to improve your pairing by changing the environment in which you're working.

Software Craftsmanship Practices Slides

The String Calculator is a guided exercise in which you only read one step at a time. Each step has parts to it, but you do them one at a time. This follows the normal scenario of receiving your requirements a piece at a time. It also keeps you following a plan and gives you a great chance to follow YAGNI. The students do well with this, and it's an exercise that does very well on paper, since you can fold the sheet over the future steps. 

String Calculator Programming Exercise

Intermediate Software Craftsmanship Agenda Workshop

After lunch, we started our second workshop. This one is a half-day workshop designed for people who attend the morning workshop or are already somewhat familiar with software craftsmanship, deliberate practice, or who just couldn't fit the morning session into their schedule. 

Steve starts off the talking in the afternoon discussing design patterns. Steve is an expert in design patterns and refactoring, so he's a great person to present this workshop with. Steve covers a lot of design patterns in this talk. You'll notice that a lot of these patterns have overlap and many of them stem directly from one or more of the principles mentioned in Steve's morning talk. This is not a session to miss!

Design Patterns Slides

Once we're done with the design pattersn talk, we make sure that we start everyone on an open-ended exercise. This year's was the greed dice game scoring exercise where you create code that will score the results of one roll in the greed dice game. It's a simple exercise, but can get a bit unwieldy if you're not refactoring or implementing a good design. We don't suggest that anyone try any specific design for their first attempt.

Greed Game Programming Exercise

We follow up this programming with even more programming practices to follow. I do this talk, which is about more advanced types of testing using mocks, fakes, stubs, etc. We want to make sure everyone knows how to do these. We sometimes do an exercise that can leverage these skills, but we did not this year. We instead showed a quick demo of doing this with a logging system that logs to the file system.

Advanced Testing Practices Slides

Back to Greed again for the final exercise of the day. We finish out the workshop asking everyone to program the Greed game scorer again. This gives the students the chance to attempt a more complex solution to the problem this time around. We had everyone attempt to implement a Rules Engine for the second attempt, which will allow them to follow the Open/Closed Principle.

Overall we received a lot of wonderful feedback from our students, and I hope that everyone who attended goes on to continue doing what we taught in that workshop. Thank you all for attending. Thanks CodeMash for putting on a wonderful event and inviting me and Steve to run one of your precompiler workshops. It is always a fantastic time! I look forward to seeing more of you there in the future. Have a great 2014!

Overmocking

One of the most powerful tool available to developers testing a legacy code base is the ability to mock out classes that their code depends on. This is of great importance since our unit tests need to limit the scope, and we do this by trying to limit our dependencies. Through mocking we can exchange one dependency on the infrastructure of our application for an in-memory mock.

Sometimes mocking is overused, and I am not just talking about cases where every objected gets mocked to the point where we’re testing nothing. I am talking about a different issue in mocking. I am talking about where developers put on their mocking blinders when unit testing. It’s an easy thing to do, and I’ve done it plenty of times myself.

Setting Up Our Example

We will start of by defining our example. We will have a method to do some sort of calculation (an ideal and easy testing scenario). It will be a legacy code base, which means that it is untested code and probably hard to test. We’ll start off by making it a private static method and put in a nice dependency that one might try to mock to avoid.

private static decimal CalculateFooOnXyz(Xyz xyzItem, 
decimal calculationParameter1)
{
var numbersInCalculation = Repository.GetNumbers()
.Where(n => n.IsActive);

foreach (Number number in numbersInCalculation)
{
// Some code that executes for each one.
}
}

Now that we have this method, which is scary and really needs some testing we’ll take a look at the simple way of testing it; we will use parameter injection to pass in the dependency since we’re static we not easily able to do any other safe forms of dependency injection. When we do this, we end up with the following code.
 
private static decimal CalculateFooOnXyz(Xyz xyzItem, 
decimal calculationParameter1, IRepository repository)
{
var numbersInCalculation = repository.GetNumbers()
.Where(n => n.IsActive);

foreach (Number number in numbersInCalculation)
{
// Some code that executes for each one.
}
}

This is a pretty simple change. We now mock out the repository and pass in the mock in our test and we tell it instead to return the collection we specify in memory instead of requesting the data from the database.

Why This is Wrong

My first question is, what is the name of the method? CalculateFooOnXyz. Notice that I didn’t say, “GetDataFromTheDatabase”. That’s because we shouldn’t be doing that. It isn’t part of the calculation. The numbers returned from the database are required for calculating, so that means that we should have that object as our dependency instead.

How We Change It

So instead of making the repository our parameter, we should make the collection of numbers our parameter. This way we’re depending on the in-memory collection of CLR objects. This is much less of a dependency, and in is not one that needs to be mocked. By doing this alternative we’re better following the Single Responsibility and Dependency Inversion principles.

Our best code for testing will look like this.

private static decimal CalculateFooOnXyz(Xyz xyzItem, 
decimal calculationParameter1, List<CalcNumbers> numbersInCalculation)
{
foreach (Number number in numbersInCalculation)
{
// Some code that executes for each one.
}
}

Comments? Have a better way of doing it? Did I make a mistake?

Unit Testing With a Base Test Class

Writing good code that can be trusted to work means unit testing your code. In order to effectively maintain these tests you will need to follow the same principles you would with any other code. This of course means that you extract logic to achieve code reuse, name methods and objects clearly, use composition, and use inheritance.

In this post I am going to show how you can get some code reuse when you’re following another good testing practice. You should keep things well abstracted. I like to keep my tests in folders and I keep test classes in those folders. I do this so that I can have each test class be small and only be testing one things. By doing this I make my test classes a lot cleaner and easier to work with. This means, however, that if I do not extract some of the duplication I could be creating more maintenance work later.

On way that I keep my tests more maintainable is to keep duplicated logic for initialization in base classes. When I am testing methods of a certain class, I will keep a folder of those tests and have a class for each area of the code I want to test. This lets me keep some of the initialization logic in a base class. Many of the test classes will need an instance of the object being tested, so I can put logic in the base class to initialize my mock objects pass them to the constructor of the class I will be testing.

Setting up a base class is easy. The following examples show how to set up a base test class in MSTest and NUnit.

Setting Up a Base Test Class with MSTest

[TestClass]
public class BaseTestClass
{
public BaseTestClass()
{
Console.WriteLine("BaseTestClass.Ctor()");
}

[TestInitialize]
public void BaseTestInitialize()
{
Console.WriteLine("BaseTestClass.BaseTestInitialize()");
}

[TestCleanup]
public void BaseTestCleanup()
{
Console.WriteLine("BaseTestClass.BaseTestCleanup()");
}
}

[TestClass]
public class ConcreteTestClass :BaseTestClass
{
public ConcreteTestClass()
{
Console.WriteLine("ConcreteTestClass.Ctor()");
}

[TestInitialize]
public void MyTestInitialize()
{
Console.WriteLine("ConcreteTestClass.MyTestInitialize()");
}

[TestCleanup]
public void MyTestCleanup()
{
Console.WriteLine("ConcreteTestClass.MyTestCleanup()");
}


[TestMethod]
public void TestMethod1()
{
Console.WriteLine("ConcreteTestClass.TestMethod1()");
}

[TestMethod]
public void TestMethod2()
{
Console.WriteLine("ConcreteTestClass.TestMethod2()");
}
}

MSTestBaseClassTestOutput

BaseTestSessionOutput

Setting Up a Base Test Class using NUnit

[TestFixture]
public class BaseTestClass
{
public BaseTestClass()
{
Console.WriteLine("BaseTestClass.Ctor()");
}

[SetUp]
public void BaseSetUp()
{
Console.WriteLine("BaseTestClass.SetUp()");
}

[TearDown]
public void BaseTearDown()
{
Console.WriteLine("BaseTestClass.TearDown()");
}
}

[TestFixture]
public class ConcreteTestClass : BaseTestClass
{
public ConcreteTestClass()
{
Console.WriteLine("ConcreteTestClass.Ctor()");
}

[SetUp]
public void SetUp()
{
Console.WriteLine("ConcreteTestClass.SetUp()");
}

[TearDown]
public void TearDown()
{
Console.WriteLine("ConcreteTestClass.TearDown()");
}


[Test]
public void TestMethod1()
{
Console.WriteLine("ConcreteTestClass.TestMethod1()");
}

[Test]
public void TestMethod2()
{
Console.WriteLine("ConcreteTestClass.TestMethod2()");
}
}

NUnitBaseClassTestOutput

BaseTestSessionOutput

Notice in these examples how the Base method setups happen first and then the local class ones. This allows you to depend on the code happening on the base class first. Then when you tear everything down you will first clear up things in the local method and then the base class will run.

Make sure you follow this example by having different names for the base methods so they don’t collide with the local names. This lets you have them both run without one having to call the other. If the names match you will either be overriding the base or hiding it, but they will not both run.

Organizing Software Projects

Managing and structuring software applications is a complicated topic. Many different ways of structuring applications exist, and there are merits to several methods of organization. In this post, I am going to roughly describe a method of organizing a software application to which I am somewhat partial.

One of the most important aspects of a well-organized project in my opinion, is the ability for a new developer on a project to be able to sit down and have a working solution as soon as they get the latest version of the application from the source control repository. Not only should that person be able to have a working solution, but that working copy should assist the developer in setting up the local environment. The solution should also be in a state which allows all of the project's tests to run without the user having to jump through a bunch of hoops.

Being able to sit down and start working immediately is very powerful. We wouldn't be able to achieve this without first having things packaged and tested nice. We are allowing anyone to figure out how things work based on the logic standardized and explained by our tests. The developer is now able to make meaningful, useful changes to the project right off the bat by making modifications and testing them. As long as they're not breaking existing tests, they can have some confidence that they're meaningfully contributing to the project.

Combined with continuous integration we are able to know that the project is in a tested, working state, and when we check out the source for the project, that it should work correctly already. There are a few pieces which must be in place in order for this to work. We need to make sure that the class libraries we're depending on are included in relative paths in the source control repository, so we don't have to worry about whether or not they are installed on the developer's machine. We also need to make sure that we have the project sufficiently well tested. Build scripts are also vital in allowing someone to sit down and start working immediately.

When deciding what tools to use for testing and for your build scripts, it is important to decide which tools you can safely assume will be on the developer's machine. As far as I know, you can't package MSTest or MSBuild along with your project. If everyone who uses this code will have these, then it is perfectly acceptable to depend on them. If that is not the case, I would recommend using NUnit and NAnt. These tools are very similar and are easy to use. Their integration with Visual Studio is not as nice, but they are easier to have dependencies on.

The trunk looks somewhat similar to this.

trunk-explorer

See that at this first level of the application, we have very little clutter. Things should be kept neat and tidy here, so that people just checking this code out aren't overwhelmed by anything before opening this. The lib folder contains class libraries that the project has dependencies on as well as the tools for building. This includes things like NUnit. By keeping it out here we are starting off having the opinion that these should be kept at arms length the whole time while working with the project. The less our dependencies integrate with our source the better off we are.

src-explorer

At this level inside the src folder, it is important to see that we still haven't shown any of our clutter. We of course get a lot more cluttered once we see the source code. I don't mean that the code is messy, but seeing a dozen files is a lot messier than a handful of folders and a solution. At this level someone can open up the solution and see the project from within Visual Studio already knowing that the solution works. How do we know it works? We ran the BuildAndTest file and it made sure that the project built and that all of the tests passed.

That little addition of having the ability to build and test the project is so valuable, because in a moment anyone can know if the project is working. We get quick feedback without even having to load visual studio. We don't have to search for the files among others; they're right there on their own.

Good luck, good organizing, good testing, good night.

And now an opportunity to promote Jeffrey Palermo's class. If you are interested in learning about Agile software practices, I recommend taking the Agile Bootcamp class offered by Headspring Systems. I learned a great deal from that class, and a lot of the practices I currently use are adaptations of what I learned. It isn't a class that tells you exactly how to do things, it shows you a way of doing things that works, which you will be able to take and adapt to how you're already working.