Archive

Archive for June, 2009

Closures in Clojure and C#

So one of the ways I learn a new language is to take new things from the new language and port them back into the old, familiar one. Eventually, I’ll port an entire application (even though that app may be very small) from the old to the new. I find that I learn things much better this way. Tonight’s example is closures in Clojure and C#.

In Clojure, you can do this:

(defn make-greeter [greeting-prefix]
(fn [username] (str greeting-prefix ", " username)))

This basically creates a closure around greeting-prefix that can be used to create custom ways to greet people. For example

(defn hello-greeting (make-greeter "Hello"))

creates a closure than can be called with

(hello-greeting "Brett")

and get “Hello, Brett”

You can do the same thing in C# though it’s a little more work because it’s a statically typed language. In C#, a small console app looks like this:

namespace ClosureToy
{
    class Program
    {
        static void Main(string[] args)
        {
            //Class1 one = new Class1();
            Func hello = MakeGreeter("Hello");
            Func aloha = MakeGreeter("Aloha");

            Console.WriteLine(hello("brett"));
            Console.WriteLine(aloha("brett"));
            Console.Read();
        }

        static Func MakeGreeter(string greetingPrefix)
        {
            return (username) => String.Format("{0}{1}{2}", greetingPrefix, ", ", username); ;
        }
    }
}

The MakeGreeter function is a closure around greetingPrefix that allows you to create custom greeter functions on the fly. Essentially, it’s doing the same thing as the Clojure code as you can see from the main body of the console app where it creates two greeter functions on the fly, one that says hello and one that says aloha. Closures make it easier to abstract common pieces of code in your applications.

Categories: Programming Tags:

More Clojure On Windows

I’m slowly working my way through Stuart Holloway’s Programming Clojure book (and using slowly in that phrase does a terrible disservice to the word) and so far, I’m enjoying it. I had a run at Common Lisp a couple of years ago and ran into a brick wall for the most part (I think football season started). I’m mostly playing with Clojure to get a different perspective on programming apart from my usual day job as a .Net developer.

All that intro aside, I wrote about both starting and stopping the REPL here. In working through the book, you need to have the clojure install directory, the clojure-contrib directory and the code from the book on your CLASSPATH. Instead of modifying that in the environment variables on Windows, it’s a great deal easier (and smarter according to IBM to just modify the CLASSPATH at startup using the command line. This is extra easy if you created the clojure.bat file I wrote about in my previous entry linked above. You can just change the one line in it to “java -cp path_to_clojure\clojure.jar;path_to_clojurecontrib\clojure-contrib.jar;path_to_book_code clojure.lang.Repl”. Once you’ve done that, you’re up and running with the book samples.

Being both a Java dummy and a Clojure dummy, I’ll try to keep posting tips from my foray here.

Categories: Programming Tags:

Exiting The REPL In Clojure

Whenever I’m learning a new language, it always seems like it’s the little things that drive me mad. With that in mind, I’m going to try and post fixes to the little things as I make my way through Clojure. To that point, I rapidly got tired of closing down the command window and starting a new one when I seriously hosed things up. Ctrl-D didn’t work though I think that’s the command for Common Lisp. As usual, Google knows everything and the command is Ctrl-C.

Also, if you’re running Clojure on Windows and want to be able to fire up the REPL from anywhere, create a batch file called clojure.bat in your Windows\system32 folder and put the following line in it:
java -cp c:\clojure\clojure.jar clojure.lang.Repl

Then you can just type clojure at the command prompt anywhere and have the REPL start up in that directory. This is helpful once you start writing larger programs that aren’t located in the Clojure install folder.

Categories: Programming Tags:

Getting Clojure Highlighting With Vim On Windows

Toralf Wittner wrote a good syntax file for Clojure but I had a hell of a time getting it to work on Windows, mostly because I’m not very smart and/or I didn’t bother to read the documentation for Vim. So sue me. Regardless, if you want to use Vim to edit Clojure scripts on Windows, save the file at the link above into your vimfiles/syntax folder and call it clojure.vim. The filename isn’t important but it makes it easy to see what syntax files are what. I’m currently just loading the file manually when I code in Clojure using :set ft=clojure and boom, you’ve got nice syntax highlighting.

You could probably set this up to automatically recognize .clj filetypes and turn syntax highlighting on for those but I’m going to stick with this small victory first. At least I learned SOMETHING today.

Categories: Programming Tags:

Getting Clojure Installed On A Windows Machine

This is mostly a public service announcement, I didn’t find a good list of requirements anywhere else. Yes, I know I must be insane to be trying to half-ass learn another language but what else would I do on Sunday mornings? There’s no football and the alternative is hard manual labor in the garden. Shudder.

That’s really it, I expected it to be more work but using the binaries over the source simplified the process even though I went ahead and installed Maven too. The Getting Started page has some links to VIM and Emacs syntax files that will probably help your editing a lot. I may try to do some of the initial work in VIM as well as Komodo and see which I prefer.

Categories: Programming Tags:

Give Me An Assertion Vasily. One Assertion Only, Please

I was working through some broken unit tests this morning for the project I’m currently on and the second one in my list looked like this:

 [TestMethod]
        public void SelectTest()
        {
            Item item = session.SelectSingle(a => a.SkuNo == _item.SkuNo);

            Assert.IsTrue(item.Id > 0);
            Assert.AreEqual(item.Id, _item.Id);
            Assert.AreEqual(item.IsAutoReplenished, _item.IsAutoReplenished);
            Assert.AreEqual(item.Status, _item.Status);
            Assert.AreEqual(item.MaintenanceLevel, _item.MaintenanceLevel);
            Assert.AreEqual(item.Description, _item.Description);
            Assert.AreEqual(item.Type, _item.Type);
            Assert.IsTrue(item.LastAdjustmentDate.HasValue);
            Assert.AreEqual(item.LastAdjustmentDate.Value.Date,
               _item.LastAdjustmentDate.Value.Date);
            Assert.IsTrue(item.LastReceivedDate.HasValue);
            Assert.AreEqual(item.LastReceivedDate.Value.Date,
              _item.LastReceivedDate.Value.Date);
            Assert.IsTrue(item.LastReturnToVendorDate.HasValue);
            Assert.AreEqual(item.LastReturnToVendorDate.Value.Date,
              _item.LastReturnToVendorDate.Value.Date);

            Assert.IsTrue(item.Upcs.Count == 1);
            Assert.AreEqual(item.Upcs[0].Id, _item.Upcs[0].Id);
            Assert.AreEqual(item.Upcs[0].IsPrimary, _item.Upcs[0].IsPrimary);
            Assert.AreEqual(item.Upcs[0].UpcNo, _item.Upcs[0].UpcNo);
        }
}

An Item gets hooked up in the Initialize method of the test suite and then this test does 17 assertions on the item it gets back from the session. There are enough things wrong with this scenario to make me write a blog post about it. Which is saying something these days, if I’m not drunk blogging VPILFs and whining about briskets, I’m pretty much not writing. I digress.

First of all, it’s not really a unit test because it goes outside the boundary of the object under test by setting up an item, saving it to the database and then selecting it back out of the database for 17 assertions. The current project has a section for Functional/Integration tests and in all reality, this test probably belongs there.

Second, it’s poorly named. The names of test methods should read like documentation for the object under test. “SelectTest” tells me next to nothing about what is going on here and in fact, isn’t really what’s being tested at all. I have to read the entire test to know what’s being tested. In truth, this test is trying to verify certain properties are set on an Item. A correct name for this test is “IdIsGreaterThanZeroAndIdIsSameAsOriginalItemIdAndAutoReplenishedIsSameAsOriginalAutoReplenished
AndStatusIsSameAsOriginalStatusAndMaintenanceLevelIsSameAsOriginalMaintenanceLevel
AndDescriptionIsSameAsOriginalDescriptionAndTypeIsSameAsOriginalType
AndLastAdjustmentDateHasValueAndLastAdjustmentDateValueIsSameAsLastAdjustmentDateValue
AndLastReceivedDateHasValueAndLastReceivedDateValueIsSameAsOriginalLastReceivedDateValue
AndLastReturnToVendorDateHasValue
AndLastReturnToVendorDateValueIsSameAsLastReturnToVendorDateValue
AndHasOneUpcAndThatUPCsIdIsSameAsOriginalItemsUPCId
AndUPCIsPrimaryIsSameAsOriginalItemsUPCIsPrimary
AndUPCNoIsSameAsOriginalItemsUPCNo”

Whew. Deep breath. In through the nose, out through the mouth. Back with me? Good. This brings us to the final problem of this test and that is 17 @#$% ASSERTIONS IS TOO MANY FOR ONE @#$%^ TEST! Tests should be like objects, they should have one function and one function only. It’s amazing how many OO gurus there are out there who think tests like this are completely normal.

There are several reasons why you’d want one assertion per test. First, it makes for a very clean documentation of the object under test. Assuming all the test names are well-written, you can just read the tests and have a great idea what the object can and cannot do. Second, most unit test frameworks fail a test at the first failed assertion. If you have 17 of them, you don’t really know what’s broken about your object when the first one fails. You have to fix that first before running the test again. Finally, you’re really running multiple tests, you’re just doing it implicitly. Any time you have the option of doing something explicitly or implicitly, you should ALWAYS choose the former. Even if your framework runs all assertions, you’re doing so on object state that is no longer clean after the first assert failed. This isn’t giving you the kind of testing that you’re looking for.

In the end, test should have a single piece of functionality to test, they should be well-named and they should have a single assert. This will prevent some jerk from writing a blog post about your test that’s he’s having to fix 5 months after you wrote it. Even if you’re that jerk.

References: Write Maintainable Unit Tests That Will Save You Time And Tears
Avoid multiple asserts in a single unit test: revisited
Testing: One Assertion Per Test

Categories: Programming Tags:

TDD With Python and Pylons

I’ve been doing some development on a website using Python and the Pylons web framework. I’m trying to stay pretty strict with Test Driven Development (TDD) though I run into problems because I’m still a complete novice with Pylons and a half-complete novice with Python. In my experience so far, unit tests are moderately difficult in Pylons and turn out to be something closer to the bastard stepchild of a redneck unit test and a 5th Avenue socialite mom. I feel that way because the unit tests require the Pylons framework to be set up correctly. They also often go outside their boundaries and I haven’t looked into any mock frameworks, though I have the feeling that using a mock framework with a dynamic language like Python is probably a stupid thing to say in public.

Regardless, I have really started to enjoy working with Pylons and that stems from the actual functional tests that are available through the framework. Specifically, my development flow has been something like this:

  • Write new functional test of the web site
  • Run tests to see them error out. Typical error message is that an action isn’t implemented on the controller.
  • Implement the basic controller and the action but leave out the functionality under test.
  • Rerun the test to see it fail.
  • Implement the functionality necessary to get a passing test. This often includes implementing database tables, keys, getting SqlAlchemy set up to correctly map data to objects and creating new templates for HTML.

The functional tests are nice because while they aren’t confirming look and feel type stuff, they at least put the flow of the application under test. I’m a purist when it comes to having unit tests only test the code they are intended for but I’m not a purist when it comes to writing unit tests first as the only way to design the application. With a website like this, I’m pretty happy writing functional tests to drive out the design of the web site.

Here are some code snippets from my current website (try to ignore the fact that this looks like it might have something to do with horses and the Kentucky Derby, especially if you work for the NSA.)

First a test:

from darlydowns.tests import *
from darlydowns.model import meta
from darlydowns.model import horse

class TestHorseController(TestController):
    def test_index(self):
        # setting up a temp horse to make sure one exists for the test
        tempHorse = horse.Horse('my temp', 'my description')
        meta.Session.save(tempHorse)
        meta.Session.commit()

        response = self.app.get(url_for(controller='horse'))
        ## Test response...
        assert len(response.c.horses) > 0

        meta.Session.delete(tempHorse)
        meta.Session.commit()

Here we have a test that tests the response from a request for a URL that is handled by the controller “horse”. This controller grabs all the horses in the database and displays them in a table. The test saves a temp horse, gets the list, verifies the list contains at least 1 horse and then deletes the temp horse to clean up after itself.

Here’s the controller code that allows the test to pass:


import logging

from darlydowns.model import meta
from darlydowns.lib.base import *
from darlydowns.model import horse
from darlydowns.model.horse import Horse

log = logging.getLogger(__name__)

class HorseController(BaseController):

    def index(self):
        c.horses = [horse for horse in meta.Session.query(Horse).all()]
        return render('/horses.mako')

One of the beauties of Pylons is how little code is required to do something, once the project is set up. Here our HorseController has an action of “index” which is defined as a method. It grabs all the horses from the database and then uses a list comprehension to collect them into the c.horses variable. It then renders the template “horses.mako” which knows how to layout the web page using the horses found in the database.

Once this was done, I wrote code to save a horse, all driven out by the functional tests. I’ve been pretty happy with how the design is driven from these tests as it’s often quite clear where to proceed next in the application from the last test. Lots of times, other functionality comes up that doesn’t logically flow next but I just add that to a growing todo list in the project to make sure nothing is skipped.

Pylons takes a little getting used to, especially since I come from a static, everything in once solution sort of background. With Pylons, you need to learn Routes and Mako and SqlAlchemy but once you get your head around all those tools, it’s a joy to work with.

UPDATE: Welcome to everyone coming here from the Python subreddit. If you have any tips for testing using Pylons, feel free to drop me a comment. I’d love to hear about other people’s experiences.

Categories: Programming Tags:

Consistency

The more programmers I work with and the more code I read, the more I realize that everyone has their own unwritten style guidelines in their head for writing code. Some people are more self-consistent than others in their implementation of their style guidelines but it seems that most people at least have some semblance of coding guidelines that they follow. This is expected behavior as programmers come from different backgrounds and experience levels. In and of itself, this isn’t a big problem.

Unfortunately, it becomes a big problem when you form teams of programmers because just like an essay written by multiple authors, it becomes difficult to get past the style issues of multiple programmers in the same codebase. A team should write code that is self-consistent across the team. This provides several benefits, among them a greater understanding of a common codebase, easier pair programming and code reviews, and less frustration for current and future maintainers of the codebase. When you form teams with no guidelines for coding style, you get a mishmash of sytles that becomes increasingly frustrating for everyone involved. If you have to maintain a large codebase written by a team without coding standards, you’ll quickly grow tired of trying to remember whether programmer A liked to prefix or suffix TextBox on all his text boxes or if in this class the private variables have underscores or not.

Coding standards are one of the skeletons in the closet for software developers. Most developers scoff at implementing them, chafing at the concept of having to bend their style to a common one. That’s too bad because it’s a big drag on productivity on teams larger than 1. Look, I love regions as much as the next guy but that doesn’t mean I can’t easily alter my “style” to avoid regions in the code I write for the benefit of consistency. It’s a joy to read a codebase that is self-consistent whether it was written by 1 developer or 50 because you don’t have to spend any cycles trying to remember what an underscore means or expanding regions in one class but not in another or searching for private fields at the top of a class or at the bottom or wherever the developer felt like declaring them.

For consulting companies, I’d think it would be a competitive advantage to have coding standards defined for the organization. When hired by clients that didn’t have their own standards, a common standard for consultants would result in teams that were more productive as well as clients that were happier in the maintenance of the codebase. On top of that, if different consultants from the same company eventually came out for a followup, they could come up to speed more quickly because the codebase would be organized in a consistent manner. A final benefit would be from a sales perspective. Being able to say that all code on all projects written by a company was consistent to the same style regardless of which consultants participated would show prospective clients that all consultants with the company operated as if on the same team and valued a common set of standards.

Emerson wrote “A foolish consistency is the hobgoblin of little minds” and he was certainly right. Consistency in coding standards isn’t foolish though and adds plenty of happy side effects to teams that write from the same standards. It’s a shame more developers and companies don’t understand this.

Categories: Programming Tags:

I Feel So Unprofessional

Next time you hear someone call themselves a “software engineer” or “software architect”, show them this link and then giggle maniacally.

And people wonder why software sucks? It’s because some people out there learned about databases from Japanese cartoons! Gah.

Categories: Programming Tags:

One Time Setup For WCF and Virtual Directories On Vista

This is mostly for my own reference but maybe someone will run across it and it will keep them from having to reinstall IIS like I just had to.

Step 1: Follow steps 1-5 here paying specific attention to the link in step 2. In step 5, I only did port 80 since I’m not interested in the samples specifically. I actually have no idea if this is necessary or not but since it’s currently working, who am I to look a gift horse in the choppers?

Step 2: Follow the steps here for manually setting up a virtual directory. On the security portions for IIS 7, do steps 1-3, skip 4-9 and just type IIS_USRS in the name field and then do steps 10 and 11.

That should work. I was careful to not do any extraneous freelancing and followed the articles pretty well and my services are up and running.

Categories: Programming Tags: