Archive for November 2007

Free Podcasts Available for OOPSLA 2007’s Keynotes and Tutorials

Here’s a treat for your ears. If you are like me and didn’t attend this year’s OOPSLA conference (doh!), you might be glad to know that you can download the keynotes and tutorials for free!  As stated in the OOPSLA website:

The different episodes include coverage of notable tutorials, workshops, technical papers, essays, invited talks, and Onward! Events.

Co-produced by the excellent Software Engineering Radio, you can download and listen to the following keynotes in your car, at the gym, in the bus to work/school, etc.

  • Peter Turchi on The Challenge of Telling the Next Story
  • Kathy Sierra on Creating Passionate Users
  • Frederick Brooks on Collaboration and Telecollaboration in Design
  • David L. Parnas on Software Documentation: Making Object Orientation Work Better (my favorite in the list)
  • And others…

There are also a bunch of episodes that were recorded prior to the conference in which the following speakers were interviewed for their tutorials:

  • No Silver Bullet with guests Dennis Mancl, Steven Fraser, and Bill Opdyke
  • The Ruby programming language with guest Glenn Vanderburg
  • Software Architecture with guest Michael Stal
  • Agile Unlimited with guest Jens Coldewey
  • Security with guest Gary McGraw
  • Domain-Specific Modeling with guest Juha Pekka-Tolvanen
  • Agile Software Development on a Global Scale with guest Jutta Eckstein
  • Unit Testing Patterns with guest Gerard Meszaros (my favorite in the list)
  • And others…

And while I’m on it, if you’re looking for a place to listen and learn more about software engineering subjects online, I urge you to go to the Software Engineering Radio’s website. It has, IMHO, the best podcasts on software development today.

Explicit Interface Members Implementation in C#

A popular and accepted definition of craftsmanship is

Aptitude, skill, or manual dexterity in the use of tools or material. Taking time to make sure a project is done well.

The tools or materials involved in software development are whatever you need and use to build your software product well. These might include various things such as IDEs, compilers, CASE tools, frameworks, APIs, external tools, and much more. These kinds of tools and materials are things that you can buy or try, and then use them as a black box, without much knowledge on their intrinsic mechanics. You know what they’re supposed to do, so you use them in a specific context.

The aptitude, skill, or manual dexterity part of the definition is more subtle, as it can mean a lot of things to a lot of people, depending on the experience and knowledge of each individual. I believe that your attitude towards your work is also a key factor that can make a difference between delivering a good software and a better software. That attitude is what separates artists and craftsmen from the rest of the crowd. I also believe that anyone that has a will to improve their aptitude, skill, or manual dexterity to develop and deliver better software can make that leap.

This post has to do with going the extra mile (or the extra bit I should probably say…) when designing your software. I’ll take a few minutes to write about explicit interface members implementation, a useful pattern when you need for your design to respect the framework’s rules (as well as the compiler’s), without shifting your design to fully submit to those rules, which might force you to please the frameworks and tools instead of pleasing the actual user of your class in terms of code readability and testability. As you probably know, even in software development, some rules can be broken and some others can be bent. In this case, we’re going to bend them…else good luck arguing with the compiler!

According to Brad Abrams’s book “Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries“,

Explicit member implementation allows an interface member to be implemented such that it is only available when cast to the interface type.

In other words, there are two ways to access an interface member that is being explicitly implemented

  • Access it by explicitly casting the object into the appropriate type holding that member.
  • Access it from inside the type declaring that member.

It is a very useful concept to know and to use because it helps to design according to the specification of the type and not according to the specification of a framework or an API.

For example, suppose we have a generic collection of books, such as described in Listing 1. This class offers the possibility to add a bunch of books in a book collection, but for some reason we don’t want the user of this class to just add a single book or “multiple single books”. If a single book needs to be added, we just create a list of one book and pass that list the Add method of the class.  Hey, it’s a specification!  What can you do?

Listing 1.

using System.Collections.Generic;
using System.Collections;

public class BookCollection : ICollection
{
public void Add ( Book[] books )
{
// This is our own Add method.
// We want the user of this class to add a list of books to the book collection.
}

public void Add( Book item )
{
// This is the Add method from the ICollection interface that this class needs to implement.
// We're respecting the rule of the framework...and satisfying Resharper
}

// The rest of this class members will be ignored for now...
}

Now, suppose we want to use this class under Visual Studio 2005, this is what Intellisense will offer us to choose when using the Add method from our BookCollection class in Listing 2.

Listing 2.

“So which one should we use?” Actually that’s a wrong question to ask. The real question is “Which one do we need to use?” The answer is in our specification. The specification for this class states that we want to add a list a books. Therefore, we don’t want Intellisense to show, nor do we want to implicitly use, the Add method required by the ICollection interface implementation. Therefore, we should hide it. As a matter of fact, since we can’t remove it (because doing so will break the contract defined by the interface), we have no other choice but to hide it or make it private. But just because we’ll hide it, it doesn’t mean that the user can’t explicitly invoke it. Remember that rules can be broken and bent. I’ll show later how to use the hidden implementation of the method (in the name of human curiosity…)

In order to hide this member, we’ll have to explicitly implement it in our class. The mechanism provided by C# to explicitly implement a member of an interface is by declaring the method’s signature which is in the following form:

Return-value InterfaceName<GenericType>.MethodName(Parameters with their types) { // body… }

In our case, we’ll explicitly implement the Add (Book item) method in the BookCollection class, as shown in Listing 3.

Listing 3.

void ICollectionAdd( Book item )
{
  throw new NotImplementedException();
}

In this case, I decided to throw a NotImplementedException() to force the user to use the other Add method if he decides to explicitly use this method. But you can do other things here, such as call another method, or write a log entry every time this method is being explicitly accessed in order to know if the design should be refactored or changed in future releases. Who knows, maybe our users do want the possibility to add just a single book. The current version of the specification doesn’t say so, but it doesn’t mean it will always be that way!

After explicitly implementing this member in our BookCollection class, Intellisense will show the following possible signature when using the Add method from our BookCollection class, as shown in Listing 4.

Listing 4.

As we can see, Intellisense now shows us the only Add method signature that we can use, therefore respecting the specification for this class.  Congratulations.

If you take a look under the hood of that method with Lutz Roeder’s .NET Reflector and view the code with its IL, you’ll see that the compiler has marked the scope of the method to be private instead of public as it is shown in Listing 5.

Listing 5.

explicitimplementationil.PNG

At this point, you might ask yourself “Why is the class implementing the generic ICollection interface anyways, if you want to provide your own Add method?” The answer is that I also want this class to have other collection-oriented methods, such as a Contains(), Clear() and Remove() method, and a Count property. Furthermore, I want this class to be used like a generic collection to other classes or components of my system. Therefore, implementing the provided generic ICollection interface is a good way to satisfy this requirement. Plus, I want to be able to enumerate on my elements.

There is also another method that I might not want this class to implicitly expose, and that is the IsReadOnly property. According to the MSDN documentation of the .NET Framework, A collection that is read-only does not allow the addition, removal, or modification or elements after the collection is created. Therefore, I can decide to explicitly implement this interface member in the BookCollection class as well. Going forward with that idea for the other members implemented by the BookCollection class, the class will expose only the methods, the properties, the events and the delegates with a more accurate match to my domain model, and thus publicly hiding the other “necessities” or “domain noise” required by some framework or API.

But (ah yes, that little word again…) you should be very careful when opting for this strategy. For instance, if some component uses an object of type ICollection<Book> (via dependency injection for example), and uses the IsReadOnly property before doing some other operation on the collection, it’ll call the implementation of the IsReadOnly property in the BookCollection. Therefore, if you are throwing an exception because you don’t want the user of the class to use this method implicitly, for example, it might backfire when some other component in the system will need to invoke it. You can limit the use of your class to some human users, but it’s much harder to do so when that user is a component already in operation.

Nevertheless, the user can still invoke the explicitly implemented member of an interface by casting the object to the interface holding that member, as shown in Listing 6.

Listing 6.

BookCollection bookCollection = new BookCollection();
((ICollection) bookCollection).Add( new Book() ); // Using the ICollection's Add method explicitly...

For instance, if the Add (Book item) method implements the following body,

void ICollection.Add( Book item )
{
  // This is the Add method from the ICollection interface that this class needs to implement.
  // We're respecting the rule of the framework...and satisfying Resharper

System.Console.WriteLine( "Using the interface member explicitly...");
}

…then we’d see the “Using the interface member explicitly…” in the console window once the method is invoked. 

So when should you explicitly implement interface members?

  • When you want your class to implement an interface, but prohibit the use of one service or substitute it for another service to the user.
  • When a class implements two (or more) interfaces with a common set of services, such as the ones provided by the IEnumerable and IEnumerable<T> interfaces. In fact, if you implement the generic IEnumerable<T> interface, you’ll have no choice to implement the IEnumerable interface as well. You’ll have to decide which members to explicitly implement.
  • When you want to hide the implementation of an interface member in your class.

That concludes this post. The main idea was to show that you should shift your design to reflect and respect the domain and its specification rather than pleasing the framework or the API. Sometimes, rules in software development can be broken, and most of the time, they can be bent, especially if it can help you to design a more consistent and concise system design. A design should speak in terms of the domain and not (or at least avoid) in the terms of the technology, framework or tool. When possible, try to abstract whatever is needed by the framework or API, but NOT required by the user of the system; the user being a human being or another system.

Related links

Book Review #1: "Coder to Developer"

"No one can disparage the ability to write good code. At its highest levels, it is an art." Such is a quote that can be read on the back cover of Mike Gunderloy’s book, "Coder to Developer" from Sybex . I got this copy from the library thinking that I would learn something new, something deep about software development. In all of its 295 pages, I can say that this book is not geared towards the intermediate to advanced software developers, but rather to those who are beginning the road to become professional software developers. The main idea behind the book is that "software development" is more than just coding and compiling a software project. There are various activities, principles and practices to set up and to follow in order to design, implement, ship and support a software product. Although Mike’s book is targeting the .NET Framework and the Windows platform, there is no doubt that his point can be shared across other technologies, tools and environments. Joel Spolsky wrote the foreword, and I must say that it was a delight to read it. Here’s an excerpt of what he wrote that pushed me further in reading this book:

"We don’t really know what software development is all about until we have a concept, put in place a team, set up state of the art development processes, design a software product, the right software product, and produce it. That’s not all…it must serve a real need of its users and be accompanied by a web page, a setup program, tests cases, multilingual versions, etc."

I think one reason that I didn’t put this book in my top reading list of all-time favorites is because most of its content, if not all of it, can be found today on blogs and with the help of Google. As a matter of fact, the book was published in 2004, therefore most of the tools that are mentioned in the book are nowadays outdated, unsupported or altogether abandoned. Have I read this book back in 2004, it would have been a great source of insights, but now that we have the power of search in our hands, I don’t really see the pertinence of authors writing any more technical books tackling such a broad field with the support of different tools. Nevertheless, the ideas and principles covered in this book were concepts preached by advocates of software engineering for a long time and are still relevant today. For example, how many organizations today have an established automated continuous integration process? How many independent consultants know how to write a solid contract before embarking on a project?

The author defines a coder and a developer in these terms, which I think is well put:

A coder knows the syntax and semantics of a computer language, whereas a developer can apply that knowledge to turning out a working application with all the necessary supporting details.

I also like the preliminary questions that each developer should ask himself or herself before jumping in the adventure of creating a software:

Ask yourself these fundamental questions before starting to write your software:

1. What are you writing?

2. When do you want to finish writing it?

3. Where do you expect it will be used?

4. Why are you writing this software?

5. How will you write the software?

Someone willing to advance in this profession should be able to answer all these basic questions before engaging a new project. I like the way that these questions don’t depend on implementation or technology.

Mike also presents various ideas to keep in mind before starting to code. Two of my favorites are the concept of the Elevator Pitch and the checklist of eliciting requirements:

Write a good elevator pitch for your software before starting to code it. Keep in mind the following:

1. Short is better than long. A long and rambling elevator pitch probably means you haven’t really decided what you’re building yet.

2. Functionality trumps technology. Potential customers care about what your software will do. They usually don’t care how your software does it.

3. Solve a problem. If you can’t explain what problem your application will solve, customers won’t know why they should buy it.

4. Pitch the benefits, not yourself. Customers won’t be buying you, your superior knowledge, or your development team. They’ll be buying the software.

5. Figure out what’s important to your audience, and make sure you address that. Are the people buying your product most interested in cost, innovation, features, compatibility, or something else entirely?

 

Eleciting Requirements Checklist (if working with an external customer)

1. Talk to the actual end users of the software. Ask them what they expect the software to do.

2. Watch how the end users perform their job now. Make sure you understand how the software will change things.

3. Write a vision and scope document, explaining what’s a part of the project and (equally important) what is not. Make sure the customer agrees with you.

4. Hold an informal group discussion with the users over lunch or a night out. People will speak up in a group of their peers even when they won’t tell you things one on one.

5. Document the requirements in plain English, and then check with the users to see if they make sense.

6. If you’re replacing existing software, find out what’s wrong with the current system. Your requirements will often boil down to fixing the problems.

7. Arrange the requirements in priority order and make sure the customer agrees on the priority.

At the end of the requirements elicitation process, you should know what the software will do. Just as important, the customer should have confidence that you understand their requirements.

 

I was delighted to see that the author mentioned two other types of documentation that should be written whether we’re developing in solo or in teams. These are the development log and the postmortem. It is the first time I encountered the existence of these two documents, which proves that you always learn something new, even when you think you know it all (note to self…). In a development log, we tend to ask and answer the following questions:

  • What the overall architecture of the application is, and why that architecture was chosen over alternatives
  • Who’s responsible for each part of the application or for each function on the team
  • Where key pieces of the network are located: Which server handles source code control? Which one is the build machine?
  • What tricky problems came up, and how they were solved

This development log can easily be maintained in a wiki, a blog, a SharePoint site, etc. I believe this artefact to be very useful for new team members.

The second type of document that should be written is the postmortem. In this document, we tend to include the following:

  • Overall description of the project
  • Who was involved on the team
  • How the project’s scope and features changed from initial design to final ship
  • A list of things that were done well during the project
  • A list of things that could have been done better
  • A list of tools used, along with an evaluation of their quality
  • A list of tools that team members wish they had had available
  • A list of important project documents with their locations
  • Recommendations for the next project: How can things be done better?

Whether you’re a student of computer science or software engineering, or someone making the shift to the software development field and looking for ways to advance as a professional, I’ll recommend you to read this book without hesitation. On the other hand, if you have "been there and done that" for a while and you’re looking to polish your skills, I’ll suggest you to keep looking around because chances are that you will learn more by reading what’s in the blogosphere.

In all, I think that Sybex has published a good resource for the newcomers in this field. Here are the topics covered in this book as stated on the back cover:

  • Choosing and using a source code control system
  • Code generation tools – when and why
  • Preventing bugs with unit testing (and mock objects)
  • Tracking, fixing, and learning from bugs
  • Application activity logging (using various tools, including the Logging Application Block from Enterprise Library)
  • Streamlining and systematizing the build process
  • Traditional installations and alternative approaches
  • Creating software documentation for users and stakeholders

On a final note, these are the contents at a glance:

  • Chapter 1: Planning Your Project
  • Chapter 2: Organizing Your Project
  • Chapter 3: Using Source Code Control Effectively
  • Chapter 4: Coding Defensively
  • Chapter 5: Preventing Bugs with Unit Testing
  • Chapter 6: Pumping Up the IDE
  • Chapter 7: Digging Into Source Code
  • Chapter 8: Generating Code
  • Chapter 9: Tracking and Squashing Bugs
  • Chapter 10: Logging Application Activity
  • Chapter 11: Working with Small Teams
  • Chapter 12: Creating Documentation
  • Chapter 13: Mastering the Build Process
  • Chapter 14: Protecting Your Intellectual Property
  • Chapter 15: Delivering the Application

Verdict: 3/5. It’s a book aimed at beginners who have at heart the need to improve the way they work, as well as their sense of professionalism in the field. The intermediate and more advanced developers would gain more insights by reading what other experts are writing in their blogs and articles. Since the book is heavily dependent on various tools and technologies supported by the 2003 release of the .NET Framework and the Windows platform, most of the content can seem to be outdated and maybe completely irrelevant in a couple of years. Especially for those of us who don’t target the .NET CLR and the Windows platform.

Happy reading!