Blog

Don't Scatter Logic All Over The Call Stack

Here’s a problem I come across all the time in legacy code and I have been guilty of it myself in the past. This isn’t going to be rocket science, but, apparently, this stuff doesn’t go without saying. Normal caveats about this being a simplified contrived example apply. Take a look at this code static void Main(string[] args) { PrintTradeBalance(); Console.ReadKey(); } Oh! to have code this simple right? It’s pretty clear what it does, it prints a trade balance, whatever that is.

Continue reading

Simplicity

Ladies and Gentlemen of the class of ‘14 If I could offer you only one tip for the future, simplicity would be it. The long term benefits of simplicity have been proven by Rich Hickey whereas the rest of my advice has no basis more reliable than my own meandering experience. I will dispense this advice now. Beware the over engineered complexity of your code; oh nevermind; you will not understand the over engineered complexity of your code until it bites you in the ass.

Continue reading

Memoization

Imagine you have a long running function that you’d like to avoid running unnecessarily. For the purposes of this post you’ll have to suspend disbelief and pretend that negating a number is an expensive task. This example prints out a message so you can see when it actually gets called. let Negate n = printfn "Negating '%A' this is hard work" n -n val Negate : int -> int > Negate 5;; Negating '5' this is hard work val it : int = -5 Now, let’s use that function when writing another.

Continue reading

When Can You Start?

He sat in the large bay window observing the potential candidates as they approached the house. His mind was made up about each of them before they rang the bell. “I’m here about the gardener job” “Sorry, the position is filled.” “Already? OK, thanks, bye” Again and again all the same. No good. Then…aha! this one. “I’m here about the gardener job” “When can you start?” “You don’t want to ask me any questions?

Continue reading

C# Is Too Damn Noisy

I am growing increasingly frustrated with C#. I think the reason for that may be my exposure to languages like F#. In many ways my feelings for C# are quite similar to feelings I had about VB.Net when I was first exposed to C#. It’s taken me a while to figure out what it is that I find irritating about C# and I think I’m ready to call it. The problem with C# is exactly the same problem I had with VB.

Continue reading

Why Do Cars Have Breaks?

Why do cars have brakes? I noticed this question on Jon Jagger’s blog and I was delighted with myself that I managed to get the “right” answer without peeking. Stop reading right now, have a think about it, then head on over to Jon’s blog to see what he has to say on the topic. Then, if you want, read on… How would you drive if your car didn’t have brakes?

Continue reading

Unfolding Sequences

In my last post I worked through an example that finds the range of numbers that sum to a target value, or gets as close as possible without exceeding the target. I mentioned that the solution felt a little too like the loopy code I would have written in non-functional languages. I felt that there might be a more “functional” way of solving the problem, but I didn’t know what it was.

Continue reading

Iterating, Incrementing, and Accumulating

Another F# session this evening and some more deliberate practice of Functional Thinking. To be fair, this post isn’t really about anything new. If you’ve ever used recursion, even in non-functional languages, this will be old news. If you are new to Functional Programming and/or recursion then this may be useful. Here’s a really simple function. It accepts a number n and sums all the numbers from 1 to n.

Continue reading

Partial Application

Warning, novice functional thinker here. If you know your stuff, what follows may cause distress. I was messing with F# last night and I got a gentle reminder that I’m still a long way from thinking functionally, it still takes a lot of effort. I started with this let evens r = List.filter (fun x -> x % 2 = 0) r > evens [0..10];; val it : int list = [0; 2; 4; 6; 8; 10] Simple enough.

Continue reading

Legacy Code Katas

I like Kata’s, I’ve get a lot out of them, but if I’m truly honest, they don’t really address one area of programming where I think I need practice, and that is in working with Legacy Code. In pondering this problem I came to the conclusion that I need a way of doing deliberate practice for legacy code work, and some variation of the Kata idea seems like it might work.

Continue reading