TechDays Canada 2009 Developer Foundations Call For Speakers

We need speakers for Vancouver and Toronto for the Developer Foundations track for TechDays Canada 2009.

We’re finalizing the abstracts right now; but, it looks like there will be four sessions in this track.

SOLID : the five OO principles that will change your life forever.

  • Will cover Single Responsibility, Open-Closed, Liskov Substitution, Interface Segregation, and Dependency Inversion.  What they are, how to use them, examples, etc.

Refactoring for Fun and Profit

  • How to improve your existing code and architecture through refactoring and avoid “another re-write”.

Advanced .NET (C# and VB)

  • Advanced generics, lambdas and anonymous methods, LINQ to Objects, Error/Exception handling and throwing, etc.

Layers, the secret language of architects

  • Detailed domain layering.  Domain-Driven design will come into play with this session.

Vancouver is September 14-15, and Toronto is September 29-30.

If you’re interested you can email me (see the Contact link on the left, if you don’t already have my email) or Justice Gray or send us a tweet (@PeterRichie and @JusticeGray) and we’ll try to get more information to you as it is available.

Why Fundamentals are Important to Microsoft’s Bottom-line

Justice Gray recently blogged about his concern of lack of SOLID coverage at TechDays despite how Joey Devilla blogged about SOLID principles (and then went on to say that “Maaaaaaaaaaaaaaaaaaaaaaaaaaybe” SOLID would be covered in the TechDays Canada tracks this year).  Joey Devilla retorted with an open letter.

Justice’s concerns are well founded; TechDays is a “showcase current Microsoft tools and technologies” and that’s primarily what it does.  Most sessions cover how to use Microsoft tools and technologies.  But, most sessions don’t do it in a way that exemplifies what many consider fundamentals.  Most sessions cover using Microsoft technologies procedurally.

What is really missing from TechDays sessions is how to use these technologies beyond the syntax and beyond procedurally and how to use them and how to design and architecture the rest of your application or system with generally accepted fundamentals.

So, why?  Why shouldn’t Microsoft simply sell its wares?

Attendees to TechDays aren’t writing software that simply uses a single Microsoft technology, they’re writing systems and applications where that technology may be but a single part of the entire system.  That system also needs to be based on generally accepted design and architecture principles and newcomers to a technology also need to know how to integrate that into systems such as this.  Coming away from a “demo” session where none of the demo code is useable in a software shop that *does* follow SOLID doesn’t add much value to the attendee.

A community who knows only how to use a technology based on demo code doesn’t produce robust and easy to maintain software.  Worse case, this leads to a community known for poorly written, low-quality, hard-to-maintain, slow-to-market software.  This drives people away from the technology into the arms of other technologies.  I’m sure many people can think of at least one Microsoft-based technology not well viewed by the industry.

A community that takes a leadership role in producing quality software inadvertently sells the technology it uses.  It’s easier for new people to join the community and get productive sooner.  This has a snowball effect as the community grows.  This is nothing but positive to the bottom-line.

Microsoft-technology-based communities already have a reputation for cowboy coding and lack of good fundamentals.

What TechDays needs more of is how to properly use Microsoft platforms like Visual Basic and C# and how to integrate other technologies into those platforms using generally accepted fundamentals.  Simply knowing the syntax of a technology doesn’t mean you know how to integrated into the rest of your system.

kick it on DotNetKicks.com

Unit testing WCF data contract serialization.

WCF service define "data contracts" for their interfaces.  These contracts are often defined in an XML schema document and used to generated WCF data contract code.  This process effectively creates a .NET type that will serialize to a chunk of XML text.

Depending on the operation of the WCF service, its code may be responsible for creating some of these objects.  A return value from an operation, for example.  While the framework handles XML serialization of these object behind the scenes.  But, if your have complex types and you end up not setting all the properties correctly in your object tracking down what properties haven't been set properly can be a chore as the server would have to enable passing exceptions back to the client, etc.

A technique that I've found useful is to add unit tests to ensure the creation of my contract objects are indeed serializable.

For example, I may have an method GetCustomer that retrieves a customer.  Where I would like to create this contract Customer I would create a method to create this contract object based on parameters, like a Model Customer object.

For example:

    PRI.ExampleWcfService.Customer ToWcfContractCustomer(PRI.Model.Customer customer)

    {

        //...

    }

Now, one of the unit tests would call this method and attempt to serialize the object to XML text, for example:

    [Test]

    public void WhenCreatingCustomerContract_EnsureXmlSerializationSucceeds()

    {

        PRI.ExampleWcfService.Model.Customer customer = CustomerRepository.Get(1);

        PRI.ExampleWcfService.Application.Customer contractCustomer = ToWcfContractCustomer(customer);

        String text = Utility.ContractObjectToXml(contractCustomer);

    }

where Utility.ContractObjectToXml is declared as:

    public static class Utility

    {

        public static string ContractObjectToXml<T>(T obj)

        {

            DataContractSerializer dataContractSerializer = new DataContractSerializer(obj.GetType());

 

            String text;

            using (MemoryStream memoryStream = new MemoryStream())

            {

                dataContractSerializer.WriteObject(memoryStream, obj);

                byte[] data = new byte[memoryStream.Length];

                Array.Copy(memoryStream.GetBuffer(), data, data.Length);

 

                text = Encoding.UTF8.GetString(data);

            }

            return text;

        }

    }

Now, when this unit test is run, it will fail if the serialization process throws an exception because the resulting XML would violate the schema.

See [DataContractSerializer.ReadObject is easily confused] for why I'm creating a new buffer and copying the serialized data before converting it to a string.

Digg ThisDotNetKick This
Posted by PeterRitchie | 1 comment(s)
Filed under: ,

DataContractSerializer.ReadObject is easily confused.

With WCF services you need to declare contracts and generate contract classes that encapsulate those contracts.  Most of the time you can simply let the framework deal with whatever it needs to do to deal with these objects.  Sometimes, you need to actually see without running a service what XML would result from a contract object or serialize a contract object from XML text.

In .NET 3.5 there exists the System.Runtime.Serialization.DataContractSerializer class that perform serialization of data contracts.

Based on its documentation it seems fairly simple to create data contract object to/from XML methods.  For example:

    public static T XmlToContractObject<T>(string xml) where T : class, IExtensibleDataObject

    {

        MemoryStream memoryStream = new MemoryStream(Encoding.Unicode.GetBytes(xml));

        using (

        XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(memoryStream, Encoding.Unicode,

                   new XmlDictionaryReaderQuotas(), null))

        {

            DataContractSerializer dataContractSerializer = new DataContractSerializer(typeof(T));

            return dataContractSerializer.ReadObject(reader) as T;

        }

    }

 

    public static string ContractObjectToXml<T>(T obj) where T : IExtensibleDataObject

    {

        DataContractSerializer dataContractSerializer = new DataContractSerializer(obj.GetType());

 

        String text;

        using (MemoryStream memoryStream = new MemoryStream())

        {

            dataContractSerializer.WriteObject(memoryStream, obj);

 

            text = Encoding.UTF8.GetString(memoryStream.GetBuffer());

        }

        return text;

    }

But, when you try to perform a round-trip from a contract object to XML and back you'll notice that you get SerializationException with the cryptic message of "There was an error deserializing the object of type <sometype>.  The data at the root level is invalid", despite that the XML seems view when examined.

When Encoding.UTF8.GetString is called, it literally translates the entire array into a String object.  This means that it reads any and all 0 bytes in the array and dumps out null characters ('\0') into the string.  Why do I mention this?  Well, MemoryStream uses a self-expanding buffer to write to.  When MemoryStream runs out of space in its buffer, it doubles the size of the buffer, zeroes it out, then copies the original buffer to the start of the new buffer.  When MemoryStream.GetBuffer is called it simply gets a reference to this buffer, regardless of how many bytes have been written to the stream.  So, most of the time you get a buffer with zeroes padded to the end of it.

If you look closely at the resulting XML, there are a bunch of '\0' characters at the end of the string.

As it turns out, DataContractSerializer.ReadObject (or something it calls) doesn't like all the extra null characters and this is the cause of the SerializationException.

Irritating as this may be, the fix is fairly straightforward.  Simply, create a new buffer equal to the length of the stream and copy the data from the MemoryStream buffer to the new one--trimming all the extra zeroes.  For example:

    public static string ContractObjectToXml<T>(T obj) where T : IExtensibleDataObject

    {

        DataContractSerializer dataContractSerializer = new DataContractSerializer(obj.GetType());

 

        String text;

        using (MemoryStream memoryStream = new MemoryStream())

        {

            dataContractSerializer.WriteObject(memoryStream, obj);

            byte[] data = new byte[memoryStream.Length];

            Array.Copy(memoryStream.GetBuffer(), data, data.Length);

 

            text = Encoding.UTF8.GetString(data);

        }

        return text;

    }

Digg ThisDotNetKick This
Posted by PeterRitchie | 1 comment(s)
Filed under: , ,

Evolving code over time

Given economics, time constraints, resource limitations, etc.; you can't write all the functionality for a given solution for a single release.  Even if you weren't limited by these constraints, you're likely to get changing requirements as development progresses and everyone learns more about the software under development.

It's fairly easy to prioritize what is developed and what isn't.  You simply develop only what you need (see YAGNI).  But, how do you manage adding new functionality without causing undue grief?  One way is to only make additive changes to the code.  For example, let's say we have the method create CreateRequestPacket that creates a blob of bytes to send to a host over the wire:

public static byte[] CreateRequestPacket()
{
    byte[] result = new byte[12];
    result[0] = REQUEST_CODE;
    result[1] = NO_OPTIONAL_DATA_FLAG;
    result[2] = (byte) (result.Length - 2);

    Random random = new Random();
    for(int i = 3; i < result.Length; ++i)
    {
        result = (byte) (random.Next() % byte.MaxValue);
    }
    return result;
}

 

 

In iteration x it writes out a request making certain assumptions about what the request contains.  But, in iteration y it needs to optionally include other data.  A non additive way is to simply modify CreateRequestPacket to do what is needed:

public static byte[] CreateRequestPacket(bool useOptionalData)
{
    byte[] result = new byte[12];
    result[0] = REQUEST_CODE;
    result[1] = OPTIONAL_DATA_FLAG;
    int index = 2;
    if(useOptionalData)
    {
        result[index] = GetOptionalData();
        ++index;
    }
    result[index] = (byte) (result.Length - index);

    Random random = new Random();
    for(int i = index + 1; i < result.Length; ++i)
    {
        result = (byte) (random.Next() % byte.MaxValue);
    }
    return result;
}

Now all calls to CreateRequestPacket need to change before a build can occure and you might not be able to modify all the files that contain these calls.  So it causes undue blocking and forces you to change a number of files before you can check the file that CreateRequestPacket is contained withing.

Another way of implementing this would be to implement an additive change.  That is, add a method that does what is needed and change the previous implementation to call the new method.  For example:

public static byte[] CreateRequestPacket(bool useOptionalData)
{
    byte[] result = new byte[12];
    result[0] = REQUEST_CODE;
    result[1] = OPTIONAL_DATA_FLAG;
    int index = 2;
    if(useOptionalData)
    {
        result[index] = GetOptionalData();
        ++index;
    }
    result[index] = (byte) (result.Length - index);

    Random random = new Random();
    for(int i = index + 1; i < result.Length; ++i)
    {
        result = (byte) (random.Next() % byte.MaxValue);
    }
    return result;
}

public static byte[] CreateRequestPacket()
{
    return CreateRequestPacket(false);
}

This allows you to check the file that contains CreateRequestPacket in right after passing unit testing and allows you to gradually change all calls to CreateRequestPacket as time permits.

Digg ThisDotNetKick This

DevTeach 2009 Vancouver

The schedule for DevTeach 2009 Vancouver has been announced (http://www.devteach.com/).  There’s lots of great software development sessions from some of the leaders in our industry.

If you’re planning on improving yourself, this is the conference to go to.  Not only can you attend excellent sessions; but you can hob-knob with the presenters and pick their brains.

If you have a friend or co-worker who’s interested, there’s a limited-time two-for-one offer for an even better price: http://www.devteach.com/Register.aspx

kick it on DotNetKicks.com

Developing with Source Code Control - Best Practices Part 2

[Edited 14-Mar-09: clarified generated code SCC practice]

This edition provides SCC vocabulary and some more practices that make development life easier.

Vocabulary
Trunk
The root of the project or database.  Sometimes called mainline or baseline; depending on the SCC structure, this is where most of the development occurs.
Mainline
The root of the project or database.  Sometimes called trunk or baseline; depending on the SCC structure, this is where most of the development occurs.
Baseline
The root of the project or database.  Sometimes called mainline or trunk; depending on the SCC structure, this is where most of the development occurs.
Tag
A snapshot in time of the system or a project/subfolder in the system.  Usually also associated with an annotation explaining the significance of that point of time.  Also known as label.
Label
A snapshot in time of the system or a project/subfolder in the system.  Usually also associated with an annotation explaining the significance of that point of time.  Also known as tag.
Branch
A "copy" of subfolders and files that are controlled separately from the trunk with an intention to eventually merge back into the trunk.
Merge
Unifies two independant changes into one.  Often the process of merging requires manual resolution of conflicts.
Merge Conflict
When two independant changes cannot be automatically merged.
Commit
Same as check in
Check in
When changes are written to the repository.
Head
The most recent version of code in the system.
Fork
For the most part, same as branch--depending on the SCC system.  More fun to use than branch, "I forked the code".
Copy-modify-merge
Under this SCC model the SCC always merges changes back into the database (trunk or branch, depending on what you're editing). This model assumes a team model where there is the likely posibility of more than one person working on the same file at the same time.  This model rarely precludes the ability to lock a file before modification if changes to the file cannot be merged).  This is the preferred SCC model.
Lock-modify-unlock
Under this SCC model the SCC forces devs to lock files they wish to edit before modifying them.  (with IDE integration this is often transparent).
ALM
Application lifecycle management.

Practices
Don't use Visual SourceSafe for your SCC system.
Believe me, I've been there and have the scars to prove it.  Visual SourceSafe (VSS) has a unique process that isn't mirrored by many major contemporary SCC systems.  VSS is a distributed file-based SCC system and as such is prone to data corruption--not really something that is acceptable in any sort of "Control" system.  Branching is really difficult in VSS.  If you're one person and you have your VSS database on the same computer; you're likely fine.  Otherwise, do yourself a huge favour and consider another SCC system, like Subversion (it's free).  VSS doesn't scale well; as time goes on and the database increases in size, developers are added to the project, VSS gets slower and slower.

Don't put generated  code in SCC
SCC provides the ability to rollback to a state in the past that allows compilation of the solution.  Code that is generated [edit start] as part of the/a build process [edit end] can be generated at any time; there's no need to put that code into SCC.  With lock-modify-unlock SCC systems, generated code that is checked-in will be locked and cause errors during build unless you lock it on every build.  This will cause conflicts in lock-modify-unlock systems in that you end up not being able to build while somone else is building (yes, you likely build serveral times a day).

Make sure unit tests pass before checking in.
This means *all* unit tests.  If you check in a change that causes a unit test to fail, someone else won't know where to start to make that unit test pass.  You're wasting your time and setting the project schedule back by not ensuring unit tests pass before checking in changes.  If your team allows it, use some sort of continuous integration suite.  This allows you to get feedback upon every check-in that tells you whether all the unit tests pass.  If you have an SCC or ALM that supports it, enable guards that force the unit tests to pass before changes are allowed into SCC.

Work on one set of related changes at a time.
It's more efficient to work on one thing at a time.  Switching back and forth from task to task means wasting time getting into a different frame of mind and sometimes a different configuration.  This also causes issues with SCC in that you've now got modified files that you're not currently working on.  The longer they're kept unmerged, the more likely there will be issues during merge (someone else made a changed that conflicts with yours, and if everyone else is checking in often, you're the one that has to deal with all the conflicts).  The more things you're working on at once, the more likely that one set of changes becomes dependant on another and you simply can't check your files in until all the changes you're working on are complete.  Work on one set of related changes at a time and check those changes in to avoid not merging your changes often.

Check-in often.
SCC provides developers the ability to keep a history of their changes.  But, this only works if you check your changes into the SCC system.  When you have something that compiles, check it in.  The more often you check changes into the SCC system the less likely you'll get into a situation where you have to deal with merge conflicts.  The more often you check changes into the SCC system the least complex the changes will be.  If the changes are not complex and you do run into a merge conflict, the less work will be involved in resolving the conflict.  Do your sanity (and the sanity of your team mates) a favour and check in often.  See next practice.


Avoid leaving changes overnight.
Check in your working changes before you leave for the day.  If it compiles and works (unit tests pass) there's likely no harm to checking in.  Leaving work for an extended period of time means it's that much harder to get back into that mindset; if you check in and describe the check in, you now have a history of the day's changes with an annotation making it easier to get back in that mindset.  But, since you've checked the files in, you may not need to get back into that mindset (if you didn't check in, you *must* get back in that mindset in order to check in).

Never destroy SCC content.
SCC offers the ability to get back to a project state at any time in the past.  If you destroy content from the system, this incapacitates this ability.  It goes against keeping any sort of history of a project to destroy content, so don't do it unless you intend never to make use of the history for that project (i.e. you're destroying and entire project and you never intend to work on it again or support it).

Don't Bypass Copy-modify-Merge
Eventually you'll be in a situation where you need to edit a file at the same time as another person.  In this case you'll need to merge your changes.  The copy-modify-merge model accepts this inevitable fact and makes merging a first-class citizen of your development process; not some nebulous process that occurs only occaisionally.  With a properly designed system the need to merge should be minimized.  I've seen teams instigate a policy that you must lock the file before you modify it.  I've also seen teams configure a SCC provider to automatically lock a file when changes are made to it.  This is the wrong thing to do on software development teams.  It leads to one of two things: 1) people just stop working when they can't lock the file they need to edit; or 2) they end up forcing copy-modify-merge that isn't supported by the tools they're using, which often leads to human errors and errors in merging.

I hope to continue this series as time goes on, if you have suggested topics, please comment.  I expect to keep pretty generic; but could get SCC-system-specific, if there's interest.

kick it on DotNetKicks.com

A Upcoming Pandemic of Domain Anaemia

There's a well-known anti-pattern called the anaemic domain model[1][2].  This anti-pattern basically says domain entities, chronically, have little or no behaviour (remember, object-oriented design is about attributes and behaviour).

It should be obvious that a domain model that isn't truly object oriented is a domain model with a problem.  But, let's look at other reasons why the Anaemic Domain Model is an anti-pattern.  Your Domain is the nexus, the essence, of your system.

An anaemic domain model is basically a reporting system.  Each "Entity" becomes, essentially, a query.  This is fine, reporting systems are necessary and prevalent.  But, to shoe-horn a domain model on top of this leads away from good reporting patterns that could add value and increases complexity, needlessly.  The designers spend most of their time trying to force entities on the system, without recognizing the basic reporting nature of the system.  This usually leads to "reports" that have to pull in multiple domain "entities" to generate the report--rehydringing data into an entity (usually through some sort of ORM) with no value added.  i.e. an ORM that will manage the child-parent relationship (and either pre-load or lazy-load aggregates) doesn't provide much value here.

The worst case scenario with an anaemic domain model is that there really is behaviour there; but it's not handled in the domain entities; it's handled in a different layer.  This is a problem because this circumvents the whole point of a domain model and layering. 

One indication of anaemia is that most of the domain classes  simply contain attributes.  Anyone familiar with patterns should recognize this as a Data Transfer Object, not a Domain Entity.  There's nothing wrong with DTOs, they're very important in almost all systems with any sort of complexity; but they're not Domain Entities.  Let's be truthful, there are systems with little or no behaviour in the domain; and that's not a bad thing.  Systems like this likely don't need a Domain Model and may not need techniques like Domain Driven Design.  The quicker people recognize that, the quicker they can be using a more appropriate architecture and design.  In some extreme cases the anaemic-domain-entity-DTOs service other DTOs

Now, where am I going with this?  Well, there's been a series of guidance out of Microsoft Patterns and Practice about some application "patterns".

First, let me describe what a pattern is.  A pattern is a way of "documenting a solution to a design problem" [3].  First, for it to be a pattern, it needs to detail the problem and it's context, then provide a solution.  The latest "patterns" from P&P do not detail the problem or a context.  They're simply architectural descriptions.

Now the association between the Anaemic Domain Model and the latest P&P guidance.  In 3 of the 5 recently publish "patterns" the following is detail is included: "A Domain Entity pattern is used to define business entities that contain data only."  This is the very definition of an Anaemic Domain Model.  Plus, in the RIA pattern the following, contradictory, detail is included: "Domain entities are responsible for implementing business rules.  Entities from the domain model represent business objects that contain data and implement behavior [sic]. In other words, the business objects are responsible for implementing business operations and interacting with other business objects."

This is disconcerting because historically sample code and guidance from Microsoft is simply reused without thought.  This leads to poorly designed and architected applications, and the .NET community as a whole is seen as one that produces poor-quality code and design.  Without context about the problems these patterns try to solve, they will be misused—likely forced upon contexts and situations where they don’t fit, simply because “they’re from Microsoft”.

[1] MF Bliki- AnemicDomainModel

[2] Anemic Domain Model - Wikipedia, the free encyclopedia

[3] Design pattern (computer science) - Wikipedia, the free encyclopedia

Digg ThisDotNetKick This

It’s More Than Syntax

Writing good software is not just about adhering to a programming language's syntax.  No programming language syntax enforces good design nor does it enforce good programming in all circumstances.

I've seen many systems that compiled perfectly fine; but they weren't good systems.  Some exhibited reliability problems.  Some exhibited maintainability problems.  Some exhibited performance issues.  Some exhibited resistance to change.  All eventually failed at some point; either the system could not change at the required pace, or could never attain an acceptable reliability, etc.

What was wrong with these systems?  The programmers didn't look beyond the syntax of the language.  They didn't accept that programming was about correctly constructing software, not simply writing syntactically correct code.  In almost all cases, the projects didn't have design experience to guide the programming.  The lack of leadership meant that other factors like time-to-market completely dominated the quality of code written.

Now, I'm not saying that big-design-up-front makes for better software.  Big-design-up-front may help with some of the problems of these systems; but it doesn't help with others and it introduces more problems.

Any good software system needs a certain amount of design, up front--you need to know what you're going to be working on both from a requirements point of view and an architectural point of view.  The lowest common denominator is generally some architectural vision based upon established wisdom.  With some systems, this doesn't even need to be complex.  Just enough to guide everything the programmers do.

With any system, its goal is to fulfill the requirements of the stakeholders.  Some requirements (often many) are known at the onset of the project; but some are elicited throughout the evolution of the system.  I say system instead of project because a successful system generally consists of more than one project.  A project could be as granular as a particular iteration, or may be as wide as a project release.  In either case a system changes over time for as long as the system is in use.  To ignore that is to doom the system to failure.  Ignoring the fact that the system must change over the life of the system usually results in things like "requirements sign-off", lack of iterations, big-design-up-front, etc.

In any case, gauging the correctness of a system based on errors/warnings from the compiler is a mistake.  The architecture of the system must utilize established patterns for it to be maintainable and for it to be able to evolve over time.  The development of the system must employ unit testing to ensure the correctness of the system in light of its evolution.  The system must be monitored over its evolution to ensure it's following the architectural design.  If it's not following the architectural design, find out why; and re-evaluate the architecture if need be.

In short, good programmers know much more than simply the syntax of the language they're using.

 

Digg ThisDotNetKick This

House of Cards Design Anti-pattern

I've had this anti-pattern in my head for years.  It's an observance of some projects and methodologies that I've witnessed over the years.  I believe it's a form of Voodoo Programming, Programming by coincidence, and is often a side effect of Cargo Cult Programming.

Anti-Pattern Name
House of Cards Anti-pattern

Problem
A problem occurs when software is written that works in a specific observed scenario but no one knows why it works in that scenario.  Observation of "working" is taken as enough evidence of completeness.  It is very often not enough to observe something working in one scenario for the software to be considered "correct".

This is often a result of continual hacks in sole response to correcting bugs without consequence to design or maintainability.  Repeated hacks (cards) are are placed on top of other hacks until something has the appearance of "working" then all development on it stops and no one wants to go near the code again for fear of breaking it.

At the very least, House of Cards design is fragile, hard to maintain, un-agile.  Worst case it's is of low quality and prone to error and data lose.

This is a general sign of cowboy coding.  I means there is no acceptable methodology, and no real management.  There's little development direction, and likely no development leadership (at least none with any meaningful experience).  Features are generally driven solely by an external source that communicates directly with developers.

Symptoms

When reviewing code or questioned about code, developers respond with comments like "don't touch it, it works", or "we don't want to change it because it works".  There's a reluctance to change the code because no one really knows why it works.  The code stagnates, new features are slow to be implemented, and there's a general un-assuredness about the code.

The code is generally procedural, although following object-oriented syntax.

Refactored Solution

Establish experienced development leadership.  Establish a development methodology that separates the stakeholders from the developers.

Invoke Agile methodologies to manage the requirements of the project and begin Agile redesign of the code employing unit testing, refactoring, patterns, etc.  Aggressively refactor the code adding unit tests to test for specific problems as they arise, until the code is robust and reliable.  Mandate adherence to SOLID principles: classes adhere to Single Responsibility Principle, Open-Closed Principles, Liskov Substitution Principle, Interface Segregation Principle, and uses the Dependency Inversion Principle

Digg ThisDotNetKick This

Unable To Step Into .NET Source

I began getting a problem a while ago that I was unable to step into the .NET source in Visual Studio 2008.  It happened suddenly, and I noticed it shortly after installing SP1.  Given my observation it appears to be due to upgrading the SP1; but I couldn’t find anyone else not having the same problem.  I had another computer where it worked, so I basically put it aside.

Today I had a chance to have a closer look.  I had configured 2008 (RTM, not SP1) to get the .NET source based on Shawn Burke’s blog and had not encountered any problems.  Once I upgraded to SP1 and checked “enabled .Net source stepping”, I all I ever got when trying to step through source is a dialog asking me for the location of the CS file.

What I had configured was to place the debug symbols into a folder in the Visual Studio 2008 user directory (c:\Documents and Settings\PRitchie\My Documents\Visual Studio 2008\Symbols, for example).

Despite creating a new subdirectory for SP1, I still could not step through the source.

It wasn’t until I moved the directory down the hierarchy that I finally got some joy.  I first tried it on the root and it worked fine.  I then tried it as a sub directory within my documents and it worked fine.  I imagine that the path was longer that 260 and rather than present the user with an error with that detail it assumed it couldn’t find the file it was looking for and asked the user for the location of it.  But, I’m guessing.

Hope this helps someone else.

DotNetKicks Image

Pass-through Constructors

Pass-through constructors is a term I use to describe parameterized constructors that have none of their own logic and simply pass parameters to the base class.  For example:

    public class BaseClass

    {

        private String text;

        public BaseClass(String text)

        {

            this.text = text;

        }

    }

 

    public class DerivedClass : BaseClass

    {

        public DerivedClass(String text)

            : base(text)

        {

        }

    }

DotNetKicks Image

Pontificating Virtual Parameterized Constructors in C#

Tom Hollander recently posted about a change he required to the Enterprise Library for date/time validation.  He had to create a new class (rather than modify the Enterprise Library) that derived from another, defective class.  One of his complaints was that in order to effectively implement the base class he had to also write matching constructors that simply called the base class.  His suggestion was effectively to add the concept of virtual parameterized constructors to C#.  I detail “parameterized constructors” because C# already effectively has virtual default constructors.  In the following example the base constructor (Form()) is automatically called by the derivative:

    public class MyForm : Form

    {

        public MyForm()

        {

        }

    }

Virtual parameterized constructors are not new, and from a mere language standpoint this seems reasonable.  Pragmatically though, I believe, this is another story.  It seems logical to be able to simply inherit the parameterized constructors of the base class; but, there are so many times that this isn't the case or some generally accepted principles that would be contravened by a language addition like this.

Let's first look at the open/closed principle (OCP).  The OCP suggests classes should be open for extension but closed for modification.  Robert Martin suggests [1] properly designed class hierarchies that obey OCP implement an abstraction; i.e. derive from an abstract class or implement an interface.  For example:

public interface IShape

{

    void Draw(Graphics graphics);

}

 

public class Rectangle : IShape

{

    //...

    public void Draw(Graphics graphics)

    {

        ///...

    }

}

 

Second, let's look at the "prefer composition over inheritance" principle.  The effect of a language change like this on a design that prefers composition should be fairly obvious.  Here's an example of this principle:

public interface IPolygon {

    void Draw(Graphics graphics);

}

public sealed class Polygon {

    private readonly Point[] points;

    public Polygon(Point[] points) {

        this.points = points;

    }

    public void Draw(Graphics graphics) {

        for(int i = 1; i < points.Length; i++) {

            graphics.DrawLine(Pens.Black, points[i-1], points);

        }

    }

}

 

public class Rectangle : IPolygon {

    private readonly Polygon polygon;

    public Rectangle(Point location, Size size) {

        Point[] points = new Point[5];

        points[4] = points[0] = location;

        points[1] = new Point(location.X + size.Width, location.Y);

        points[2] = new Point(location.X + size.Width, location.Y + size.Height);

        points[3] = new Point(location.X, location.Y + size.Height);

        polygon = new Polygon(points);

    }

    public void Draw(Graphics graphics) {

        polygon.Draw(graphics);

    }

}

 

Obviously there is no way to use virtual parameterized constructors here.

Clearly, designs that take into account OCP and prefer-composition-over-inheritance would not benefit from a "virtual parameterized constructor" language addition.

Finally, let's look at why a class might have many constructors causing such friction for derivatives.  There's many reasons why a class might have many constructors.  I believe all are indications of a poorly designed class.  My first thought would be that many constructors is a result of a large class and that the large-class-code-smell should be an indication for redesign.  A large class could be in an indication of a motherclass; but in either case this is likely a single responsibility principle (SRP) violation and the class is doing much more than it should and be redesigned.  If the class isn't large but has many constructors, this was likely done not in response to how the class should/would be used but to cover every possible way of constructing the type.  This would then be a YAGNI violation and the number of constructors should simply be pared down.

But, what about when you have to deal with poorly design hierarchies and don't have the ability to modify them?  A valid point; but, simply for the lack of friction of writing pass-through constructors I don't think adding to the language to support poorly designed classes is a good for the language or its developers.

While an addition like virtual parameterized constructors seems benign, its limited actual usefulness makes the effort not worth the reward.  Plus, it introduces greater abilities to create poorly designed types.

[1] http://www.objectmentor.com/resources/articles/ocp.pdf

DotNetKicks Image

DevTeach 2008 Includes over $1,000 In Free Software.

Announced recently, registering for and attending DevTeach Montreal 2008 will land you with $1,000 of free software.  Including:

  • Visual Studio 2008 Pro
  • Expression Web 2
  • TechEd Conference DVD set

The sessions have shaped up to be some of the best training money can buy, now you get a bunch of free software to boot.

For more details on the free software see: http://www.devteach.com/News.aspx

For a look at the sessions see: http://www.devteach.com/Schedule.aspx

To register go to: http://www.devteach.com/Register.aspx

.NET 4.0, Evolving .NET Development

.NET 4.0 is the first release of .NET since 2.0 that evolves .NET for every programmer.  .NET 3.0 was largely LINQ and .NET 3.5 was largely new namespaces (like WCF, WWF, etc.)

.NET 4.0 evolves the programming and design for any programmer.  It offers framework support for parallel processing (PFX will be released), Code Contracts (now DbC is a reality at the framework level, and opens the possibility of it being a reality at the language level post 2010), variance changes (co- and contra-variance on generics interfaces and delegates is now a reality).

Parallel Processing
Moore's law has changed from single processors doubling in speed every 18 months to doubling in processing power through increased core count every 18 months.  This means for applications to make use of processing power increases they must increasingly make use of parallel processing and multi-threading.  The PFX makes this more a reality by providing a framework by which application designers can more easily write code to support multi-core processors and multi-processor computers.

With PFX writing a loop to make use of multiple processors (while still supporting single processors) will be as easy as:

    uint[] numbers = new uint[] {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
    Parallel.ForEach(numbers, delegate(uint number) { Trace.WriteLine(CalculateFibonacci(number)); });

Code Contracts
Design by contract is a form of writing software with verifiable interface specifications.  These specifications can be used at compile time to find code that breaks the contract and not require checking of the contract at run-time.  For example:

    [ContractInvariantMethod]
    public int Calculate()
    {
        int result = 0;
        foreach (int value in values)
        {
            this.operation(ref result, value);
        }
        return result;
    }

If anything modifies the current class within the Calculate method, an exception will be thrown at run-time.  Compilers will eventually be able to perform rudimentary checks at compile-time to ensure these contracts are abided by.  For example:

    [ContractInvariantMethod]
    public int Calculate()
    {
        int result = 0;
        foreach (int value in values)
        {
            this.operation(ref result, value);
        }
        this.date = DateTime.Now;
        return result;
    }

...may eventually cause a compile error on the assignment to this.date.  The person designing this type intended this method to be invariant, meaning it doesn't change the state of the object to which it belongs.  This design attribute can now be guaranteed.

Being able to include more design aspects in code and code definitions is a great step forward in not only writing intention-revealing code but in the ability to write more reliable code.

Variance changes

C# has always had intuitive variance when it came to arrays.  For example, the following is valid code:

    Shape[] shapes = new Triangle[10];

Given:

    class Shape {
        //...
    }
    class Triangle : Shape {
        //...
    }
 
Generics variance was a different storey.  Prior to Visual C# 2010, this following is a compile error:
    Func<Triangle> triangle = () => new Triangle();
    Func<Shape> shape = triangle;
 
...despite Triangle being a type of shape (or otherwise known as "bigger" than Shape).  This is known as invariant. In Visual C# 2010 you can now create delegates (as well as types and methods) that are no longer invariant.  For example, a Func delegate could be created that is covariant:
        delegate T Func<out T>();
(not the new use of the out keword) ...which could make our previous code:
    Func<Triangle> triangle = () => new Triangle();
    Func<Shape> shape = triangle;
...compiler without error.
 
The same can be done for contravariance with the the new use of the in keyword:
        delegate T Action<in T>(T value);
 
For more details on generics variance, please see Eric Lippert's series on generics variance: http://blogs.msdn.com/ericlippert/archive/tags/Covariance+and+Contravariance/default.aspx

Other
Another notable improvement is side-by-side (SxS) support for multiple versions of .NET.  This allows hosting of more than one version of the CLR within a single process.  This makes writing shell extensions, for example, in C# a reality in .NET 4.0.  You shouldn't need to target .NET 4.0, but as long as .NET 4.0 is installed you should be able to write shell extensions in a current version of .NET (like .NET 2.0) and it will be supported.  Prior to .NET 4.0, a process could only have one version of the CLR loaded into a process, making extending 3rd party native applications (like the Windows shell) very problematic because what version of the CLR that was loaded into a process would depend on the first extension loaded.  If the first extension loaded was a .NET 1.1 assembly then any other extensions loaded requiring .NET 2.0 would subsequently fail.

DotNetKicks Image

Microsoft Techdays 2008

I've been lax on posting about my involvement in Microsoft Techdays 2008.  I'll be doing two sessions in Ottawa.  One is titled "Internet Explorer 8 for Developers - What you need to know".  The other is "Blackbelt Databinding in WPF".

The description of Internet Explorer 8 for Developers - What you need to know:

Internet Explorer 8 has plenty of exciting new features for developers, from Web Slices and Activities, to new support for HTML5, CSS2.1, and CSS3. In this session, you'll learn how to utilize these features in your latest and greatest Web applications. You'll also learn how features like Compatibility Mode can help preserve the user experience during development.

The description of Blackbelt Databinding in WPF:

Most rich applications present data, often lots of it. To avoid writing lots of code to support the presentation of that data, you should take full advantage of the data binding capabilities of your presentation platform. In this session we discuss and demonstrates the data binding capabilities of WPF. You will find out what data contexts are and how they work with the element hierarchy. We will show you what Bindings are, how to declare them, and what optional capabilities they support. We will demonstrate binding to data sets as well as custom objects and collections, and discuss considerations for implementing those types. In this session we show you how to use data templates to define reusable chunks of UI that can be rendered automatically for each item in a data collection. We also discuss the way you can interact with your bound data programmatically to control the presentation of that data and to make changes to it in code.

For more information on Techdays in general, see http://www.microsoft.com/canada/techdays/default.aspx.  For more information on the sessions in Ottawa, it's location, etc., see http://www.microsoft.com/canada/techdays/sessions.aspx?city=Ottawa.

If you're coming to any of the Ottawa sessions, track me down and say HI.

 

Posted by PeterRitchie | with no comments
Filed under:

Closure Tip

In C# 2 and greater you have the ability to write closures.  A closure is a function that is evaluated in an environment containing one ore more bound variables[1].  In C# 2 this is done by creating an anonymous method that accesses a variable declared outside the body of the anonymous method.  Writing closures (which can evolve from an anonymous method that is not a closure) must be very deliberate and must be given great attention.  Closures offer a very specific way of essentially creating code at runtime based on runtime values.  But, with closures, they can be bound to a mutable variable.  When you bind to a mutable variable you get the value of the variable when the closure is run, not when the closure was created.  You intuitively expect to get the value when the closure was created, not when it was executed.  For example

            String[] numbers = new[] {"one", "two", "three"};
            List<MethodInvoker> delegates = new List<MethodInvoker>();
            foreach(String number in numbers)
            {
                delegates.Add(delegate() { Trace.WriteLine(number); });
            }
            //...
            foreach(MethodInvoker method in delegates)
            {
                method();
            }

With this code, you would expect the following trace:

one
two
three

But, you get this:

three
three
three

This is because the anonymous method is bound to the variable number which was “three” when the anonymous method was executed.

But, what can you do to create an anonymous method at runtime that will output the value of a specific value in a collection?  Well, the answer is very simple, bind to a variable that doesn’t change.  For example:

            foreach (String number in numbers)
            {
                String text = number;
                delegates.Add(delegate() { Trace.WriteLine(text); });
            }
            foreach (MethodInvoker method in delegates)
            {
                method();
            }

The addition of the text variable that is simply initialized with the value of number means the closure isn’t bound to a mutating variable and end up getting results that are more intuitive:

one
two
three

In C# 3 you also have the ability to write closures in the form of lambdas.  You could do the same as the above with lambdas as follows:

            foreach (String number in numbers)
            {
                String text = number;
                delegates.Add(() => Trace.WriteLine(text));
            }
            foreach (MethodInvoker method in delegates)
            {
                method();
            }

Related advice may be found in Item 33 of Bill Wagner’s More Effective C#.

With Resharper you get an added warning that warns you that you’re accessing a modified closure.  So, in the first example, the IDE would show number as the WriteLine parameter as a warning.

[1] http://en.wikipedia.org/wiki/Lexical_closure

DotNetKicks Image

Developing with Source Code Control Best Practices Part 1

This post will detail some first principles about source code control (SCC) and provide what I consider the most basic of practices that every dev should follow.

What is SCC?
SCC provides developers the ability to keep a history of their changes.  SCC allows developers the ability to group file changes together with an annotation. SCC allows developers the ability to get back to the state the project was in at any given point in time, arbitrary or specific (Beta 1, RTM, etc.).  SCC provides development teams the ability to more easily work on the same files at the same time.  SCC allows developers to see who did what, when, and usually why.  SCC allows development on more than one branch of the code at a time.  Depending on the SCC, SCC allows developers to link changes to tasks, work items, etc.

Basic practices
Never check in changes that cannot be built.
This is pretty self-explanatory.  Referring back to what is SCC: "SCC allows developers the ability to get back to the state the project was in at any given point in time".  This means anyone can get back to a state where the project won't build if you check in changes that cannot be built.  This causes undue friction at points temporally distant from the changes--which means there lots of work (time) involved to find and implement a solution (e.g. is the state 5 seconds before better, is the state 5 seconds after better, etc?  All things that need to be researched and tested before continuing--wasting time and resources).

Check in related changes atomically.
This builds on the first practice, if all related changes are checked in as a unit the likelihood of that check-in breaking the build is highly improbable.  Atomically means all related changes are checked in at the same time.  With most SCC systems this is extremely simple, and usually means selecting all the files you have edited (or all the subfolders you have modified folders in--or the one subfolder if you have unrelated changes, see part 2).  This might mean avoiding SCC IDE integration if your changes span several solutions. If you can't check in changes atomically (really question this if you think you can't.  If you honestly can't, I'd suggest changing SCC systems) order the check ins to avoid build problems.  For example, if you added an enum to somefile.cs, then used that new enum in someotherfile.cs, check-in somefile.cs before checking-in someotherfile.cs.  Checking in somefile.cs first will not put the source code in a state where it cannot be compiled.  You're working on a team, anyone on the team can get the state of the project at any time, like between non-atomic file check ins.  This means they get code that doesn't compile and they won't know why or how to fix it--blocking them from doing their work and wasting time and impacting the project schedule.

Make use of shelving.
If what you've coded needs to be given to someone else to get the source code to build properly, don't check it in in order to give it to that other person.  On cases like this, if your SCC has shelving or shelvsets, shelve the changes and inform the other person of the shelvset instead of checking in.  Let them check in the working changes as an atomic unit.  If shelving isn’t an option (consider or petition for an SCC system that does, then), send them the files for them to check in after modifications.  See next practice.

Get all current code and build before checking in.
Someone may have made changes between when you got the code you working on and when you want to check in.  If you don't get the latest code (and potentially merge it with yours), what you check-in may leave the controlled code in a state where it cannot be built.  Get all the current code to make sure you have what will be the state of the code when you check in and that it builds first.  If your build process is long, this may require several tries.  You should address build times if this is a common problem.  Communicate to the rest of the team that you need to check in changes and that they hold off on checking in related components if necessary.

kick it on DotNetKicks.com

Software Process and Reduction of Quality

Ken Schwaber had a conversation with Scott Hanselman about the concept of "done".  He said that software developers have a habit of culling down all the generally accepted practices of software development except the writing of code.  He says that, when pushed, software developers reduce quality in an effort to produce a software product by a certain time.  This leads to a huge debt that must eventually be paid. 

These generally accepted practices include things like unit testing, refactoring, design, documentation, etc.  In the case of producing an API or and SDK, I believe these practices includes community involvement.  When a team developing an SDK, API, or IDE "goes dark", they're not doing their job properly--they're ignoring the fundamental reason for their work: the customer.  This is a huge problem in our industry because software developers are effectively trained that schedules only include writing code and meetings.

I agree with Ken and would say that part of an Agile process includes a significant amount of time to communicate with customers need be scheduled.

Even if not professing to using an Agile methodology, if unit tests, integration tests, refactoring, and research don't take up a significant amount of the software development schedule or there's no scheduling of a significant amount of time to communicate with customers; the team is inept.  In this day and age, I would hazard to say its downright felonious and fraudulent.

DotNetKicks Image

Becoming a Visual Studio Jedi Part 1

Becoming a Visual Studio 2008 (and often Visual Studio 2005) Jedi

In much the same grain as James' Resharper Jedi posts, I'm beginning a series of posts on becoming a Visual Studio Jedi.  It involves getting the most out of Visual Studio off-the-shelf, doing things as quickly as possible and with as little friction as possible.  I think it's useful for all users; but especially useful for those who are in situations where they can't install refactoring tools like Refactor Pro! or Resharper.

First, familiarize yourself with Sara's Visual Studio Tips blog; then subscribe to her blog.

I'll attempt to provide detail at a less granular level than Sara's blog (i.e. using a series of commands to perform a specific task); but I may overlap here and there

Take advantage of Auto-Hide
Like Jimmy Bogard and Jeffery Palermo, I have my Visual Studio UI very lean. 99% of the time, I'm working in code.  The Solution Explorer (SE), Properties, Output, etc are auto-hide panes.  When I need to use them I hover the mouse over the tab to make them visible, do what I need to do with them, then get back to the code.  The Code Editor is the only window that isn't auto-hide or floating.

Navigate find results via keyboard
Whenever anything is displayed in a find results window, you can iterate each item in the list via a keystroke.  The default C# keyboard map had F8 and Shift+F8 as the shortcuts for next and previous.

For example:

  1. Press Ctrl+Shift+F to bring up the Find in Files form.
  2. Enter "TODO\:.*refactor" in the "Find what" text box.
  3. Ensure Match case, Match whole word are unchecked.
  4. Ensure Use is checked and has Regular expressions selected.
  5. Press Alt+F to search.

A Find Results window is displayed that shows the results of the search.

  • F8 goes to the first result.
  • Pressing F8 again goes to the next.
  • Pressing Shift+F8 goes to the previous result.

See also:
http://blogs.msdn.com/saraford/archive/2008/04/18/did-you-know-you-can-use-f8-and-shift-f8-to-navigate-among-errors-in-the-output-window.aspx
http://blogs.msdn.com/saraford/archive/2007/11/08/did-you-know-how-to-use-f8-to-navigate-the-find-results-window.aspx
http://blogs.msdn.com/saraford/archive/2005/03/30/403887.aspx

F8 and Shift+F8 work for most lists like Find Results 1, Find Results 2, Error List, Output:Build, etc.

File name extensions when adding classes
Do you find your self selecting text in the file name when you use the Add Class wizard?  Or, do you always type ".cs" at the end of your file name?  You may be happy to know you don't have to do that.  Simply invoke the Add Class wizard and type the name of the class.  The wizard adds the missing .cs for you.  For example:
Press Alt+P, C
Enter "MyNewClass"
Press Enter
A file MyNewClass.cs is added to your project and it contains class named "MyNewClass".

Consider a Custom toolbar

There's generally only handful of toolbar buttons that you might need, especially if you're a keyboard user like me.  There's some things that simply don't have a default keyboard mapping.  Another good reason for having a custom toolbar item with only the buttons you use is if you often change the size of your Visual Studio window.  The default layout has two or more toolbars (depending on the edition and any add-ins you have installed).  You can carefully position those toolbars so they may take up one or two lines; but when you then shrink the size of your window the get wrapped and they won't restore if you expand the size of your window.  Having a single toolbar means this wrapping of toolbars can never happen.

Export Settings

Once you get your UI the way you want it, you can actually save the layout. 

  1. Click on Tools\Import and Export Settings.
  2. Select Export selected environment settings.

This is really handy if you get into low-resource situations (like the application you're developing or its framework uses up too many GDI handles and Visual Studio can't allocate a handle to display a toolbar or a frame.  When this happens Visual Studio actually turns off those GUI elements; when you close and restart those panes/frames are no longer displayed by default.)

DotNetKicks Image
More Posts Next page »