Phil posted on January 18, 2010 15:22

Bugs vs. Defects

I really don’t like the word ‘bug’ when describing a piece of (missing) functionality that doesn’t work correctly or has unintended consequences. I much prefer the more accurate word: defects. If you use ‘bugs’, you’re doing yourself, your projects, and your customers a disservice.

It’s a Mindset

The word ‘bug’ brings a cutesy picture of a ladybug or maybe a little grasshopper, or maybe an uglier thing like a house fly or cockroach. Get real and be professional: these are defects. Don’t trivialize your work and its consequences. Call it what it is.

Imagine you buy a car or a house (here we go with the metaphors) – how would you feel if the driver’s door had a flaw whereby the handle or lever would open the trunk on each odd turn, and open the driver’s door on each even turn? What about a defect where your GPS took you to the wrong destination? How frustrating or expensive would it be, for you as the customer, to work around that?

If your software doesn’t do what you intended/written, then you haven’t tested enough. Shipping that product is, by and large, up to you. If your product contains untested/unpredictable behaviour, that’s all on you!

Not Helping

Yes, it’s a nice story about Grace Hopper and the moth at Harvard. Of course, it’s an easy feel-better-about-yourself term that’s used. However, it becomes an uphill battle for those professionals who care about terminology when there are products called Bugzilla and FogBugz in the bug-tracking line of programs. I use both, and both are great at what they do.

Of course, the term ‘bug’ is well engrained into our minds and language, and has even crept into the language of the non-programmer.

Personal Software Process

Personal-Software-Process-Watts-S-Humphrey This reminds me of one of the textbooks at BCIT’s CST program. It’s the Personal Software Process by Watts S. Humphrey. Watts is crazy-focused on software quality. One distinct lesson I got from that particular course was the concept of constantly recording and measuring. Defects per 1000 lines of code (KLOC). The other theme, perhaps in the book, or with that particular instructor, was this idea of ‘defects’, and not ‘bugs’.

Own Your Defects

I’ve come to this conclusion for myself when thinking about flaws in software:

Don’t trivialize those flaws with your words, own them. Call them what they are – flaws and defects!


Posted in: development , punditry  Tags:
Actions: E-mail | Permalink | Comments (0) |

Consider your behaviour and desires as a customer when you visit:

a) your favourite local coffee shop - you specify what you'd like, rather than how it's made. You care about the outcome, but rarely care about the process/sequences/steps that are followed as it's being constructed.
b) a Subway/Quiznos shop - you care equally about the outcome and the construction. Meats, veggies, bread type, order of placement of each, precise amount of mustard, pickles, etc.

baristaThe difference here is: you aren't instructing the coffee barista on how to steam the milk, or when to start brewing the espresso. You don't remind them that their level of ground beans is getting low, or where to store the milk. You are happy to assume they know their job best, and order of operations is properly under their control. They have their efficiencies to care about, and you're happy to let them manage that.

Consider now your desires as a programmer when your task is to find customers with a condition. Let's say we want to use this contrived example:

    Find the customers whose account balance owing is over $5,000. Find the youngest customer in that set. 
 

The Sandwich Model of Algorithms

//find all customers with the appropriate account balance
var owingOver5000 = new List<Customer>();
foreach (Customer c in myCustomers)
{
if (c.TotalAmountOwing > 5000)
{
owingOver5000.Add(c);
}
}

var youngestCust;
DateTime youngestBirthdate=null;

foreach(Customer c in owingOver5000)
{
//initialize on the first go-round; many ways to do this.
if (youngestBirthdate==null)
{
youngestBirthdate=c.BirthDate;
}

//find the youngest by their age
if (c.Birthdate < youngestBirthdate) //pretend nobody has the same birthdate ;)
{
youngestCust = c;
}
}
//you now have youngestCust populated (in most cases)


Very fine-grain operations are explicitly laid out by the developer, and execution path follows exactly what the developer wrote. Defects and all! The number of defects is up to you!

The Coffee Shop Model of Algorithms

Consider now the Coffee Shop model of this algorithm. We'll use LINQ.

var youngestCust = myCustomers
.Where(c=>c.TotalAmountOwing > 5000)
.OrderBy(c=>c.BirthDate)
.SingleOrDefault();

It should be obvious to you now, if it wasn't at the start: the LINQ extension methods are doing all the looping for you, and taking care of all the small bits and housekeeping. You, as the customer, don't want to care about how it's found, but rather, you declare what you want.

Outsource Your Loops

I hate to steal Eric Lippert's thought on this, but it's worth saying once more in a different way:

Avoid loops. They’re becoming a code smell. Let built-in methods and functionality do the boring non-value added logic for you.

You should be focused on YOUR business logic or end-goals (i.e. eating your sandwich and drinking your coffee), and less on syntax + language constructs. Take advantage of more declarative constructs provided in your language/framework. LINQ is a perfect example of this.

*this post is a mashup of Luca Bolognese's PDC 2008 F# metaphor and Eric Lippert's post on loops. Apologies to both!


Posted in: development , best practices , linq  Tags:
Actions: E-mail | Permalink | Comments (0) |
Phil posted on January 8, 2010 09:52

2009 was a great year for me professionally. I've focused on continually learning new things and patterns in software development. There was a fair amount of professional growth, learning, and the realization that there is so much more to learn.

One benefit of taking a new job/position is that it helps you realize how much you don't know. I've taken on a few new skills/technologies, sharpened a few existing skills, and challenged myself to re-evaluate some beliefs or patterns I've held for a while. One exciting event was a presentation to the British Columbia Health CIO Council. The topic was our adoption of the Enterprise Master Patient Index project as part of the Electronic Health Record initiative.

Tech Learnings


ASP.NET MVC

My opinion of ASP.NET MVC went from 'terrible idea' in 2007 to 'awesome' today. At first I just did not get ASP.NET MVC; I thought it was classic ASP all over again, and I totally balked. I was looking for asp:GridViews and trying to output server controls. Then I got it. What really nailed it for me was Dino Esposito's talk at DevConnections in Fall 2008 where he made clear the distinction of ASP.NET (the HTTP runtime) and WebForms (the abstraction of state and page lifecycle). I’ll frequently use Hanselman’s Motorcycle vs Car analogy.

Not to knock on WebForms, because it’s provided a lot of benefit to me, my projects,  and my customers for years. WebForms provides so much abstraction with its incredible productivity gains, but feels a bit dirty when it comes to control-over-HTML.

I dove into ASP.NET MVC, and the NerdDinner sample app was a huge help in many respects. I followed up by purchasing the Hanselman/Haack/Guthrie/Conery MVC book, which is a great resource.

I was lucky enough to develop and implement 3 MVC apps, and have 2 personal projects using ASP.NET MVC. I am excited to use ASP.NET MVC for projects where I care about markup and testability. More from Dino on ASP.NET MVC.

LINQ

linq A major tool to discover and learn more about in 2009. Upcoming investment in the dead-tree edition of LINQ In Action http://www.manning.com/marguerie/. I am finding it fun to have a list of objects and write a LINQ query. It makes coding that much easier! I started with the 'query' syntax, but much rather prefer the 'dot notation' syntax. The 'query' syntax for joins, however is much easier to read and write

jQuery

No question JavaScript has been a help in my web projects, but a pain to work with in the browser at development-time. Most of my logic has been server-side, but I know I can do better to give the user a better experience in using my apps. jQuery has been fun to start learning, and it's satisfying to be able to use a few lines of code with terse syntax to have your form elements behave as you want. The usability of the jQuery libraries is stunning. Truly this is a 'for-developers, by-developers' library, like they've anticipated your needs. I have yet to have a need and not have jQuery have a solution. I am looking forward to more jQuery in 2010.

Systems Integration & Health Care Messaging

A major learning for me was around integrating different existing systems. I had gotten used to creating greenfield solutions or solutions where both/all ends were totally under my control. I had smaller integration subprojects where, for example, I was taking waterslide orders from an Excel document, and creating new records in my app. In that case, my solution was the consumer of that data.

In my recent integration projects, the source and end consumer is a downstream system that I don't have control of. Actually my project is the broker/intermediaries between the source and destination. Further complicating things, there are 3 systems within this intermediary, each on different technology stacks. So the learning for me was to be the middle-man, and to work with constraints on both side of that data transfer.

Unit Testing

In my projects previous, I'd tried to create unit testing projects to help keep my code meet its requirements, and to keep code changes from deviating away from those requirements. Only recently have I begun to rely on unit tests to ensure my changes don't break.

osherove_cover150With VS 2005, I was using NUnit to test my business logic in .dll assemblies. At the time I had lots of code in code-behind, and of course, that's difficult to put under test. Fast forward to today with Visual Studio's Test Project, I've abandoned NUnit. After writing a (large ? 70+ tests) set of unit tests for this particular set of web service logic have I finally proved to myself the value of unit tests. The payoff moments came when my implementations had to change (LINQifying some algorithm), and when new requirements/feature were added.

From here on out, any important logic will be under test. I feel a book purchase upcomingThe Art of Unit Testing by Roy Osherove

Large Scale Applications

I've had the pleasure of taking on two large scale health care applications this year. One had its technical requirements well laid out (mostly), and the other was a collaborative effort with my counterparts 500km away. Some lessons learned around large projects: 

  • large numbers/skills/discipline/remoteness of team members make for interesting and different challenges on each of those attributes
  • scalability will come back to bite you if you haven't prepared!

Project Successes


Deployment - I was lucky enough to have been a part of a successful software project this year. I developed and deployed a large scale enterprise app that brokers 6000+ messages per day. The important attribute is that humans are directly waiting for these messages to cross & acknowledge back to them. It would be nice to be able to be able to publish a high level systems diagram here instead. In this case, success is measured in user acceptance, on-time delivery, and stability.

Managing Changes - A few times I've gotten the call/email or attended a meeting where a *breaking change* was discussed/agreed on. Not to get into a performance review here, but I found myself reacting quickly to those changes, and have breathed a sigh of relief when my implementation handled those changes well. Is it luck?

Looking Toward 2010

2010_inukshuk My answer to New Years resolutions is: why wait until the turn of the year to set goals for self-improvement? What were you doing throughout the year?! In any case, my list of professional goals this year are:

  • Learn A New Language! I have been listening to the endless talk of Ruby and Python. I fully value the concept of 'knowing more than one and many languages', both in software and in life. So this year, I'll be taking on (one of) Ruby, Python or F#. The great thing about these languages is that they're dynamically typed. F# is a first-class .NET language, but it's not clear whether IronRuby and IronPython will be first class languages in Visual Studio 2010. I am looking forward to F# in that it's a functional programming language. I will be learning WHEN to use a functional language, and I will be posting my findings. My first intro is Luca Bolognese's PDC 2008 video presentation: "An Introduction to Microsoft F#".  'let' doesn't mean assign a value to a variable, but bind a value to a symbol!
  • Deploy More Personal Projects! There's nothing worse for a developer with ideas than to NOT convert on them. Smaller designs, shorter iterations, deploy more. Cost isn't an issue, really!
  • Find A Good Webhost. I think I've already done this with SoftSys. Their .NET 3.5 hosting packages include SQL Server, which is my #1 requirement for hosting. Goodbye DiscountASP!
  • Move This Blog to SubText! I feel that BlogEngine.NET was an upgrade over DasBlog. Both aren't being actively developed and maintained. After having used SubText for my internal blog, I feel comfortable enough with SubText and *its continued development*! It requires SQL Server 2005 / Express.
  • Do These Things Better: Document changes. Make more graphs.

How Are You Improving ?

What professional challenges and successes did you have? How are you striving to be better in what you do? Are you ‘sharpening the saw’ in regards to your professional skills?


Posted in: development , unit testing , improvement  Tags:
Actions: E-mail | Permalink | Comments (0) |
Phil posted on December 12, 2009 07:08

Recently I found myself with the desire to start a new site. Rather than jumping into Visual Studio headfirst, I sat down and thought about how to start.

  • What technology am I going to use? I always jump to this in the Top 5 Things I Consider, and I know it’s not terribly important, and I know I should be thinking of other things. I always know the answer to this question, though.
  • What features should be on this site? Getting warmer. I typically write down the main features of the site goals of the user in point form. I am trying to train myself with this way of thinking: The site doesn’t matter without the users wanting it to. Don’t think about features, think about the user’s goals.
  • Monetize? Sure, in some small way, but that’s not the main point of this new site, though.
  • How will the user see and contribute to the site? Most important and relevant question. Piss off your users, or make it too complicated, or too lame, and you’ll lose visitors.

The typical process I go through when faced with a new idea and a fresh start is to pull out the pad of paper and a pen. I am not a design genius, like most software developers, but try my best to grok user experience. Sometimes I fall flat, sometimes I look back at previous work and throw up a little, and sometimes I am happy. Most of the time, I think I’m just lucky that I don’t have too much scrutiny on my layouts and flow in my corporate line of business web apps.

mockingbird

I found this site recommended a few times on StackOverflow. It’s almost self explanatory to how the site works.

mockingbird mockup of youtube

Draggy, Droppy, Stretchy, Copy

There’s a palette on the left, and a design surface on the right. Make a page for each of the user-goals. This isn’t going to be set in stone, and things will change. Arrange the page elements on the design surface as you like.

Some neat features or user experiences I noticed:

  • Labels/text scale nicely as you grab the corner and stretch. It (smartly) figures out when to bold and/or increase the size of your label. Other elements scale beautifully, especially those that are icons – calendar, pie chart, Twitter logo, etc.
  • Double click any element to modify its text or contents – just as you’d expect.
  • Configuring a Linkbar was easy. Mockingbird really nailed this element.
  • You’re able to associate links with pages that you’ve already defined. Just drag the Page on the left onto a form element.
  • Elements are very generic – no Windows or Mac bias. It’s just a rounded rectangle representing a button.
  • There are lots of great elements that trigger new ideas. Seeing the Map and the Banner ad were great. Aside - tag clouds – meh… does anyone really like and use the tag cloud in the real world? I know StackOverflow has one, but I have never used it.
  • The overall feel or experience is very much like a sketch. Positioning elements is made easier thanks to the horizontal and vertical alignment bars. Nice touch!

mockingbird thumbnail grid

Go Try It

Just jump into mockingbird. You don’t need to create a mockingbird account to try; only to save and retrieve your designs. It’s free!

I wonder what platform mockingbird is running? Oh wait, it’s called Cappuccino  (learn more) and it’s

implemented using a new programming language called Objective-J, which is modelled after Objective-C and built entirely on top of JavaScript

Very cool user experience! Kudos to the mockingbird developers Saikat and Sheena.


Posted in: development , design  Tags:
Actions: E-mail | Permalink | Comments (0) |
Phil posted on December 11, 2009 03:06

Here’s a class that’ll make your life easier when you want to deal with saving information in cookies on your user’s browser. Everyone needs a wrapper class for all those external data-stores – session, cookies, file system, web.config and app.config, registry, log files, etc. Here’s a class usable in ASP.NET Web Forms and ASP.NET MVC.

Wrapper Class

Here’s a static class that you can simply include in your web project, and refer to its static properties to get to your cookies. Any and all simple datatypes can be used, and heck, even serialized versions of your POCO objects can be saved/retrieved here. Image if you wanted to save those shopping cart items, a collection of user prefs, or whatever, you could simply override the .toString() method in your custom class.

Just Make Properties

The key pattern here is that you purposefully create new properties for each piece of data that you want to save/retrieve. This solves the problem of:

  • having to remember strings all over your project.
  • ensuring no duplicates exist – imagine if multiple developers created a defect by using the same string indexer for their cookie, and ended up stomping each other’s value?
  • typos in cookie names.

Instead, the data access cookie-retrieval is done through named properties. This solution solves all those potential problems. Here’s a peek at one of these properties.

public static string UserFullName
{
get { return GetCookieVal(CookieItem.UserFullName); }
set { UpdateCookieVal(CookieItem.UserFullName, value, 365); }
}

Enums Help

With the aforementioned ‘remembering strings’ problem, the pattern that this class uses relies internally on an enum to handle the naming of the value in the cookie. The enum will boil down to an integer, but really we don’t care what the key’s is actually stored as in the cookie. We really only care to access/read/save the values constantly and easily from our calling code.

Download

Download the cookie class, or copy/paste from below. You can see that I pre-loaded it with some amusing properties for your entertainment!

Be sure to change the ApplicationName const at the top.

Special thanks to Special-K!

using System;
using System.Web;

namespace MyNamespace
{
public class Cookies
{
private const string ApplicationName = "MyCoolApplication";

private enum CookieItem
{
UserGuid,
UserFullName,
UserLoginExpiry,
UserHadForBreakfast,
UserTimezone
}
/**************
All cookie values are accessible by public static methods.
No typos/duplicates are possible from calling code!
**************/

public static string UserFullName
{
get { return GetCookieVal(CookieItem.UserFullName); }
set { UpdateCookieVal(CookieItem.UserFullName, value, 365); }
}

public static Guid UserGuid
{
get { return new Guid(GetCookieVal(CookieItem.UserGuid)); }
set { UpdateCookieVal(CookieItem.UserGuid, value.ToString(), 365); }
}

public static DateTime UserLoginExpiry
{
get { return DateTime.Parse(GetCookieVal(CookieItem.UserLoginExpiry)); }
set { UpdateCookieVal(CookieItem.UserLoginExpiry, value.ToString(), 365); }
}

public static string UserHadForBreakfast
{
get { return GetCookieVal(CookieItem.UserHadForBreakfast); }
set { UpdateCookieVal(CookieItem.UserHadForBreakfast, value, 1); }
}

private static string GetCookieVal(CookieItem item)
{
HttpCookie cookie = GetAppCookie(false); //get the existing cookie
return (cookie != null && (cookie.Values[item.ToString()] != null)) //value or empty if doesn't exist
? cookie.Values[item.ToString()]
: string.Empty;
}

private static void UpdateCookieVal(CookieItem item, string val, int expireDays)
{
//get the existing cookie (or new if not exists)
HttpCookie cookie = GetAppCookie(true);

//modify its contents & meta.
cookie.Expires = DateTime.Now.AddDays(expireDays);
cookie.Values[item.ToString()] = val;

//add back to the http response to send back to the browser
HttpContext.Current.Response.Cookies.Add(cookie);
}

private static HttpCookie GetAppCookie(bool createIfDoesntExist)
{
//get the cookie or a new one if indicated
return HttpContext.Current.Request.Cookies[ApplicationName] ?? ((createIfDoesntExist) ? new HttpCookie(ApplicationName) : null);
}

}
}


Posted in: c# , asp.net , best practices , code blowout , development  Tags:
Actions: E-mail | Permalink | Comments (0) |
Phil posted on November 5, 2009 05:07

Perhaps you’ve got a collection of objects that you want ordered/sorted. I recently did. Perhaps you want them displayed in a non-deterministic manner or sorted randomly each time you write to an HTML view or otherwise consume that collection. Indeed, I did. (OK, lame shtick over…)

Randomize()

So imagine we’re working with a List of Customers.

data

Problem being, we don’t really have anything non-deterministic to work with. We could do this in SQL Server:

SELECT *
        ,(sin(Cust.ID * rand())) AS R
FROM Cust
ORDER BY R

This though, puts your presentation logic in your data tier. Maybe you don’t care, maybe it’s not a big price to pay. I wanted to take it up to the presentation tier and do this sorting right before binding or writing these objects to the page.

Taking the lead from Bruno Silva, I immediately realized this was the seed of the algorithm that I was looking for. Here’s my modification of Bruno’s algorithm:

   Random r = new Random(); 
   myCusts.OrderBy(x=>(r.Next())); //bind this, or use in a foreach

The call to r.Next() is obviously the key. Each object as it is evaluated in the OrderBy() will get a new random number associated with it. Celebrate good times!

Turn It Into an Extension Method

I haven’t written much here about how much I love extension methods in the .NET CLR 3.0. I can’t count the number of times that I’ve created a static ‘utility’ method that did something like this:

  • take in an object of some kind.
  • modify it.
  • return it back to the caller.
  • caller reassigns the return back into the object that it calls.

Rather than the above, I’ve created an extension method that I’ll be able to call whenever for any collection that implements IEnumerable. It’s trivial then to write the extension method for IQueryable.

ExtensionMethod I’ve submitted my Randomize() extension method over at ExtensionMethod.net. What a great site, by the way, kudos to those guys for taking user submissions and helping grow the use of this feature in .NET.

 

http://extensionmethod.net/Details.aspx?ID=236

public static IEnumerable<t> Randomize<t>(this IEnumerable<t> target)
   {
       Random r = new Random();
       return target.OrderBy(x=>(r.Next()));
   }


Posted in: development , c# , linq , extension methods  Tags:
Actions: E-mail | Permalink | Comments (0) |

On two recent projects, I've had the need to write out the properties of multiple custom entities. The example here will be around the venerable Customer class. Let's pretend that a requirement would be to send an email each time a customer makes an order to a support rep in your company. Yes, we'll be logging the order to the database, but the value-add here is that the recipient of the email will receive a link to the order, plus all the details of the customer and order included in the email.

So we need to take an object, iterate through its properties and their values, and put into a string for the body of an email.

The first thought might be: "just loop through each property and write it to the email body". That's fine, but as soon as you add a property in the future, you need to remember to change the emailing code. That's not helpful for the developer needing to maintain this application. Let's look at the Reflection namespace in the .NET framework.

Reflection in .NET

Reflection lets you programmatically find out information about types in your assemblies at runtime. Using classes in the System.Reflection namespace, you can learn details of an class's methods & properties. In the topic at hand, we're interested in the names, values, and datatypes of properties. You could possibly use reflection to get info at runtime about a method's parameters.

Override Customer.ToString()

The Customer class needs its own ToString() method overridden. Here's the class:

 
using System.Reflection; 
using System.Text;
 
public class Customer{
    
    public string Name{ get; set; } 
    public string Email { get; set; }
    public string Address { get; set; } 
    public string City { get; set; } 
    public string State { get; set; } 
    public DateTime JoinDate { get; set; }
        
    public override string ToString() {
    
        StringBuilder personString = new StringBuilder(); 
    
        foreach (PropertyInfo pi in this.GetType().GetProperties()) {
            //get the name of the property and its value. 
            personString.AppendLine(string.Format("{0}: {1}", pi.Name, pi.GetValue(this,null))); 
        } 
 
        return personString.ToString(); 
    }
} 

 

Use It!

 
Customer cust = new Customer{
    Name = "Mike",
    Email = "some@dot.com",
    HomeAddress = "1 Some Street",
    State = "ZZ"};
 
string customerOuput = cust.ToString(); //now put this in your email body!
 
Console.WriteLine(customerOutput);
 
/* Console will show:
Name: Mike
Email: some@dot.com
HomeAddress: 1 Some Street
City: 
State: ZZ
JoinDate: 
*/

 

Consider This…

There are a few things to consider with this implementation:

  • collections aren't handled well.
  • complex datatypes aren't either.
  • consider customizing your implementation to include special formatting for DateTimes.
  • what happens when you want the output to have a well-formatted output for a property like HomeAddress. We'd probably want it to show as "Home Address".
  • consider handling null values better than writing "null".

Wrap Up

I know this will save developers time in two ways:

  1. Not having to iterate manually through X properties to build your string for email. That'll scale depending on the number of properties and your adeptness at Ctrl-C, Ctrl-V.
  2. When you add a new simple property, you will NOT have to adjust anything for it to show in the .toString() method.

How are you using System.Reflection?

Posted in: c# , development , code blowout , .net framework  Tags:
Actions: E-mail | Permalink | Comments (0) |

When hammering away at a unit test, I often found myself wanting to test the current unit test and its associated code. I of course wanted to use the debugger, break points, inspect values, use the Watch tool, etc.

The First Iteration

  • Put the cursor in the test method.
  • Go to Visual Studio IDE’s menu 
  • Click Tools - Run - Tests in Current Context
  • Debug as per normal

ide

Uh, Do It Smarter

Then I remembered that you could put the shortcuts in the right click context menu. Ah ha!

rightclick

You can see where this is going if you paid attention to the first iteration, as well as the title of this blog post. Taking your hands off the keyboard to move to the mouse adds so much more wasted time in development. File this under “what were you thinking?”.

Smartest

Now it’s all keyboard goodness:

  • Cursor in the test method
  • use a cute mnemonic in your head when you're typing in this combo. Re Test
  • Ctrl-R, Ctrl-T

What is YOUR favorite Visual Studio keyboard shortcut?


Posted in: development , shortcuts , visual studio  Tags:
Actions: E-mail | Permalink | Comments (0) |