Sunday, January 31, 2010

Thoughts on OO design

OO design is a creative activity. To be successful at it, you’ll need a mixture of both talent and learned skills. There is no such thing as correct design. There is only good, bad and somewhere-in-between design. OO program is simply a collection of objects that send messages to each other. But how do you design these objects? What objects to create? Which objects should interact? In my work, I usually begin OO design by identifying abstractions. I look at the problem from higher level. I try to identify abstractions that model true nature of the problem. An common mistake is to completely model real-world entities. In software development we have possibility of using abstractions to make our job/life easier. You should use this to your advantage. I assign each object a single responsibility. Note that assigning responsibilities is not easy at all. If you are good at assigning responsibilities then chances are, that you’re are good at OO designs too.

Understanding responsibilities is key to good object-oriented design”—Martin Fowler

Once you’re done with the design, how do you know if it’s good or bad? There are two metrics/principles that you can evaluate your designs against. These are coupling and cohesion. These principles are very powerful in assisting you to make your OO designs good. Funnily, the principles don’t even come from OO, but from Structured design. Coupling is simply a degree to which each object is dependent on each one of the other objects (Wikipedia definition). Coupling quantifies the dependencies among objects. It can be loose or tight. Loose coupling is desired, since it’s more change-friendly. Fewer dependencies make introducing changes easier. Cohesion tells us how focused a class is in fulfilling its responsibility. Does a class have well defined responsibility? Are all parts of class (methods etc) focused on fulfilling that, and only that responsibility? High-cohesion is desired, since it promotes robustness, reusability, and understandability.

To sum up, here are the key concepts I use in my day-to-day OO design:

  • Abstractions
  • Responsibilities
  • Loose-coupling
  • High-cohesion

Following books helped me to gain more understanding of OO design:

Final advice: The best way to become better at OO design is to do it a lot. A huge amount of it.   

Saturday, January 30, 2010

DRY – The first step

It has been said that a thousand miles journey starts with a single step. So in case journey = becoming a better developer, then first step = DRY. DRY stands for Don’t Repeat Yourself. I see way too much copy-paste development around. This needs to stop. If you constantly copy-paste code around, you’re limiting yourself of becoming a better developer. Code duplication is evil, and it’s simply against common sense. So why do we do it? We do it probably because it’s easier, it doesn’t require us to think. When we stop thinking, our journey of becoming a better developer ends. Anyway, does anybody know how do you disable CTRL+C in VS :)? 

Wednesday, January 27, 2010

Is anybody interested in open-source tool like Silverlight Spy?

A few months ago I started to work on a Silverlight project. It was a business-line application, pretty much standard of that type. It was my first project in Silverlight. Anyway, soon I found this great tool called Silverlight Spy. It’s a tool for runtime inspection of Silverlight applications, something like Firebug but only for Silverlight. I was impressed with the software since it was very powerful. The tool costs around 99 EUR, which is not much for the value you get. However, as a passionate developer, I like to develop my own tools (during my free time of course:)). It was then the idea of similar open-source tool was born. I decided to develop a proof-of-concept application called Silverlight Buddy(now thinking of renaming it to Silverlight Inspector). Here’s the what I have done by now:

  • Intercepting Silverlight application before it’s displayed in browser
  • Injection of custom code logic into an Silverlight application  
  • Solved communication between Silverlight application and Silverlight Buddy tool. (both using COM and JavaScript)
  • Support for hosting DLR inside Silverlight applications (Just like in Silverlight Spy)
  • Very basic “Inspect Element”-like functionality

The app is not stable since it is intended only to be a proof-of-concept, written in a quick-and-dirty way. But as you can see, all the major issues/parts have been solved. There is still a huge amount of work to be done in order to make it a usable application. Now,I have never run open-source project, and to be honest, I don’t have time to. That’s why I thought there was somebody out there willing to continue to work on the project. I will probably be a committer myself, but don’t have time to devote fully to the project. If somebody is interested please write a comment.

Here are some screenshots of the app so far.

image

Notice the red border around “Search” textbox, it represents “Inspect Element” functionality as you move your mouse over the elements.

I hope someone will be willing to continue the work on this project so the effort I have made so far will not be wasted.

Saturday, August 15, 2009

ASP.NET : Using ClientID in external JavaScript files

In the past year I have been writing a lots of JavaScript code. Mostly because I have been working on same ASP.NET WebForms application for more then 10 months by now. We use JavaScript for things like validation, async HTTP requests etc. To keep our code clean,we try to keep all of our JavaScript in external files. The problem with external JavaScript files is that you cannot use server tags in them, so you cannot obtain ClientID of ASP.NET controls by using <%= control.ClientID %> syntax. My first workaround for this problem was to add JavaScript variables manually on every page by using ClientScript.RegisterClientScriptBlock() method. Every page needed to have a collection of controls which was called JSControls. In the Page_Load event of the page I would add to JSControl collection all controls for which I need access from JavaScript. Code would look like this:

private List<Control> JSControls = new List<Control>();
protected void Page_Load(object sender, EventArgs e)
{
     JSControls.Add(txtName);
     JSControls.Add(txtLastname);
}

Then I had a function that would generate JavaScript code for each control in JSControl collection:

public static string GetClientScriptBlock()
{
StringBuilder sb = new StringBuilder();
sb.Append("<script type=\"text/javascript\">");
foreach (Control c in JSControls)
{
    if (c != null)
    {
        sb.Append(string.Format("var {0} = '{1}';", c.ID + "ClientID", c.ClientID));
    }
}
sb.Append("</script>");
return sb.ToString();
}

Then, again in the Page_Load event I needed to call RegisterClientScriptBlock with JavaScript code block:

protected void Page_Load(object sender, EventArgs e)
{
JSControls.Add(txtName);
JSControls.Add(txtLastname);                         
Page.ClientScript.RegisterStartupScript(this.GetType(), "myClientBlock", GetClientScriptBlock(), false);
}

This works fine, so if I need to access for example ClientID of TextBox control called txtName I would just refer to txtNameClientID variable, like this:

document.getElementById(txtNameClientID); 

While this approach is fine it has some disadvantages. One problem is that there could be controls with same ID but in different parent controls. Another disadvantage is that this approach is not very reusable, it would force me to violate DRY principle, since the same code had to be added to every page.

A better solution

There is a better solution. A reusable custom control can be created to automate JavaScript variable creation for us. The idea is to put the control on a Page, define for which controls from the page you need ClientID’s and the control would do the rest of the work. The custom control would also have some kind of Namespace property, that would solve the problem with same ID on different controls. Design-Time support would also be nice to have here, since we would not need to write control id's manually. Let’s make our idea to realization!

Control syntax

Goal is to have control with syntax as simple as possible. Suppose that we call our control JSClientIDList, we want to have syntax like this:

<ReducingComplexity:JSClientIDList runat="server" ID="JSClientIDList1" Namespace="namespace1">
<JSControls>
    <ReducingComplexity:ControlItem ControlID="btnOK" />
</JSControls>
</ReducingComplexity:JSClientIDList>
We see that this syntax is very straightforward. JSClientIDList control has two important properties. The first one is Namespace property, which defines some kind of prefix for each of the controls. The second important property is JSControls which defines list of ASP.NET controls for which JavaScript variables will be created. We will need another class for representing a control in our JSControls list. We will call that class ControlItem. The whole code would look like this:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.ComponentModel;
using System.Diagnostics;
using System.Web.UI.WebControls;
using System.Text;

namespace ReducingComplexity.Web.Controls
{
[PersistChildren(false)]
[ParseChildren(true)]
public class JSClientIDList : Control
{
    private string m_Namespace;
    [Browsable(true)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public string Namespace
    {
        get
        {
            return m_Namespace;
        }
        set
        {
            m_Namespace = value;
        }
    }

    [Browsable(true)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public List<ControlItem> JSControls { get; set; }

    public JSClientIDList()
    {
        JSControls = new List<ControlItem>();
    }
    protected override void OnPreRender(EventArgs e)
    {
        Page.ClientScript.RegisterClientScriptBlock(this.Parent.GetType(),this.ClientID, GetClientScriptBlock(), true);
        base.OnPreRender(e);
    }
    protected override void AddParsedSubObject(object obj)
    {
        if (obj is ControlItem)
        {
            this.JSControls.Add((ControlItem)obj);
            return;
        }
    }
    private string GetClientScriptBlock()
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendFormat("var {0}=new Object();", this.Namespace);
        foreach (ControlItem ci in this.JSControls)
        {
            Control c = this.FindControl(ci.ControlID);
            string clientId = c != null ? c.ClientID : "";
            sb.AppendFormat("{0}.{1}='{2}';", this.Namespace, ci.ControlID, clientId);
        }
        Debug.WriteLine(sb.ToString());
        return sb.ToString();
    }
}
public class ControlItem
{
    [TypeConverter(typeof(ControlIDConverter))]
    public string ControlID { get; set; }
}
}

I’ll try to explain most important parts of the code. To enable clean syntax we needed to use PersistChildren and ParseChildren attributes. These attributes define how nested content of the control will be interpreted. More details can be found here. Next we also needed to override AddParsedSubObject method, which will add nested controls to JSControls collection.
protected override void AddParsedSubObject(object obj)
{
if (obj is ControlItem)
{
      this.JSControls.Add((ControlItem)obj);
      return;
}
}
Another interesting part is usage of ControlIDConverter as TypeConverter. This will enable us to use Design-Time support for ControlID property, or to be more precise it will provide dropdown list of all controls available for addition, so that we can choose from the list.

image

When I first started implementing this functionality I didn’t really know about ControlIDConverter class. My plan was to write my own Type Converter which would provide such functionality, but I failed. To cut the long story short,the reason I failed was I didn’t know I could use GetService() function of ITypeDescriptorContext interface to get the instance of IDesignerHost. Anyway, Reflector reveals all the secrets:

image

We can also see that the actual registration of JavaScript code block is done in OnPreRender event since in this event all controls in Control collection are available.

For example if we define Namespace property to “Namespace1” and have a ASP.NET button control with ID "btnOK" then to get ClientID of the btnOK button from external JavaScript file we would use following syntax:

document.getElementById(Namespace1.btnOK);

And of course JSClientIDList control would have to be declared like this:

<ReducingComplexity:JSClientIDList runat="server" ID="JSClientIDList1" Namespace="namespace1">
      <JSControls>
          <ReducingComplexity:ControlItem ControlID="btnOK" />
      </JSControls>
</ReducingComplexity:JSClientIDList>

By using this simple control our goal of using external files for JavaScript code has been achieved.

Friday, August 14, 2009

Good design comes over time

image Have you ever tried to provide design for Mark IV coffee maker problem that Robert C. Martin presented in his "Designing Object Oriented C++ Applications using the Booch Method" book. Well I have and I was not successful to come up with any kind of elegant solution. But I was quite impressed with the solution that Uncle Bob presented for this design problem. You can find the solution here.

What I find interesting in the above document is section titled "How did I really come up with this design?". In that section Uncle Bob says:

I did not just sit down one day and develop this design in a nice straightfoward manner. Indeed, my very first design for the coffee maker looked much more like Figure 11–1. However,I have written about this problem many times, and have used it as an exercise while teaching class after class. So this design has been refined over time.

This paragraph was very encouraging to me, because as you see even Robert C. Martin himself did not get it right the first time. We are not going to get it right the first time, no matter how much we try. Good designs come over time, they are not obvious immediately. Our initial design will change over time, and we need to ensure that changes that happen will lead to better design. I see great power in refactoring here. By doing refactoring we change design of our software to something better and still preserve clean and maintainable code. Unit testing and TDD in general is of great help here as well. It is the immediate feedback that makes us less fearful of making changes to our code.

Tuesday, August 11, 2009

Pseudocode Programming Process

Today I had to implement a feature on project I'm working on. The feature was not a trivial one. It was rather complex. I had a general idea how it could be implemented. The implementation I was having in mind involved several complex data structures and some recursion function calls as well. After short thinking about the problem I started to do the actual implementation in C#. To be more precise I tried several implementations, but I kept failing, and found myself rewriting the code over and over. I was completely lost in complexity of the problem and all the details (complex data structures and recursion calls). I kept doing so for about 1.5 hours (maybe two), and I still had no working solution. Then I realized that I have to change my problem solving approach.

If you have read "Code complete" book you probably remember Pseudocode Programming Process (PPP) that Steve described in the book. Pseudocode Programming Process is a way of designing algorithms in pseudo code. Basically, this means that an algorithm is described in high-level English-like way. You can find more about the PPP on following links:

Anyway, after writing down the algorithm in high-level English and then making every line of comment into a line or fewof code (applying PPP practices), I had working solution in about 20 minutes. YES, 20 minutes including pseudocode and C# implementation. How that compares to 1.5 hours spent for literally nothing? I can only but recommend PPP as way of handling complex algorithms.

Thursday, August 6, 2009

Bugs and bytes

Yesterday, at work we had a bug in application we’re developing. Nothing critical but rather inconvenient. Our application has a very common functionality of file download. Some users can upload files to the system and some other users can download those files. Pretty straightforward functionality, right? It’s an ASP.NET application so it should be very easy to implement this. Here's the code (not the real code but the relevant part of it):

 protected void ViewFile(int fileId)
       {
           byte[] fileData = DAL.GetFile(fileId);
           Response.ContentType = "application/octet-stream";
           Response.AddHeader("Content-Disposition", "attachment; filename=test.txt");
           Response.OutputStream.Write(fileData, 0, fileData.Length);
           Response.End();
       } 

So what could possibly go wrong here? Not much? Except…, files can be EMPTY too! An empty file is a file that is 0 bytes in size. So if you try to write an empty file with the code above an exception would be throw at Response.OutputStream.Write() line. How do we write an empty file to HttpResponse then? Very simple: you DONT write anything. Just skip Response.OutputStream.Write line and call the Response.End(). Anyway, what am I trying to prove here with this simple example? My point is that we need to be more thoughtful when writing code. We must carefully think about code we write. What could go wrong? How is API we’re using behaving? What exceptions can be thrown? What assumptions are being made? One approach I find useful with dealing this kind of issues is Defensive programming. Here’s quote from wiki about Defensive programming:

A difference between defensive programming and normal practices is that few assumptions are made by the programmer, who attempts to handle all possible error states. In short, the programmer never assumes a particular function call or library will work as advertised, and so handles it in the code.

If the code had been written in Defensive programming mind-set this bug would have never been made. I must also note that Defensive programming is not only choice for solving these kind of issues. Another approach is Design by contract. In the end it really does not matter which approach you choose, the goal is to create more robust and quality software.

Wednesday, August 5, 2009

Strings and performance

I really cannot stress enough importance of using StringBuilder class for string concatenation. The reason is obvious: PERFORMANCE! I know that most programmers are aware of the possible string concatenations performance problems but somehow this issue is still being overlooked and many make the mistake.The difference in performance between using and not using StringBuilder is HUGE as I'll show in simple demo application. I'm not saying that you should use StringBuilder for every string concatenation, but be very alert when you have loops that do string concatenation. Stop for a minute and think about possible performance issues. How much iteration do you expect your loop to have? If you loop through collection that you expect to get bigger and bigger over time, or a collection that you know nothing about (such as one coming from external system) then using StringBuilder has no alternative. Let's take a look how long does it take to do for example 30 000 string concatenations. by using StringBuilder. Code looks as follows :

class Program
  { 
      static void Main(string[] args) 
      { 
          StringBuilder sb = new StringBuilder(); 
          DateTime start = DateTime.Now;  
          for (int i = 0; i <30000; i++) 
          { 
              sb.Append("some string"); 
          } 
          TimeSpan ts = DateTime.Now - start;  
          Console.WriteLine("Finished!"); 
          Console.WriteLine(ts.TotalSeconds); 
          Console.ReadLine(); 
      } 
}

On my computer it takes exactly 0.15625 seconds which is very very fast.

image

Now let's take a look at same code but without using StringBuilder

 class Program
  { 
      static void Main(string[] args) 
      { 
          string str= string.Empty; 
          DateTime start = DateTime.Now;  
          for (int i = 0; i <30000; i++) 
          { 
              str+= "some string"; 
          } 
          TimeSpan ts = DateTime.Now - start;  
          Console.WriteLine("Finished!"); 
          Console.WriteLine(ts.TotalSeconds); 
          Console.ReadLine(); 
      } 
  }

Without StringBuilder it takes 21.396625 seconds.

image

Difference in performance is very noticeable and benefit of using StringBuilder is obvious. It's worth noting that performance issues are usually not discovered until system has been deployed and used in production, but by that time damage can already be done. That's why string concatenation must be taken with care.

Monday, August 3, 2009

Tip on employing the domain model pattern

Today, I read Udi's latest article about domain model pattern published in MSDN magazine. I want to comment on following part of the article:

When designing a domain model, spend more time looking at the specifics found in various use cases rather than jumping directly into modeling entity relationships—especially be careful of setting up these relationships for the purposes of showing the user data. That is better served with simple and straightforward database querying, with possibly a thin layer of facade on top of it for some database-provider independence.

Udi couldn't be more right here. I had similar dilemma few weeks ago,and my reasoning happened to be same as Udi's. I had an association that seemed so natural and correct. Association was as follows:

image

But after looking deeper at problem I was trying to solve it turned out that this association was only needed for presentation purposes. So I dropped the association altogether from the domain model. Presentation issue was solved by using a simple query (using NHibernate). This shows that domain model should be used for capturing core business behavior only. In the article Udi also wrote about Domain Events pattern that can help solve complex problems quite elegantly. I strongly recommend reading this article.

Thursday, July 16, 2009

Fluent ADO.NET. An attempt!

Yes, I know there is NHibernate and other decent ORM solutions but, there are times when using an ORM is not an option. Still the ADO.NET API is VERY boring thing to use. Yes, you can make some SqlHelper classes that can help you but still many of the SqlHelpers lack the needed flexibility. So, I’ll try to make a Fluent wrapper for ADO.NET API.

What are we trying to achieve?

Our goal is to make usage of ADO.NET API more friendly and easier. So let’s start with some examples of our desired code usage.

Wouldn’t it be nice if we could use SqlCommand object in following way?

SqlCommand command = new SqlCommand();
SqlConnection connection = new SqlConnection();
command.UsingConnection(connection)
.ExecuteQuery("SELECT * FROM USERS WHERE ID=@id")
.AddParameter("@id",    1)
.AsDataTable();
or..
SqlCommand command = new SqlCommand();
SqlConnection connection = new SqlConnection();
command.UsingConnection(connection)
.ExecuteQuery("UPDATE Users SET Active=1 WHERE ID=@id")
.AddParameter("@id",1)
.ExecuteNonQuery();

Note: Before we go further with this fluent ADO.NET I need to say that there is already an open source project related to ADO.NET fluent interface. As a matter of fact I had a short discussion with coordinator of the project about some design aspects of the API. The design differences between the two approaches are related to supporting different database engines. You can read about this short discussion here.

How to achieve our goal?

To achieve usage of SqlCommand as shown above, we can go and write wrapper around SqlCommand object and that would definitely work. But what if I need to use Access as data source? In that case I wouldn’t be using the SqlCommand class, I would be using OleDbCommand instead. To avoid writing wrapper around all possible (Oracle,Postgre,MySql) databases, we can instead write wrapper around IDbCommand interface which all of the specific commands implement. This approach is rather good, but has some major drawback. Problem is that we would not be able to take advantage of specific features that are available on some of the IDbCommand implementations. One such property on SqlCommand object is "Notification", which is not present in IDbCommand interface.

To get around this problem we could make use of extension methods, which could be written in fluent approach and thus enable that specific features of IDbCommand implementations can be configured fluently. So let’s start the implementation.

The implementation

Please note that I will not be implementing whole API, but I’ll only show the concept.

Our wrapper class will be called FluentDbCommand, it is generic class that receives a IDbCommand as the type parameter. Here’s the code listing:

public class FluentDbCommand<T>
where T : IDbCommand
{
    T command = default(T);
    Func<T> commandConstructor = null;

    public T InnerCommand
    {
        get
        {
            return command;
        }
    }
    public FluentDbCommand()
    {
        // In case we don't provide any factory for creating command objects
        // try to build the object using reflection
        command = Activator.CreateInstance<T>();
    }
    public FluentDbCommand(Func<T> commandConstructor)
    {
        this.commandConstructor = commandConstructor;
        command = this.commandConstructor.Invoke();
    }

    public FluentDbCommand<T> AddParameter(string name, object value)
    {
        IDbDataParameter parameter = command.CreateParameter();
        parameter.ParameterName = name;
        parameter.Value = value;
        command.Parameters.Add(parameter);
        return this;
    }
    public FluentDbCommand<T> QueryToRun(string query)
    {
        command.CommandType = CommandType.Text;
        command.CommandText = query;
        return this;
    }
    public DataTable AsDatatable()
    {
        DataTable tblResults = new DataTable();
        tblResults.Load(command.ExecuteReader());
        return tblResults;
    }
    public FluentDbCommand<T> UseConnection(IDbConnection connection)
    {
        this.command.Connection = connection;
        return this;
    }
    public int ExecuteNonQuery()
    {
        return this.InnerCommand.ExecuteNonQuery();
    }
    public object ExecuteScalar()
    {
        return this.InnerCommand.ExecuteScalar();
    }
    public IDataReader ExecuteReader()
    {
        return this.InnerCommand.ExecuteReader();
    }
    public FluentDbCommand<T> RunStoredProcedure(string strProcedureName)
    {
        this.InnerCommand.CommandType = CommandType.StoredProcedure;
        this.InnerCommand.CommandText = strProcedureName;
        return this;
    }
}

And usage of the FluentDbCommand would be:

FluentDbCommand<OleDbCommand> oleCmd = new FluentDbCommand<OleDbCommand>();

DataTable result = oleCmd.UseConnection(connection)
                 .QueryToRun("SELECT * FROM USERS")
                 .AddParameter("@id", 1)
                 .AsDatatable();

We see that it’s rather simple to implement the fluent interface around IDbCommand, but as I said earlier, to support the specific features of concrete database engine we need to use extension methods.

For example to support NotificationAutoEnlist property of SqlCommand class we would need to write following extension method

public static class SQLServerSpecificExtensions
{
public static FluentDbCommand<SqlCommand> NotificationAutoEnlist(this FluentDbCommand<SqlCommand> command, bool value)
{
command.InnerCommand.NotificationAutoEnlist = true;
return command;
}
}

And that would enable us to use the FluentDbCommand in following way:

FluentDbCommand<SqlCommand> sqlCmd = new FluentDbCommand<SqlCommand>();

sqlCmd.UseConnection(connection)
            .QueryToRun("SELECT * FROM USERS WHERE ID=@id")
            .AddParameter("@id", 1)
            .NotificationAutoEnlist(true)
            .AsDatatable();

That would be it. This implementations is just a beginning of the full-blown wrapper, but it’s shows the concept very well.