Network

Follow kosalanuwan on Twitter
View Kosala Nuwan Perera's profile on LinkedIn

Writing user stories: a practical approach

Let me be clear. Software projects are predictably unpredictable. You cannot solve all the problems in the project plan. It’s good if we can schedule good ideas but people don’t think like this way. People solve problems in unexpected ways. This is where good ideas come from. Many are called “Ahaa Thoughts”.

Explaining things are easy. Documenting it is the hardest part, not only that, many devs hate this part, in a software development process. We could only interpret around 50% of "Ahaa Thoughts" into "words".

Writing user stories is not a hard task. Once you master it you can say:

"Documentation in Development processes was my Unicorn, and now it is my bitch"

Or

"Writing user stories was my Unicorn, and now it is my bitch"

As a good citizen, a Business Analyst or a Product Owner could use a template/standard. I use something like this:

User Story

Name: Customer List
As a: User
I want: Interactive Customer List
So that: I can change the details of my customer
Size: XS, S, M, L, XL

This user story comes from my Product Backlog List. It is a White Note on my wall. Things I can do about this story are called Story Points, that explains further about the story. A story point is a Yellow Note on my White Note. I write it like this:

Acceptance Criteria/Story Points

Name: Delete an address
Given: The customer is in an editable state
When: The remove address button is pressed
Then: The address will be removed from the list
And: The change will persist after a refresh

Name: Add an address
When: The application is loaded
Then: A list containing customer addresses will be displayed
And: The name of each customer will be displayed with an image next to it

To reduce the number of documents that we have to maintain in a development process, I use these User Stories and Acceptance Criteria for Testing. A Bugs is reported under a White Note or a specific Yellow Note, if not, a separate Defects Backlog List (parallel to my Product Backlog List).

For more readings;

Beyond Functional Requirements On Agile Projects by Scott W. Ambler
Complex Requirements On an Agile Project by Scott W. Ambler
Introduction to User Stories by Ambysoft
User Interface Prototypes by Ambysoft

Classic Data Access Layer Helpers to implement data access layers in application architecture

In a simple application architecture with three layers there is always a layer that queries data from the database, extract, shape it to some CLR typed entities, push it to next layers that initiates it. Right now we get plenty of frameworks to get this job done. But those who are not fond of ADO.NET Datasets, nHibernate, LINQ-to-SQL, EF etc. will usually stick with the classic old school approach of handling CRUDs in data access layer.

You will see how I have used the Classic DAL Helpers (from CodePlex.com) to get this job done in a much cleaner and easier way. In this article, I will try to demonstrate following topics with samples;

  • Creating and handling stored commands.
  • Materializing relational data into CLR types.
  • Using template classes to reduce repetitive tasks.

The code samples are based on the Mini NorthWind project which have 3 main classes:

Sample class diagram

In a traditional approach, code to call a stored procedure in the database (with proper connection handling), extract relational data, create set of objects and identities could be very large even with rapid tools have been used such as Data adapters, EF, LINQ-to-SQL. Here is what I had to write:

public List<Category> GetAllCategories()
{
var connString = ConfigurationManager.ConnectionStrings["SampleModel"].ConnectionString;
using (var conn = new SqlConnection(connString))
{
using (var comm = conn.CreateCommand())
{
comm.CommandType = CommandType.StoredProcedure;
comm.CommandText = "dbo.GetAllCategories";

if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
try
{
using (var reader = comm.ExecuteReader())
{
var cats = new List<Category>();
while (reader.Read())
{
var idOrdinal = reader.GetOrdinal("cid");
var nameOrdinal = reader.GetOrdinal("name");

if (!reader.IsDBNull(idOrdinal))
{
var category = new Category
{
Id = reader.GetInt32(idOrdinal),
Name = reader.GetString(nameOrdinal)
};
cats.Add(category);
}
}
return cats;
}
}
finally
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
}
}
}


Below sections demonstrates how you can start refactoring this code using Classic DAL Helpers. There are few important classes, helpers and extensions methods that you can use in this refactoring process.


Creating and handling stored commands



As a good citizen, you have to add library namespace to your classes in order to use the extension methods and helpers in Classic DAL Helpers library.

using Microsoft.Data.Extensions;


Now you can use SqlDatabaseHelper.HandleCommand method to manage connection lifetime and to create a stored command as below:

var cats = SqlDatabaseHelper.HandleCommand<List<Category>>(
"SampleModel",
"dbo.GetAllCategories",
new SqlDatabaseHelper.CommandHandler<List<Category>>(GetAllCategoriesCommand));


Once this is done, you can get rid of few steps relates to stored command such as initialization of the database connection, retrieving connection string in the application configuration file, and creating and managing lifetime of a stored command. The HandleCommand<T> method facilitates an IDbCommand with a live connection for you to handle your stored command execution and you do not need to validate and verify whether the connection is opened or not since the helper method takes care of that part for you.


In order to pass parameters to your stored procedure you will require to instantiate SqlParameter objects, assign relevant values to those, pack it to your command before the execution. Here is how we used to do:

comm.CommandType = CommandType.StoredProcedure;
comm.CommandText = "dbo.GetAllCategories";

var catParam = new SqlParameter { ParameterName = "cid", Value = categoryId, Direction = ParameterDirection.Input };
comm.Parameters.Add(catParam);

Instead of this, you can use the extension methods that provided by the Classic DAL Helpers library:

command.AddParameter<int>("cid", (int)args.First());

Materializing relational data into CLR types



I heard the term “Materialization” from the EF Team. It means; converting the relational data returned via stored command into actual objects, pretty much similar to an ORM process. Here is a sample code that we used in previous section to populate Category objects:

using (var reader = comm.ExecuteReader())
{
var cats = new List<Category>();
while (reader.Read())
{
var idOrdinal = reader.GetOrdinal("cid");
var nameOrdinal = reader.GetOrdinal("name");

if (!reader.IsDBNull(idOrdinal))
{
var category = new Category
{
Id = reader.GetInt32(idOrdinal),
Name = reader.GetString(nameOrdinal)
};
cats.Add(category);
}
}
return cats;
}


Instead, I can materialize relational data into Category objects like this:

return reader.Materialize<Category>(this.Materialize);


The materialize extension method requires a delegate that knows how to map data record into a single Category object. The generic extension method Field<T> in IDataRecord can return value relevant to the Ordinal or the Column name. This is how it would look like:

protected override Category Materialize(IDataRecord record)
{
var cat = new Category
{
Id = record.Field<int>("cid"),
Name = record.Field<string>("name")
};

return cat;
}


While the materialization is more convenient, it is much more efficient to use a single materialize method per CLR type. However, you can use the same materialize method to instantiate multiple CLR types if they all derive from a common super class.


You can use the same ProductDataAccess object to materialize both Product and DiscontinuedProduct objects as below:

protected override Product Materialize(IDataRecord record)
{
Product prod = record.Field<DateTime?>("discontinued_date").HasValue ?
new DiscontinuedProduct
{
Id = record.Field<int>("pid"),
Name = record.Field<string>("name"),
CategoryId = record.Field<int>("cid"),
DiscontinuedDate = record.Field<DateTime>("discontinued_date")
} :
new Product
{
Id = record.Field<int>("pid"),
Name = record.Field<string>("name"),
CategoryId = record.Field<int>("cid")
};

return prod;
}

The CLR type could be a Business Entity in the model, a View Model used to present data, a Data Transfer Object that used to transport data among tiers, or it could be a Data Contract in your WCF service.


Using template classes to reduce repetitive tasks



A traditional Data Access Component would contain all the CRUDs related to a single business entity, facilitating ORM functionality to that same entity. The Classic DAL Helpers library contains few template classes. DataAccessObject template is one of the main classes you can use to implement CRUD functionality. Now I can declare Category and Product Data Access Objects as below:

public sealed class CategoryDataAccess : DataAccessObject<Category>
public sealed class ProductDataAccess : DataAccessObject<Product>


In order to use CRUD functionality via template DataAccessObject, you must make sure your CLR types are derived from RootEntity template. Now I can change my class declaration as in below sample:



image


I have extent Product class further to a DiscontinuedProduct class. The class declarations for this diagram look like this:

public sealed class Category : RootEntity
public class Product : RootEntity
public sealed class DiscontinuedProduct : Product

The Classic DAL Helpers library source code is now available in CodePlex.com. You can download directly from the below link;


CodePlex.com, Classic Data Access Layer Helpers and Samples
http://classicdalhelpers.codeplex.com/

Rapid way to improve JavaScript’ing skills

Found some interesting tools and techniques that could improve our JavaScript’ing skills. Worth trying!

jslintJSLint is an online site that can do static analysis on your JavaScript code. You just have to Paste it on the site and Boom! You get the all the BS in the code. You can configure the level of analysis as well from the site. Why not try JSLint to improve the scripting as well.

blackbirdBlackbird can help you debug the JavaScript you have written with fun. It is difficult to debug the JavaScript code, watch the variable values etc. and one option is to use alert() snippets or break the script and debug if the IDE supports script debugging. But you may agree that it still sucks. You have to make sure you remove all the alert() snippets, break snippets in your script before you commit as a good citizen. Why not try Blackbird instead?

JSLint, The JavaScript code quality tool
http://www.jslint.com/

Blackbird, Open source JavaScript logging utility
http://www.gscottolson.com/blackbirdjs/

Why Software Estimations Sucks?

A long story short (a true story)
There is this Developer doing his thingy in his cubicle, one day, a Project Manager comes up to him and says; “Hey, you know that project we’ve been talkin’ about?”
Developer replies; “um.. yeah! I know.”
The PM continues; “You  know what… we would really really like to get it done in this August, coz’a this and this and this and that… can we do that?”
and the Developer thinks a lil bit… and says; “Yeah… I think its possible... I think we can do that... you know… there might be a lil bit’a work… but I think we can do that.”
The PM was so excited and runs off to a meeting… they makes decisions about spending thousands and millions of dollars and resources based on a freaking 15 seconds estimation that Developer did.

The consequences are pretty severe if you mess an estimate. Many developers get scolded for messing estimates. Many developers’ credibility was shocked coz they messed the estimates. Many developers were tribute losing their job to messing an estimate.

If you are a Developer, you might agree with me at least 100% in all three things. But if you are a Project Manager or a Delivery Manager you might think that this is a bit offensive, but please don't take this personally. This is just what I see from a Developer’s point of view. Well, its becoz, you often asking to;

Why Software Estimation Sucks

What does projects under schedule pressure do to Developers? In Drive, the author Daniel H. Pink says that, when you get the pressure involved or a reward involved, your mind goes into a kind of a transaction mode. And the problem solving part of the brain is kind of turned off. So your puzzle solving or the problem solving skills actually affected. This is the way our mind works.

Biggest paradigm shift

A convincible lifecycle of an estimate would be Size, Effort, Data, and Schedule. The biggest paradigm shift that people have in trying to get better in estimating, is to Learn to think in Size. What you need for Sizing is an Unit and a Measure.

Why Software Estimation Sucks2

People have enjoyed certain amount of success trying to solve this puzzle for quite a while using various kinds of matrix such as counting the Line of Code, Function Points, Pages of Requirements, Screens or Web Pages etc. but its been in the last few years I have actually seen a method that I think that might actually work and that’s Story Points.

Always keep in mind to re-estimate when there is more visibility on where are you are heading. Here are some simple steps to start loving Software Estimates :)

Software Estimations Sucks3

  1. Information required: Approved Business Requirements.
  2. Multiple people estimate separately, then meet to review and revise.
  3. Cone of uncertainty applied: (-50%, +100%).
  4. This is only an Estimate only.
  5. Re-estimate after Detailed Requirements Phase.

You can apply these steps on any software development method you like.

More readings;

Drive: The surprising Truth about What Motivates Us by Daniel Pink
  • Drive: The Surprising Truth About What Motivates Us by Daniel H. Pink
  • Software Estimation: Demystifying the Black Art by Steve McConnell
  • Agile Estimating and Planning by Mike Cohn