Category: Featured

19 Dec

On technology books

book

Jeff Atwood mad some explicit about technology books here: http://www.codinghorror.com/blog/2007/10/do-not-buy-this-book.html. At first I was astonished and strongly disagreed with him. Later I thought about it, digested its meaning and must admit, I agree.

So this is a how I will choose which books I will buy or not:

 

Buy

  • Books about architecture, design and techniques – design patterns, dependency injection, test driven development. Interestingly also useful are books of these topics applied to a specific language. Even more interestingly, also useful are books of these topics applied to a specific language you broadly know but don’t use. In particular, you can mix and match books on these topics between Java, C# and ActionScript whether you are a Java, C# or ActionScript programmer. Doing the “translation” (adapting examples to missing language features, etc.) might even help.
  • Books about the fundamentals of a technology you know nothing about. This means that if you have to learn say Silverlight and you are a Java programmer, buy a book about C# and XAML. Do not waste time in buying books containing practical examples like “form validation” or “how to create modal windows” and so on. There’s Google for that.
  • Books about organization and (self-)management practices. From The mythical man month to I.M. Wright’s Hard Code, you can’t go wrong with this kind of books. With “you can’t go wrong” I mean that even if you pick up an obsolete, wrong and heretical book about soft topics you’ll find more food for thought than by buying a “recipe” book with solution for specific problems. An unexciting book on Waterfall planning gives you more to think about than “how to make round corners in CSS”, which takes a whole 5 seconds to find out and other 5 seconds to forget (there is also no reason you should remember this, unless you do front end formatting every day, in which case you’ll remember it anyway).

 

Think twice before buying

  • Books about examples which teach you the questions instead of the answers. These are difficult to tell from plain examples books. The difference is in the details: instead of telling you how to do something, these tell you how to fish, but they still do it through examples. You can usually tell them apart because the examples are thoroughly explained in detail, and become food for further thought instead of pre-cooked solutions of your problems.
  • Books about examples for some (but not all) DSLs (domain specific language). Things like regular expressions, XSD schemas, XSLT transformations and GLSL/Cg/HLSL shaders are best learned through examples. The complexity of a shading language – for example – does not lie in understanding the concepts (which are rather basic) but in finding a way to apply them to real problems (lighting, mapping, etc.); examples provide real ways to overcome this difficulty. I wonder if the same approach could be used for parallel assembly language (MMX, SSE, AVX) since it shares the same problem (easy instructions of which the purpose is almost impenetrable). More complex DSLs (like SQL) require a more conventional approach.
  • The “complete reference” of <anything>. You already have the “complete reference” of <anything> book. It’s on your keyboard, top-left,  and is called “F1”. Books named “the complete reference” really are divided in two parts: a first part, teaching you the basics and which may or not fall in the “buy” category, and a second one which is “the useless reference” which is little more than the online help dumped on paper. You are actually paying (in money, weight, etc.) for 1000 pages while only 200 are worth. Consider buying this if the price is low or you just discovered you’ll spend the rest of the next ten years on a given technology AND you deem memorizing the whole standard library an useful experience.

 

Do not buy

  • Books with “recipes” for solving specific problems. In real life you will have your own problems to solve. Even if they are problems solved by other people out there (I want to embed an image on a tooltip while hovering a list) you’ll find your own version of the problem has some important detail that will mess everything up (I want my tooltip to fade in, be always aligned on the left of the list and have a small arrow pointing to the list item). Besides, if you are not an UI developer these books are more and more useless. Make exceptions for DSLs like shading languages, regular expressions etc. as said above.
  • Books excessively pragmatic. You want to learn the inside out of things and a “getting started in 24h for dummies” book is not worth the paper it’s written on.  You want to know that in curly brace languages a for(…) is equivalent to a while with some syntactical sugar and not just a way to count from 0 to 100.
  • Books requiring to be read from start to end. You want to skim read it and then go back and forth as you wish.

 

Requirements

  • As a must, you want the book to tackle the topics your brain isn’t able to tackle within 5 minutes of internet access. Unbelievably there are many books about C#, for example, which spend chapters and chapters about conditionals and loops and have a single chapter dedicated to iterators, anonymous delegates, lambdas and closures together; just to bring you to 4 chapters dedicated to LINQ. This is because they are “pragmatic” books. For pragmatic content, please insert an RJ-45 cable in your Ethernet socket and type www.google.com.
  • It must have “gotchas” of the technology clearly highlighted so that you can catch them during your skim read. You are going to forget them in 5 minutes, but they really reside in some distant corner of your brain. When you’ll be stuck diagnosing strange behaviour in the system or debugging some code which just seem to be a compiler bug, you can remember it in a flash and solve the issue. I’m thinking about things like “this” in JavaScript closures, for example – either you read about it, or you are going to bang your head against the wall for a whole day (until some helpful colleague will enlighten you).

 

 

Photo: a book from Christopher Columbus travels, found in Porto Santo museums.

Filed under: Featured, Programming, Rants

18 Feb

Top 10 music for programming

57_Brugge_Belfort_Interno

For programming one needs silence to concentrate at the problem at hand. But comes a time (often) when one has to go in auto mode, and have the hands write the ideas accumulated at the time. Good music is the best thing to let them go, and it must be non-boring, lengthy enough to avoid having to switch back and forth, with the right level of darkness and, above all, top quality. The rule I followed is 1 per author, if an author has more than one submission, they share the same slot (and most, share). So, from bottom to top:

10 – Dido – Life for Rent, No Angel

For some reason when programming sad, melancholic  music feels better than a beach Boys album would, probably because I want my brain to be kidnapped by the music while the hands write like an automaton, and not my brain to start . Life for Rent has fewer good tracks than No Angel, but I’ve listened to No Angel literally too much and it’s starting to get boring.

9 – Dire Straits – Love over Gold

Dire Straits are able to reap a 9th place in this list, and it’s strange. Their music don’t fit the theme, and it’s generally not good to code with; you can’t concentrate with Twisting By The Pool. Still Love over Gold is a strange beast, and they are able to get on the list with just two songs of a single album of all their life. The first two of course.

8 – PFM – Storia di un minuto

The only italian album on the list, this is one of the highest moments of italian progressive rock of the 70s. There are two reasons for being only 8th place: first, the italian lyrics are easily distracting; second Impressioni di Settembre is too beautiful to be listened while working.

7 – Weather Report – Mysterious Traveller, Black Market
Many Weather Report albums are better than Mysterious Traveller. Ok not many, and WR quality is so high that it would not matter anyway, but Mysterious Traveller has the top qualities as a programming music, specially the first track, Nubian Sundance. Black Market, however, is Black Market; you can’t go wrong with Black Market, and code produced under the Black Market effect is basically bug-free! Try it.

6 – Portishead – Dummy

The first trip-hop album in this list (unless you include Dido in the genre but it’s a stretch I think) and there are many, and all at the top – but don’t give it too importance to this fact, infact almost all the list is shared between 70s progressive, trip-hop and jazz/fusion. Dark, disturbing, distressing. Perfect. The reason it’s not first is that it lacks positive energy. But it has plenty of negative one, so plenty to get a 6th place without debates. Plus, one of the less significant songs in this album, shares an introductory sample with another of the less significant tracks of another album in this very list. Go figure.

5 -Miles Davis – Kind of Blue, A tribute to Jack Johnson

No list of top whatever music can be missing Kind of Blue. The only issue with Kind of Blue is if you want something more dynamic. Right Off from “A Tribute To Jack Johnson” is that for a reason. This reason.

4 – Morcheeba – Big Calm, Charango

No other album from Morcheeba has the same feeling as this one. And none will ever be since Skye Edwards left the band. Oh Wait! Skye Edwards returned! But they will not do anything like Big Calm anyway, because Big Calm is unique. Charango is a good alternative, but, slow down!, it’s way beyond no Big Calm, even if you think otherwise; get along with it. And if they can’t redo it who can you trust ?

3 – Hooverphonic – A New Stereophonic Sound Spectacular / The Magnificent Tree bonus album

Not everything from Hooverphonic is good, and no album satisfy the programmers needs fully. A New Stereophonic Sound Spectacular is the one getting closer, but some tracks really don’t cut, with The Magnificent Tree at second place. However the two “Visions” tracks (bonus tracks of the new editions of The Magnificent Tree) are perfect; 100% pure programming tracks.

2 – Pink Floyd – every long suite, Animals, The Dark Side of the Moon

More than 20 minutes of varying tones, with up and downs and ranging from dark to charging. Never boring, you can’t go wrong with Pink Floyd long suites. Be them “Echoes”, “Atom Hearth Mother Suite” or “Shine on you crazy diamond”. Bonus points for the entire Animals album, with Dogs getting again in the >20′ range and being top quality together with Sheep and Pigs while older albums offer gems like “Careful with that Axe, Eugene” and “Astronomy Dominé”. When long suites start getting boring (after some years of continuous listening), classic Floyd albums will take their place; The Dark Side of the Moon is a pleasure to listen, from Clare Torry vocals to the odd tempo changes in Money. Beware the clocks in Time, if you are tired.

1 – Massive Attack – Mezzanine

I don’t like Massive Attack that much. But tracks 1 to 6 of Mezzanine seem to simply have been composed right for programming. Not boring, rhythmic, dark, impressive, obsessive. When almost every track is that famous, with a track used in the Matrix soundtrack and another being the soundtrack of almost everything (from House M.D. to Assassin’s Creed) and featuring Elizabeth Fraser, you can’t be wrong. Speaking of Elizabeth Fraser, of Cocteau Twins fame, if this list was of 11 places, they would be in.

01 Feb

A smarter but not smartest greedy algorithm for hotel bookings

hotel

Doing homework for my university studies, I stumbled upon the problem of having a somewhat smart logic for hotel bookings. The assignment was to build a model-2 jsp/servlet based website for bookings of a generic hotel chain; the required booking facilities were simple but I wanted to do something smarter, and solve the problem in a satisfactory way.

Let’s say that you are eager to go to Sidney between the 29th Jan and 5th of Feb.. because – well, any reason you want to be there in that period. What bothers me is that most booking facilities just check for a whole availability – a single room available for the entire period. Now, if I really want to go to Sidney, and I’m already willing to take 12+ hours of plane to go there, renew the passport, fight spiders, etc., probably having to change the room at the mid of my stay there is the least of the problems – specially if the alternative is not going to Sidney at all.

Now, apart from the fact that this is a stupid example because I would never want to go to Sidney because there are some of the worst (1) spiders (2) of the world(3) there,  the problem was interesting to solve and the solution happened to me while walking towards home, listening to Alesha Dixon – this to justify the non-optimality of the algorithm. (The rest of the project was built over Miles Davis prestige recordings I received as a present for Christmas… much better food for the brain :))

Step 0 – the data structure

As always, the first step of a good algorithm is a good data structure. Since this is a simple algorithm, the data structure is very simple as well.. the data structure is an integer matrix of rooms rows by days columns. This already suggests that the algorithm complexity will be O(rooms*days), and it is, unless the data fetch from database is more complex for some reason. This step is already O(rooms*days) in space, there is no need to zero-set the matrix, every cell will be overwritten in step 1.

Example of pass0 result

Step 1 – fill data structure with room occupation data

The step 1, of O(rooms*days) time, is simple : store a 1 in each cell where  the corresponding room is available in the corresponding day, a 0 if the room is already booked.

Pass 1 example

Step 2 – does the magic

The step 1, of O(rooms*days) time, is also simple and does the magic that allows the final step to run: parse each row from right to left. If you are in zero cell, leave it as it is. If it’s a 1 cell, sum to it value to its immediate right. The final result is that each cell contains a number which says for how many following days that room is free.

Sample of pass 2

Step 3 – gets the result

Step 3 – O(rooms*days) worst case, but O(rooms) on average – leads us to the final result. Let’s parse each column from left to right. For each column, get the maximum number of that column, choose that room for the period until it reaches a 0 and advance of that many columns. Repeat until time ends, or until a column of only zeros is found – which means no booking is possible even with changes.

The end result

On optimality

The algorithm is a greedy algorithm of O(rooms*days) space and time complexity, optimal in the minimization of  room changes but sub-optimal in fragmentation. With this I mean that the solutions it finds may lead to small fragments of rooms for future bookings. For sure a human can do better, for example:

Better result - from human :)

does not leave a 3 days hole in room3, leaves room1 free for 4 days, sacrificing one day in room2 which would be hard to book anyway (unless it is already free in the weekend!).

On Storage

On a side note, for storage a number of solutions are available. However you have to consider that you need the booking to be a transaction and you want the transaction to fail if one or more days/rooms are not available anymore; basically the two ways I found are using constraints and from/to columns in the table, or simply having a separate entry for each day with (room,day) as the table key : this way if the two bookings conflict they will result in a duplicate key violation and one, or both, transactions will fail. Constraints and from/to periods work too, with the pro of having less rows in the database and reducing the algorithm execution time, and the cons of requiring a bit more database-fu. Of course other solutions might be available, but I’m definetely not a database expert (actually, I more of a database rookie :)).

22 Nov

Porting .NET app to Vista.. DEP strikes!

STOP

An interesting issue happened these days when I tried to port some apps to Vista.

Basically all .NET apps compiled with Visual Studio 2005/2008 are marked “NX compatible” by default. If your .NET app uses an incompatible DLL or COM object, the app will crash. What I found funny was that the message was a standard access violation error instead of a more specific DEP error.

Debugging it was not easy: I was unable to step through the code in VS2005 and using WinDbg wasn’t much of help too, except that the line where it crashed was something like a mov [esp+24h], constant with ESP well within limits — an instruction which should not generate an access violation exception given the current ESP value.  At that point I was starting to think that my “attempt to read or write protected memory was, in fact, something else.

Luckily my mind went to DEP and in less than 1 minute of Google search I was able to find this link with a good solution which, for the lazy and to preserve history is to add the following two lines as a post-build step.

call “$(DevEnvDir)..\tools\vsvars32.bat”
editbin.exe /NXCOMPAT:NO $(TargetPath)

I tried, without much faith, and it worked. And the C# app was finally working on Vista. And while you are fuddling with post-builds, go on and enable LARGEADDRESSAWARE while you are at it. 32bit users will thank you.