RSS
 

Archive for the ‘Uncategorized’ Category

The Craft of Coding

23 Oct

On the 17th of October I presented a talk at SMU to a group of computer science students.

Here’s my abstract:

This presentation will show how fundamental computer science concepts such as data structures, recursion, object orientation, and runtime analysis are not only useful beyond the classroom today but also essential skills for tomorrow’s software engineers. I’ll begin by demonstrating real world code where these fundamental concepts come into play. I’ll then show how a robust understanding of fundamentals can mean the difference between an elegant solution and a hack. I’ll conclude by discussing how tomorrow’s reality of many cores, functional languages, massive scalability, and the coming cloud revolution necessitate a solid computer science foundation.

The value of a college education to a career in software engineering has always been questioned. The venerable Robert Martin recently tweeted:

“It doesn’t take a college degree to be a programmer.  Programming is a trade that can be learned through apprenticeship and mentoring.” – Robert Martin

Bob is being narrow; Programming is a broad concept.  Designing compilers, operating systems, embedded systems, circuits and DSLs does require a foundation of computer science. While you can succeed in object oriented business programming without a college degree, you will appreciate knowledge of fundamentals when you step outside this comfort zone.  Yes, you can succeed in today’s 4GL languages without a degree ,but you will be at a disadvantage each time the industry shifts.

Joel Spolsky offers the following on the subject of fundamentals:

“I want my ER doctor to understand anatomy, even if all she has to do is put the computerized defibrillator nodes on my chest and push the big red button, and I want programmers to know programming down to the CPU level, even if Ruby on Rails does read your mind and build a complete Web 2.0 social collaborative networking site for you with three clicks of the mouse.” – Joel Spolsky

I couldn’t agree more.

 
 

Connect with your Kinect Slides and Code

09 Oct

I had the pleasure of presenting the expanded version of my “Connect with your Kinect” presentation at both the C# Special Interest Group and the Ft Worth .NET User Group this month.   I’ve really been enjoying both speaking on Kinect and developing with the peripheral.  I promised code and slides so here they are!

The code can be pulled from BitBucket here. Slides are available here

I also just learned I get to do this one more time at SMU. The SMU Computer Science Department has invited me to give the talk again on Oct 27th to students.  The talk is open to everyone so if you are interested in attending look here for more information later this month.

 
 

GDI+ Architect is Finally Open Source!

26 Feb

I’ve been meaning to tackle this for awhile. GDI+ Architect is a designer I created for GDI+ code under the DBA name (The Modern Renaissance Group (Mrgsoft). It lets you draw objects on a canvas and generate SVG or GDI+ source code. I had moved it a while back from .NET 1.1 to 2.0, but there was a proprietary icon set which prevented me from releasing the source.  I decided to just remove the icons and keep my ugly ones I created in instead.

logoMy reason for open sourcing it is want to make sure if anyone had legacy code from the .NET 1.1 time frame that used this product, they could still get at it. While I don’t expect a lot of new development on this project, if you need help with it please contact me. It’s also interesting code you ever need to do anything with snapping, drawing, text wrapping, or anything of that sort regardless of your target platform. Even though this code is geared toward .NET GDI+, the concepts are universal. Warning though – it’s in VB.NET instead of C# as this was once my primary language.

It’s rough to look at your own code from 8 years ago. It’s missing clear separations of concerns. It’s got some “mammoth methods” that need to be torn apart and refactored. It’s littered with somewhat useless comments because I thought code wasn’t “done” until it was commented ad nauseam. Also since it’s from the 1.1 timeframe there is no use of generics, Linq, etc. In fact checking it into BitBucket today was the first time it ever saw a source control repository! My how things change.

All that aside, it shipped, was fairly bug free, and made me a decent return on investment and received positive feedback from customers.

If you are interested, you can get the source from BitBucket.

 
 

Where the code lives

24 Feb

Since the ALT.NET movement a couple years ago, Microsoft developers have, to varying degrees, embraced open source technology.  Many of us can’t imagine planning a project or designing an architecture without leveraging at least some open source tools.  What surprised me is where the source for these tools live.

Here’s the skinny:

image

A couple note on my data. 

First, there are excellent open source project from Microsoft including Unity, MEF, the Silverlight toolkit, and more hosted on CodePlex, but I have excluded these since they are not community open source projects.

Second, some of these projects are not on created on the .NET stack but I believe they are still relevant to .NET developers so I have included them. Other projects I have left off because they are not relevant to developers, rather consumers.

Finally, this is my subjective list of projects. I’m sure I forgot some.

Based on this data, it’s GitHub by a landslide.  The truth is almost every important C# based open source project lives there.  And amazingly, Git is only sort of supported on Windows using Cygwin. What’s going on?

It turns out that GitHub has turned open source collaboration into a social experience.  Contributors can gain status and recognition for participating in GitHub.  Projects rank against each other in a fun, competitive manner.  It’s easy to search projects by language and to collaborate.  In other words, GitHub really is “cool”.

GitHub also revolves around Git which is distributed version control.  For SVN or TFS users, this is a completely new and liberating experience.  I recently worked on an SVN project where certain folders were only committable by one individual, effectively stopping collaboration.  Nothing like this happens with a Git based project.  You are encouraged to fork and you can always commit against your own repository, 

If you haven’t used Git, there are some good resource to help you get started.  I think the best is Rob Conery’s series on TekPub called Mastering Git.  It’s not free, but it’s really good and it shows using Git from windows.

Last May my friend Shawn Weisfeld recorded a quality presentation by David O’Hara presenting at the North Dallas .NET User Group on Git.  You can see that presentation here.

For me, I think Mercurial is the better distributed version control system for Windows users.  It doesn’t require Cygwin and there is a nice Tortoise and command line client.  You can also play nice with GitHub using this plugin.

My data set below.

Project Where it Lives
Autofac GitHub
Automapper GitHub
Caliburn MIcro CodePlex
Castle GitHub
CouchDB SourceForge
CruiseControl.NET SourceForge
Farseer Physics CodePlex
Fluent NHibernate GitHub
FluentMigrator GitHub
Fubu MVC GitHub
GitSharp GitHub
JQuery GitHub
Kayak  GitHub
Knockout GitHub
Log4net Apache
Manos de Mono GitHub
Mass Transit GitHub
MongoDB GitHub
Mono GitHub
Moq Google Code
MVVM Light CodePlex
NANT SourceForge
NCQRS GitHub
NHibernate GitHub
Ninject GitHub
NSeviceBus GitHub
Nunit Launchpad
OpenRasta GitHub
Owin GitHub
Raven GitHub
RestSharp GitHub
Rhino Mock GitHub
Sparkle View Engine GitHub
SpecFlow GitHub
Spring.Net SourceForge
StructureMap GitHub
Subsonic GitHub
Voldemort GitHub
 
 

Fun with Generics

30 Nov

Generics were a great addition to .NET but sometimes their behavior can be unexpected.  Check out these somewhat unintuitive bits.

Static Constructors

Consider the following class with a static constructor:

        public class GenericDemo<T>
    {
        static GenericDemo()
        {
                Console.WriteLine("static (shared) constructor");
        }

        public void SayHello()
        {
            Console.WriteLine("Hello!");
        }

    }
 

Given the following usage of the type, how many times is the shared constructor called?

 

            1:  public static void Main()
            2:   {
            3: 
        
            4:   GenericDemo<string> instance1 = new GenericDemo<string>();
            5: 
        
            6:   GenericDemo<string> instance2 = new GenericDemo<string>();
            7: 
        
            8:   GenericDemo<int> instance3 = new GenericDemo<int>();
            9: 
        
           10:   GenericDemo<float> instance5;
           11: 
        
           12:   GenericDemo<double>.SayHello();
           13: 
        
           14:   }

 

The answer is 3 which makes sense if you think about it.

This gets at the heart of what generics really are.  Static constructors are only called once (single threaded assumed) per type when first instantiated or when invoking a static method.  So lines 4, 8, and 12 exclusively manipulate new types for the first time and trigger the call.

Overloading

Consider the following declaration.


        public class GenericDemo2<T>
    {

        public void SayHello(T myinput)
        {
            Console.WriteLine("generic overload invoked");
        }

        public void SayHello(string myinput)
        {
            Console.WriteLine("string overload invoked");
        }

    }

Will it compile? It surprised me that this is indeed valid and does not throw a runtime error if T is a string.  If called when T is a string, it calls the non generic method every time.  Interestingly VB does not allow this to compile because its implementation varies slightly.

How about this which uses generic methods:

        public class Example3
{
    public void SayHello(string toSay)
    {
        Console.WriteLine(toSay);
    }

    public void SayHello<T>(string toSay)
    {
        Console.WriteLine(toSay);
    }
}

 

It looks like there would be a conflict and the compiler would reject the overload.  But it does compile!  The T is a true parameter and gives it a different signature!

 
 

Socrates On SOLID: The Single Responsibility Principle

24 Nov

This is the second in a series of (hopefully) amusing posts into Robert Martin’s SOLID principles.  It’s written as a dialog between Socrates and a student named Loej in the spirit of the dialectic method popularized by Plato.  In this post, the principle explored is the “Single Responsibility Principle”.  I hope you find this enjoyable.

Socrates:  Welcome again Loej.  Last time we agreed that software development is, as Steve McConnell puts it, a wicked problem.

Loej:  Right.  We have to solve software problems again and again to get them right.  This means change is an inherent part of the software development process.

Socrates:  And we agree that when we write code, we must anticipate change in order to manage complexity.

Loej:  Right and managing complexity is the fundamental concern of software engineering.

Socrates:  Very good.  When we talked before you said that in your experience, even a simple change can be difficult to make and may cascade into unrelated changes.

Loej:  Right. I remember when our company went international we had to add a shipping country for just about everything.  Products, customers, you name it.  I mean dragging the new fields to our forms was easy enough.  Master are you OK?

Socrates:  (Shivers) Continue…

Loej: But then we had to change the database code in the Customer form, the Product form, and the Supplier form. And we had to change our validation controls on all the forms.

Socrates: And?

Loej:  We introduced a defect. But at first we thought it was a bug with our printing object.

Socrates:  Your printing object?  How’s that?

Loej: We had an object which printed everything out.  It did things like PrintW2, PrintCustomerInvoice, PrintAddressLabel, stuff like that.  All of a sudden invoices and address labels stopped printing!

Socrates:  Did you track it down?

Loej: Yes after a few long days.  It turned out that we were writing a null to the customer table and when we retrieved the customer from the database in the Printer object, we got back a null as well.  This null was being appended to the address string and this caused the printer to not print!

Socrates:  Ouch.

Loej: Yeah bad memory.

Socrates:  No pun intended?

Loej:  Hmm?  Oh, no.

Socrates: I see.  Let’s start at the beginning.

Loej:  Sounds good.

Socrates:  You had one user directed change, to internationalize shipping, but this change required you to change some really big classes.

Loej:  Right.

Socrates: And in doing so you introduced a defect.

Loej:  Right again.

Socrates:  And the defect was originally thought to be in the Printer object, which was also a large class which did all things printer related.

Loej:  Yes.

Socrates:  If the printer class had been simpler, would it have been easier to debug?  Easier to test?

Loej: Well, that code would just need to be somewhere else.

Socrates: Right, but would you have had to look so hard to find the defect?

Loej:  You mean if we had a class whose job was to just print customer invoices? And a class just to print address labels?

Socrates:  Exactly.

Loej:  But wouldn’t this violate the don’t repeat yourself principle, you know, DRY?  I mean wouldn’t we need a bunch of code repeated in each of those little classes?  And doesn’t a bunch of little classes add complexity which we were trying to mitigate in the first place?

Socrates: Good questions. Let me address your first concern, DRY.  Why do you think it would be violated?

Loej: Well, we had methods in the printer object that formatted strings and checked that columns were correct and all that stuff.  All of these things relied on shared methods in the printer object.  We’d have to duplicate that in every new object.

Socrates:  Would you really?

Loej:  Oh, you’re suggesting we could keep those things in the printer class?

Socrates:  Or a couple new classes.

Loej:  Ok I get it.

Socrates:  Right.  So let’s not worry about DRY.  Now about your second concern, bunches of little classes.

Loej:  Right, we would have like 20 classes instead of one.

Socrates: And this adds complexity?

Loej:  Totally. I mean who can keep track of 20 things in their head.

Socrates:  Would you rather keep track of 20 classes or 20 methods of one class?  Isn’t this really the same thing?

Loej:  Well, there are a lot more files around.

Socrates: Could you use folders to simplify that?  And don’t multiple small files work better with your revision control system anyway?  And don’t modern IDEs take care of this?

Loej:  Points taken.  But automatic completion would become a mess…

Socrates:  What about using namespaces to handle that?

Loej: Ok.  So there’s not much difference in terms of complexity between twenty methods and twenty classes in modern software development with modern IDEs.

Socrates:  And actually using namespaces and folders you can simplify things more with separate classes than you can a single monolithic object.  Let’s move on then.  We agreed earlier it’s easier to find defects if they are more focused.

Loej:  Right.

Socrates: Would it be easier to not inadvertently introduce defects if objects were smaller and more focused?

Loej:  I suppose so.

Socrates: So would it make sense to divide up your validation logic, display logic, data logic, and business logic into separate classes?

Loej:  Well it would have made it possible to do some testing.

Socrates: Taking this idea further, what if each class had a single responsibility.  If each class just did one thing?

Loej:  That would make it easier to test.  But how do I know when I’m done dividing things up?  How do I know when I’ve got the right level of granularity? I mean, our printer object only handled printing.  And our forms only did one thing.  Like the supplier form handled suppliers. I mean at some level of abstraction, everything only does one thing.

Socrates: Well what do you mean when you say the printer object only handled printing?

Loej:  In the case of the printer object it meant that it only handled printing.  It printed invoices and it…

Socrates: Hold up!

Loej: What?

Socrates:  You said the word "and".  I think that’s a good sign you are breaking this rule when describing a class.

Loej: You mean that if I use the word "and", I have more than one responsibility?

Socrates:  Exactly.

Loej:  Ok I’ll buy that.  But how do I know if I have the right responsibility?

Socrates:  That’s going to depend on your domain model and a second rule of thumb.

Loej: What’s that rule?

Socrates: According to Robert Martin who formulated these SOLID principles, each class should have one and only one reason to change.

Loej: That sounds arbitrary and academic.

Socrates: Well, but what would it mean if we had two reasons to change?

Loej: I suppose it meant that the class did more than one thing.  It had two responsibilities.

Socrates: Right.  So either we planned for the class to have two responsibilities which we have decided is not a good idea, or we didn’t understand the class well enough in the first place because we didn’t see that there were two responsibilities – two reasons to change.

Loej:  I guess it makes sense.  Still, I can think of some cases this wouldn’t be good practice or good design.

Socrates: Sure. SOLID should help us think about good software design but they are principles of good design and not laws. Your domain model requirements should sometimes take precedence.

Loej:  So what you are saying is to keep this idea in mind as I code.  This will lend my design to looser coupling, easier testing, and smaller more focused objects.

Socrates:  Very good. Now go and meditate deeply on the letter S until we speak again.

Loej:  Yes master!

 

AtomSite blogging software

11 Oct

Carl Sagan once remarked, “To create an Apple pie from scratch, you must first invent the universe." So it almost went with my attempt at a blog site back in August.

Eventually I came to my senses and decided that I would check out the wide array of open source solutions for blogging. Surely there was something out there that met my needs. I started by looking at some of the ones I was familiar with by their reputation.

WordPress has such wide adoption I frequently see articles and classes for graphic designers focused on WordPress template creation. The shear number of templates available is a strong argument for WordPress adoption. It also has a very mature, polished feel. Yet I decided not to adopt WordPress in part because I am running a Windows server and also in part because I’m a .NET developer and want a platform I can tweak if needed.

I also decided to look at the various engines available via the Microsoft Web Platform Installer. These included Das Blog of Scott Hanselman fame, Subtext of Phil Haack fame, and BlogEngine.NET. All of these were more than adequate for my needs and I really had a tough time decided amongst them. I also decided to downloaded a new engine I had not heard of called AtomSite .

After playing with all of them, much to my surprise I decided on AtomSite .

About AtomSite

AtomSite is available on CodePlex as well as via the MS Platform Web Installer.

Because of its relative newness, AtomSite lacks some of the features of more mature frameworks. For example, it doesn’t have the breadth of themes some frameworks have. Administrative features are still under construction though everything can be done directly through the service.config file. Documentation is also fairly scant. Still, if you are willing to work through these issues, it really is a good framework for hosting.

From a development standpoint, it is nascent and as such has exciting and frequent updates. It is also the first open source blog engine I am aware of that is based on the ASP.NET MVC framework (aside from the Mix 09 one which didn’t appeal to me). In its source AtomSite strives to embrace C# features which I appreciate. It also makes good use of JQuery and the HTML is well done and readable. It also makes use of YUI grids, a technology I wasn’t very familiar with but really does make clean design across multiple browsers a cinch. Lastly, it embraces XHTML which I find refreshing.

AtomSite also has a reasonably nice plugin architecture. One plugin featured prominently is support for OpenID. I feel it is now essential for every developer blog to support OpenID. In terms of file storage, it uses angle brackets on disk and adheres well to the atom protocol. If you have access to your disks you can do lots of nice find and replace with Powershell or your tool of choice. And backup is as easy as copying files.

Initial setup was a little rough but this was largely because I had trouble understanding how the Service.config file worked and how the general site was structured . Recently AtomSite’s coordinator Jared Vance requested users send in their third party config files, and this really helped clarify what sites were doing.

Understanding the Service.config File

As defined on AtomSite, the service.config file adheres to the service document standard for Atoms.

In the service.config file, one or more workspaces are created each which consist of one or more collections. I’m going to skip covering workspaces as I imagine most sites will consist of one workspace.

Collections are where the action begins. Each collection refers to a set of “atoms” which is just an XML document adhering to the atom standard. In fact in AtomSite’s implementation, each collection gets its own folder on disk which contains all of the “atoms” that make up that collection.

An example custom collection is shown below based on this site.

AtomSite_Collections

Let’s look at these some of these properties.

Element / Attribute

Description

href

An IRI (A URI but International Resource Identifier) that identifies the collection. Should be unique to the workspace. Required by the Atom standard.

svc:dated

Refers to whether to use dates when generating URLs and IDs for items in this collection. A blog would likely be dated whereas static pages would not.

The source code shows two examples.

Dated: /2008/10/10/MyBlogPost.xhtml

Not dated: http://example.com/info/About.xhtml

svc:ratingsOn

Whether to allow ratings for items in this collection (or not). Be careful because everything supports ratings whether you want them rated or not!

svc:annotationsOn

Whether to allow comments on items in this collection (or not). As with ratings, everything can be annotated so be careful when this is set to true (Do you really want your “About picture” to have comments?)

svc:syndicationOn

Whether to allow syndication feeds of items in this collection or not.

svc:Visible

Whether a collection is “visible” or not. Primarily this means it doesn’t show up on the menu widget when auto populated nor does it show up in syndication regardless of the value of syndication on. Secondarily it causes items not to show up in search and a couple other instances.

This property makes the most sense when the contents of a collection are there purely to be referenced by another collection as in the case of media.

svc:bloggingOn

This property seems to indicate if AtomSite should act in a protected mode when parsing content (trusting comments). It also overlaps a lot with annotationsOn. I need to get more clarification on this property.

svc:default Determines if this collection is the default collection in the workspace.

If a request doesn’t indicate a specific collection, the one marked default is returned. When a request is made of a “workspace” (typically a site) without a specific collection specified in the URI, the collection marked as default handles the request.

For example, if you hit http://www.codeconfessions.com, the blog collection is default.

atom:title Required by the Atom standard. An arbitrary title.
atom:id Unique id identifying a collection.

Pages aren’t really

Probably the most difficult to grasp concept in Atomsite are svc:pages. I can understand why there was difficulty naming them as “view” is taken in MVC.

Svc:pages do not refer to physical pages. Rather, they refer to a layout of widgets and are used to determine how collections at various views will be shown.

Perhaps more important to know is that the svc:page name property is not arbitrary. In fact name actually refers to the purpose of a widget and refers to a predefined class in the source code.

<svc:page name=”BlogHome”> refers to the widget layout set for the landing page for a workspace. To generate the initial landing page for your site, AtomSite takes the widget set defined in BlogHome and applies it to your default collection in your default workspace.

<svc:page name=”BlogListing”> refers to the widget layout set used to display a list of items in a collection. When a collection is requested (rather than a specific entry in a collection) this view is used.

<svc:page name=”BlogEntry”> refers to the widget layout used to display a single item in a collection. Examples include a single blog entry or a single page of content.

Summing Up

AtomSite has a lot of potential and I am very happy with the quality of work that has gone into creating it. There are still rough edges, but for a relatively new community contribution, it really is shaping up quickly. If you are considering starting a blog, I would definitely give it a try.

Addendum 7/31/2010: As you can see I did finally decide to transition from AtomSite to WordPress. I still consider AtomSite to be a great piece of software, but I needed something with wider adoption and more templating options.