Jonathan Birkholz

Reducing Exceptional Cases

Ryan starts with describing his love affair with F#, which leads to a discussion of FubuMVC, which leads to a discussion of Performance Problems (not related to Fubu MVC)… etc. So a standard Wizards of Smart conversation. :)

I titled this Performance since most of the conversation centered around our experiences with targeting and improving performance.

Reducing Exceptional Cases

We have a third party control suite that provided some aggregation functionality. The third party controls had some built in aggregate functions but was missing some other more business centric functions.

We ended up coding every aggregate as a separate custom class and not using any of the built in aggregates. My team member asked me why not use the built in aggregates?

The reasoning is that if we used some built-in aggregates and then built the custom aggregates for the other functions, we would have 2 methods of aggregation to maintain. If we treat all the aggregates as custom, then instead of having 2 scenarios, we have one. We don’t have any exceptions. All aggregates are custom aggregates.

Not only do we simplify our code and future maintenance, we eliminate any dependency on the third party aggregates. Now we are free to change control vendors and not worry about losing our aggregation functionality.

The point I want to make is look to simplify your code. Eliminate exceptional cases as much as possible. Make everything the same and you achieve simplicity and flexibility.

Linq - Sum

Last week I started an ongoing section during the Virtual Brown Bag where I go over a Linq function (or two). The general idea is that although most people ‘use’ Linq, there are lots of functions that are overlooked.

In this blog post I will demonstrate basic usage cases for the Linq Sum function.

Numbers

If you have a collection of numbers and you want the sum of their values, then simply call Sum. It can’t get any easier.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[Test]
public void should_sum_values()
{
var nums = new List<int>
{
5,
9,
1,
10
};
int sum = nums.Sum();
sum.ShouldBe(25);
}

Non-Numbers

If you want to sum a collection that isn’t made up of numbers, then you will be forced to specify which number property you would like to sum.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[Test]
public void summing_non_number_collection()
{
var jedi = new List<Jedi>
{
new Jedi("Yoda").MidichlorianCountIs(1000),
new Jedi("Anakin Skywalker").MidichlorianCountIs(3000),
new Jedi("Luke Skywalker").MidichlorianCountIs(1500),
new Jedi("Obi-wan Kenobi").MidichlorianCountIs(500),
};
// can't compile
//var sum = jedi.Sum();
}
[Test]
public void should_be_able_to_specify_property_to_sum()
{
var jedi = new List<Jedi>
{
new Jedi("Yoda").MidichlorianCountIs(1000),
new Jedi("Anakin Skywalker").MidichlorianCountIs(3000),
new Jedi("Luke Skywalker").MidichlorianCountIs(1500),
new Jedi("Obi-wan Kenobi").MidichlorianCountIs(500),
};
int sum = jedi.Sum(j => j.MidichlorianCount);
sum.ShouldBe(6000);
}

These tests can be found in my Learning Solution on GitHub and found in the Learning CSharp project : http://github.com/RookieOne/Learning

Wizards of Smart - Episode 6

The general theme for this podcast is handling Complexity through Composition. But we end up talking about Entity Framework, Fubu MVC, ISP, Rails, and reach new levels of nerdness with sentence composition.

Virtual Brown Bag - 6/3/2010

A rather link filled, topic filled VBB. Not many direct coding examples but lots of great information.

LINQ Examples

I did share how to use Sum, Select Many, and Aggregate. The code was put into my learning csharp project in my learning solution on GitHub.

I plan on writing a couple of blog posts on these LINQ items so stay tuned if you want more LINQ goodness.

http://github.com/RookieOne/Learning

Wizards of Smart Podcast – Episode 5

More rails talk on this episode. But I did share the link with the VBB. http://www.theabsentmindedcoder.com/2010/06/wizards-of-smart-episode-5_01.html

Absent Minded Coder Posts

I shared my 2 posts from the last brown bag…

Structure Map 2.6 constructing the concrete type http://www.theabsentmindedcoder.com/2010/05/structure-map-26-constructing-concrete.html

Interface Segregation Principle http://www.theabsentmindedcoder.com/2010/05/interface-segregation-principle-in.html

I also shared George’s comment on the Udi InfoQ article : http://www.infoq.com/presentations/Making-Roles-Explicit-Udi-Dahan

Unlocker

Claudio shared a helpful tool to get rid of an annoying Windows error. http://ccollomb.free.fr/unlocker/

VS 2010 Step into .Net Framework

Claudio also shared a blog post showing how to setup Visual Studio 2010 to allow stepping into .Net Framework code. http://weblogs.asp.net/rajbk/archive/2010/04/21/setting-up-visual-studio-2010-to-step-into-microsoft-net-source-code.aspx

Why Your Code Sucks

This might be my favorite link Claudio shared. The blog post talks about why your code sucks… yes he is talking to you. (and me…) :D

http://www.artima.com/weblogs/viewpost.jsp?thread=71730

Resharper and Regions

Find out how to configure Resharper to remove regions http://bit.ly/wtjK3

You can also prevent Resharper from adding regions http://bit.ly/9QgLYe

Monads

Uncle Bob uploaded a WTF is a Monad presentation : http://bit.ly/aBMIaL <- I plan to look at this tonight ;D

Revamp Your Code Review

Ryan wanted me to share a blog post by Kyle Baley discussing the code review process : http://codebetter.com/blogs/kyle.baley/archive/2010/06/01/rietveld-or-how-to-revamp-your-code-review-process.aspx

Nine Things ie9 is doing Right

Ryan also wanted me to share : http://sixrevisions.com/web-development/five-things-ie9-is-actually-doing-right/

RSA Animate - Drive: The surprising truth about what motivates us

I think @TheCodeFoundary tweet’d this link earlier this week. It is an interesting video discussing how we are actually motivated by more than money. :D

http://www.youtube.com/watch?v=u6XAPnuFjJc

Git + GitHub

Claudio was asking about Git so I shared some links and walked through a simple workflow into how I commit locally and then push to my GitHub repository.

Git Extensions can be found here : http://sourceforge.net/projects/gitextensions/

We also discovered this : http://learn.github.com/

Architecting TekPub

Great article on InfoQ. In the article they interview James Avery and Rob Conery about their experience with TekPub going from MVC to Ruby on Rails and the reasoning why.

http://www.infoq.com/articles/architecting-tekpub

Future Rails Hands-On Demo?

I tossed out the idea of having a rails hands on talk where as Claudio talks, I code the application in rails. The purpose is to showcase how rapidly we can build an application and deploy the application using rails.

Wizards of Smart - Episode 5

I recently was doing some volunteer work using Rails and started a conversation about my experience. This went on to general discussions on Ruby, Rails, and benefits of Dynamic Languages.

Interface Segregation Principle in Practice

I am working with some other team members integrating components to be consumed by different controls. For example someone could use a grid and then opt in to have sorting or filtering etc. It is a rather weird system but was decided upon before I arrived.

Anyway, I am currently looking to implement an adapter for the formatting component. When I looked at the interface, ISP immediately jumped out at me. So I thought, why not do a blog post about identifying the problem and delivering the solution.

Interface Segregation Principle

The Interface Segregation Principle states that clients should not be forced to implement interfaces they don’t use. Instead of one fat interface many small interfaces are preferred based on groups of methods, each one serving one submodule. - http://www.oodesign.com/interface-segregation-principle.html

So instead of one large interface we should have many smaller interfaces with grouped behavior.

When we see an interface like this, the violation of ISP should be readily apparent. (warning.. be prepared to scroll…..)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/// <summary>
/// An interface defining the contract for anything that has columns that can be formatted.
/// </summary>
public interface IColumnFormattingAdapter
{
#region Methods
/// <summary>
/// Hides the column.
/// </summary>
void HideColumn();
/// <summary>
/// Bests the fit header.
/// </summary>
void BestFitHeader();
/// <summary>
/// Bests the fit column.
/// </summary>
void BestFitColumn();
/// <summary>
/// Bests the fit header column.
/// </summary>
void BestFitHeaderColumn();
/// <summary>
/// Bests the fit all columns/headers.
/// </summary>
void BestFitAllHeaderColumns();
/// <summary>
/// Formats the column.
/// </summary>
void FormatColumn();
/// <summary>
/// Formats all columns.
/// </summary>
void FormatAllColumns();
/// <summary>
/// Shows the advanced formatting.
/// </summary>
void ShowAdvancedFormatting();
/// <summary>
/// Shows the advanced report formatting.
/// </summary>
void ShowAdvancedReportFormatting();
/// <summary>
/// Chooses the column.
/// </summary>
void ChooseColumn();
#endregion
#region Feature Support
/// <summary>
/// Whether or not the adapter supports hiding of columns
/// </summary>
Boolean AllowHideColumn { get; }
/// <summary>
/// Whether or not the adapter supports best fit based on the header text
/// </summary>
Boolean AllowBestFitHeader { get; }
/// <summary>
/// Whether or not the adapter supports best fit based on the column contents
/// </summary>
Boolean AllowBestFitColumn { get; }
/// <summary>
/// Whether or not the adapter supports best fit based on the longer of header text and column contents
/// </summary>
Boolean AllowBestFitHeaderColumn { get; }
/// <summary>
/// Whether or not the adapter supports best fit on all columns based on the longer of header text and column contents
/// </summary>
Boolean AllowBestFitAllHeaderColumns { get; }
/// <summary>
/// Whether or not the adapter supports formatting of a column
/// </summary>
Boolean AllowFormatColumn { get; }
/// <summary>
/// Whether or not the adapter supports formatting of all columns
/// </summary>
Boolean AllowFormatAllColumns { get; }
/// <summary>
/// Whether or not the adapter supports advanced conditional formatting
/// </summary>
Boolean AllowAdvancedFormatting { get; }
/// <summary>
/// Whether or not the adapter supports advanced report property formatting
/// </summary>
Boolean AllowAdvancedReportFormatting { get; }
/// <summary>
/// Whether or not the adapter supports choosing columns
/// </summary>
Boolean AllowChooseColumn { get; }
#endregion
}

Identifying ISP Violation

The first characteristic that the interface violates ISP is the sheer size. This interface weighs in at 117 lines. Granted there are comments, etc but it is still rather large.

The second characteristic that the interface violates ISP (and the biggest eye sore) is all the Allow properties. That should make you stop and scratch your head.

Fixing the interface

Let’s simplify the bad interface and then learn how to fix it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/// <summary>
/// An interface defining the contract for anything that has columns that can be formatted.
/// </summary>
public interface IBadColumnFormattingAdapter
{
/// <summary>
/// Hides the column.
/// </summary>
void HideColumn();
/// <summary>
/// Chooses the column.
/// </summary>
void ChooseColumn();
/// <summary>
/// Whether or not the adapter supports hiding of columns
/// </summary>
Boolean AllowHideColumn { get; }
/// <summary>
/// Whether or not the adapter supports choosing columns
/// </summary>
Boolean AllowChooseColumn { get; }
}

So in this scenario we can hide a column and then choose a column. Instead of having a large interface for these two behaviors, we should package the behaviors into separate interfaces.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/// <summary>
/// Interface for supporting Hide Column feature
/// </summary>
public interface IHideColumn
{
/// <summary>
///Hides the column.
/// </summary>
void HideColumn();
}
/// <summary>
/// Interface for supporting Choose Column feature
/// </summary>
public interface IChooseColumn
{
/// <summary>
///Chooses the column.
/// </summary>
void ChooseColumn();
}

Now our functionality is encapsulated into behavior centric interfaces. Our adapters can then opt in to provide the functionality, and we don’t have the superfluous Allow properties.

Structure Map 2.6 Constructing the Concrete Type

Basic Use with Func

Ryan yet again was asking for how to do another crazy thing with Structure Map. :)

He wanted to know if there was a way to control the construction of a concrete type. Well.. he didn’t put it that way but it is what he wanted to know.

Besides the regular For().Use()… we have the ability to construct the concrete type ourselves with a simple action.

To demo this I have a Foo class that accepts a string in the constructor.

1
2
3
4
5
6
7
8
9
class Foo : IFoo
{
public Foo(string message)
{
Message = message;
}
public string Message { get; set; }
}

We want to pass in the message explicitly so we configure our container and tell it how to provide the concrete implementation.

1
2
_container = new Container();
_container.Configure(x => x.For<IFoo>().Use(() => new Foo("Hello")));

And the tests to verify it works :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[Test]
public void IFoo_should_resolve_to_Foo()
{
var foo = _container.GetInstance<IFoo>();
foo.ShouldBeType<Foo>();
}
[Test]
public void message_should_be_set_during_construction()
{
var foo = _container.GetInstance<IFoo>();
foo.Message.ShouldBe("Hello");
}

Using Func<IContext, T>

There are other scenarios where not only do you want more control on a concrete types construction, but also need to resolve other dependencies. SM also allows you to specify a Func<IContext,T> where the IContext allows you to resolve classes from the container.

So if we have a scenario with an IBar and an IFoo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Bar : IBar
{
public IFoo Foo { get; set; }
}
class Foo : IFoo
{
}
interface IBar
{
IFoo Foo { get; }
}
interface IFoo
{
}

We can use the Func<IContext,T> similar to how we used the Func.

1
2
3
4
5
6
7
8
9
10
11
12
_container = new Container();
_container.Configure(x =>
{
x.For<IFoo>().Use<Foo>();
x.For<IBar>().Use(c =>
{
var bar = new Bar();
bar.Foo = c.GetInstance<IFoo>();
return bar;
});
});

And naturally our test to verify it works:

1
2
3
4
5
6
7
[Test]
public void should_set_foo_property_on_bar()
{
var bar = _container.GetInstance<IBar>();
bar.Foo.ShouldBeType<Foo>();
}

So that was a brief introduction into using Funcs to provide the concrete implementation of an interface for Structure Map.

All the Learning Structure Map code can be found in my Learning solution on Git Hub : http://github.com/RookieOne/Learning

Virtual Brown Bag 5/27/2010

Claudio was a no show today. As I predicted he had meetings he could not get out of.

But we powered through and covered a range of topics.

Wizards of Smart podcasts

Since our last meeting there have been 2 episodes of Wizards of Smart.

Episode 3 – Testing : http://www.theabsentmindedcoder.com/2010/05/wizards-of-smart-episode-3-testing.html

Episode 4 – Code Reviews, Work Environment, Developers : http://www.theabsentmindedcoder.com/2010/05/wizards-of-smart-episode-4.html

Ruby on Rails I talked a bit about my experience this week with Ruby on Rails. In particular I pointed out how I enjoyed the migration functionality.

http://guides.rubyonrails.org/migrations.html

Alper pointed out that c4mvc also has a session on some .net tools for migration :

http://www.c4mvc.net/Home/Events –> March Show and Tell : Topic Database Migration Frameworks : MigratorDotNet and RikMigrations

I also point out the Dev Chix wiki for learning Ruby on Rails : http://wiki.devchix.com/index.php?title=Server_2003

The guide walks through every step from installing Ruby, rails gems, setting up git, heroku, and pushing your app to heroku. It’s a great guide for those new to rails (like me!)

SQLite Database Browser

I pointed out this tool for looking at Sqlite databases. I am using it to look at my rails database.

http://sourceforge.net/projects/sqlitebrowser/

Domain Driven Design

Ed asked about DDD for everyday apps. I then fumbled around and gave a high level talk on how to apply some DDD principles to everyday code. I think I will work on a presentation on the subject because I find it an interesting perspective.

We did point out some DDD resources :

Greg Young on Herding Code : http://herdingcode.com/?p=189

Greg Young on InfoQ : http://www.infoq.com/interviews/greg-young-ddd

David Laribee on Deep Fried Bytes : http://deepfriedbytes.com/podcast/episode-6-talking-domain-driven-design-with-david-laribee-part-1/

Eric Evan’s book : http://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215/ref=sr_1_1?ie=UTF8&s=books&qid=1274981635&sr=8-1

Jimmy Nilsson’s book : http://www.amazon.com/Applying-Domain-Driven-Design-Patterns-Examples/dp/0321268202/ref=sr_1_2?ie=UTF8&s=books&qid=1274981635&sr=8-2

Houston Techfest I mentioned the Houston Techfest again and discussed some potential presentations Claudio and I might be doing.

http://www.houstontechfest.com/dotnetnuke/default.aspx

They are still taking abstracts. Email your abstracts and bio to speakers@houstontechfest.com

Linq

I walked through some linq examples that can be found in my learning C# project in my Learning Repository : http://github.com/RookieOne/Learning

We looked at Select, my pet peeve of using a Where and then FirstorDefault, and also how to use Any instead of Count to verify if a list is not empty.

I took a poll and there was a lot of interest in doing more Linq best practices and techniques. I am working on a presentation as well and it was nice to see there was a good deal of interest.