May 19, 2013

RabbitMQ and Windows 8

Recently I had strange error on one of customer's laptop. We use Rabbit MQ as a message queue in our application, and it didn't want to start on that laptop. Main idea for now is that it's because of Windows 8 on that laptop, but maybe it's just coincidence. So, it was preamble, and now let's go to real story...

Oh, small sidenote - it's a story about Erlang v14 and RabbitMQ v2.8.1. Maybe, in latest versions everything is fixed already.

So, RabbitMQ's service didn't want to start. In log (they are in c:\Users\<your_user>\AppData\Roaming\RabbitMQ\log\, more detailed about RabbitMQ's folders you can read here) we didn't find anything readable except something like "there is an exception when I try to open port". So, first idea was that somebody holds one of necessary ports (they are 4369 for Erlang and 5672+55672 for RabbitMQ. Latter one changed to 15672 in RabbitMQ v3.0). But real problem was in filesystem.

I don't know if it's new defaults for Windows 8, but C: volume has disabled short names (8.3) for files/folders. As result - RabbitMQ tried to start some application to check if port is free or something like that, and didn't manage to do it. You can check if this is your case by this command (type it in command line):

fsutil behavior query disable8dot3 c:

If value is not 0 (8dot3 name creation is disabled) - it's most probably that you have the same problem. In this case you should first - enable creation of short names:

fsutil behavior set disable8dot3 c: 0

Maybe it's even better to enable it for all volumes (in this case just skip "c:" param in line above).
Bad news is that short names will not re-created automatically. And we need short names for all parts of path to RabbitMQ/Erlang (e.g., even for "Program Files"). Theoretically, you can try do the following:

fsutil file setshortname "c:\Program Files\" PROGRA~1

But I had "access denied" without any success to resolve it. So, workaround in this case is to uninstall Erlang + RabbitMQ and then install them in new folder (e.g. - to c:\tools\). Be sure, that ALL parts of path to RabbitMQ + Erlang has short names (you can check it with for example "dir /X" command).

That's it. I hope you're have running RabbitMQ now...

January 28, 2013

Engineering and development

Why engineering systems work almost without any errors, but software has bugs usually? What is a difference? What we should steal from engineering disciplines to make software robust as engineer devices? 

Side note: Does anybody know something good to read about this? I am reading “Lean Startup” now, and Eric Ries notices that “lean manufacturing” from Toyota was the basis of his idea. After this book, I will read about lean manufacturing for sure, but… Is there anything else to read? 

3 years ago, we bought a new house. House was empty – just foundation, walls and roof. It had only single lamp in center of each room, and single socket per room. Everything was in horrible state. No water, no sewerage, no ventilation – nothing at all. Everything was done from the scratch. The hardest parts were electricity and plumbing. I liked and I like to do it. You have a set of components/blocks. For instance, you have water pipes, pipe elbows, T-branches, threaded connections, boiler, filter etc. For electricity – wires, sockets, switches, circuit breakers etc. You connect everything together – and everything works! Without errors! Yes, you can have a problem, if you will pass too high or too low voltage to input. But you can solve it with voltage relay. In result, if your system gets wrong “input data” (voltage) – it just turns off. 

Let’s talk about programming now. For instance, C#. We have something like 50-60 reserved words + 30 “characters” (brackets, plus, minus etc.) As result – something like 100 “blocks”. It’s quite close to a set of blocks for electricity or plumbing. Than why software has bugs but plumbing doesn’t? OK, but usually software is much more complex than plumbing in a house. Maybe. But if we will try to compare electric grid of whole country? I think it’s not simpler than OS even… 

Another example – CPU. Modern CPUs have from 1.5 (Core i7-3770K) to 3 billions (NVidia Fermi/GT300) of transistors. They have from ~200 (i7) to 512 pipelines (Fermi). They predicts next possible instructions (even if they don’t have yet data from RAM), execute them, and if prediction was right – they return result almost immediately. By the way, this prediction block was removed from Atom, and it is one of the reason why it’s quite slower. And CPUs don’t contain bugs. They don’t have patches, they don’t have service packs. Once manufactured CPU works for tens years… Why? 

I think that some reasons could be: 
  • Very detailed specification of input and output data. Each “block” of such system needs input data in exact range (e.g., 220V +- 10%), and returns output data in exact range also (e.g. 5V +-5%). 
  • Engineering systems are tested scrupulously. Very scrupulously. If we will use development terms, engineering samples have “100% code coverage”, and each “line of code” is covered not just once. They have unit tests, integration tests and much more. 
  • Once specification is developed – it doesn’t change. It doesn’t change after start of manufacturing – for sure! Nobody asks to move elevator shaft one meter to the left (it’s just a little!) 
  • Nobody except certified specialists could interfere in engineering system. You cannot just come from outside and connect your power plant/factory/ultra-modern-quantum-transformer to power grid of country. For obvious reasons. 
  • Very often developers use “unnatural” way to create “dependencies” – inside object that needs another object. It looks… quite strange when fridge (that needs 220V) builds power generator near itself, and this plant builds small gasoline pipe to closest gas station. No, in real world everything is not like this. In real world YOU buy fridge, power generator and can with gas, then you connect fridge to power generator and pour gas in power generator. Or you buy sockets/switches/lamps/circuit breakers and then connect everything together with wires. 
I understand that if you will add all these stuffs in development process – speed of this development will decrease in times (probably, even in tens times). And probably, these principles are used in development of critical system (like power plants systems or artificial heart valves). But there should be some analogs that you can use in “cheap” way, without significant increasing of development time (you can read – cost of development). 

All stuffs below is regarding C#, but I’m sure that almost all object oriented (and other) languages should have something similar. 
  • Clean specification of input and output data. And checking of these constraints. In programming it is – checking of range of input data, processing of boundary conditions etc. I think the closest analog is “Design by Contract” proposed by Bertrand Meyer in Eiffel. Many languages have libraries that provides close functionality (Code Contracts for .NET, Contracts for Java/jContracts for Java etc.) Ideally these libraries should contain two functions (and they usually do) – checking of input data in runtime (as voltage relay) and static checking (e.g. if method returns value in range of 5..10 then obviously it’s unsafe to pass this value to another method than needs input data in range of 7..15, and it’s obviously wrong to pass this value to method that needs input value in range of 20..30). Unfortunately current implementation of Code Contracts from Microsoft has huge con that prevents its usage in production – it adds too big overtime to compilation. But idea is cool and very useful. And I’m sure that development of static analyzers will add a lot of benefits to usage of contracts. 
  • Testing. Everything is obvious here. More (proper) tests is better. But it’s a lot of time. And money. As result you should always look for compromise. Is it critical if your DVD player will hang once per year and you have to reboot it? And is it critical if it’s nuclear power plant? Next step in testing is automation. Human’s time costs much more than computer’s one, and this is why you should have automated tests as much as possible, because more automated tests you have – it’s cheaper to execute these tests. But you should write these tests, and it increases the cost of development. Again – you should always look for compromise. Unit tests are obvious but when you move higher in hierarchy you see more and more problems. How to test layer that works with database? How can you emulate internet connection? How to abstract from database at all? How to test UI? And I think if you will investigate these questions – it will increase your code quality a lot. Dependency Injection (I will talk about it later) and MVC/MVVM/etc. patterns help a lot exactly with testing. Test Driven Development, Continuous Integration… And if there is still discussion regarding TDD (it’s especially cool when you develop math libraries), then regarding CI everything is clear. You don’t use CI? Well, it’s sadly… 
  • Specification doesn’t change after creating. Well, here are too many opinions. Somebody uses waterfall (and it’s perfect when you need big product with good quality), somebody uses Agile (I’m not sure if “specification” exists at all in Agile). And, you know – both are right! But more closer you are to Agile – more automated tests you should have. Otherwise you will find yourself in fixing hell very soon. 
  • Nobody except certified specialists could interfere in engineering system. Abstraction will help a lot on library/application level. For OS – you should also separate everything in layers without possibility of easy access to level below (e.g. kernel level). Separation of processes (so one process could not use e.g. memory of another one) helps a lot too. 
  • Inversion of Control, proposed by Martin Fowler in far 1988, absolutely wrong implementation with Service Locator, and much better implementation with Dependency Injection. Dependency Injection is really proper way to connect “blocks”. In example with fridge – fridge doesn’t care who will be provider of electricity. It could be home electric grid, it could be offline power generator. Fridge just needs something that will implement interface “IElectricityFactory”. Power generator doesn’t care who will provide gas – man, gasoline pipeline or gas tank. It needs just something that will implement interface “IGasFactory”. So, we have 2 independent classes – “Fridge” and “PowerGenerator”. Then man (Dependency Injection Container) appears and decides to connect power generator as dependency to fridge, and decides that can will “implement” “IGasFactory”. If you will have two fridges – you can use two independent “ElectricityFactories” or use the same – you are DI Container, you decide, and not fridge. 
  • There are flying cars, but independent car is better for movement on roads, and plane is better for flying. So – split, split, split. Each class or service should be responsible for single area. Ideally – it should be single. In reality, it’s often 2-3. Max – 5. Yes, short-term memory can hold up to 7 objects usually, but you cannot hold more than 3 areas of responsibility for single class in head. 
So, there is brief list below where I’m trying to summarize everything: 
  • Continuous Integration. Must have. 
  • TDD where it could be applicable. For math/processing libraries – must have. 
  • Reasonable test coverage. 
  • Dependency Injection. Once you will lost in your system. You will forget one-half of dependencies, but –you will maintain your system without big problems, believe me. 
  • Services. Use them as layers between applications, use them as layers in your application inside. Use them everywhere. 
  • Split, split, split. 

March 18, 2012

Software should be for humans

Software should be for humans.

Software should be usable. Software should satisfy business needs. If software cannot do what you need – you don’t need such software. Ideally – if you will have a set of features exactly you need for. No less, no more. Each additional feature is the possible location of a bug. Each feature which doesn’t exist is your unearned income.

Software should be robust. Each crash is time and nerves wasting. Even worth – you can lose important information. For example, you arrange of insurance policy. You have been requested a lot of information from your customer, and… Your accountant application is crashed. Maybe, your customer will not visit you anymore… But if you have robust application, if you’re sure that you can serve each customer in no more than 10 minutes – you will always have a lot of customers. Good example is McDonalds – how often did you see queue near order window, because “my terminal hangs”? Personally me – never.

Software should be extensible. Especially it’s right for LOB applications. Business changes all time. And you will always need new features in your software. New business rules, new screens, even new business entities. Please, don’t have illusion, that you will develop your LOB application and it will not change for some years. It happens in one case only – if your business is dying. But if your application changes every time when your business changes, and if this changes are done fast – you will be always two steps ahead. Of course, it’s very important not to have regressive bugs after changing your software…

Software should be friendly. It shouldn’t say: “Hey you, you click on wrong button! You can’t click here!” Application should just disable all wrong buttons. “I have an update. Do you want to install it? – Error occurred during installation of update. Or – Update is installed. Please, read readme.txt for more information” I like Chrome updates behavior – I never know when it updates. Never. I use it from 1stversion. First 4 time I understood that it was updated only because some UI elements was changed. And from 5th or 6th version till current 17th I never know when it updates itself. Never. Or let’s look at Windows Media Player. Shortcut for “pause” is Ctrl+P. Are you kidding, man? I’m lying down on bed in dark room with bottle of beer, I’m hugging my woman, and I want to pause film. Of course, I’m very happy that I should put somewhere my beer, that my woman hasn’t enough attention from now, just because I should look for Ctrl+P. Why not “spacebar”? Why? The same about volume control… This is why I never use Windows Media Player.

Software should be simple. Did you ever see those terrible “one screen” LOB applications? When one screen contains hundred fields for data? It’s awfully. Why don’t join information about customer (name, second name, surname, personal code birthdate, address etc.) in a single field? It’s too hard? Common, you’re just too lazy. It’s easier for you not to do anything, instead of to do things one time as it should be. And thousands of users will say you “thank you”. And they will use your application – instead of crying each time when they should fill this stupid form again.

Chrome, Skype, TeamViewer… These are almost ideal software. They are usable – they do all they should. They are robust – they work always, even behind firewall, NAT and all other scarried words. Okay, Skype becomes not very robust in Microsoft’s hands, but it still works everywhere. They are friendly. They are simple – Chrome has only one field for both urls and search queries (why Firefox still has two?), Skype uses double click and “call” button, TeamViewer… They follow “principle of one button”. Download, Install (without options!), Start (without configuring!), Press “Do it!” button – and you have result. One screen with one button – “Do it!” This is where we should go.

Software should be for humans.

March 14, 2012

Extended ObservableCollection

Intro

.NET Framework has very useful class – ObservableCollection, which is almost usual collection, but has great feature – it supports notification about collection changes. This class is used very wide in WPF with it’s binding engine. But.. I was needed for some additional features, which aren’t supported by it. So – this is result of work on “Extended ObservableCollection” – ObservableCollectionEx.

I will use additional class TestItem for showing examples of usage of ObervableCollectionEx. The code of this class you can download by the link in the end of the article (as the code of ObservableCollectionEx itself). All you need to know actually that it has property Value of type int and it supports INotifyPropertyChanged interface, so user of this class can know about Value’s changes.

Features of ObservableCollectionEx

ObservableCollection

First of all – this is usual ObservableCollection. It supports all it’s features – as usual List, as Collection, Enumerable, notifications about collection changes etc. A lot of code was written after exploring internals of Microsoft’s ObservableCollection with Reflector. So, if you use anywhere ObservableCollection – you can easily switch to using ObservableCollectionEx – just by changing type of variables and constructors’ callings.

Notification about Item’s properties’ changes

ObservableCollection notifies about changes of items set, it notifies about properties’ changes of itself also (for example, about changes of Count property or Item). But it doesn’t notify about changes of properties of it’s items. And actually this is very-very useful thing in WPF developing. So, I had added event ItemPropertyChanged to my ObservableCollectionEx. You can attach event handler to it and check when item of collection is changed. Something like:

var TestCollection = new ObservableCollectionEx();
TestCollection.ItemPropertyChanged += TestCollection_ItemPropertyChanged;
void TestCollection_ItemPropertyChanged(object sender, PropertyChangedEventArgs e)
{
  var msg = string.Format("Property '{0}' of '{1}' was changed", e.PropertyName, sender);
  MessageBox.Show(msg);
}

Thread-safe collection

Almost all collections in .NET Framework isn’t thread-safe. I’m sure that this is done for performance reasons, but.. Sometimes you need thread-safeness. Especially in WPF, when one thread (UI) can display items from collection, second thread (receiver from some external application) can add items to collection, and third thread (some background worker) can remove items from collection. So, ObservableCollectionEx now is thread-safe! You shouldn’t do anything – it’s “from the box”. Just use it and you can be sure, that if one thread is using some method (for example, Add()), noone can use another method (for example, Remove()).

Filter

Another feature which I’d like to see in ObservableCollection (I want too much, don’t I?) is filter. Very often I have 2 scenarios – I want to show only part of collection in the screen (for example, only failed executions of something) or I want to separate one collection to some ItemControls – for example, show failed executions in one and successful in another. And.. I want to be sure that items are displayed in right ItemsControl, even if they are changed dynamically – if there are new items, or if existed items are changed, so they should be removed from collection or they should jump to another ItemsControl. ObservableCollectionEx supports filtering. Even more – this filter is thread-safe, it monitors changes of items, and it can even use another ObservableCollection as source of items. Here is the example of usage of Filter:
var TestCollection = new ObservableCollection();
TestCollection.Filter = p => p.Value > 10;
var item1 = new TestItem(0);
var item2 = new TestItem(10);
var item3 = new TestItem(20);
TestCollection.Add(item1);
TestCollection.Add(item2);
TestCollection.Add(item3);
After execution of code above collection will contain only 1 item actually – with value 20. But – collection will monitor for changes in all 3 items. So, for example, if we will execute:
item2.Value = 30;
collection will have 2 items – we didn’t add any item to it, but one item, which is “virtually” there, now passes filter. So, now collection has 2 items – item3 (20) and item2 (30). The same, if we will execute:
item3.Value = 5;
this item will be removed from collection. But monitoring is still working. So, if we will change value of item3 back to 30, it will come back to collection.

Yet one way of using Filter is to use another observable collection as source of this collection, like:
var sourceCollection = new ObservableCollection<testitem>();
var filteredCollection = new ObservableCollectionEx<testitem>();
filteredCollection.Source = sourceCollection;
filteredCollection.Filter = p => p.Value > 10;
From now, filteredCollection will contain only items from sourceCollection which passes filter criterion. And even if sourceCollection will change or items’ Values will change – filteredCollection will still contain all (and only) needed items. As source can be used any IEnumerable<T> collection. Sure, in this case ObservableCollectionEx cannot monitor changes of source collection, but if it’s static – it’s not a problem. For example, ObservableCollectionEx can be used as filter layer in FilteredTextBox.

Source files

February 9, 2012

Thoughts about software

Software should not be created in order to create software. Software should not be created just for business needs. Software should be for humans.

If your employees (or yourself) do not get satisfactions from using your application, or worse – they have difficulties with your application – they will not use it. They will try to find any way not to work with application. Maybe, your application could almost everything. But, if users don’t like it – they will do everything to make the program guilty. And they will be right.

How many times did you try to configure Remote Desktop for remote access? And how many times did you have questions or problems with TeamViewer? Could you configure VoIP? And how many times do you have questions or problems with Skype? Software like TeamViewer and Skype has their own secret – simplicity. User should not know any technical details. User should not be computer professional. User should be just user.

Perfect software: one screen, one button. “Do it”. And all. Options? Develop your application so that they will not be required. Error? Choose the way, when it could not appear. And if it has occurred – don’t say user, that he is stupid. Your application is stupid. It’s better to describe, what happens, what user should do to fix this situation, and how do not see this error in the future. Well commented code? If your code needs comments – rewrite your code.

This is idealism. This is mostly unreachable. But we should seek for it.

October 29, 2011

INotifyPropertyChanged and “magic-strings”


WPF contains great things like Bindings and interface INotifyPropertyChanged. This two things allow forget the hell from WinForms and almost all previous UI technologies, where you spend 90% of your time for developing and maintenancing UI code (like updates in UI fields, hide or show some controls etc.) If you still don’t know what is it (hmmm, you develop in WPF and don’t know what is “binding”?), you can read about them, for example, here. And I want talk about problem named “magic-strings”.

How it was

So, how usually properties’ implementations look when you use INotifyPropertyChanged? Something like this:

public class Author
  : INotifyPropertyChanged
{
  #region Properties

  #region FirstName

  private string _FirstName;

  public string FirstName
  {
    get { return _FirstName; }
    set
    {
      if (_FirstName == value)
        return;

      _FirstName = value;
      RaisePropertyChanged("FirstName");
    }
  }

  #endregion

  #endregion

  #region Implementation of INotifyPropertyChanged

  public event PropertyChangedEventHandler PropertyChanged;

  public void RaisePropertyChanged(string propertyName)
  {
    if (PropertyChanged != null)
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  }

  #endregion
}
The problem is in highlighted line – there is “magic-string” – “FirstName”. You can make typo error in this string or when you will change property’s name using refactoring – this string will not change. Also, there is yet one place, when this “magic-string” is used – PropertyChanged event’s handler. It looks like this:

public class AuthorViewModel
{
  public AuthorViewModel()
  {
    Author = new Author();
    Author.PropertyChanged += Author_PropertyChanged;
  }

  public Author Author { get; private set; }

  void Author_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  {
    switch (e.PropertyName)
    {
      case "FirstName":
        MessageBox.Show("Author.FirstName was changed");
        break;
    }
  }
}

The problem is the same – you can make typo error or you can change name of this property later. Also, I don’t like how “switch” code looks like. So, what to do?
The answer is – Lambdas!

How it is

I want to warn all optimizators and other perfectionists. Yes, I know that my code below works slower than “magic-strings”. But I prefer more robust code than faster. If your code is fast but it works wrong – do you really need it?
I saw a lot of articles where another guys describe the same decision with RaisePropertyChanged() method, and Prism also uses this approach. But I never saw before usage of the same principle in PropertyChanged event’s handler.
OK, sorry for a lot of words, here is how the same code looks after.

public class Author
  : NotificationObject
{
  #region Properties

  #region FirstName

  private string _FirstName;

  public string FirstName
  {
    get { return _FirstName; }
    set
    {
      if (_FirstName == value)
        return;

      _FirstName = value;
      RaisePropertyChanged(() => FirstName);
    }
  }

  #endregion

  #endregion
}
Here is actually not too much changes. We move implementation of INotifyPropertyChanged to base class NotificationObject and (attention!) remove “magic-string”. So, now we can refactor Author class and do anything you want with FirstName property’s name! And PropertyChanged event’s handler changes to:

void Author_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
  On.PropertyChanged(e, (Author a) => a.FirstName, () => MessageBox.Show("Author.FirstName was changed"));
}
It’s also now simple and robust.
All necessary code you can download by links in the end of this article.

Fast property implementation

It’s terrible to write every time the same code for implementation of properties. So, if you use ReSharper, you can download code template for it, and implement properties with just some keystrokes. All you should do:
  1. Type “pcp” (without quotes) + press Tab
  2. Type property’s type + press Tab
  3. Type property’s name + press Tab
It’s everything you should to do!
You can import that code template by VS main menu –> ReSharper –> Live Templates… –> Import…

Typical use cases

Raise property change notification after changes of another property

For example, class Author has properties FirstName, LastName and FullName, which is equal to FirstName + LastName. When FirstName or LastName is changed, of course you want to notify about FullName changes also. You can do this 2 ways:
1. Add additional code to FirstName’s and LastName’s setters:

public string FirstName
{
  get { return _FirstName; }
  set
  {
    if (_FirstName == value) return;

    _FirstName = value;
    RaisePropertyChanges(() => FirstName);
    RaisePropertyChanges(() => FullName);
  }
}
2. Add additional code to Author.PropertyChanges event’s handler:

public class Author
{
  public Author
  {
    PropertyChanged += Author_PropertyChanges;
  }

  void Author_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  {
    On.PropertyChanged(e, () => FirstName, () => RaisePropertyChanged(() => FullName));
    On.PropertyChanged(e, () => LastName, () => RaisePropertyChanged(() => FullName));
  }
}
Maybe there are some more code, but I think it’s more understandable. All additional notifications and actions on properties’ changes are now in separate place.

Additional action on property’s changes

For example, you want to get from database some additional information after setting ID.

public class Author
{
  public Author
  {
    PropertyChanged += Author_PropertyChanges;
  }

  void Author_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  {
    On.PropertyChanged(e, () => ID, UpdateData);
  }

  void UpdateData()
  {
    // Your code here
  }
}

Additional actions in parent class

public class AuthorViewModel
{
  public Author Author { get; private set; }

  public AuthorViewModel
  {
    Author.PropertyChanged += Author_PropertyChanged;
  }

  void Author_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  {
    On.PropertyChanged<Author>(e, a => a.ID, UpdateData);
    // Or you can use this syntax:
    // On.PropertyChanged(e, (Author a) => a.ID, UpdateData);
  }

  // This method invokes when Author.ID changes
  void UpdateData()
  {
    // Your code here
  }
}

Downloads