Skip to main content

Posts

Showing posts from December, 2014

Pair Programming vs Pairing

I'm a fan of pair programming. I owe a lot of this practice to my improvement early on in my career. I define pair programming as two developers working on a task using one or more machines at the same time.I have had some excellent pair programming sessions. I can even remember some of them in great detail. Here I went away learning something new, solved a difficult problem, or just generally had a fun time.On the other hand I've also had some awful experiences, which unfortunately I can still remember. Here my partner wouldn't play the role of the driver or navigator correctly, wouldn't be engaged, or just generally didn't get into the flow of pair programming.Team's mandating 100% pair programming is bad. Some tasks don't need two developers to be working on them concurrently. Here pairing should be used. Pairing is two developers working together to solve a task, but doing so separately. During pairing regularly communication, design sessions and feedba…

A Unit is Not Always a Method or Class

Part three of my Three Steps to Code Quality via TDD series. The most important concept when coupled with the previous two points - not every unit will relate to a method or class.Part 1 - Stop Making Everything PublicPart 2 - Limit the Amount of Dependencies you Use Most introductions into TDD use simple examples. Even the excellent TDD by Example uses a value object in terms of Domain Driven Design. Most introductory articles on the Internet suffer the same fate. While these are great for demonstrations, they don't relate to what most developers need to code on a day to day basis. It's around this point where people proclaim that the benefit of automated testing (even after the fact) is a waste of time.One of my biggest revelations with TDD was that each unit does not need to equate to a single method or class. For a long time I followed what others did. Each collaborator would be injected and replaced with a test double. Each class would have a corresponding test file. Howe…

Limit the Amount of Dependencies you Use

Part two of my Three Steps to Code Quality via TDD series and ties very closely into step one, limiting the visibility of your classes.Part 1 - Stop Making Everything PublicPart 3 - A Unit is Not Always a Method or Class The more dependencies you use the more your tests are coupled to implementation.Consider the constructor below.Code like this is common and difficult to work with. Each dependency you inject requires a mock, stub or fake when writing tests. This couples the implementation to the test despite the use of interfaces or abstract base classes.Every public dependency here increases the resistance for change. If I was to remove the builder and replace with some equivalent code to construct a Bar instance, the test would fail despite being functionally equivalent. This is wrong.A constructor is part of the public API of an object even though this is not detailed as part of interfaces in languages such as C#/Java. Every collaborator that is provided by a constructor should hav…

Stop Making Everything Public

Part one of my Three Steps to Code Quality via TDD series.Part 2 - Limit the Amount of Dependencies you Use Part 3 - A Unit is Not Always a Method or Class We always default to public class when creating a new class. Why? The concept of visibility in OO languages appears very early on in programming books, yet more often than not most of the classes we create default to public visibility.@simonbrown stated that each time you make something public you need to make a donation to charity. In other words we should think more about why the class we are making should be visible to everyone. I really like this idea that the use of the public keyword should be a well thought out decision.Server side development has a part to play in the lack of concern given to visibility issues. Library or framework developers on the other hand must carefully consider what is part of the public API. Any changes made after are considered breaking and require careful consideration. Yet in the land of server s…

Three Steps to Code Quality via TDD

Common complaints and problems that I've both encountered and hear other developers raise when it comes to the practice of Test Driven Development are: Impossible to refactor without all the tests breakingMinor changes require hours of changes to test codeTest setup is huge, slow to write and difficult to understandThe use of test doubles (mocks, stubs and fakes is confusing)Over the next three posts I will demonstrate three easy steps that can resolve the problems above. In turn this will allow developers to gain one of the benefits that TDD promises - the ability to refactor your code mercifully in order to improve code quality.StepsStop Making Everything PublicLimit the Amount of Dependencies you Use A Unit is Not Always a Method or ClassCode quality is a tricky subject and highly subjective, however if you follow the three guidelines above you should have the ability to radically change implementation details and therefore improve code quality when needed.

Factory Obsession

I have noticed a pattern over the years with developers of which I will refer to as factory obsession. Everything is a factory or builder object. To some, the use of new is banned.Consider a object that is responsible for some business logic and finally saves the result to a persistent store.Message here is a value object, however the new can cause an odd fear within developers. Therefore a factory is required. Or is it? How can we test the repository is updated with the new message without reference equality?An example test in C#, using the Mock framework with this newly introduced factory would look like:This fear of new is wrong.Instantiating value types is a good thing.Instantiating entities is a good thing.Instantiating services can depend - if the service is expensive we don't want to create lots of instances on a whim.Here the factory offers nothing but a more strongly coupled solution.If we ignore the factory the test becomes easier to write. To do this equality should be …