Tag Archives: programming

Book review: The Go Programming Language, Donovan & Kernighan, 2016

This is a fairly good book to read, especially if you are new to the Go programming language. It also serves as a useful reference while you are getting familiar with the Go language. However, it is not a serious contender for a programming language reference guide such as The C++ Programming Language, by Bjarne Stroustrup. That also makes it somewhat easier to read!


I have extensive experience in many programming languages (C, C++, x86 Assembler, BASIC, Java, C#, F#, Visual Basic) and passing familiarity in several (Lisp, Pascal, Fortran, OCaml, EcmaScript)━I’ve probably forgotten some━but I’m a relative newcomer to the Go programming language and ecosystem.

In the course of the past few weeks I have started my journey in Go, writing fairly substantial utility programs and providing one (so far) small pull request on a major and well-known open source product, Hashicorp’s Terraform. I have also read this book cover to cover, as it is a learning style that has worked very well for me in the past.

Who should read this book?

This is a book for developers, but you should already be proficient with at least one other mainstream imperative programming language (C, C++, Java, C#, etc.) before tackling this book because it does presume a fair amount of basic programming competence.

Obviously, you should be interested in the Go programming language, but ideally you should have opportunity to use it on some real implementation work, ideally something more substantial than the exercises in the book. Those exercises will almost certainly help cement an understanding of the material in the book, but they are not a substitute for real programming challenges, particularly as they guide the reader in the overall design and limit the scope of thinking━exactly what you need in exercises, I think. I didn’t do any of the exercises in the book, that’s just not how I roll.

If you are already proficient in Go, this book isn’t likely to tell you much that’s new━perhaps one or two things about the dangers of code involving uintptr and unsafe.Pointer, but not a lot else. It certainly isn’t going to stop you having to read package documentation, even for standard packages like fmt.

The bad

I’ll present the bad things first so that by the time you’ve finished reading this post━if you make it that far!━you’ll be more likely to remember the good parts.

  • This book won’t take you far into mastery of the language. It will help you become competent, but won’t really get much further than that
  • The authors━or at least one of them━provide some very spurious arguments to justify particular language design elements. For me, these were irritating but thankfully they became less frequent after you passed the halfway mark
  • The early chapters are particularly slow. It’s quite easy to get frustrated at the pace, but if you haven’t promised to provide a review you might find it easier to just skim read those parts, but don’t think you are buying a book with 366 pages of dense language instruction, I think they could have covered the same material in half the number of pages, particularly if they removed or shortened some of the examples

The good

  • This is a good book. I would not call it a great book, but it is a good book
  • The authors appeal to several learning styles: decent textual coverage of content, example code (available to download and play with), guided exercises of varying complexity
  • There is extensive coverage of each language element. Go is a fairly small language with few edge cases, and so there is a decent amount of content for each and every construct in the language
  • The main toolchain (go run, go build, go get, go test, go install, go doc) is introduced, including brief coverage of the way imports are handled, how to develop automated tests and benchmarks, review test coverage and, of course, create executables
  • Decent introductory coverage of how to write concurrent programs in Go (two chapters), including certain design approaches, e.g. event broadcast using close
  • Basic introductory coverage of the unsafe and reflect packages, including a simple example of interfacing with external, C library code

Where to go next

Since the book doesn’t really get to mastery of the language, where do you go for more details?

Book details

  • Alan A. A. Donovan · Brian W. Kernighan
  • Published Oct 26, 2015 in paperback and Nov 20 in e-book
  • Addison-Wesley; 380pp; ISBN: 978-0134190440
  • http://www.gopl.io/

Why Big Data Analytics is harder than you think: numerical precision matters!

How big is your Big Data data set? If it is not measured in petabytes then perhaps it doesn’t qualify. In any case, as the amount of data grows the challenges grow disproportionately and in areas that perhaps you don’t expect. In this post, I am raising a number of challenges and concerns that you should be thinking about, especially if you are interested in business analytics on large data sets.

I recommend that if you are serious about large data sets and numerical precision, you have no choice but to adopt 128-bit arithmetic in most scenarios, especially financial.

Continue reading

A continuous thread of execution it isn’t!

I have to admit it, I was really surprised this week. While investigating a mysterious issue I discovered that I knew less about the hosting platform of ASP.NET and IIS than I thought I did. What I found makes sense, but it was surprising nonetheless.

What I found has made me believe more strongly what I have recently been advocating. Affinity is dangerous. The model of pure functions in functional languages is much easier to understand and thus reason about. Whenever affinity is used as a back-door to rely on some previously established state, you are essentially adding input to your function, and when you do so you had better understand the immutability or otherwise of that information. The problem? Something believed to be immutable was not in fact immutable and thus the correctness of the code was gone.

Now this is all quite mysterious, so I’d probably get to telling you about what it is that I found.

I had a HttpModule that was impersonating a user, and therefore changing the return value of WindowsIdentity.GetCurrent(). I also caused a change in Thread.CurrentPrincipal because I wanted any .NET code in the ASP.NET pipeline to consider this account to be the current account. I thought everthing was fine! (I should point out that I am dubious about the quality and purpose of this code, it is just what I had when I was investigating. I suspect a rewrite is due…)

In fact, ASP.NET interleaves request tasks (note: request tasks, not just whole requests) on the same thread and therefore has logic to switch the current thread identity and impersonation behaviour. It only needs this because it interleaves request processing, otherwise it could have just left the identity as it was. The problem is the following: it does not determine the behaviour based on Thread.CurrentPrincipal or WindowsIdentity.GetCurrent(). Instead, the request’s execution context is represented by the HttpContext class and HttpContext.Current instance. The User property of HttpContext is actually an instance of IPrincipal and ASP.NET will undo impersonation before switching to a new task. Without setting the HttpContext.Current.User property, this impersonation approach is not going to work!

The solution is clearly trivial: set the HttpContext.Current.User property. However, that misses the point. Server-side code oftens requires the splitting up of work into smaller units. When this happens, each of these units of work may be executed on the same thread without interruption, on the same thread with infrastructure interruption and then an immediate resumption, on the same thread with the interleaving of an alternate unit of work or on another thread. Modern systems have a large amount of co-operative multi-tasking on the same thread. This is true for ASP.NET, WCF and the TPL. It also means that, when traversing threads intentionally, you have to take responsibility for taking this state with you.

The large and complex subsystems of .NET include several examples of this. The ExecutionContext manages the CLR state as it reuses different operating system threads. WCF has the OperationContext and ServiceSecurityContext classes. ASP.NET has the HttpContext. Of course, you’ve probably also used the SynchronisationContext to interact with a UI thread that has its own thread affinity.

In retrospect, a lot of this looks obvious. I knew that ASP.NET supported asynchronous page execution, and of course it may need to load the page from disk and even compile it in some cases, so an asynchronous approach seems obvious. Similarly, I’ve coded custom WCF bindings and so I know that they are also an asynchronous design. Nevertheless, it is all to easy to make the incorrect assumption that these methods and events are just executed as a monolithic block of code with the infrastructure providing the simplest of glue. The reality is far more complex.

Thanks go to Scott Hanselman for a nice blog post on some of this: System.Threading.Thread.CurrentPrincipal vs. System.Web.HttpContext.Current.User or why FormsAuthentication can be subtle.  The Microsoft Patterns & Practices team also have a detailed description of ASP.NET authentication, although the article is quite old now: Explained: Windows Authentication in ASP.NET 2.0.

NHibernate: How to filter on primitive collections

I am using NHibernate with a client and I keep hitting the same issue. I have entities with basic collections of strings. I want to search for entities on the basis of filtering criteria expressed against the elements of the collection.

The easy solution is to treat the collection elements as entities but this is not ideal. That really complicates the domain. In some cases, the elements are simply references to foreign entities outside the scope of NHibernate, for example in a remote service or configuration file.

In SQL, I can pose the query as a correlated subquery or as an (outer) join. This has the advantage of being efficient and does not result in the loading of the collection.

An example would be searching for a Cat that is only black when each Cat has a collection of Colours, perhaps represented by an RGB triad. It would be true normal form to extract the colours into their own table but it would also be ridiculous to do so because a foreign key already exists – the RGB triad! Another example would be finding all Cats that are partly black or partly white. There is no reason why the criteria cannot be arbritrarily complex.

I have not found a way in HQL or the Criteria API and my scenarios require the filtering to take place in the database. The result is that I am using SQL directly with NHibernate’s ISession.

Any better solutions? Does ADO.NET Entity Framework also lack this concept?

Introducing SQL Service Broker

I have been investigating SQL Server’s Service Broker component as a possible choice for enabling messaging solutions in a .Net environment for a client and I have been impressed by the feature set.

The architecture does require a bit of a mind shift as it encourages the separation of processing logic from the reception of messages. Sending messages should of course still be part of the process logic.

Why this distinction? This approach allows for rapid verification of message transfer and controlled processing of message content. It encourages developers to recognise that transfer of messages is a different task to responding to a queue of received requests. Messaging is about sending messages and reliably receiving them, not about any particular business process.

If you do not apply this design then you are in danger of causing rollbacks during message reception and this may stop a queue if it occurs five times in a row.

Do you agree with the design? What are your experiences?

F#: Generic Fold

The fold functions in functional languages are very powerful constructs, but how about when you want a different function applied to the first element, or to the last? What about when the sequence is empty? Rather than solving this problem repeatedly, I have written a few functions to cover the range of cases. As I wrote these, I found it interesting that the solutions for Array, Seq and List all look very different.

Continue reading

F#: Pipelined Monads – Solutions From the Community

I was actually quite pleased that I got my state/error monad working (F#: Pipelined Monads), but there was a far more elegant solution staring me in the face. Thanks to other members of the F# community, I have reverted my code back to how it was and added two operators. In doing so the code is now far more elegant. Read on to see more, but thanks again to:

Continue reading