The dangers of primitive obsession
Primitive obsession is, like many facets of modern programming, something that has been written about many times before (e.g. http://sourcemaking.com/refactoring/primitive-obsession, http://blog.thecodewhisperer.com/2013/03/04/primitive-obsession-obsession/), yet still we see its ensnaring of unwary programmers.
C# needs to allow us to write code in a more functional, immutable style
Since C# 6.0 is now feature-locked (and I can’t wait) it’s time to start talking about something that might be in the next version, assumedly called C# 7.0.
The big feature for me would be to get C# to help us write immutable code. That means they need to make it easy to write immutable classes (see my last post on Record types) and immutable local variables too.
I propose the following syntax:
My proposed syntax for record types in C#
With the impending release of C# 6.0 and all its new features, I have been thinking about what might come in the next version of C# (v7.0, I’m guessing). There has been talk in the Roslyn forums about things such as pattern matching and record types (the latter being especially important since the primary constructors feature was dropped from C# 6.0).
Below is a quick sketch of how I think record classes should work in C#. You can probably tell that I spend a lot of time programming functionally, because I’ve included a number of syntax features here to make immutability a very easy option.
London photowalk
Last week I went on a photowalk with friends. The idea is that people with a shared interest in photography meet up and walk along a pre-prepared path, taking pictures as they go. The walk started at the Hays Galleria near London Bridge and we continued through the Inner London Pool starting on the South side of the river and travelling over to the North side and back again.
Along the way we took in some of London’s finest landmarks through the back streets finding views of London Bridge, the Shard, the Tower of London, Monument and more. This one was part of the Kelby Worldwide Walks and there was a competition to go with it.
I have included a gallery of the shots I took (and decided to keep), and the photo I entered into the competition too.
An introduction to F#: Part 1
A few days ago I ran an introduction to F# course for colleagues at work. The idea of the course was to explain the basics of F# including syntax, programming style and the FSI window, as well as to get people excited. I didn’t correctly judge how long the presentation would take (people were asking lots of questions as I went, which is good!), so I ended up splitting it up into two parts. Here I am publishing a rough written copy of the presentation, compiled from my notes that I worked from as I talked, for you to peruse. It’s somewhat rough, but for those who were there it should serve as a reminder. Next time we will talk through some worked examples of Fizzbuzz & fizzbuzzbaz, we will cover types (classes, structs, interfaces, enums, records, units of measure and discriminated unions). We will also go through some application design, such as: MVC, DDD, CQRS, WTFBBQ.
Classic mashup: An FSharp coding dojo
Yesterday I attended the FSharp Dojo: Picasquez and Velasso at Skills Matter in London.
This dojo is all about manipulating images and creating mashups, like this:
Finding overlapping rectangles
This is a classic quick programming puzzle: write a function that, given two rectangles, will determine if the triangles overlap. I found this puzzle over on http://leetcode.com/2011/05/determine-if-two-rectangles-overlap.html, and thought it was worth talking about because the solution is wrong, and it was interesting to me because it shows how thinking about a domain can influence your implementation of behaviour.
The mistake made by the original author is with one of the assumptions / interpretations made during their reasoning process. They said “when two rectangles overlap, one rectangle’s corner point(s) must [be contained within] the other rectangle”. This appears reasonable, given the example below:
Programming interview questions
At my last company I was a team lead and, as such, was one of those responsible for interviewing new candidates. Although I didn’t enjoy the process much, I did become interested in the sorts of technical questions / exercises we could set to get a good judge of a candidate’s abilities.
Developer interviews typically cross a number of disciplines (often arranged in stages) that may include:
- a CV / résumé screening
- a telephone interview
- face-to-face questions and answers
- a technical exercise or written test to be done at home, or to be done in front of the interviewers
Be careful with attribute routes in projects with both MVC and WebApi
This is a pretty easy trap to fall into! I have been tearing my hair out recently trying to figure out why, in my pet project website, one of the controllers was not having any routes generated. I was able to inspect the generated routes during debugging by setting a breakpoint after the line
routes.MapMvcAttributeRoutes();
and inspecting the routes object. Sure enough routes for the HomeController and ChatController were there, but nothing for the BlogController. What could it be? Everything appeared to be set up correctly:
[<RoutePrefix "blog">] type BlogController ( _filterBlogPostsQuery: FilterBlogPostsQuery, _getBlogPostQuery: GetBlogPostQuery, _getAllBlogPostsQuery: GetAllBlogPostsQuery) = inherit MySiteController() [<Route "">] member this.Index() = task { let! posts = _getAllBlogPostsQuery() return this.View posts }
Eventually I figured it out after stumbling across this StackOverflow post: https://stackoverflow.com/questions/25727305/asp-mvc-5-attribute-routing-not-registering-routes/. It turns out that, when you have a project with both WebApi and MVC installed, you have two RoutePrefixAttribute classes and two RouteAttribute classes available; one in System.Web.Http and one in System.Web.Mvc. Make sure you’re referencing the right one!
This was also, perhaps, a consequence of using F#, and the fact that the Visual Studio tooling is not as feature-complete as the other languages; I didn’t get a warning from the compiler that there was a naming clash between these two Attributes, despite the fact that I had both the System.Web.Http and System.Web.Mvc namespaces opened at the top of the file. In the end, removing the System.Web.Http namespace was all that was needed to fix the problem.
Lesson learned. Still, it highlights a fragmentation issue between the Mvc and WebApi teams; one which I hope will be fixed in ASP.NET vNext (I assume this will be the case because MVC and WebApi will now share the same base controller).
SOLID, CQRS and functional dependency injection
I was prompted to write this after reading Mark Seemann’s post (http://blog.ploeh.dk/2014/03/10/solid-the-next-step-is-functional/)
Following Single Responsibility Principle will lead you down the road of having plenty of small classes.
Open Close Principle will lead you down the way of encapsulating the core logic of each type in it, and then delegating all other work to its dependencies, causing most of those small classes to have multiple dependencies.
- ← Previous
- 1
- 2
- 3
- 4
- Next →