Software developer blog

I have just released a code cast in which I do Bob Martins version of the bowling game kata in php, but take it further with a refactoring.

In the previous part we have built a very simple chat application, but it stored messages in memory. Every time it was restarted, all messages were gone forever. In this part of the series we will look at MongoDB, a document database. There are many other database technologies, some of them are more frequently used than MongoDB, but for our purposes it is the best choice.

If you haven't read the previous parts, click here.

In the first two parts we have examined how to create web pages using Sinatra and Mustache templates. In both cases we fed a small amount of data through the url, and then we returned something that was based on that data. However having to type input data into a url is not necessarily a good user experience, especially if we wish to send a larger chunk of data. In this part we will learn how to send user inputs properly.

Another problem we haven't addressed yet, is storing that data, so that we can show it later, and show it to other users, but that will be the topic of the 4th and final part of this series.

If you haven't read the previous parts, click here.

In the previous part we dipped our toe into git, bundler, sinatra and mustache. In this part we will first explore how to add some interesting logic to our web application, while keeping that logic unaware of the very existence of the web. In fact we will go as far as to create a web app and a command line application that will do the same thing through different user interfaces. In the process of creating these applications we will also explore the realm of test driven development, a practice that can lead us to better software.

If you haven't read the previous part, click here.

This is a beginner tutorial that shows you how to build a Ruby app using a certain set of technologies that I prefer to use. It will not make you a web developer right away, but it will show you the basics, and serve as a starting point for further reading. I assume that you already have basic knowledge ruby, HTML and CSS, or at least have read a tutorial that introduced you to them. If not, visit Codecademy. Another assumption I make, is that you are using Ubuntu (or possibly another Debian derivative) as an operating system. If not, you can download it free, and install it next to your existing operating system.

The approach I have taken is to show you most of the tools at once at a very low level, so that first you get the big picture, and then we will drill deeper later. This may be overwhelming at first, but bare with me, slowly everything will unfold.

By the end of the first part we will have a web page, that can say hello. Not much, but it's not the the goal we reach that will be of importance, but the way we get there. I will show you the tools developers use from day to day. We will look at how we manage gems, the building blocks of software development. Then we will look at some of those gems, most importantly Sinatra, that is designed to respond with web pages for requests of web browsers. We will also take a quick look at git, a tool, that is designed to store source code, and keep several versions of it around, so we can go back, when something goes wrong. Finally we will look, at how you can make your page look better with a Mustache.

On the first day of NDC London 2013 there was a cage match between Gary Bernhardt of Destroy All Software - known for the WAT talk, and Useing You're Type's Good - and Jon Skeet of Google - known for his "C# in Depth" book, and his activity on Stack Overflow. They were discussing the topic of dynamic versus static typing.

Jon Skeet started by asking, what is it that Ruby can do and C# can't. In response Gary tried to show stuff that seems idiomatic in Ruby, and relies heavily on the dynamic nature of Ruby. Where the discussion went a bit south was when Jon Skeet tried to do all that in C#, and did the ugliest hacks just to make it work, while relying on C# features that are explicitly there to make C# a bit more similar to dynamically typed languages. All that proves to me, is that C# is trying really hard to be Ruby, and sucks at it. So what? Besides, we all know that it's not true at all.

I personally have changed back and forth between statically typed and dynamically typed languages quite a lot, and both hated and loved them every time I did. After a while I started to have very exciting discussions between the left and right side of my brain. (Rest assured: we are not schizophrenic, although the rest of this blog entry may suggest differently.) The odd thing is that both of me seem to have realized that the other has interesting points to support his beliefs, but neither of me is willing to give up any of it. Instead I kind of became the devil's advocate on the matter. (And that's really funny if you know what my last name - Ördög - means in Hungarian.) Whenever I meet anyone who seems to strongly believe in one or the other, I try my best to convince them that they are completely wrong and total idiots. So if you came here with the intention of finding out which one is better, here is a final clue: you won't! However I promise there is an interesting conclusion at the end.

Let's give a name to my brains, just to make the rest more fun: Steve is a firm believer in static typing, while Dan is this ruby / nodejs fan boy.

If you wish to read a transcript of the video click here.
@ //

Last Saturday we had the first ever Lean Poker Tournament, an event much like a code retreat, but with a slightly different format and purpose. A lean poker tournament's aim is for participants to practice concepts related to lean start ups and continuous deployment. A poker team is a small group of developers (ideally 4 people forming 2 pairs) whose aim is to incrementally build a highly heuristic algorithm within a one day time frame that is just smart enough to beat the other robots. Professional poker robots are developed for years, so the purpose is definitely not to come up with something really smart, but to be the smartest among the current competitors. With this in mind teams can come up with simple to implement heuristics, examine their effect on game play during training rounds, and then improve their algorithm in a similar lean fashion.

The participants of the very first Lean Poker Tournament.

The participants of the very first Lean Poker Tournament.

@ //

Few weeks ago we held an internal Coderetreat at Emarsys for all programmers within the company. When the business intelligence team heard about it they started to joke around, that they should drop by and implement Conway's Game of Life in SQL. I'm not much of an SQL wizard, but I rose to the challenge. First I tried it in MySQL, since that's the dialect I have the most experience with:

CREATE TABLE generations (`id` INT UNSIGNED, `x` BIGINT, `y` BIGINT);
INSERT INTO generations VALUES (0, 0, -1), (0, 1, 0), (0, -1, 1), (0, 0, 1), (0, 1, 1);
 
CREATE TABLE offsets (`x` BIGINT, `y` BIGINT);
INSERT INTO offsets VALUES (-1,-1),(-1, 0),(-1,1),(0,-1),(0,0),(0,1),(1,-1),(1,0),(1,1);
 
INSERT INTO generations 
  SELECT g.id+1 id, g.x+o.x x, g.y+o.y y
    FROM generations g 
      INNER JOIN offsets o 
    WHERE g.id=(SELECT MAX(id) FROM generations)
    GROUP BY g.x+o.x, g.y+o.y
      HAVING (MAX(o.x=0 AND o.y=0) = 1 AND COUNT(1) - 1 BETWEEN 2 AND 3)
        OR (MAX(o.x=0 AND o.y=0) = 0 AND COUNT(1) = 3);

It works, and it's not even that complicated. First we join the last population with an offset table to generate the set of living cells and their neighbors. Grouping by the cell coordinates plus the offsets we can get all necessary information. The expression max(o.x=0 and o.y=0) is one exactly if the cell was alive in the previous generation, otherwise it's zero. Using that we can count the number of neighbors too: count(1)-max(o.x=0 and o.y=0). The having clause utilizes that knowledge to filter the grouped rows to contain only the cells in the next generation.

@ //

This year I have talked at different Hungarian venues about software development related topics several times. Although these presentations are captured on video and are available on the internet they are in Hungarian, so I decided that the next few posts will explore some of them in English, and in more depth.

I gave the Test Driven Mockery talk at the April event of PHP Meetup Budapest. Although code examples and frameworks are in PHP, the talk is basically language agnostic, and it's just as useful for PHP programmers as it is for Java, C#, Ruby or even C++ coders. After all: it's about test doubles in general.

At the time of writing this blog entry more and more software developers and companies decide to adapt test driven development as one of their primary practices to avoid code rot and fear of change. Of course there are crowds who still haven't even tried TDD either because they haven't heard about it, or it sounded too weird for them to try. However there is another lot more interesting group of educated professionals who after practicing TDD for a considerable amount of time decide to abandon it. As I was talking with a group of these people I recognized that most of the time their disappointment in the technique is easily tracked back to their misunderstanding of a very important related topic: test doubles and mocking. On one hand if you completely avoid using them you end up with a slow and fragile test suite that's hard to maintain, but if you use too many of them the promise of ease of change, well tested and flexible code becomes a lie. Finding a good balance is kind of an art.

A lot has been said about good and bad design decisions all over the web, and I hardly think I could add anything new to the list of code smells. Yet they still keep popping up in every single piece of legacy code I come across. My theory is that many of these patterns do not simply arise as the evil doings of a single bad programer, but more as a result of collaboration between brilliant programers, who just fail to realize the problem early. If you look at it that way it becomes apparent that bad designs are inevitable in the long run, and so eventually they need to be fixed.

Or do they? Well... it all depends of course. There is nothing more harmful than putting countless hours of work into changing code for the sole purpose of aesthetics. I'm an idealist at heart, and I'd love to spend all the time I can find on prettifying the existing code base before I ever change behaviour, but the reality is that beautiful code won't earn you a single penny unless it delivers business value. On the other hand we all know that bad code can hurt us big time: not only makes it expensive to add behaviour, it also gets hard just to keep things from falling apart.

Refactoring legacy

Refactoring code without tests feels like tightrope walking for the first time.

Are you confused yet? Should you refactor or even rewrite that piece of ugly spaghetti code tomorrow morning, or not? Let's say that by default you should never touch legacy code that works as expected. (It gives me enormous pain to even think that this is a good idea...)  Accepting the premise here is a list of the most important exceptions to this rule, when you should most definitely become obsessed with cleaning your code.

@ //
Next Page »

Switch to our mobile site