Skip to main content

Posts

Showing posts from August, 2010

Total WTF moment whilst reading Clean Code

I've just spent a useful and enjoyable day finally getting around to reading Clean Code, definitely a book to come back to time and again for reference, etc.  However given the subject matter the book contains I was amazed to find in the section about "magic numbers" a statement that "some constants are so easy to recognise that they don't always need a named constant".  Two of the examples given are the number of feet per mile and the number of work hours per day.   I'm not sure how many people that work with imperial units could easily state how many feet are in a mile.  If you are more used to using metric units then that figure has next to no meaning what so ever.  And at least in the UK working hours vary depending upon the industry, company and even department that you work in.  Whilst it doesn't detract from the content of the book I'm not sure those statements should have got past the proof reading / draft review stage.

Response to "Two different ways to create bad logic in unit tests"

Today I read Roy Osherove's blog post "Two Different ways to create bad logic in unit tests".  It's an interesting article that covers logic included in many unit tests, is it acceptable to compare a returned string value using a comparison value built via string concatenation?  It is perfectly possible that an issue introduced by the concatenation process is duplicated in both the code and the unit test - more often than not the logic has been cut / pasted from the code base into the unit test anyway.   As an example directly from Roy's blog consider the following code: 1: [TestMethod]


2:publicvoid SomeTest()


3: {


4:string user = "user";


5: ComponentUnderTest cut = new ComponentUnderTest();


6:string result = cut.SayHello(user);


7:string expected = "hello " + user;


8: Assert.AreEqual(expected,result);


9: }



Thinking this through, it makes perfect sense, the following could be a better test:



1: [TestMethod]


2:publicvoid …

Project Euler – Problems 18 & 67

The challenge set by problem 18 was By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.3
7 4
2 4 6
8 5 9 3That is, 3 + 7 + 4 + 9 = 23.A 15 row triangle was then supplied for which the program must determine the corresponding maximum value taking a similar path through the data.  A foot note to the problem highlighted that due to the “limited” number of paths for this puzzle it could be solved using brute force determining the total for each path through the triangle and selecting the maximum total returned.  However a link to problem 67 was provided which exactly the same problem, but this time the data was for a hundred row triangle. For that problem a brute force attack could not be used as the number of possible routes meant that:If you could check one trillion routes every second it would take over twenty billion years to check them all. There is an efficient algorithm to solve it.…

Project Euler – Problem 54

The challenge set by Problem 54 was to determine the number poker games payer 1 won given a text file detailing 1,000 hands dealt to 2 players.  Given the logical nature of the rules, the solution was just a case of finding the best way to 1) implement the rules and 2) duplicate the rule hierarchy.  I quickly re-factored my first attempt that attempted to place the ruled based logic inside of the PokerHand class as I found whilst this made it easy to compare the matching combinations of a single hand it was much hard to compare two hands and determine the winning combination – this was made even harder when two hands contained the same winning combination and the next highest combination had to be selected from each to be compared.At this point I simplified the PokerHand class to just being a container for an IEnumerable Card collection and some basic logic to confirm the hand was valid (i.e. contained 5 cards).  The logic to find the winning player was migrated into a PokerEngine cla…

$(Document) is null or not an object

I've just spent a very painful hour or so debugging a jQuery issue that turned out to be a self inflicted problem!  The site I was working on had been working perfectly all morning, then a particular page began to fail on a refresh (browser F5).  It was possible to browse to the page and it displayed correctly, but press F5 to refresh the page and it failed on the statement below.  Checking the contents of document showed it to be populated, but $(document) failed with an error saying it was either null or not an object.

$(document).ready(function() {
   AttachCloseEvents();
});

It just didn't make sense, going to the page everything would work as expected but refreshing the very same page caused it to fail - it didn't matter if it was the start up page of the visual studio project or not.  In one of the many attempts to isolate / identify the problem I cleared out the browser cache.  Amusingly this now broke the entire site - the error occurred every time the page was viewed.…