Thursday, January 19, 2012

Not All RRSP Contributions Are Equal

Ah, January, when thoughts turn to taxes.  I still do my own because my situation is pretty simple.  My yearly routine is to put on Ace Ventura Pet Detective and fill out forms for me and my wife.  Since switching away from paper forms a few years ago to the fabulous and free StudioTax I’ve been able to watch more of the movie.  StudioTax also has a special place in my heart because it’s written in .NET.  I’m guessing the name comes from the development tool .NET folks use, Visual Studio.

This post isn’t about StudioTax, though.  It’s about something I’m sure I’ve understood from newspapers and magazines turning out to be wrong: that your entire RRSP contribution results in a rebate equal to your highest tax rate.  This is the reason tax planners sometimes recommend not contributing to an RRSP until you are in a high tax bracket, like the last 2 federal ones.  You don’t want to waste getting about 40% back by contributing when you’ll only get about 32% back.  The advice is to set aside the money until you reach one of these brackets, just not in an RRSP.  These days that place would be a TFSA account.

It’s not so simple, though, as waiting until your total income moves you to a higher tax bracket and then plowing all of your savings into an RRSP account that year.

The Math

Disclaimer: I am not a tax planner, accountant or financial consultant. There are lots of other considerations than those below to make when deciding what’s right for your situation.  I’m just exploring something I came to realize recently, which applies to my situation.

The Canadian income tax system is what’s called progressive.  Not progressive in the “this is the better way” sense, but in the “progression from one level to another” sense.  Your entire income isn’t taxed at the same rate.  The first $x is taxed at a certain level, the next $y is taxed at a higher level, the next $z at an even higher level.  The feds have their own set of x, y, z values and rates, as does each province.  Except Alberta where there is no provincial income tax.

I’ll use small, hypothetical amounts for this discussion, and assume there are no automatic tax credits, as there are in the real tax system.

Let’s say the brackets and rates are:

Income Rate
First $50 15%
Next $50 25%

You make $60 a year.  That means you owe:

Income Tax Calculation Amount
On the first $50 $50 x 0.15 $7.50
On the next $10 $10 x 0.25 $2.50
  Total $10.00

Your employer would have taken this from your pay cheque, so you’ve already paid.  If you don’t do anything more to do with taxes you owe nothing and you’ll get nothing back.

Every dollar you contribute to an RRSP (or pension plan for that matter) lowers your taxable income by 1 dollar.  Let’s do the same calculation but with a contribution of $4.

Total Income $60
RRSP Contribution $4
Taxable income $60 – $4 = $56

Plug $56 into the tax calculation table and you get:

Income Tax Calculation Amount
On the first $50 $50 x 0.15 $7.50
On the next $6 $6 x 0.25 $1.50
  Total $9.00

You’ve paid $10 through your pay cheque, you owe $9, so you get a rebate of $1.  This is equal to your contribution times the higher rate, or $4 x 0.25 = $1.

However, if your contribution lowers your income into the next bracket down, the entire contribution is not worth 25%.  Only the portion that lowers your income to the edge of the higher bracket is worth the higher bracket’s rate.  The remaining portion’s rebate rate is the rate of the lower bracket.

To demonstrate, lets say you contribute $15 of your $60 income.

Total Income $60
RRSP Contribution $15
Taxable income $60 – $15 = $45

Now you owe:

Income Tax Calculation Amount
On the first $45 $45 x 0.15 $6.75
  Total $6.75

You’ve paid $10, you owe $6.75, so you’ll get a rebate of $3.25.  Note that this is not equal to your contribution of $15 times the higher rate, or $15 x 0.25 = $3.75.  This is because it’s a combination of the potion that brought your income down to $50 at the high rate, plus the remaining portion at the low rate.  That is:

Contribution Rebate Calculation Amount
Portion to get to $50 $10 x 0.25 $2.50
Remaining portion $5 x 0.15 $0.75
  Total $3.25

Conclusion

If you’ve been waiting to make contributions until you’re in a higher bracket, and you’ve finally reached that bracket, it’s not really worth contributing more than is enough to bring your income down into the next bracket.  In the example above, it’s not worth contributing more than $10 of the $15 to bring your income down to the lower rate.  Any more contributions will only earn a rebate of 15%.  You’re better to hold on to any other savings to use in future years in hopes that your income rises faster than inflation.

Friday, January 6, 2012

Web Client Software Factory vs. Code Metrics

 

I’ve been building a web application with the Microsoft Web Client Software Factory (WCSF), and it has an interesting default option.  The option is to put interface definitions in an assembly separate from the implementation when creating a new module.  This will generate 2 projects for each module, which means at least 2 namespaces, if you don’t change anything.  This post will investigate whether the code metrics generated by this approach, specifically abstractness and instability as defined by NDepend, are worth the increased maintenance compile time.

The option can be seen here when creating a foundational module in WCSF:

image

WCSF also presents this option when creating a business module, although it still places the interface for the module controller in the “concrete” project, not the interface project.

The effect is that you’ll end up with 2 new projects, as seen in the picture above; Blah, and Blah.Interface.  The thinking is that the projects that use the services of the module need reference only the interface project.  Dependency injection will take care of locating the actual implementations.  A rather high-minded architectural choice, especially considering that for foundational modules WCSF doesn’t even add a reference to the interface project to the web project.

Let’s say you follow the intention, though, and add some interfaces to Blah.Interface, some services that implement them to the Blah project, and some views in the web app that use the interfaces.  WCSF’s behaviour aside, what’s of real interest to me is how NDepend scores these 2 projects on the abstract/instability graph:

AbstractnessVSInstability5

Blah is right near the WebClient5 assembly.  It’s perfectly instable, meaning no types depends on its types directly, and perfectly concrete, or non-abstract, because it has no abstract types.  It just implements abstract types.

Blah.Interface is perfectly abstract because it has only abstract types, and is very “stable” because while lots of types depend on its type, its types (interfaces) do not depend on anything else.  (NDepend claims Blah.Interface has 1 external type dependency, but I don’t know what.  If I eliminated this dependency then Blah.Interface would be perfectly stable.)

Now, compare the graph of the same project refactored to merge Blah and Blah.Interface:

AbstractnessVSInstability5NoInterface

Blah’s abstractness has increased and it’s “stability” has decreased.  These in themselves don’t mean much, but their relative values are still well within the area closest to the ideal dotted line. 

Given that NDepend suggests that this approach won’t result in pain or uselessness in this simple case, I conclude that the 2-project approach is not worth the extra maintenance.  This approach should be used only when it solves an actual problem, not by default for every module.

Thursday, October 27, 2011

Occupy Stack Overflow!


image
Comparing cumulative reputation of users sorted by reputation vs. number of users.  Source:  StackOverflow Sept 2011 data dump file stackoverflow.com.7z, users.xml

Did you know that the top 1% of StackOverflow.com users own just under 60% of all reputation?  While the rest of us answer questions but seemingly get no farther ahead, Jon Skeet and his ilk get farther and farther ahead.  This situation is intolerable!

My modest proposal:  the bottom 99% of StackOverflow.com users occupy the site by posting questions with the text “We are the 99%” and tagging them with OccupyStackOverflow or OSO.  If you see these questions upvote them, then answer it with anything, like “Me too”, and then upvote all the other answers.  Power to the people!

If you don’t realize yet, I’m kidding about Occupy Stack Overflow.  But I’d like to compare the “inequality” on Stack Overflow with the “inequality” in incomes that the Occupy Wall Street movement appears to be concerned about, among other things. 

Stack Overflow’s design is such that almost every point of reputation is earned. There may be people who run around the site automatically voting up all of Jon Skeet or Mark Gravell’s answers, but for the most part the top 1%’s scores are a function of the long term consistent quality of their answers. We contributed to their scores because they answered our questions well and made our jobs easier. The top 1% hold 60% of the reputation but they’ve given us their knowledge in exchange.

Compare that to the top 1% of income earners in 2011.  Unless they’ve literally stolen money from others, or were given no-strings-attached money by the government to prevent their folly from impacting their own company, they owe us NOTHING. And I say still because they’ve already given us a lot. They’ve given us cheaper computers and smart phones and gasoline cheaper than beer every 10 blocks, access to our money almost everywhere, cars that go farther on less gas with less problems, and warmer/cooler/dryer houses. The Internet, entertainment, like TV shows, sports and video games.  Bicycles get better every year.  Drugs and technology that let us live longer lives with more healthy years.  Labour saving devices like dishwashers, frostless fridges, washing machines and dryers.  Clean drinking water.  Indoor toilets.  Ikea furniture.

Warren Buffet, LeBron James and Steve Jobs didn’t take their fortunes from anyone, and they weren’t handed it by the government.  They created wealth for others, entertained others and brought people incredible technological advances.  Their fortunes are not only the result of how good they are at their jobs, but also how many people can afford what they produce. 

Just like the top 1% of Stack Overflow’s users they translated their talents into rewards for themselves and a better life for many other people.  This is the invisible hand at its best.  To the 1% I say, keep up the good work!

P.S. I created a query on data.stackexchange.com that lets you find the percent of reputation found by the top x% of users here: data.stackexchange.com/stackoverflow/s/2001/reputation-inequality.

Monday, October 24, 2011

Music

Imagine it’s 1805 and you’re the Emperor of Austria.  You’ve commissioned a composer for a symphony.  He returns after just 3 days and delivers something he came up with by playing random notes on random instruments as fast as possible.

“Dies ist nicht Musik!” you say in German.  “This is not music!”

“Was?” replies the genuinely shocked composer in German.  “What?  These are all real musical notes on these sheets.  I certainly didn’t just pick random frequencies.   Did I not write parts for orchestral musical instruments?  I didn’t bang on trash bins and play empty jugs.”

“There’s no structure to it.  No melody, no chords, no counterpoints between instruments, no use of time signatures.  Just a long run-on sequence of random notes.  There’s nothing that we’ve learned about making orchestral music, or music in general, for the last 500 years.  I won’t pay,” you say.

“I’ll take you to court.  You asked for music and I delivered music,” threatens the composer.

(Let’s pretend this is a very democratic 1805 Austria and common citizens really can take the emporer to court and hope to win.)

“No reasonable person would consider what you’ve created music.  No musical theory would hold this as an example of an expression of that theory.”

The composer stews for a moment and then realizes that he’s out of his league.  He hangs his head and then looks up.

“I’ve seen symphonies performed.  They’re so complex and there’s so much structure to learn.  How can I even hope to do this in a reasonable time?” asks the composer.

“PRACTICE!”

A Real Life Example

I’ve seen real line-of-business web applications that are just page after page of this kind of thing:

ASPX File

<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
</asp:GridView>
</div>
</form>
</body>


Code behind

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BoundField bf = new BoundField();
bf.DataField = "Title";
bf.HeaderText = "Title";
GridView1.Columns.Add(bf);
bf = new BoundField();
bf.DataField = "Year";
bf.HeaderText = "Year";
GridView1.Columns.Add(bf);
HyperLinkField hlf = new HyperLinkField();
hlf.HeaderText = "IMDB Link";
hlf.DataTextField = "ImdbUrl";
hlf.DataNavigateUrlFields = new string[] { "ImdbUrl" };
GridView1.Columns.Add(hlf);
using (ServiceReference1.MovieServiceClient mc = new ServiceReference1.MovieServiceClient())
{
var mvs = mc.GetMovies();
DataSet1.MovieDataTable mt = new DataSet1.MovieDataTable();
foreach (var mv in mvs)
{
var mr = mt.NewMovieRow();
mr.Title = mv.Title;
mr.Year = mv.Year;
mr.ImdbUrl = mv.ImdbUrl;
mt.AddMovieRow(mr);
}

GridView1.DataSource = mt;
}

GridView1.DataBind();
}
}


What?  It’s C#.  It compiles.  It even produces the required output.


If you are thinking these things without irony and sarcasm then please, please, please keep reading to find out why they’re ironic.


(I’m actually getting angry writing this code because of the pain I endure looking at this garbage every day.)


A difference between code and music is that even though the output that the code produces is correct the underlying stuff that produced it can fail to conform to any accepted theory of structure, called design patterns, or best practices, like the SOLID principles of object oriented programming.  It’s like using auto tune to correct someone who never learned to sing.  You just speak into the mic and a (somewhat) pleasing sound comes out the other end.


Another difference between a song and a program is that you don’t have to add things to a song or change it after it’s been finished.  The above program is fine to illustrate a point, like I’m doing, but it is not acceptable for any program that people will actually use.  They’ll eventually need a change or find a bug that will require updates.


A problem with this code, but not the main one, is the obtuse variable names.  What’s GridView1 supposed to represent?  Oh, a list of movies.  Let’s call it MovieGridView then.  Mc?  Oh, it’s the data service client.  Let’s call it movieServiceClient.  Bf used again and again to represent different things?  If we really need to add columns dynamically to a grid view how about separate instances with distinct names.  Mvs, mt, mr, mv?  Wtf?  Poor variable names make code much, much harder to maintain than it needs to be.


Here’s the same code with just the variable names updated:


ASPX File

<body>
<form id="MoviesForm" runat="server">
<div>
<asp:GridView ID="MoviesGridView" runat="server" AutoGenerateColumns="false">
</asp:GridView>
</div>
</form>
</body>


Code behind

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BoundField titleField = new BoundField();
titleField.DataField = "Title";
titleField.HeaderText = "Title";
MoviesGridView.Columns.Add(titleField);
BoundField yearfield = new BoundField();
yearfield.DataField = "Year";
yearfield.HeaderText = "Year";
MoviesGridView.Columns.Add(yearfield);
HyperLinkField imdbUrlField = new HyperLinkField();
imdbUrlField.HeaderText = "IMDB Link";
imdbUrlField.DataTextField = "ImdbUrl";
imdbUrlField.DataNavigateUrlFields = new string[] { "ImdbUrl" };
MoviesGridView.Columns.Add(imdbUrlField);
using (ServiceReference1.MovieServiceClient movieServiceClient = new ServiceReference1.MovieServiceClient())
{
var moviesFromService = movieServiceClient.GetMovies();
MovieDataSet.MovieDataTable movieTable = new MovieDataSet.MovieDataTable();
foreach (var movie in moviesFromService)
{
var movieRow = movieTable.NewMovieRow();
movieRow.Title = movie.Title;
movieRow.Year = movie.Year;
movieRow.ImdbUrl = movie.ImdbUrl;
movieTable.AddMovieRow(movieRow);
}

MoviesGridView.DataSource = movieTable;
}

MoviesGridView.DataBind();
}
}


Now other folks’ brains don’t need to work nearly as hard to understand the intent.  We’re still using a DataSet to store non-SQL data internally, which is depraved, but I’m just mirroring what I see every day.  I also didn’t bother to re-add the service reference with a better name.


But here’s the biggest problem with this code:  no separation of concerns.  I’m talking about the S in SOLID, the Single Responsibility Principle.  This states that a class should be responsible for only 1 aspect of the application.  “A class should have 1 and only 1 reason to change”. 


In general, a “data over textboxes” app like the one above has 3 main aspects:



  1. moving data to and from a source
  2. formatting data/interpreting user input
  3. presenting data.

A single class currently handles all 3 of these aspects, the code behind.  Oh, you didn’t realize that it’s a class and should be treated like any other?  The declaration “public partial class” before the name of the page in the code behind wasn’t a clue?


Multiple aspects will require a change to this 1 class – a change in the data source, a change in the internal data model (the DataSet), a change in formatting of an existing field, or a requirement to get updated data some time after the initial page load.


There should be at least 3 more classes:



  1. One with a method named something like GetDataForInitialPageLoad that takes the page as an argument.  This will be called from within the if (!IsPostBack) block and pass the page in using the “this” keyword.  The method will use the next class to get data and wire it to the page’s GridView.  This class houses business logic.
  2. One with a method named something like GetMovies that retrieves data from the service and uses the next class to convert it from the service’s format to a DataTable.  GetDataForInitalPageLoad would call this method.
  3. One with a method named something like Convert that takes the service’s output as input and returns a DataTable.

In addition, the GridView fields being added in code should be specified in the ASPX file.


Now, if any 1 of the aspects I mentioned above change, only 1 class needs to change and you know no other aspect of the app has changed.  This cuts down on the testing needed and dramatically lowers the odds of inadvertently introducing a change in an unrelated aspect.  It also sets you up to follow the rest of the SOLID principles and allows unit testing.


My favorite pattern for separating concerns when developing WebForms is Model-View-Presenter.  The ASPX page and code behind are the View, some business logic class specific to the view is the Presenter, and the DataSet, converters and service comprise the Model.  The refactored version of this app using MVP follows below.  You’ll see that there’s not much more code, but there are more classes and interfaces.  The biggest leap to make is grasping the concept of the Single Responsibility Principle and the way the presenter and view share things.


But after you grasp it, you have to try implementing it again and again until it becomes second nature.  Learn the VS2010 shortcut keys for creating classes and code snippets for adding properties.  It’s a pattern; it’s not different every time.  Once you learn the pattern it will flow from your brain to your code just as quickly as mvs=mc.GetMovies().


Conclusion


Whether you’re the Emperor of Austria in 1805 commissioning a symphony or a private citizen in 2011 commissioning a piece of art you wouldn’t accept something that didn’t follow some well known precepts of the medium in which your artist works. No true artist would try to deliver something that they didn’t feel was an expression of some theory which they’d studied, if only a little. The more studied and practiced the artist the quicker they can produce works that conform to that theory. The top notch ones even expand on the theory.


So why is is that people who call themselves programmers think it totally acceptable to produce code that meets requirements but follow none of the best practices determined over the last 40 years?


I once sat in a class where the instructor went over the Model-View-ViewModel pattern in Silverlight and how, like any pattern that separates concerns, it leads to much more maintainable code. A “developer” put up his hand and asked “Yeah, but what if you’re trying to meet a deadline?” The instructor replied, “The more often you implement this the faster you’ll get".”


In other words, PRACTICE! 


Code Plz


The ASPX File

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="NotTheWayItsDone.aspx.cs"
Inherits="Wrong.NotTheWayItsDone" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="MoviesForm" runat="server">
<div>
<asp:GridView ID="MoviesGridView" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Title" HeaderText="Title" />
<asp:BoundField DataField="Year" HeaderText="Year" />
<asp:HyperLinkField DataNavigateUrlFields="ImdbUrl"
DataTextField="ImdbUrl" HeaderText="IMDB Link" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>


Reasoning


The ASPX file is all about display.  Unless you’re adding columns dynamically to the GridView, specify here which data go in which fields and how they should be formatted.  Wire up client-side JavaScript calls here.  Use HTML class attributes to let CSS do its thing.  You get a pretty good preview of what things will look like.  You’ll go from this:


image


to this:


image


You can also start splitting the work between developers without everyone working on the same file.


Code behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using Wrong.ServiceReference1;

namespace Wrong
{
/// <summary>
/// Really this is a page that displays a list of movies
/// </summary>
public partial class NotTheWayItsDone : Page, IMoviesView
{
/// <summary>
/// Class that contains business logic
/// </summary>
private readonly MoviesPresenter presenter;

/// <summary>
/// Initializes a new instance of the NotTheWayItsDone class.
/// </summary>
public NotTheWayItsDone()
{
// Pass this page instance to the presenter. Specify the IMovieService implementation to use, as per Inversion
// of Control principle.
// The presenter sees only the items defined in IMoviesView
this.presenter = new MoviesPresenter(this, new MovieServiceClient());
}

/// <summary>
/// Handles the Load event of the Page control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.presenter.OnViewInitialized();
}
}

/// <summary>
/// Populates the movies onto the view.
/// </summary>
/// <param name="movies">The movies.</param>
public void SetMovies(MovieDataSet.MovieDataTable movies)
{
this.MoviesGridView.DataSource = movies;
}
}
}


Reasoning


The code behind file’s job is to react to events from the user and communicate data to and from the Presenter, which implements business logic.  That’s all.  No retrieving data.  No left hand-right hand code transforming data from one format to another.  No adding visual elements that don’t appear conditionally. 


It shares information with the Presenter by implementing an interface that the presenter knows about.  In this case, a method that allows the Presenter to hand the GridView some data, and we’ve exposed Page.DataBind.


The presenter doesn’t know that a GridView will receive the data.  It’s the View’s job to present the data to the user properly.  We could change the View to use a ListView and the presenter would be none the wiser.


We’ve exposed DataBind so that the Presenter can let the View know when it’s time to bind.  This is useful if you’re not using DataSource-type objects to receive data in your view, which would normally detect changes to source data and call DataBind for you.


Another class should implement business logic, like “when save is clicked, gather entered data and put it in the repository.” 


The View Interface

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

namespace Wrong
{
public interface IMoviesView
{
/// <summary>
/// Populates the movies onto the view.
/// </summary>
/// <param name="movies">The movies.</param>
void SetMovies(MovieDataSet.MovieDataTable movies);

/// <summary>
/// Causes the view to bind data-bound controls to their source data.
/// </summary>
void DataBind();
}
}


Reasoning


The documentation in the interface and the code behind reasoning should suffice.


The Presenter

using System;
using System.Collections.Generic;
using System.Linq;
using Wrong.ServiceReference1;

namespace Wrong
{
/// <summary>
/// Contains business logic for the movies view (page)
/// </summary>
public class MoviesPresenter
{

/// <summary>
/// Initializes a new instance of the <see cref="MoviesPresenter"/> class.
/// </summary>
/// <param name="view">The view.</param>
/// <param name="movieService">The movie service.</param>
public MoviesPresenter(IMoviesView view, IMovieService movieService)
{
this.View = view;
this.MovieService = movieService;
}

/// <summary>
/// The movie service
/// </summary>
protected IMovieService MovieService { get; private set; }

/// <summary>
/// Gets or sets the view.
/// </summary>
/// <value>
/// The view.
/// </value>
protected IMoviesView View { get; set; }

/// <summary>
/// Called by the view when it is first loaded.
/// </summary>
public void OnViewInitialized()
{
var moviesFromService = this.MovieService.GetMovies();
MovieToMovieDataSetConverter converter = new MovieToMovieDataSetConverter();
using (MovieDataSet.MovieDataTable movieTable = converter.ConvertAllFrom(moviesFromService))
{
this.View.SetMovies(movieTable);
}

this.View.DataBind();
}

/// <summary>
/// Releases unmanaged resources and performs other cleanup operations before the
/// <see cref="MoviesPresenter"/> is reclaimed by garbage collection.
/// </summary>
~MoviesPresenter()
{
IDisposable moviesServiceAsDisposable = this.MovieService as IDisposable;
if (moviesServiceAsDisposable != null)
{
moviesServiceAsDisposable.Dispose();
}
}
}
}


Reasoning


This is where all the action is.


The constructer requires an instance of something that implements IMovieView.  Typically this is the page, but for unit testing purposes you could pass in an object that implements the same interface.  It also needs the movie client implementation specified to it.  (WCF ) This is an example of the Inversion of Control principle.


In the destructor we need to check if the movie client requires disposing.  The real WCF client does, but any pretend versions used for unit testing doesn’t necessarily.


The Converter

using System;
using System.Collections.Generic;
using System.Linq;
using Wrong.ServiceReference1;

namespace Wrong
{
class MovieToMovieDataSetConverter
{
/// <summary>
/// Converts all from.
/// </summary>
/// <param name="moviesFromService">The movies from service.</param>
/// <returns>A MovieDataTable populated with the data from moviesFromService</returns>
public MovieDataSet.MovieDataTable ConvertAllFrom(Movie[] moviesFromService)
{
MovieDataSet.MovieDataTable result = new MovieDataSet.MovieDataTable();
foreach (Movie movie in moviesFromService)
{
result.AddMovieRow(this.ConvertFrom(movie, result));
}

return result;
}

/// <summary>
/// Converts from.
/// </summary>
/// <param name="movie">The movie.</param>
/// <param name="movieTable">The result.</param>
/// <returns>A MovieRow based on the movieTable parameter and populated with the data from the movie parameter</returns>
public MovieDataSet.MovieRow ConvertFrom(Movie movie, MovieDataSet.MovieDataTable movieTable)
{
var movieRow = movieTable.NewMovieRow();
movieRow.Title = movie.Title;
movieRow.Year = movie.Year;
movieRow.ImdbUrl = movie.ImdbUrl;
return movieRow;
}
}
}


Reasoning


This class’s only function is to populate a DataTable with incoming data.  If the incoming data type or the DataTable changes then only this class needs to change.

Wednesday, September 14, 2011

ASP.NET 4.0 Menu Outputs a Table or Anchors Instead of an Unordered List

 

The Short Answer (In My Case, Anyway)

A configuration setting in web.config is causing the .NET 3.5 behaviour of outputting a table or a set of anchors.  The setting is:

<pages controlRenderingCompatibilityVersion="3.5" />

Remove this to get the desired 4.0 behaviour of building an unordered list. Visual Studio added this because you created your project using .NET 3.5 then changed the version to 4.0. Um, thanks, VS.


The Long Answer


I’ve spent the last few days trying to understand why my instance of a Menu control was creating a table and not an unordered list. It’s a .NET 4.0 site and every test 4.0 app I’ve created has produced an unordered list.


I thought maybe it was because I was using a SiteMapDataSource. I tried defining the items within the Menu control, but this made no difference.


Maybe it was because I was using WCSF to create populate the SiteMapDataSource. So I created a new WCSF-based project and found it output an unordered list. So nothing in WCSF was triggering the behaviour. I studied the code generated in a new ASP.NET 4.0 website (the non-empty template) and in a new WCSF and I couldn’t find anything different.


Some Googling finally revealed that certain controls look at a configuration setting called controlRenderingCompatibilityVersion to decide how to act. This isn’t normally present. It gets set when you change your web project from 3.5 to 4.0. Once I read this I remembered accidentally creating my app targeting 3.5 and then switching to 4.0.


This appears to be the only significant change to a web app when switched to 4.0.  Lots of entries disappear from web.config. This is probably because 3.5 is really a bunch of extensions to 2.0, and all of the extensions are now just part of 4.0. 


This config setting sounds like it will make Menu act the same way under 4.0 as 3.5, but it turns out that’s not true.  Native 3.5, 3.5 “compatibility” under 4.0 and native 4.0 Menus all produce unique HTML.  Let’s have a look.


Create a new 3.5 ASP.NET website (the non-empty template) in Visual Studio 2010 and add:


  1. A Sitemap file with the default name of Web.Sitemap.  Make the contents
    <?xml version="1.0" encoding="utf-8" ?>
    <siteMap>
    <siteMapNode title="Home" >
    <siteMapNode title="Services" >
    <siteMapNode title="Training" url="~/Training.aspx"/>
    </siteMapNode>
    <siteMapNode title="Services" >
    <siteMapNode title="Training" url="~/Training2.aspx"/>
    </siteMapNode>
    </siteMapNode>
    </siteMap>
    

  2. A SiteMapDataSouce in Default.aspx, e.g.
    <asp:SiteMapDataSource ID="SiteMapDataSource1" Runat="server" ShowStartingNode="false" />

  3. A Menu control in Default.aspx, e.g.
    <asp:Menu runat="server" ID="MenuTest" DataSourceID="SiteMapDataSource1" Orientation="Horizontal">

Now run it and take a look at the source:

<span><a class="MenuTest_1" href="javascript:__doPostBack('MenuTest','oServices')">Services<img src="/WebResource.axd?d=wAEvwChqppIeDD27eosuLSSKK1v72lmXTc50uPK75upm7hfsxNhE9GeVh7SSC19_P7eqejSkCMIMAhKEvlwbQTVPYtk1&amp;t=634244938920000000" alt="Expand Services" align="absmiddle" style="border-width:0px;" /></a></span>
<span><a class="MenuTest_1" href="javascript:__doPostBack('MenuTest','oServices')">Services<img src="/WebResource.axd?d=wAEvwChqppIeDD27eosuLSSKK1v72lmXTc50uPK75upm7hfsxNhE9GeVh7SSC19_P7eqejSkCMIMAhKEvlwbQTVPYtk1&amp;t=634244938920000000" alt="Expand Services" align="absmiddle" style="border-width:0px;" /></a></span>

A messy set of spans and anchor tags.

Upgrade the project to 4.0, run it again and look at the source code:

<table id="MenuTest" class="MenuTest_2" cellpadding="0" cellspacing="0" border="0">
<tr>
<td onmouseover="Menu_HoverStatic(this)" onmouseout="Menu_Unhover(this)" onkeyup="Menu_Key(event)" id="MenuTestn0">
<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<td style="white-space:nowrap;"><a class="MenuTest_1" href="#" style="cursor:text;">Services</a></td>
<td style="width:0;"><img src="/WebResource.axd?d=Jl7bM-JFbdoWBEh7C2q3EyZR8K6O6d4YLATzREzpY2ex3k5XYksROon9Z1cxmO8alNxDSkn-ofGqiqw-z6u74-AwPDdR9USUgzqIlqL9sBA1&amp;t=634486182303750217" alt="Expand Services" style="border-style:none;vertical-align:middle;" /></td>
</tr>
</table>
</td>
<td onmouseover="Menu_HoverStatic(this)" onmouseout="Menu_Unhover(this)" onkeyup="Menu_Key(event)" id="MenuTestn1">
<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<td style="white-space:nowrap;"><a class="MenuTest_1" href="#" style="cursor:text;">Services</a></td>
<td style="width:0;"><img src="/WebResource.axd?d=Jl7bM-JFbdoWBEh7C2q3EyZR8K6O6d4YLATzREzpY2ex3k5XYksROon9Z1cxmO8alNxDSkn-ofGqiqw-z6u74-AwPDdR9USUgzqIlqL9sBA1&amp;t=634486182303750217" alt="Expand Services" style="border-style:none;vertical-align:middle;" /></td>
</tr>
</table>

Now it’s a messy table.

Finally, remove the controlRenderingCompatibilityVersion setting from web.config and look at the source:

<ul class="level1">
<li><a>Services</a><ul class="level2">
<li><a class="level2" href="/Training.aspx">Training</a></li>
</ul></li><li><a>Services</a><ul class="level2">
<li><a class="level2" href="/Training2.aspx">Training</a></li>
</ul>
</li>
</ul>

A nice, clean unordered list.

I really don’t understand why the controlRenderingCompatibilityVersion setting doesn’t produce exactly what a 3.5 Menu does, but I don’t care that much. I just want my unordered list, and now I know how to get it.

This is the kind of thing for which people criticize ASP.NET WebForms. Some people want almost complete control over the HTML that a framework produces, which leads them to ASP.NET MVC, Ruby on Rails et al. Those are great, but WebForms hasn’t grated on me that much in my life so I‘m nowhere near ready to give up on it. People’s lack of understanding of event-based web programming and getting data directly from the page code-behind has lead to far worse headaches for me.

MS Test, the unit testing framework built into Visual Studio, on the other hand…

Sunday, July 10, 2011

Four Types Of Programmers

 

I recently rewrote some code that calculates the distance between 2 latitude/longitude coordinates.  I was using a method that used Pythagoras, which assumes Earth is flat.  Apparently this works well for distances under 20 km, but my app needs to deal with all of Ontario, which is larger that 20 km.  I came across the haversine formula, which assumes a spherical Earth.  Still not 100% accurate (the Earth is an ellipsoid, and has mountains and valleys) but much closer.

Some of the example implementations I found in JavaScript (about 1 screen down) and C# were oddly similar.  Same variable names, no code comments that indicate an understanding of the algorithm.

I sat with the Wikipedia article open in one window and the JavaScript version on the other until I understood what was going on.  (Basically,  haversin(x) = (sin(x/2))^2 so you can use a language’s sin function to calculate it.)  Though, I still don’t understand why atan2 is used to calculate the inverse of haversin and not arcsin (or even what atan2 does).  My inverse implementation uses arcsin, but I wrote a parallel one that uses atan2 and gives the same result for the few unit tests I wrote.  Given that these are trig functions I ‘m pretty confident this isn’t a coincidence. 

I was happy with my implementation, but a few things still bothered me.  What’s the relationship between atan2 and arcsin?  What’s the derivation of the haversine formula?  It should be high school or first year trig, but I don’t get it.  I could probably keep Googling and refresh my knowledge at Khan Academy, but I’ve spent enough time for now.  I have a house and family to tend to too.  (This was a personal project so no employer time was harmed in the writing of this article.)

But it got me wondering about the thinking behind the C# example I found vs. what I did.  I think you could tell the difference between 4 types of programmers with this interview question:

You’re writing an app that needs a mathematical algorithm.  Nothing too fancy, but the math is just a little beyond you because it’s been 10 to 15 years since you really understood trig/calculus/algebra/stats.  You Google for it and find the following:

  1. Info on the theory behind the algorithm on, say, Wikipedia
  2. Some code example in your programming language on various websites

How do you proceed?

This shouldn't be presented as multiple choice.  You’re looking for answers of the following types:

A.  Copy the code examples into your code and move on.
B.  Use the code examples as inspiration for you own version, perhaps with a little refactoring and variable renaming and move on.
C.  Use the Wikipedia article to write your own implementation from scratch, adding comments to document what each complex bit does, and move on.
D.  Do C and spend some time trying to understand how the algorithm was derived and why it works.

If you answered A you now have 2 problems.  You now have code that you can’t support because you don’t understand it, and it’s probably a copyright violation.  SCO will find you.  Next applicant please.

If you answered B you’ve avoided the copyright problem but you still don’t understand the algorithm.  Junior programmer material.

If you answered C, nice work.  You have code that you understand and that others can understand.  It’s a well known algorithm so it’s not vital that you can explain why it works.  Just keep the URLs to your research handy, or add them to your comments for justification.  Welcome, new senior developer.

If you answered D there is a follow up question – how much time did you spend?  An hour or two, or did you get lost down a rabbit hole and spend a whole afternoon (as I did)?  If you spent just a couple of hours you get the team lead job, whether you stopped because you figured things out or you knew enough to not spend any more time.  If you spent a whole afternoon, you still get the senior developer position, but you need to learn some hardcore time management skills before advancing.

Diving deep into a problem is pretty typical of good coders but the very best keep this tendency in check.  This is where a personal system of organization can save you from rabbit holes.  Or at least let you get your code out the door and look into the lingering details at an appropriate time.

 

PS:

I now understand the relationship between arcsin and atan2, and why the JavaScript examples are written the way they are.

First, the inverse of haversin(x) is

haversin-1(x) = 2sin-1(√x)

Second, the relationship between arcsin and arctan is

sin-1(x) = tan-1(x/√(1-x2))

So,

haversin-1(x)= 2tan-1(√x/√(1-x))

When x > 0 the atan2(y,x) function in most languages gives you

tan-1(y/x)

So,

haversin-1(x) = 2*atan2(Math.Sqrt(x)/Math.Sqrt(1-x))

I suppose this is a handy definition for those languages that don’t have an inverse sine function but do have inverse tan.

I still don’t understand why the haversine function works, but that’s fine.  I’m now confident my implementation that uses inverse sine is equivalent and I’m moving on.

Friday, July 1, 2011

The Three Types of Project Documents

 

All project documentation can be given one of the following classifications:

  1. Bridesmaid dress
  2. Christmas tree
  3. Monument

The first 2 were inspired by a line from Fight Club (about 1/4 of the way down).  Here’s how to tell which type you’re working on, with examples and how much time you should spend on each.

 

Bridesmaid Dress

A bridesmaid dress is important only to the bride, costs serious money and is thrown away after 1 wearing.  A bridesmaid dress document is a document that is just as expensive to create as any other document, whose sole purpose is to fulfill a process requirement, and read only once, if at all.

Like any other project document someone has to spend time filling in the control sections (e.g. version history, links to other docs, approver list, distribution list), in addition to crafting the real content.  The control sections take longer to fill out than the real content.  The content usually comes from a source to which project members already have access and could look up themselves if they really cared.  Project members must attend a meeting to review it.  Real time is spent, not just 1 person’s afternoon.

It is important only to the project manager because she is the one responsible for championing the process.  Mind you, it’s not her fault that it has to be completed, she’s just doing her job.  No one else really cares about it, though.

Only a few lines are relevant; many other pages won’t be read.  Even the relevant sections will be read only once, and only by the people for whom it really is relevant.  It might require 5 people to sign off, but only 1 will actually care about the contents.  Bridesmaid dress documents could easily be replaced with a maximum 10 line email, but no one creates template emails for projects.  Only a 10+ page Word template with a ton of control content will do.

An example is a test stage exit report.  Only the page with the defect status count summary is read by those interested.

As little time as possible should be spent crafting such a doc.  If you are starting from a template leave as many sections “N/A” as you can.  Only fill them out if someone asks, and even then with the bare minimum.


Christmas Tree

A Christmas tree costs time and money to assemble.  But the cost is borne because it is vital to those celebrating Christmas, and everyone benefits.  It’s enjoyed for weeks at a time, but after Christmas is thrown away or put back in the box for another year.

As with any project documentation, it costs real time and money to produce.  The template has all the same control sections as any other doc, like the bridesmaid dress doc, but the content takes much longer to produce than the control sections.  Team members review it, make changes that make it better and really read it before signing off.  People spend serious time on the doc but it is well justified.

A document of this type is a real necessity for the project.  Without it you couldn’t have the project.  People look at multiple sections multiple times throughout the duration of a project because it has useful, important information that project members must reference .  This doc is the first time in which this content appears – it’s not just a summary of other content.  The information feeds people’s efforts.  But it’s context is still limited to the project itself.

At the end of the project, though, the document itself is forgotten.  The content itself should be incorporated into a monument document.  Failure to do so will cause many future headaches because information is spread across multiple documents that no one can find.

An example is project business requirements for an application or system.  There’s nothing for a project to do without requirements.  They’re specific to the project, though, so they only express the changes, not the whole application or system.  By themselves they’re pretty useless to other projects, especially after the same requirement has been changed in a few projects.  They need to be added to a monument document to make sense across projects.

It’s totally appropriate to spend time on a Christmas tree document.  Everyone must remember, though, that it will be tossed aside or put away after the project and that the content must be put into a monument document to stay relevant.  Link to as many other project and monument documents as possible – don’t copy and paste content.  This is a waste of your readers’ time, and you’ll just have to spend time updating your copy when the source content changes.

Monument

A monument costs a lot of money, is relevant to many people and is used and lasts a long time.  Think war memorials, gravestones, the pyramids.

A monument document template has all the control sections of the other two, but are a tiny fraction of the total content.  The cost of producing the information may have been paid in previous projects.  Indeed, if you’re doing things right the content should already exist in Christmas tree documents, short of a complete rewrite of an existing monument document.  You’ll pay some time incorporating and reviewing new information, especially if the new info supersedes old info.  It’s well worth the cost, though, because people will be able to reference this one true source of information years later.

And people will reference it. This will be the authoritative source of knowledge about what a system should do, how it does it, how to test it or how to use it.  It will be your starting point for creating project Christmas trees and bridesmaid dresses.  Folks will actually enjoy reading it because it will be the one doc that others really care about and want to ensure its usefulness.  When you update it you’ll feel like you’re making a real contribution to something that will last longer than your employment at the company.

Which leads into the lifespan of a monument document.  It will be around as long as the thing it describes.  This could span a decade, even in technology.  I personally have worked on live code that is literally 10+ years old.  If a design doc had been written when the code was written people would still be updating it today.

Examples of monument documents are application or system design, or system/application requirements.  It doesn’t have to literally be a document, either.  A database of test cases or requirements would count too.  Really, anything that aggregates project-to-project changes and is kept up to date.

This is where anyone should spend serious time.  This is the most valuable type of document a company can own.  It will cut down on  new hires ramp up time.  It will be invaluable when you rewrite an application 5 years from now.  It will save you from mistakenly asking for a change that will affect changes made last year.  Such documents ARE your business.

This has been a summary of the three types of project document.  Hopefully it will help you spot the differences so that you spend the right amount of time on each one.