Author Archives: Scott

Methods should include the state needed to execute them as parameters

I’ve been seeing the following pattern in code that I’ve been working on lately.

var muhClass = new MuhClass();
muhClass.MuhProp = "foo";
muhClass.MuhOtherProp = "bar";

muhClass.MuhMethod();


public class MuhClass {

public string MuhProp {get; set; }
public string MyOtherProp {get; set; }

public void MuhMethod() {
  Console.WriteLine(MuhProp + MuhOtherProp);
}

This annoys me, I don’t like having to set properties before I call a method I would prefer the method signature to contain all of the data needed to execute the method.


public void MuhMethod(string muhProp, string MyOtherProp)

In fact, if you can use defaults use them, or use overloads.

public void MuhMethod(string muhProp, string MyOtherProp = "BallZacks")

public void MuMethod(string muhProp) 
{
    MuMethod(muhProp, "NutButter");
}

Using Toothpaste as spackle

Scott Hanselman has a nice post outlining various command line replacements and enhancers for Windows.

Have you tried to set up a Windows Core Server? If you haven’t, you’re in for a treat. And by treat I mean horrible, painful, undocumented PAIN. Unless it’s gotten better in the past 2 years, which I doubt.

All these command line tools are great, I’m a ConEmu convert too, but until the OS is really CLI oriented it’s just using toothpaste as spackle.

The biggest problem with Windows Core servers is a lack of a common API to the various configurations. You use WMI with VBScript for this, PowerShell for that, batch files for this other thing. Granted, you can do the initial setup of a Windows Core server 2012 using sconfig.cmd , but once you are past the main server setup and into setting up things like Active Directory and IIS, it’s painful.

How do you start a new project with TDD?

Bobby Johnson (@NotMyself on Twitter) wrote a post detailing how and why he “tests reality” when he starts a new project and gives two examples of what he means.

node.js:

'use strict';

var assert = require('assert');

exports.test_reality = function(test) {
    test.equals(true, true, 'true should still be true');
    test.done();
};

C#:

using NUnit.Framework;

namespace Simple.Data.SqliteTests
{
    [TestFixture]
    public class RealityTests
    {
        [Test]
        public void true_should_be_true()
        {
            Assert.That(true,Is.True);
        }
    }
}

At first glance, you look at those and think they are silly because all they are doing is testing that the test framework is working correctly. But Bobby has a different reason for including them.

So when I am setting up my basic project structure and automation, I like to have at least one unit test ready to run. This allows me to test the automation script and ensure failures in tests cause failures in builds. In .NET I want to confirm that the compilation step fails properly and the unit tests fail properly. In node.js, I want to ensure that linting errors fail as well as unit tests.

That’s a valid point, however this is more of a configuration test of your infrastructure. I personally wouldn’t write a test like this, I’d want the test to have some value before I committed it to the main repository or build/CI server. I strongly feel that developers should use the same build procedure on their desktops to build the application as they do on their CI server. IDE’s like Visual Studio abstract away the building of the application, so we often have to write scripts, rakefiles, psake files, whatever to do all the things that Visual Studio doesn’t do when it builds your application. Things like running the tests and reporting the results, building an installers or setting test/staging/production values in config files. I like to have a repeatable build system setup for projects I’m working on so I tend to re-use scripts or keep it really simple. Which means that I have confidence that I have configured the build script correctly so that it will work properly when I put it in my CI server. Which allows me to start focusing on the design of my application sooner.

MVC for the client side web is very simple

Model:

<body>
    <h2>Food List</h2>
    <div id="food">Apple</div>
    <input type="text" id="foodToAdd" /><button id="AddFood" text="add"/>
</body>

View:

body { background-color:#000;font-family:serif;color:#ff00ff; font-size:1em  }
#food { border: 1 solid black; }

Controller:

<script>
$(function() { $("#AddFood").click(onAddFoodClicked);
             });

function onAddFoodClicked(ev) {
 var currentlist = $("#food").text();
 var foodToAdd = $("#foodToAdd").val();
  $("#food").text(currentlist + "," + foodToAdd);
  $("#foodToAdd").val("");
  
}
</script>

See?

JS Bin