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.

RIP, Tony Hillerman

Tony Hillerman died yesterday at the age of 83. He was a masterful storyteller of the West, famous for his tales of the Navajo reservation. I enjoyed reading his books, often times handed down to me from my grandfather and father. He wrote stories about the mysticism of the Navajos that didn’t rely on blood and terror. They were often times cerebral and I’ll miss reading his novels.

Live Search Seems Pretty Broken

So after seeing a very odd spike in traffic to my blog today regarding Crossfit, I went to Live Search to see why quite a few people were finding my humble corner of the tubes. As it turns out, if you search for “crossfit training” using Live Search, I’m number four in the result list all because of this post. Does that make any sense to you?

In comparison, I’m not even in the top 100 on Google which is what I would expect given the fact that there are innumerable other sites out there that discuss Crossfit in much more detail than I do in that post. Seems to me that Live Search operates under an awfully odd algorithm if I show up in the top 10 results for crossfit training.

Google Is Starting To Scare Me

I wrote a post this morning at 11:10 AM detailing getting Clojure set up on my system and I briefly mentioned that I would probably try both Vim and Komodo as editors. At 3:39 PM, someone had come to my site from Google using the keywords “clojure komodo”. I don’t think I’m ever going to get used to the power of Google and its creepy little bots that document the entire freaking Internet. That’s just over 3.5 hours to crawl my site and assimilate the post from this morning into the Google brain. Yikes.

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.

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.

Wells Fargo Chairman Prefers To Save His Own Ass

Bloomberg is reporting that Wells Fargo Chairman Richard Kovacevich prefers the latest plan from the US to invest directly in banks through stock purchases over the original plan to buy up toxic assets from troubled banks. Of course he prefers this plan, it’s the one that works best for him as the CEO of a major bank. However, it’s also the plan that bends the American taxpayer over and has him grab his ankles for the next 5 years or so.

In case you hadn’t heard, Hank Paulson is now selling us on this plan to partially nationalize the banks of the nation as a way to get the economy going again. The problem is, we’re getting practically nothing out of our $250 billion dollar investment. We’re not buying preferred shares, we’re not getting a guaranteed interest rate, we’re not getting seats on any of the boards of these banks. We’re just a really big investor, nothing special at all. Warren Buffett got an infinitely better deal when he bought into Goldman Sachs a few weeks ago because he got preferred shares paying a 10% dividend plus the option to buy more shares at 10% below the strike price of the shares he bought. He got all this with a mere $5 billion dollar investment.

The American taxpayer, via our buddy Hank Paulson, is investing 50 TIMES THAT AMOUNT and yet we’re getting a much, much worse deal. We can’t force the banks to use the money to lend, we don’t have the option to buy more shares at a lower price and since we’re not getting preferred shares, we get a shitty interest rate return. There is nothing good about this plan for us and everything good for people like Wells Fargo Chairman Richard Kovacevich who gets a basic free $25 billion investment.

And the dog-shit icing on the elephant turd cake is that we’re not even solving the right problem. It’s not a liquidity problem that is causing the economic markets to tighten up their collective lending assholes, it’s a trust problem. We’re basically throwing a trillion dollars at the wrong problem. Anna Schwartz talked about this in this weekend’s Wall Street Journal. I strongly urge you to read that article to understand why we’re in so much trouble. We’re being led by people who don’t seem to be able to determine what the problem is, much less how to solve it. Either that or they are kowtowing to public opinion and when the original plan, which did address the correct problem, didn’t serve to immediately relieve the financial markets, they folded like a house of cards and went looking for a way to get immediate gratification.

Back to the original theme of this post, in the original plan to buy toxic assets from banks, we would have been addressing the central problem of trust. Currently, banks don’t know who to trust so they are refusing to loan to other banks. They have this sense of distrust because they have no way of knowing how much worthless shit is sitting on the other banks’ balance sheets. Until that distrust is resolved, we’re likely to continue to have a very cold credit market. If Paulson and Bernake would have stuck to their guns to buy toxic assets, at least banks could start to feel comfortable lending to other banks. Once that happened, the credit markets would start to loosen and money could start to flow again. It was a bailout that actually had a chance of working.

As it is, we’re about to flood the system with an insane amount of money, probably leading to a scenario where hyper-inflation terrorizes the dollar in a year or two and on top of that, we the taxpayers are getting a deal that’s almost guaranteed to not make us money. Welcome to your US government. It’s gonna be a fun ride.

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:

        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.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

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