9.10.10

You gotta love Design By Contract

Code Snippet
  1. [TestMethod]
  2. [ExpectedException(typeof(TradingServiceException))]
  3. public void ShouldThrowExceptionForInvalidTrade()
  4. {
  5.     var mockTrade = new Mock<AbstractTrade>(MockBehavior.Strict);
  6.     mockTrade.SetupAllProperties();
  7.  
  8.     mockTrade.Setup(trade => trade.IsValid()).Returns(false);
  9.  
  10.     var tradeManager = new TradeManager(mockTrade.Object);
  11.     tradeManager.Execute();
  12.  
  13.     mockTrade.Verify();   
  14. }

And when the test fails, we get the following message:

TradeGeneratorTests.ShouldThrowExceptionForInvalidTrade : Failed

Test method Zainco.Commodities.Unit.Tests.TradeGeneratorTests.ShouldThrowExceptionForInvalidTrade threw exception: 
Zainco.Commodities.Exceptions.TradingServiceException: Precondition failed: Trade.IsValid() == true  Trade execution failed

at System.Diagnostics.Contracts.__ContractsRuntime.Requires<TException>(Boolean condition, String message, String conditionText) in :line 0
at Zainco.Commodities.TradingService.TradeManager.Execute() in TradeManager.cs: line 28
at Zainco.Commodities.Unit.Tests.TradeGeneratorTests.ShouldThrowExceptionForInvalidTrade() in TradeGeneratorTests.cs: line 36

Sweet!

CodeContracts break encapsulation and Resharper 5.0 is blissfully unaware of them

Seems I have to make my helper methods public if I want to use them within a code contract, but I avoid this by defining a property with the private setter to end up with the Contract.Requires<TException>(…) implementation below:

Code Snippet
  1. public AbstractTrade Trade
  2. {
  3.     private set { _trade = value; }
  4.     get { return _trade; }
  5. }
  6.  
  7. public void Execute()
  8. {
  9.     Contract.Requires<TradingServiceException>(Trade.IsValid(),
  10.                                                "Trade execution failed");
  11.     if (TradeExecutedEvent != null)
  12.     {
  13.         TradeExecutedEvent(this, new TradeEventArgs(Trade));
  14.     }

C:\Sandbox\Pricing\CommodityServer\TradingService\TradeManager.cs(24,13): error CC1038: Member 'Zainco.Commodities.TradingService.TradeManager.get_Trade' has less visibility than the enclosing method 'Zainco.Commodities.TradingService.TradeManager.Execute'.
C:\Sandbox\Pricing\CommodityServer\TradingService\TradeManager.cs(24,13): warning CC1036: Detected call to method 'Zainco.Commodities.Interfaces.AbstractTrade.IsValid' without [Pure] in contracts of method 'Zainco.Commodities.TradingService.TradeManager.Execute'.
  elapsed time: 294.0169ms

8.10.10

Are all circular dependencies created equal?

I am using the separated interface pattern to implement a commodity trading engine for a bourse in the emerging markets. My unit test package is mocking one of the interfaces and consequently has a dependency on the interfaces package. Likewise the Service package depends on the interfaces package.

Superficially I appear to have a circular dependency and eager Resharper 5.0 complains about this with a fairly descriptive error message “Failed to reference module. Probably reference will produce circular dependencies between projects.”. The result is that intellisense breaks!

image

image

What to do?

Are all circular dependencies created equal? Does it matter that the offending dependency in this case is an abstraction rather than a concrete type?

On further examination, the only real dependency is between the test package and the trading service, the other dependencies essentially enforce the contracts between the interface package and those that must either implement the behaviours defined by this package or use the behaviours provided by these contracts.

Tools hmmm….

29.7.10

Refreshing Exceptions…

Ever faced the cryptic exceptions that leave you red-eyed from looking at the debug window and inspecting automatic variables? Well if you have spent most of your adult life making money from software development, the probability of this happening is fairly high. So when you encounter an exception message like the one below, you kind of know that the software/API has been developed by developers that care:

 

WorkOrderTests.ShouldSaveWorkOrderDocument : Failed

Test method WorMaSysUnitTests.WorkOrderTests.ShouldSaveWorkOrderDocument threw exception: 
System.InvalidOperationException: The maximum number of requests (30) allowed for this session has been reached.
Raven limits the number of remote calls that a session is allowed to make as an early warning system. Sessions are expected to be short lived, and 
Raven provides facilities like Load(string[] keys) to load multiple documents at once and batch saves.
You can increase the limit by setting DocumentConvention.MaxNumberOfRequestsPerSession or DocumentSession.MaxNumberOfRequestsPerSession, but it is
advisable that you'll look into reducing the number of remote calls first, since that will speed up your application signficantly and result in a 
more responsive application.

at Raven.Client.Document.InMemoryDocumentSessionOperations.IncrementRequestCount()
at Raven.Client.Document.DocumentSession.SaveChanges()
at WorMaSysUnitTests.WorkOrderTests.ShouldSaveWorkOrderDocument() in WorkOrderTests.cs: line 58

Playing with the Raven

image

Zainco is currently evaluating NoSQL approaches for a groundbreaking application we are developing for a security client. At present Raven is looking very promising.

18.5.10

30 sprints later…

I have spent the best part of the last 1 year on a large project with many cross functional teams building a .NET application that integrates with a specialised vendor ERP system. As with any experience, you learn a great deal about yourself and others.

My greatest lesson is that there is a lot of crap software out there making lots of money and that good software (read this as open source) rarely, if ever, makes money.

With the crap software come the zealots and snake oil salesmen selling their bogus cures and panaceas .

If ever I had to express this in mathematical terms, I would say that:

The rate of return on investment on a software asset is inversely proportional to the quality of the code deployed.

This appears to tie with the empirical data, but as with any such observations, outliers or exceptions will exist.

I suppose our challenge as software craftsmen is to ensure that quality and ROI is balanced.

3.10.09

Eurotunnel...

The hiatus is over; in death is renewal and the realisation that we are indeed mortal.
One
Anyway back to the subject of this blog, software.

At present we face a number of software engineering challenges amongst these is the centralisation of field application logs(FAL). SaaS appears to be a natural solution to this problem. The service interface would ideally accept a log message and persist this for later consumption by development and support staff.