Visar inlägg med etikett software. Visa alla inlägg
Visar inlägg med etikett software. Visa alla inlägg

söndag 21 juli 2013

Tutorial: Using the MVC design pattern in Visual C#


This is a small tutorial to explain about the MVC (Model View Controller) design pattern, and to show how it can be implemented in Visual C#. The example also makes use of the Observer pattern and the Publish/Subscribe pattern.

The application we will implement is a simple calculator. Actually it won't be a very useful calculator - to keep the tutorial simple the calculator will only have two functions: addition and subtraction. How's that for a calculator? Well, this will be enough to illustrate the usage of the design patterns.

First let's say a few words about MVC and how to design our application. (If you already know well enough about the MVC pattern, you can skip this section and go directly to "1. Start Visual Studio..." further down below.)

MVC stands for Model View Controller and it is a way to separate the user interface (UI) logic from the business logic. Taking a calculator as an example, the UI consists of the buttons and the number display, while the business logic corresponds to the internal handling of numbers in the calculator: addition, subtraction, multiplication, division, and so on.

Why do we want to separate the UI and the business logic from each other? Well, the idea is that the code will be easier to maintain by doing so.

  • If the UI needs to be extended in the future, it is easier to do so if the UI is not entangled with business logic.
  • We could also write a completely new UI, without having to rewrite any of the business logic, or perhaps let the user choose between different UIs, without having to duplicate the business logic for each one of them.
  • If the UI and business logic and very clearly separated, we could even have them running on different computers if we want to. A server could handle the business logic and a client could handle the UI.
  • Also, having the UI and business logic separate means development work can be parallellized more efficiently: a UI developer can work with the UI while a business logic developer works with the business logic, and they will not be stepping in each others files.

In the MVC pattern, an application is split into three parts:

  • (M)odel: this is where the data representations and business logic is at. The model keeps track of which state the application is in, and has logic for transitioning between states
  • (V)iew: this is the UI, i.e. this is what the user sees and interacts with. Here might be buttons, text boxes, labels, and so on.
  • (C)ontroller: this is responsible for taking user actions and pushing them to the Model so that the Model can do its work and calculate a new state.

So, in order to have the UI decoupled from the business logic, we don't want any strong references between the View and the Model. It should be the Controller who is responsible for pushing the user actions to the Model. But then again it is actually the View that is wired by the OS to receive user input. (Considering Visual C#, the View is typically the window form and thus it is the receiver of all user input events for the application.) How can we make the Controller aware of the user input, so that it can notify the Model about it? Here it is fitting to use the Observer pattern. With the Observer pattern, one entity can notify another entity - the Observer - when something interesting happens. To implement this, we will let the View offer a registration function, where the Controller can register itself. Then when interesting things happen in the View, the View will call the registered entities to notify them about the user actions. This way the Controller can be kept up to date with what the user does, and notify the Model so that the Model can do its work.

Now let's say the Model has done some work - for example it has calculated the result of adding two numbers that the user has chosen. We don't want the result to just stay inside the Model - it should of course be shown to the user. It is the View who has all the UI elements and so it is finally the View that should show the result to the user. How do we pass the calculation result from the Model to the View, without using strong references between them? Here we can use the Publish/Subscribe pattern. In this pattern, there is an additional entity that offers others to both publish, and subscribe to, data of different types. It is the Publish/Subscribe entity that keeps track of who subscribes to what, and who publishes what, i.e. a publisher and a subscriber do not have direct references to each other, they just post and receive data independently. Nice! Applying this to the calculator example, let's have the Model publishing its results to the Pub/Sub entity, and the View subscribing to such results so that it can update the UI when necessary.

Now let's move on to creating our calculator application.

1. Start Visual Studio and select New Project -> Visual C# -> Windows Application. I named the app MVCexample.

2. Let's design the UI. I added:
* buttons for numbers 0 to 9, named button0, button1, and so on
* two buttons for adding and subtracting, named buttonAdd and buttonSubtract
* one button for clearing, named buttonClear
* one button for calculating results, named buttonEquals
* one text box for showing the results, named textBox



3. Let's create an event class to use when we implement the Observer pattern. Events like this will be passed from the View to the Controller to let the Controller know which user actions are happening. To create the event class, I selected menu Project -> Add Class, and named the new file Event.cs. And here is the code it should contain:


using System;
using System.Collections.Generic;
using System.Text;
 
namespace MVCexample
{
  public enum EventType
  {
    eventTypeNumberClick,
    eventTypeAddClick,
    eventTypeSubtractClick,
    eventTypeClearClick,
    eventTypeEqualsClick
  }
 
  public class Event
  {
    private EventType mEventType;
    private int mData = 0;
 
    public Event(EventType eventType)
    {
      mEventType = eventType;
    }
 
    public Event(EventType eventType, int data)
    {
      mEventType = eventType;
      mData = data;
    }
 
    public EventType getEventType()
    {
      return mEventType;
    }
 
    public int getData()
    {
      return mData;
    }
  }
}

4. We'll also create an interface that the UI event listener (i.e. the Controller) can implement to receive UI notifications: go to menu Project -> Add Class, choose name IEventListener.cs. This interface will be very small: it will just declare one function, receivedEvent, which will be called whenever something interesting happens in the UI. The function will take one parameter of the Event class type, which we just wrote.


namespace MVCexample
{
  public interface IEventListener
  {
    void receivedEvent(Event ev);
  }
}

5. Now we'll add UI event handling code to Form1.cs to handle the user's button click events. Also we'll add code to Form1.cs to let someone (the Controller) register in order to receive UI event updates. The button click handlers should call the registered listeners.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
 
namespace MVCexample
{
  public partial class Form1 : Form
  {
    List<IEventListener> mNumberClickListeners = new List<IEventListener>();
    List<IEventListener> mAddClickListeners = new List<IEventListener>();
    List<IEventListener> mSubtractClickListeners = new List<IEventListener>();
    List<IEventListener> mClearClickListeners = new List<IEventListener>();
    List<IEventListener> mEqualsClickListeners = new List<IEventListener>();
 
    public Form1()
    {
      InitializeComponent();
    }
 
    public void registerListener(IEventListener listener, Event ev)
    {
      switch (ev.getEventType())
      {
        case EventType.eventTypeNumberClick:
          mNumberClickListeners.Add(listener);
          break;
        case EventType.eventTypeAddClick:
          mAddClickListeners.Add(listener);
          break;
        case EventType.eventTypeSubtractClick:
          mSubtractClickListeners.Add(listener);
          break;
        case EventType.eventTypeClearClick:
          mClearClickListeners.Add(listener);
          break;
        case EventType.eventTypeEqualsClick:
          mEqualsClickListeners.Add(listener);
          break;
        default:
          Debug.WriteLine("Tried to register listener for unknown event type " + ev.getEventType());
          break;
      }
    }
 
    private void button1_Click(object sender, EventArgs e)
    {
      for (int i = 0; i < mNumberClickListeners.Count; i++)
      {
        Event ev = new Event(EventType.eventTypeNumberClick, 1);
        mNumberClickListeners[i].receivedEvent(ev);
      }
    }
 
    private void button2_Click(object sender, EventArgs e)
    {
      for (int i = 0; i < mNumberClickListeners.Count; i++)
      {
        Event ev = new Event(EventType.eventTypeNumberClick, 2);
        mNumberClickListeners[i].receivedEvent(ev);
      }
    }
 
    private void button3_Click(object sender, EventArgs e)
    {
      for (int i = 0; i < mNumberClickListeners.Count; i++)
      {
        Event ev = new Event(EventType.eventTypeNumberClick, 3);
        mNumberClickListeners[i].receivedEvent(ev);
      }
    }
 
    private void button4_Click(object sender, EventArgs e)
    {
      for (int i = 0; i < mNumberClickListeners.Count; i++)
      {
        Event ev = new Event(EventType.eventTypeNumberClick, 4);
        mNumberClickListeners[i].receivedEvent(ev);
      }
    }
 
    private void button5_Click(object sender, EventArgs e)
    {
      for (int i = 0; i < mNumberClickListeners.Count; i++)
      {
        Event ev = new Event(EventType.eventTypeNumberClick, 5);
        mNumberClickListeners[i].receivedEvent(ev);
      }
    }
 
    private void button6_Click(object sender, EventArgs e)
    {
      for (int i = 0; i < mNumberClickListeners.Count; i++)
      {
        Event ev = new Event(EventType.eventTypeNumberClick, 6);
        mNumberClickListeners[i].receivedEvent(ev);
      }
    }
 
    private void button7_Click(object sender, EventArgs e)
    {
      for (int i = 0; i < mNumberClickListeners.Count; i++)
      {
        Event ev = new Event(EventType.eventTypeNumberClick, 7);
        mNumberClickListeners[i].receivedEvent(ev);
      }
    }
 
    private void button8_Click(object sender, EventArgs e)
    {
      for (int i = 0; i < mNumberClickListeners.Count; i++)
      {
        Event ev = new Event(EventType.eventTypeNumberClick, 8);
        mNumberClickListeners[i].receivedEvent(ev);
      }
    }
 
    private void button9_Click(object sender, EventArgs e)
    {
      for (int i = 0; i < mNumberClickListeners.Count; i++)
      {
        Event ev = new Event(EventType.eventTypeNumberClick, 9);
        mNumberClickListeners[i].receivedEvent(ev);
      }
    }
 
    private void button0_Click(object sender, EventArgs e)
    {
      for (int i = 0; i < mNumberClickListeners.Count; i++)
      {
        Event ev = new Event(EventType.eventTypeNumberClick, 0);
        mNumberClickListeners[i].receivedEvent(ev);
      }
    }
 
    private void buttonClear_Click(object sender, EventArgs e)
    {
      for (int i = 0; i < mClearClickListeners.Count; i++)
      {
        Event ev = new Event(EventType.eventTypeClearClick);
        mClearClickListeners[i].receivedEvent(ev);
      }
    }
 
    private void buttonAdd_Click(object sender, EventArgs e)
    {
      for (int i = 0; i < mAddClickListeners.Count; i++)
      {
        Event ev = new Event(EventType.eventTypeAddClick);
        mAddClickListeners[i].receivedEvent(ev);
      }
    }
 
    private void buttonSubtract_Click(object sender, EventArgs e)
    {
      for (int i = 0; i < mSubtractClickListeners.Count; i++)
      {
        Event ev = new Event(EventType.eventTypeSubtractClick);
        mSubtractClickListeners[i].receivedEvent(ev);
      }
    }
 
    private void buttonEquals_Click(object sender, EventArgs e)
    {
      for (int i = 0; i < mEqualsClickListeners.Count; i++)
      {
        Event ev = new Event(EventType.eventTypeEqualsClick);
        mEqualsClickListeners[i].receivedEvent(ev);
      }
    }
  }
}

(If you think it looks bad to have explicit functions for all the number buttons like this, I guess you could create the buttons programatically instead and put them in an array. That way you could reuse one click event handler for all the number buttons.)

Please do not forget that you have to map all the buttons' click events to the click functions you just wrote. You do this from Properties, choosing each button and assigning the Click event to the corresponding click function.

6. Now let's create the Controller: from Project -> Add Class, I just named the new file Controller.cs.
In the Controller, we want to call the View's registration function so that we can receive the button events. Let's also add a private reference to a Model, and a function for setting the model. We will call this function later to associate the Controller with the Model. And we should implement the IEventListener interface to act on those button events. When we receive button events we will call the Model using our private reference. For now I have commented out all the references to the Model, we'll activate those later.


using System;
using System.Diagnostics;
 
namespace MVCexample
{
  public class Controller : IEventListener
  {
       // private Model mModel = null;
 
    public Controller(Form1 viewForm)
    {
      Event myNumberClickEvent = new Event(EventType.eventTypeNumberClick);
      viewForm.registerListener(this, myNumberClickEvent);
 
      Event myAddEvent = new Event(EventType.eventTypeAddClick);
      viewForm.registerListener(this, myAddEvent);
 
      Event mySubtractEvent = new Event(EventType.eventTypeSubtractClick);
      viewForm.registerListener(this, mySubtractEvent);
 
      Event myClearEvent = new Event(EventType.eventTypeClearClick);
      viewForm.registerListener(this, myClearEvent);
 
      Event myEqualsEvent = new Event(EventType.eventTypeEqualsClick);
      viewForm.registerListener(this, myEqualsEvent);
    }
 
    /* public void setModel(Model model)
    {
      mModel = model;
    }*/
 
    public void receivedEvent(Event ev)
    {
      switch (ev.getEventType())
      {
        case EventType.eventTypeNumberClick:
          numberClick(ev.getData());
          break;
        case EventType.eventTypeAddClick:
          addClick();
          break;
        case EventType.eventTypeSubtractClick:
          subtractClick();
          break;
        case EventType.eventTypeClearClick:
          clearClick();
          break;
        case EventType.eventTypeEqualsClick:
          equalsClick();
          break;
        default:
          Debug.WriteLine("Received unknown event: " + ev.getEventType());
          break;
      }
    }
 
    private void numberClick(int number)
    {
      Debug.WriteLine("You clicked number " + number);
      if (mModel != null)
      {
        // mModel.pushNumber(number);
      }
    }
 
    private void addClick()
    {
      Debug.WriteLine("You clicked add");
      // mModel.add();
    }
 
    private void subtractClick()
    {
      Debug.WriteLine("You clicked subtract");
      // mModel.subtract();
    }
 
    private void clearClick()
    {
      Debug.WriteLine("You clicked clear");
      // mModel.clear();
    }
 
    private void equalsClick()
    {
      Debug.WriteLine("You clicked equals");
      // mModel.equals();
    }
  }
}

7. Now let's create the Model! This is where all the cool and complicated calculations of the application will be going on. Only in our case they're not so cool and complicated after all: we'll only have functions for adding and subtracting numbers. Hm. (Let's make a game another time! Then we would put all the cool physics calculations and collision detection routines here.)

In general, if the application can have states, the state implementation should typically be here in the Model. Our simple calculator will have three different possible states: Idle, Adding, and Subtracting. We'll also keep a private member for the "current number" of the calculator. And we'll have functionality for modifying this value (pushNumber), and for temporarily storing a value, as well as adding, subtracting, clearing, and calculating results.

To create the Model, I selected menu Project -> Add Class, and I named the new file Model.cs. Here is the code it should contain:


using System;
using System.Collections.Generic;
using System.Text;
 
namespace MVCexample
{
  public class Model
  {
    private enum State
    {
      IDLE,
      ADDING,
      SUBTRACTING
    }
 
    private int mCurrentNumber = 0;
    private int mStoredNumber = 0;
 
    private bool mClearOnNextNumber = false;
 
    private State mCurrentState = State.IDLE;
 
    public void pushNumber(int number)
    {
      if (mClearOnNextNumber == true)
      {
        mCurrentNumber = 0;
        mClearOnNextNumber = false;
      }
 
      mCurrentNumber *= 10;
      mCurrentNumber += number;
    }
 
    public void add()
    {
      mCurrentState = State.ADDING;
      mStoredNumber = mCurrentNumber;
      mClearOnNextNumber = true;
    }
 
    public void subtract()
    {
      mCurrentState = State.SUBTRACTING;
      mStoredNumber = mCurrentNumber;
      mClearOnNextNumber = true;
    }
 
    public void clear()
    {
      mCurrentNumber = 0;
    }
 
    public void equals()
    {
      if (mCurrentState == State.ADDING)
      {
        mCurrentNumber = mStoredNumber + mCurrentNumber;
      }
      else if (mCurrentState == State.SUBTRACTING)
      {
        mCurrentNumber = mStoredNumber - mCurrentNumber;
      }
      mCurrentState = State.IDLE;
    }
  }
}

Now that we have the Model in place, we can go back to the Controller and activate all the references to the Model. Open up Controller.cs again and turn on the code that we commented out before, i.e:

 
private Model mModel = null;
 
    public void setModel(Model model)
    {
      mModel = model;
    }
 
    private void numberClick(int number)
    {
      Debug.WriteLine("You clicked number " + number);
      if (mModel != null)
      {
        mModel.pushNumber(number);
      }
    }
 
    private void addClick()
    {
      Debug.WriteLine("You clicked add");
      mModel.add();
    }
 
    private void subtractClick()
    {
      Debug.WriteLine("You clicked subtract");
      mModel.subtract();
    }
 
    private void clearClick()
    {
      Debug.WriteLine("You clicked clear");
      mModel.clear();
    }
 
    private void equalsClick()
    {
      Debug.WriteLine("You clicked equals");
      mModel.equals();
    }

By now we have most of the functionality in place:

  • We have a UI.
  • We are taking care of the user's button click events.
  • We can call the Model's calculator functionality to add or subtract numbers.

What we are lacking is UI updates when the calculator updates its result. For this, we will use the Publish/Subscribe pattern: the Model will publish its results, and the View will subscribe to such updates and update its UI accordingly. For this mechanism to work, we will create one interface and one class, in steps 8 and 9 below.

8. Create a new interface that the subscriber will implement, and which contains the different things we can subscribe to. In our example we will just have one thing we can subscribe to: calculator number updates. We'll name this type NUMBER_UPDATED.
Go to menu Project -> Add Class, choose name ISubscription.cs.

 
namespace MVCexample
{
  public enum SubscriptionType
  {
    NUMBER_UPDATED
  }
 
  public interface ISubscription
  {
    void numberUpdated(int number);
  }
}

9. Create a new class that will handling publishing and subscriptions: menu Project -> Add Class, choose name PubSub.cs. The class should have one function that the publisher calls to publish data, and one function the subscriber calls to register subscriptions of a certain type. In our simple example, NUMBER_UPDATED will be published whenever the Model updates its number representing the calculator's value to be displayed. Here is the code of the file:


using System;
using System.Collections.Generic;
using System.Text;
 
namespace MVCexample
{
  public class PubSub
  {
    private List<ISubscription> mSubscribers = new List<ISubscription>();
 
    public void subscribe(ISubscription subscriber, SubscriptionType type)
    {
      if (type == SubscriptionType.NUMBER_UPDATED)
      {
        mSubscribers.Add(subscriber);
      }
    }
 
    public void publish(SubscriptionType type, int data)
    {
      if (type == SubscriptionType.NUMBER_UPDATED)
      {
        for (int i = 0; i < mSubscribers.Count; i++)
        {
          mSubscribers[i].numberUpdated(data);
        }
      }
    }
  }
}

Now we need to make use of the PubSub class. It's time to add code for actually starting a subscription, and for publishing data.

10. Let's start with the publishing: modify Model.cs by adding a private reference to a PubSub object, and adding a function to set the PubSub object, and adding code to call the PubSub object's publishing function whenever the Model has updated its value.

 
private PubSub mPubSub = null;
 
    public void setPubSub(PubSub pubSub)
    {
      mPubSub = pubSub;
    }
 
    public void pushNumber(int number)
    {
      if (mClearOnNextNumber == true)
      {
        mCurrentNumber = 0;
        mClearOnNextNumber = false;
      }
 
      mCurrentNumber *= 10;
      mCurrentNumber += number;
 
      if (mPubSub != null)
      {
        mPubSub.publish(SubscriptionType.NUMBER_UPDATED, mCurrentNumber);
      }
    }
 
    public void clear()
    {
      mCurrentNumber = 0;
      if (mPubSub != null)
      {
        mPubSub.publish(SubscriptionType.NUMBER_UPDATED, mCurrentNumber);
      }
    }
 
    public void equals()
    {
      if (mCurrentState == State.ADDING)
      {
        mCurrentNumber = mStoredNumber + mCurrentNumber;
      }
      else if (mCurrentState == State.SUBTRACTING)
      {
        mCurrentNumber = mStoredNumber - mCurrentNumber;
      }
 
      if (mPubSub != null)
      {
        mPubSub.publish(SubscriptionType.NUMBER_UPDATED, mCurrentNumber);
      }
 
      mCurrentState = State.IDLE;
    }

11. Now add subscription code to the View. We will let Form1.cs implement the ISubscription interface and update the textbox in the UI when something gets published. Open Form1.cs and add this in it:


public partial class Form1 : Form, ISubscription
 
    private PubSub mPubSub = null;
 
    public void setPubSub(PubSub pubSub)
    {
      mPubSub = pubSub;
      mPubSub.subscribe(this, SubscriptionType.NUMBER_UPDATED);
    }
 
    public void numberUpdated(int number)
    {
      textBox.Text = "" + number;
    }

12. Now we need to wire some things together. Open Program.cs (which should have been autogenerated when you created the project), and modify it to look like this:


using System;
using System.Collections.Generic;
using System.Windows.Forms;
 
namespace MVCexample
{
  static class Program
  {
    [STAThread]
    static void Main()
    {
      Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault(false);
      PubSub myPubSub = new PubSub();
      Form1 myForm = new Form1();
      myForm.setPubSub(myPubSub);
      Controller myController = new Controller(myForm);
      Model myModel = new Model();
      myModel.setPubSub(myPubSub);
      myController.setModel(myModel);
      Application.Run(myForm);
    }
  }
}

To recap what we are doing here, we create a PubSub object, and we attach it to both the View and the Model, so that they can subscribe and publish independently of each other. We create the Controller and make it aware of the View, so that the Controller can register and listen to UI events using the Observer pattern. We also make the Controller aware of the Model, because the Controller needs a reference to the Model in order to give user data to it.

And that's it, we're done! Now you can run the project and play around with the calculator.

In case you get compile errors, make sure you are using the right namespace in all of your files.

And in case you have a problem that nothing happens when you click a button, it's likely you have forgotten to map the buttons' click events with the corresponding click functions. (See "Please do not forget..." above in point 5.)


Note that the UI is unaware of the Model, and the Model has no direct references to the View either. (MVC mission accomplished!) So if you would want to make a more fancy UI, you would only have to update the View (Form1.cs) without messing around with the internals of the calculator.

And in case you would want to run the Model and the View on separate computers (for example running the Model on a server), you could modify the PubSub implementation so that it passes subscription messages asynchronously over the network instead, for example using TCP or UDP.

If you'd want to use fewer classes, I guess you could skip using the event listener and instead reuse the PubSub object for sharing UI events with the Controller from the View.

One important shortcoming with this calculator (other than that it only handles plus and minus, ehum...) is that it uses a signed int for storing its value. This means it can't handle numbers larger than 2^31 minus 1. If you'd want to implement a real calculator you should use a larger datatype.

If you want to get hold of the files for this project, I have uploaded them here: https://github.com/mattiaserlo/MVCexample

onsdag 19 juni 2013

Productify

I think a good way of learning something well, is to try to expand upon what you want to learn, and make a product out of it. Instead of wanting to learn about X, looking up X in Wikipedia and be done with it the next minute, how about spending some time figuring out something you can actually create once you know X?

At least for me, instead of learning a bunch of disparate things separately, it's much more motivating to put things in a larger context and actually build something tangible out of it.

And by product I don't necessarily mean something you can earn tons of money from. Of course it's nice if your product can make you a gazillion dollars, but it's ok too to make a product that noone wants to pay a dime for. (I'm from a socialist country!) As long as you are having fun and learning something during the process, it's all good. I believe all learning has value, strengthening you and increasing your chances of favorable positions down the road.

To create a product means to identify a chain of sub-problems that needs to be solved, deciding on an order in which to solve them, and then solving them one by one. Some sub-problems may require you to look things up and learn (it's ok to use Wikipedia!), and some sub-problems are things you need to invent, or just execute. Some of the sub-problems are obvious when you think of the product in the first place, but I believe most products also have a bunch of hidden sub-problems. Problems that you don't come across or imagine unless you actually start breaking things down and get working on making the product. Stumbling upon and solving those problems are valuable too, sometimes more so than working on the most obvious ones.

Besides from learning many new things from solving these sub-problems, creating a product is a nice feeling. You can show a product to others as a tangible demonstration of your skills and creativity. Perhaps you can even sell the product and earn money from it. Or if not, perhaps it spawns ideas for new products that will. (Then go make those!)

So how do you start? Which product will you make? Perhaps you don't have a killer idea for a completely new revolutionary product that will rock the world. It's ok. I know a fun exercise: Think of an existing product and try to figure out how it's made. Which problems need to be solved to make product X? What do you need to learn, and which things do you have to create, and in which order? As a next step, how about seeing if you actually can make it? Perhaps you're not able to set off enough time to create say a next-gen particle accelerator, but hey if you look around there are tons of smaller products that could be fun to emulate and learn from. Be kind to yourself and let the definition of product encompass things that can be made in one or a few hours.

So, try to come up with a product. Or steal one.

Eventually when you're finished making the product, write about it. As they say, "learning by teaching" is perhaps the best way of learning. By figuring out how to teach something in a clear way to someone else, you force yourself to organize your thoughts. You think about which are the important parts and which parts can be left out. You think about in which order to present all the sub-problems. You imagine questions, forcing you to really think things through and not brushing over any important details. Also it gives you valuable writing practice.

And once you're finished writing, you know what? You just got yourself another product. Because your writing is most definitely a product too. Hey, now you have two!

To summarize:

1. Think of a problem or something you want to learn
2. Make a product out of it
3. Write about it

Simple, isn't it?



A particle accelerator. You know, for inspiration. No pressure!

måndag 17 juni 2013

Node.js


Admittedly, besides from a little dabbling with PHP a long time ago, I don't have much experience in writing server side applications in other frameworks than Node.js. So I don't have much to compare with. But I have to say I'm surprised by how quick and easy it is to write server side apps using Node.js.

For those who don't already know, Node.js is a software platform for server side apps, and it is built upon the same Javascript engine (called V8) as used in Google Chrome. Thus Node.js makes it possible to execute Javascript code on the server. Before Node.js, Javascript was regarded solely as a client-side language.

Perhaps the first benefit that comes to mind with this is that we can now share code between the client and server implementations. Let's say you are making a simple multiplayer game in Javascript, and you want the server to be in charge of the game logic, but you also want clients to run game logic to be able to predict the game state while waiting for updates from the server. Without running the same language on server and client this could be quite a hazzle, and even small differences between the client's and the server's engine state calculations could lead to large confusing differences in state. Thus it's a really good advantage to be able to run the exact same logic on both sides. (Besides, obviously you wouldn't have to implement the game engine twice.)

This leads us to another aspect of Node.js: Node.js apps are typically single-threaded, meaning one single thread on the server handles all the requests made from multiple clients. Client-server work should be designed to be asynchronous, i.e. typically the client sends a request to the server and the server will call the client back shortly later using a callback function. As a benefit of being single-threaded, the overhead needed to serve a new connecting client is very low: the server does not need to create any new process or allocate any new memory, and it does not have to do any context switching to serve multiple clients.

As a result, one server app can typically handle a large number of clients without bogging down. On the other hand, this means that it's bad to spend a long execution time serving one client as work will not be going on in parallell. Going back to the multiplayer game example above, it's then actually a bad idea to run game engine code on the server unless the executions can be made quickly. I guess Node.js apps should be designed to do just short operations. (I have heard there are some attempts in using something like webworkers to spawn threads for longer running operations, but I have not tried it out.)

Also, serving clients in one process is obviously dangerous if some code is misbehaving: if one operation crashes, the whole server app goes down. As an example, I found out that the file system call to read from a file will throw an error if the file does not exist. Forget to write exception handling for that, and you'll watch your server go down in flames when one single client cannot find the file it is looking for.

Anyway. For my experiments so far Node.js has been nice to work with, and the benefits have greatly outweighed the limitations. Knowing a bit of Javascript it's usually very quick, and does not require much code, to achieve quite a lot. There are tons of open-source packages you can use to speed up your work, and a package manager (aptly called NPM, Node Package Manager) to help you installing things.

So far, using Node.js, I have written:
  • One file server, which I use as a web server on my Linode machine, i.e. to serve files over http at www.mattiaserlo.com
  • One "session server", which uses socket.io to handle communication between clients. I'm using this for simple multiplayer games and chat-type apps.
  • A few RESTful services, that I used for testing to store and retrieve data from the server.
I'm running all of those on my rented Linode server.

To get started using Node.js on a Linux machine, go to http://nodejs.org/ and download an archive, then unpack and install like this, using shell:

tar xzvf node-v0.6.7.tar.gz (or whichever version you have downloaded)
cd node-v0.6.7
./configure
make
make install

You can run Node.js apps on your local computer using localhost, although it's much more fun to host your stuff on a real server that is always accessible by your friends. If you don't already have access to a server, I recommend renting one: see my earlier posting, http://mattiaserlo.blogspot.jp/2013/06/virtual-private-server.html

Finally, as an example, let me show how easy it is to create a file server using Node.js and a few packages:

Assuming you have installed Node.js (see above),

1. Do "npm install node-static"
2. Do "npm install http"
3. Create a file named myfileserver.js, containing this:

var static = require('node-static');
var http = require('http');
var file = new(static.Server)('/var/www'); // The folder which you want to serve files from
var httpServer = http.createServer(function(request, response) {
request.addListener('end', function() {
file.serve(request, response);
}
}).listen(80); // The port number your server will listen to requests on

4. Run the server like this: node myfileserver.js

Now you can connect to your server from another computer and fetch files from /var/www.

To run your fileserver as a service, instead of from a blocking shell call, break the Node.js execution above and create a file named myfileserver.conf, containing this:

description "My node.js server"
author "Me"

start on started mountall
stop on shutdown

# Automatically Respawn:
respawn
respawn limit 99 5

script
export HOME="/root"
exec /usr/local/bin/node /node/myfileserver.js >> /var/myfileserver.log 2>&1
end script

post-start script
# Optionally put a script here that will notifiy you node has (re)started
# /root/bin/hoptoad.sh "node.js has started!"
end script

The exec line above assumes you have stored myfileserver.js in a folder called /node.

Store the conf file in /etc/init on your machine, then you can start and stop your service whenever you want, like this:

start myfileserver
stop myfileserver

måndag 10 juni 2013

Drive



I took a decision some time ago to try to change my role at work to be able to spend more time working actively with software again.

I don't mean only programming, but learning more about software and code quality, frameworks, patterns, languages, algorithms and data structures, operating systems, trends and so on. And yes, programming too.

I believe I used to be a good programmer before that point in time, but the way things had developed at the company I found myself in roles where I did a lot of coordination and management-like work for a couple of years, gradually losing engineering competence from not doing enough of the technical stuff.

You know, the fun stuff.

The thing is, if a tech company would be a racing team, I think of the software engineer as the race car driver. Even though the outcome of course depends on a large group of people in the team, all with different roles, it's arguably the driver that has the most exciting job. And it's the driver that brings the car over the finish line. Although I appreciate skilled people in any role, it's the talented driver that earns my highest respect. I want to be the driver.

Yet after driving some races usually your company will want you to move on to do other stuff, like watching races and giving feedback to the new younger racers. And write documents about driving processes. And help driver A communicate with driver B in another country. And so on. Perhaps the company might even outsource all the racing! What's up with that?

Getting older, people around you may look up to you as an awesome driver, a driver with tons of racing experience. Yet you may not have driven any race in years. The thing is, maintaining skills as a driver requires that you actually keep driving now and then. Otherwise, as time passes by, one day you'll have forgotten how to do it.

My plan was two-folded: try to change my work role so that I would do less administrative tasks and spend more time doing hands-on technical work, and also to set off time at home during evenings and weekends to learn new things, by reading articles and doing experimental software projects as a hobby.

And the winnings I imagined would also be two-folded: I would have more fun, and I would be better prepared for change because I would be technically stronger.

It's good to be prepared for change. Even if you don't have a plan to change jobs anytime soon, the fact is you don't have absolute control over the company you're working for - no-one does - and so you really don't know whether you might be forced to change. And if you need to change, be it for internal or external reasons, most likely you'll need to go to interviews. Are you prepared to go on an interview and explain in detail what you have been doing the last two years and which parts of product X you have contributed to, and how? Later being asked to write source code on a whiteboard to solve a technical problem, in case it is a software engineering position. I went to an interview and failed, realizing far too late that I had fallen behind technically. Not from not taking work seriously at my workplace, but because I had done exactly everything my company asked me to do, and not all of those things were strengthening my profile to prepare me for other roles in other companies. Actually several of the things I had been working hard on were rather counter-productive, distancing me from the profile I wanted to maintain. And I had failed to see that I needed to compensate for this by spending free time to actually stay up to date and skilled.

This was a late realization for me, that I needed to reflect more actively on where I was going and whether I was ok with the direction, and whether I felt I was learning enough. Not to mention whether I was having fun or not. Work is supposed to be fun. And learning should be fun.

Shortly thereafter, with my new approach, I felt joy again. Joy to learn new things. Joy to relearn things I had been studying or practicing long before. And most of all, joy to do what got me interested in computers in the first place: creating cool things with software.

I also changed the way I see "gaps" in my knowledge. Whereas before I would find a gap and be disappointed that there was something I didn't know, I decided to appreciate gaps as they are opportunities to learn something new and improve my skills. Moreover I started seeking out gaps, trying to track down things I felt I ought to know about but yet didn't. It's much better to find gaps on your own in a controlled environment than in a future job interview. And it's fun to feel how you are progressing and becoming better at something. I had almost forgotten that.

So, be prepared, and get behind the wheel and drive now and then. If not in the sense of doing hands-on software engineering, in case that's not your cup of tea, then in the sense of having control over where you are heading and your self development. And enjoy the ride!