Archive

Archive for 2009

Favoured podcasts

November 1st, 2009 2 comments

I migrated my desktop PC to Windows 7 Professional. I didn’t choose the upgrade path, I install Windows 7 from scratch. So, after installing iTunes I had to register my favoured podcasts again:

.Net

Java

Currently I work in a .Net environment, so the list of .Net podcasts is a bit longer. If you are interested in more Java podcasts, there is a question about that topic on Stackoverflow. The following three podcasts are definitely interesting:

Do you have other interesting podcasts?

Share
Categories: .NET, Java, Private, Software engineering Tags:

Kaizen and Software Engineering

October 27th, 2009 5 comments

Kaizen is a very interesting approach, also in the software industry. On Wikipedia you’ll find the following description of Kaizen:

Kaizen is a Japanese word adopted into English referring to a philosophy or practices focusing on continuous improvement in manufacturing activities, business activities in general, and even life in general, depending on interpretation and usage.

Kaizen aims to eliminate waste, to improve the process or activities (techniques). I heard about Kaizen the first time at an economic course, and I found the Muda (seven wastes) very interesting. I tried to find locations or situations where you can detect such wastes:

Overproduction

Overproduction happens each time you engage more resources than needed to deliver to your customer. For instance, large batch production, because of long change over time, exceeds the strict quantity ordered by the customer. For productivity improvement, operators are required to produce more than the customer needs. Extra parts will be stored and not sold. Overproduction is the worst Muda because it hides or generates all others, especially inventory. Overproduction increases the amount of space needed for storing raw material as well as finished goods. It also requires a preservation system.

Overproduction happens then, when you don’t follow the YAGNI principle. Don’t try to make assumptions about the future or the customers. Just produce the things as simple as possible.

Unnecessary transportation

Each time a product is moved it stands the risk of being damaged, lost, delayed, etc. as well as being a cost for no added value. Transportation does not make any transformation to the product that the consumer is supposed to pay for.

Don’t build chatty interfaces, make use of the coarse-grained interface pattern.

Inventory

Inventory, be it in the form of raw materials, work-in-progress (WIP), or finished goods, represents a capital outlay that has not yet produced an income either by the producer or for the consumer. Any of these three items not being actively processed to add value is waste.

Deliver early and often. Don’t wait at the end of the project to deliver the whole software at once. If you deliver your software earlier and often, you will receive more feedback and so the costs to fix bugs will reduce. Also the customer will be happier because he could use much earlier important parts of your software and he could observe better the progress of the software project.

Motion

As compared to Transportation, Motion refers to the producer or worker or equipment. This has significance to damage, wear, safety. It also includes the fixed assets, and expenses incurred in the production process.

Good hardware and a good chair is very important to be productive. Slow computers are just horrible to work with and a bad or cheap chair is not very good for the health of the software engineers. If the software engineer feels comfortable, he’s much more able to be concentrated or work longer.

Defects

Whenever defects occur, extra costs are incurred reworking the part, rescheduling production, etc.

Reduce Defects. Unfortunately there doesn’t exists a programming language where it isn’t possible to make no errors. So testing is absolutely necessary. Do unit testing with or without TDD, but a modern software project should have tests (unit tests, integration tests or acceptance tests, etc.). Without tests you don’t have a safety net (regression tests) if you change something in your project. One of the worst error a customer could find is one which he already found and was fixed.

Over-Processing

Over-processing occurs any time more work is done on a piece than what is required by the customer. This also includes using tools that are more precise, complex, or expensive than absolutely required.

Overengineering and Patternoholics are a danger to do more than it is necessary.

Waiting

Whenever goods are not in transport or being processed, they are waiting. In traditional processes, a large part of an individual product’s life is spent waiting to be worked on.

Slow computers where you have to wait for a build step or just for the compiler are an unnecessary waste. Also if you wait for an answer from your customer or an other teammate. Don’t wait, make a follow-up, make a phone call or write an other mail. Also don’t let the others wait. If you receive an email, answer it or write a short email to say that you will response later, so that the other knows, that you saw his mail and you planed to response him.

Conclusion

One important question is: “Do you add value to your solution, product or service which the customer is ready to pay for?” This question or view isn’t new. It focus all the actions in software engineering on business value, the value which the customer pays for. To reduce the waste you have to improve (Kaizen) your code, methods and communication with others. The seven wastes are a help to find locations or situations where waste could happen.

Share

Fail Fast principle

September 22nd, 2009 5 comments

Recently I received a NullReference-Exception when I called another method from a foreign component. Fortunately I had the source code of this component and I found the following code:

public class AgeValidator
{
	public Dictionary<string, int> Config { get; set; }

	public bool Validate(int nAge)
	{
		int nMinAge = FindValue("MinAge");

		return nAge > nMinAge;
	}

	private int FindValue(string strParameter)
	{
		int nValue = -1;

		if(this.Config.ContainsKey(strParameter)) nValue = this.Config[strParameter];

		return nValue;
	}
}

There are several problems with this code. First, there are no preconditions which checks if the object is in a valid state or the parameters are valid. This cause to behaviour which isn’t intended. The problem with a not available configuration could already detect in the Validate method. The Find method could check if the parameter is valid, if not it doesn’t make any sense to continue. The FindValue method doesn’t follow the Fail Fast principle. Jim Shore wrote an excellent article about the Fail Fast principle. After a refactoring the code looks like that:

public class AgeValidator
{
	public Dictionary<string, int> Config { get; set; }

	public bool Validate(int nAge)
	{
		if(this.Config == null || this.Config.Keys.Count == 0) throw new InvalidOperationException("No valid configuration available.");
			
		int nMinAge = FindValue("MinAge");

		return nAge > nMinAge;
	}

	private int FindValue(string strParameter)
	{
		if(string.IsNullOrEmpty(strParameter)) throw new ArgumentNullException("strParameter");
		if(!this.Config.ContainsKey(strParameter)) throw new ArgumentException(string.Format("No configuration for the key '{0}' found.", strParameter));

		return this.Config[strParameter];
	}
}

What I advise junior software developers is to follow the Fail Fast principle, disciplined use of preconditions and if it adequate the use of postconditions. But all these advices don’t replacing unit testing.

Share

To be a model

September 9th, 2009 No comments

Currently I study for a MAS-IT. Last semester came a photograph and asked if he could take some pictures. So I thought nothing of it and I continued discussing with my colleagues about a topic.
Today I saw on the page of the Berner Fachhochschule, Technik und Informatik two images with me:
bfh1
And here:
bfh2
That’s funny. Normally I describe models, but now I’m the model.

Share
Categories: Private, Software engineering Tags:

NHibernate mapping possibilities

August 24th, 2009 1 comment

I prepare currently a new talk about NHibernate. In this talk I’ll show the different mapping possibilities with NHibernate. To demonstrate the three possibilities (and a fourth one) I chose a quite simple model: I implement three classes Cat, Dog and Bird an for each one I chose a different mapping approach. For my little sample application I used NHibernate 2.1.0, NHibernate.Mapping.Attributes 2.1.0 and Fluent NHibernate 1.0.0.545.

NHibernate classic style – XML-Files
For the class Cat I chose to use the classic way to map the class.

public class Cat
{
	private string id;
	private string name;
	private char sex;
	private float weight;
		
	public virtual string Id
	{
		get { return id; }
		set { id = value; }
	}

	public virtual string Name
	{
		get { return name; }
		set { name = value; }
	}

	public virtual char Sex
	{
		get { return sex; }
		set { sex = value; }
	}

	public virtual float Weight
	{
		get { return weight; }
		set { weight = value; }
	}
}

And the XML-File Cat.hbm.xml looks like:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernateTest.Domain" assembly="NHibernateTest">
	<class name="Cat" table="Cat">
		<id name="Id">
			<column name="CatId" sql-type="char(32)"/>
			<generator class="uuid.hex" />
		</id>
		<property name="Name">
			<column name="Name" length="16" not-null="true" />
		</property>
		<property name="Sex" />
		<property name="Weight" />
	</class>
</hibernate-mapping>

This approach has different problems: You couldn’t easily refactor your POCOs, you have to adapt the hbm.xml files too. Another problem is that you write your entity twice: in C# and in XML, so there is a lot of noise. This is the entry point for code generation, but it isn’t a real solution, it is just a workaround to handle the necessary XML file correctly and to reduce errors by manual writing.

NHibernate.Mapping.Attributes
For the class Dog I prefer the attributes to map the class. In JPA it is my preferred way for simple and small projects, so I was very interested how they ported this approach to .Net.

[Class(NameType = typeof(Dog))]
public class Dog
{
	private Guid id;
	private string strName;

	[Id(0,
	Column = "ID",
	Name = "ID",
	TypeType = typeof(Guid),
	UnsavedValue = "(00000000-0000-0000-0000-000000000000)"
	)]
	[Generator(1, Class = "guid.comb")]
	public virtual Guid ID
	{
		get { return id; }
		private set { id = value; }
	}

	[Property(0, Column = "Name",
	Name = "Name",
	TypeType = typeof(string),
	Length = 50,
	NotNull = false
	)]
	public virtual string Name
	{
		get { return strName; }
		set { strName = value; }
	}
}

Without the property NameType in the Class-attribute the sample doesn’t run. Another thing which disturbs me is the noise in the code by the attributes. It isn’t a clean POCO approach, also because the needed specific using statements.

Fluent NHibernate
Finally there is a third approach how to map a class. To demonstrate this possibility a use the Bird class:

public class Bird
{
	private Guid id;
	private string strName;

	public virtual Guid ID
	{
		get { return id; }
		private set { id = value; }
	}

	public virtual string Name
	{
		get { return strName; }
		set { strName = value; }
	}
}

For the mapping you have to use an additional class, which looks like that:

public class BirdMapping : ClassMap<Bird>
{
	public BirdMapping()
	{
		Id(x => x.ID);
		Map(x => x.Name);
	}
}

This approach is really interesting. You separate your mapping from your entity, but the mapping is written in the same language like the entity itself. So you can profit of the refactor features of your IDE or of an additional tool like Resharper.

Use all together in the same project
I tried to run all the three possibilities in one project and I used a factory method to create the NHibernate configuration instance. This method looks like that:

public static Configuration CreateConfiguration()
{
	Configuration config = new Configuration();
	config.Configure();
			
	// XML-Files
	config.AddAssembly(Assembly.GetExecutingAssembly());

	// Attributes
	HbmSerializer.Default.Validate = true; // Enable validation (optional)
	HbmSerializer.Default.Serialize(System.Environment.CurrentDirectory, Assembly.GetExecutingAssembly());
	config.AddDirectory(new DirectoryInfo(System.Environment.CurrentDirectory));
			
	// Fluent
	config = Fluently.Configure(config).Mappings(m => m.FluentMappings.AddFromAssemblyOf<Program>()).BuildConfiguration();

	return config;
}

In a real world application I wouldn’t recommend a such configuration. An architect should decide which mapping approach is adequate. But this code snippet demonstrate that it is possible to use all three mapping possibilities at the same time.

The secret fourth possibility
When I study NHibernate Fluent I saw on their wiki the possibility to use their Auto Mapping.

private static Configuration CreateAutoConfiguration()
{
	var config = Fluently.Configure()
				.Database(MsSqlConfiguration.MsSql2005.ConnectionString(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Test.mdf;Integrated Security=True;User Instance=True")
							.ShowSql())
				.Mappings(m => m.AutoMappings.Add(AutoMap
								.Assembly(Assembly.GetExecutingAssembly())
								.Where(t => t.Namespace == "NHibernateTest.Domain")))
				.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(false, true))
				.BuildConfiguration();

	return config;
}

The other three possibilities are a little bit different: there you define the mapping for each entity, which is more or less the opposite. You map by default all your entities by convention over configuration and only the exceptions have to be declared explicitly. Note that it is even possible to generate the DB schema with the ExposeConfiguration method.
For greenfield applications or prototypes it is a great way to map your entities, but for brownfield applications it is much harder to use. This is the case when you couldn’t discover a pattern in the database or in the entities, which you want to map. In a such scenario I would prefer one of the three other possibilities.

Share
Categories: .NET, Software engineering Tags: