Posts

Showing posts with the label clean code

Making Data Relationships Obvious

When you are writing tests you end up using literals or hand-constructed data objects. However, using literals can obscure the meaning a little. Not horribly; most people can puzzle them out quickly enough, but you can make it easier on the reader. In most of these tests, the values are pretty much arbitrary, but the relationship between the values is essential. For instance, when checking that no bonus is given when sales is less than quota, I could state that sales is 10,000.00 and that the quota is 9,900.00.  Pretty much anyone could tell that the quota is less than sales here, right? But what if we made it really explicit?      double quota = 10000;      double sales = quota - 100; This is how I like to roll. For instance, in python:      event_date = datetime(2015,9,10)      later = event_date + timedelta(days=10)      earlier = event_date - timedelta(days=2) And how ...

Why Your Code Has So Much Duplication

Here is a quite nice example code block for C#'s Dataflow blocks, specifically showing usage of the WriteOnceBlock: ActionBlock writeToConsole1 = new ActionBlock ( integer => Console.WriteLine( "Console 1: " + integer ) );   // true if the source should unlink from the target after successfully propagating a single message // otherwise, false to remain connected even after a single message has been propagated bool unlinkAfterOne = false ;   WriteOnceBlock writeOnceBlock1 = new WriteOnceBlock ( integer => integer ); writeOnceBlock1.LinkTo( writeToConsole1, unlinkAfterOne );   // prints 12 via Console 1: // Console 1: 12 writeOnceBlock1.Post( 12 );   // create 4 additional Targets ActionBlock writeToConsole2 = new ActionBlock ( integer => Console.WriteLine( "Console 2: " + integer ) ); ActionBlock writeToConsole3 = new ActionBlock ( integer => Console.WriteLine( "Console 3: " + integer ) ); Acti...