January 2011 Entries

Great Engineering Quotes

Charles F. Kettering was an engineer, founder of Delco, and the head of research at General Motors for 27 years. I ran across a few choice quotes from the man. “A problem well stated is a problem half solved.” “A man must have a certain amount of intelligent ignorance to get anywhere.” “If you want to kill any idea in the world, get a committee working on it.” “It's amazing what ordinary people can do if they set out without preconceived notions.” And here is one especially applicable to the world of software development:...

The Big Rewrite

Unlike refactoring, a "big code rewrite" bulldozes an entire application and starts over from scratch. Is it ever the right thing to do? In a recent post, Steve Blank says no - never for start ups in a fast moving market (Startup Suicide - Rewriting the Code): CEO’s face the “rewrite” problem at least once in their tenure. If they’re an operating exec brought in to replace a founding technical CEO, then it looks like an easy decision – just listen to your engineering VP compare the schedule for a rewrite (short)...

It's Design

If I come across this code: if (widget.Price < 10.0m && widget.UnitsInStock > 100) { // ... } Then the first thing I'll suggest is refactoring to:  if (widget.IsEligibleForDiscount) { // ... } But It's Not Reusable! "IsEligibleForDiscount will never be reused!", someone will shout. "Why do we go to the trouble if the logic is never reused?". Ah, yes - but if we only use encapsulation as a means to generalize and achieve...

The Refactoring Curve

Empirical studies on software refactoring are hard to find, but I believe refactoring follows the 80/20 rule - 80% of the possible benefit comes by applying just the first 20% of effort. Like most work in engineering and economics, the curve flattens under the pressure of diminishing returns. In this context I'm not thinking of individual refactoring operations (like extract method), but overarching efforts to improve software quality through rework, or by following a process like test-first design. I think most of use could agree that the best place to be on this curve depends on context. Start-up...

What’s Wrong With This Code? (#26)

A measurement consists of a high reading and a low reading, but the high and low values are optional. We also need to add measurements together, so the following C# code: var m1 = new Measurement(lowValue: 3, highValue: 5); var m2 = new Measurement(null, 3); var m3 = new Measurement(6, 2); Console.WriteLine(m1 + m2 + m3); … should produce a total measure showing a low value of 9, a high value of 10 (effectively ignoring null values). Unfortunately, something is wrong with the implementation... public struct Measurement { public Measurement(int? lowValue, int? highValue) { ...

Injectable, Configurable Action Filters

One of the advantages to using a custom IFilterProvider is how you gain total control over the instantiation of action filters in ASP.NET MVC 3. When you combine an IoC container with a filter provider and a custom IDependencyResolver (see Brad's posts for details),  then all the pieces fall into place. Building on the code from the last post, we can change our filter provider to utilize Ninject for action filter instantiation. public class ConfiguredFilterProvider : IFilterProvider { private readonly IKernel _kernel; public ConfiguredFilterProvider(IKernel kernel) { ...

Configurable Action Filter Provider

In MVC 3 you can implement an IFilterProvider to create and feed action filters to the MVC runtime. Assuming you have the configuration classes in place from the last post, you can create a custom filter provider to add action filters to the MVC pipeline.  public class ConfiguredFilterProvider : IFilterProvider { public IEnumerable<Filter> GetFilters( ControllerContext controllerContext, ActionDescriptor actionDescriptor) { var filters = FiltersSettings.Settings.Filters; foreach...

Configurable Global Action Filters for ASP.NET MVC

ASP.NET MVC 3.0 introduces global action filters - an easy way to apply an action filter to every action in an MVC application. All you need to do is register the filters during application startup: protected void Application_Start() { ... GlobalFilters.Filters.Add(new HandleErrorAttribute()); GlobalFilters.Filters.Add(new FooFilter()); GlobalFilters.Filters.Add(new BarFilter()); ... } But what if you wanted to add (or remove) filters through configuration? <configSections> <section name="filters" type="ConfigurableFilters.FiltersSettings, AssemblyName "/> </configSections> ... <filters> <add type="System.Web.Mvc.HandleErrorAttribute, System.Web.Mvc..." /> <add...

HTTP Modules versus ASP.NET MVC Action Filters

ASP.NET MVC has action filters, while ASP.NET has HTTP modules. Inside their respective processing pipelines, these abstractions serve similar purposes, and I've heard the following question a few times: When should I write an HTTP module and when should I write an action filter? If you are creating an MVC application then I'll almost always recommend going with an action filter instead of an HTTP module. This is my recommendation even if the functionality you are creating is generic enough to work from inside a module (in other words, it doesn't depend on MVC specific pipeline...

Razor Tip #2 : Inheritance & Configuration

You can change the base class of a Razor view in ASP.NET MVC using an @inherits directive. @inherits MyWebViewPage<dynamic> The  default base class for a Razor view in ASP.NET MVC is WebViewPage<T>, and in most cases you don't need to fiddle with @inherits  - it's more common to use the @model directive and just strongly type the default base class with a specific ViewModel type. But, when you really need to set a different base class for a view, you'll probably need to set the same base class everywhere. This is where configuration comes into play. Razor Configuration In the ~/Views...

ASP.NET MVC 3 Interview

Mehul Harry interviewed me at DevConnections 2010 about MVC 3.   I'm not sure why my picture makes me think of Hannibal Lecter, but maybe Mehul had a Jodie Foster thing going on that day.

MonoDroid Preview

MonoDroid allows you to write Android applications using C# and Visual Studio. Compare the code in the last post to the following: [Activity(Label = "My Activity", MainLauncher = true)] public class Activity1 : Activity { protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.layout.main); Button button = FindViewById<Button>(Resource.id.button); TextView view = FindViewById<TextView>(Resource.id.text); ...

IntelliJ IDEA for Android Development

I really like IntelliJ IDEA - and the free community edition now supports Google Android development. Part of the reason I like this IDE is because it feels familiar. I've learned the JetBrains sense of style from using ReSharper in Visual Studio, and seeing familiar commands for navigation and refactoring is comforting. It's great to have the muscle memory for Alt+Enter and Alt+Insert, and the auto-completion works well both for Java code and XML resources (which is a tremendous help for someone new to the platform, like myself). Now I find myself in the wiring up UI events...

Razor Tip #1

The Razor parser is smart. For example, if you want to display an email address, you can enter the following: <p> Serial@Model.com </p> ... and Razor will happily output Serial@Model.com into the output - it recognizes the email address. But what if you want to output a serial number? <p>    Serial@Model.SerialNumber</p> Unfortunately, Razor still thinks you have an email address and outputs "Serial@Model.SerialNumber". In a few rare cases you have to be more explicit and let the parser know where the C# code begins and ends. <p> <text>Serial</text>@Model.SerialNumber </p> But a prettier approach is to use an “explicit code...

Preprocessed Razor Templates

Preprocessed T4 templates generate all the code you need to execute a template at runtime. You can do the same "preprocessing" with Razor, but it takes a little more work. The first step is creating a custom tool for Visual Studio. Visual Studio will feed the template into this tool and expect you to generate the contents of a code file to include in the compilation phase of a project. Visual Studio communicates with the tool using the IVsSingleFileGenerator interface. [Guid("2678AC94-69B1-4B2B-8939-F15BE231C468")] public class RazorGenerator : IVsSingleFileGenerator { public int DefaultExtension(out string pbstrDefaultExtension) ...

Preprocessed T4 Templates

A preprocessed T4 template is an easy, out-of-the-box technology you can use for generating text from a template at runtime. Preprocessed templates are a little different than the T4 templates you might have used in the past. For details, read Oleg Sych's post on the topic.  As an example, let's say you add a preprocessed template named "LetterTemplate.tt" to a project, with the following content: <#@ template language="C#" #> Hi <#= Model.FirstName #>, Thank you for the email. Although our schedules are very busy, we decided to take some time and write you a personal reply. We...

Test First Development Course Online

David Starr and I recorded a test first development course for Pluralsight's On-Demand! library (subscription required). This is the first course in a series, and we designed the course for someone who is new to the landscape. I sincerely hope the course helps viewers find a path towards success in software development. Outline I. Introduction to Test-First Development II. Writing Unit Tests (Part I) III. Writing Unit Tests (Part II) IV. Introduction to Refactoring V. Driving Design with Unit Tests

Scott Allen
Posts - 869
Comments - 4493
Stories - 14