Monday, February 9, 2009

Doing it wrong, fun with Clojure

I certainly take a liberal approach to programming, some may call it quantity over quality but I like to enjoy everything I do. This may include doing it wrong, but sometimes I like the end result.

I am working on a SWT GUI tool and could not figure out a clean way to avoid SWT's illegal thread exceptions. When a user clicked a menu option on the gui, I needed to first take text from an edit box field and then use that data to pass to a long running thread. This is what I ended up with:

;; Define the clojure 'agent', save the state of our search box
(def *search-text-state* (agent nil))

;; Define an implementation of 'SelectionAdapter/Interface'
;; SelectionAdapter requires one method 'widgetSelected'
;; With display.syncExec, pass an implementation of java.lang.Runnable
;; The thread updates the 'agent' search-text-state text.
;; After we get this text value, then launch another thread 'start-findgrep'
(def findgrep-listener
(proxy [SelectionAdapter] []
(widgetSelected [event]
;; With the async call, set the agent value
;; based on the search box value
(. display syncExec
(proxy [Runnable] []
(run []
(let [val (. search-box getText)]
(send *search-text-state* (fn [_] val))))))
(let [widg-str (str (. event widget))]
(. (new Thread
(start-findgrep-thread widg-str @*search-text-state* true)) start)))))

Basically, in 10 lines of Clojure Lisp code, we implemented two method class (proxy) bodies and invoked a thread. It doesn't look pretty, but that is still pretty powerful that you are able to do that.

Saturday, February 7, 2009

Test Driven Development, BDD and Junit

Here is a question I asked on stackoverflow. The responses are pretty interesting.

I don't know if you read the recent post by Joel, but basically he says that you don't need unit testing for every unit or piece of code in your code base.
"And that strikes me as being just a little bit too doctrinaire about something that you may not need. Like, the whole idea of agile programming is not to do things before you need them, but to page-fault them in as needed." -- Joel
And I would say that most developers don't have full coverage anyway, yet there are some blogs out there touting the 100% code coverage.
"Yeah. In fact, if you're making any kind of API, a plug in API, it is very important to separate things into interfaces and be very very contractual, and tightly engineered" -- Joel

"Right. The longer I think about this, the less I care about code hygiene issues (laughs). Because the unit of measure that really matters to me is, how quickly you can respond to real business needs with your code." -- Jeff
On Kent Beck:

"It doesn't seem like you could actually get any code written if you're spending all your time writing 8,000,000 unit tests, and every single dinky little class that you need to split a URL into four parts becomes an engineering project worthy of making a bridge, where you spend six months defining 1000 little interfaces" -- Joel.

My question is this, do you agree with Joel? Do you spend a lot of time writing unit tests with full coverage.
He is my stance. I believe in more BDD, Behavior Driven Development. For me, I prototype a piece of code, establish some setup code and then execute a bunch of these driver routines. If something goes wrong, an exception or unexpected behavior then that is flagged as fail and otherwise a pass.

The junit style testing:

assertEquals(a, b);

Just seems so unnatural for a large majority of developer testing. Don't get me wrong, I can see where junit style testing is a good idea. But for a majority of business web applications. I can't imagine trying to retrofit a unit test to every piece of code in your application.

I was looking at scheme lisp implementations in Java. Scheme has a well known set of standards and a lot implementations attempt to be scheme compliant. So a junit test for scheme compliance is a good idea. You could do something like this.

assertEquals("3", "(+ 1 2)")

If it fails, the scheme implementation is not compliant, a very useful unit testing metric.
For something like a web application, you might have an action method 'process' that does a bunch of stuff. Connects to a database, sets up html form variables. On and on. How are you going to unit test that. You can and I am sure most will, but it will be so unnatural and won't keep up with the pace of the project. I can see where backend code (whatever it is) could be tested but the plug and play MVC web apps (think Spring MVC or Struts), I just don't see full test coverage, especially the junit kind.

Edited - To Charlie:

Let me get a little bit more specific. This is an example from wikipedia on unit testing, this is a good example and use of the 'junit' approach. The goal of the adder is to properly add two integers together. This might work as a good test of a interpreter.

public class TestAdder {
public void testSum() {
Adder adder = new AdderImpl();
assertTrue(adder.add(1, 1) == 2);
assertTrue(adder.add(1, 2) == 3);
assertTrue(adder.add(2, 2) == 4);
assertTrue(adder.add(0, 0) == 0);
assertTrue(adder.add(-1, -2) == -3);
assertTrue(adder.add(-1, 1) == 0);
assertTrue(adder.add(1234, 988) == 2222);

But, lets say you are writing a business web application. The ultimate goal is to display a user's bank account information to them. All bank transactions, their savings account information, their credit card info, render it to the browser.

Ideally, like the adder application, I guess you could do:

assertTrue(getPage() == displayedCorrectly())

I guess that is at a higher level of granularity. That 'getPage' might include database calls to the bank account table. Or invoking the server page and sending over the form values. Invoking the web framework and make sure the right parameters are established. Etc, etc. What 'unit' do you test in that case? And I read some TDD articles where you normally are not supposed to write tests that includes code where you make a remote connection (like a database server), write to a file, etc.

Well? if you can't connect to the bank database because you hard coded the wrong database name, seems like a very valid test to check for?

I am not implying anything, these are just points where I am confused on how to about unit testing for more complex apps.

There were some good responses:
"Behaviour Driven Development (BDD) is about specifying the behaviour of the system by writing exectable specifications in the form of tests. When there is a thing that the code does not yet do, but it should do, then you write a test/spec for that thing, which in turn helps you to design code that passes the specification. For example to specify a ball, you might have specs (test methods) such as "the ball is round", "when nobody touches the ball then it stays in place", "when somebody hits the ball then it moves in the opposite direction", "when the ball hits the floor then it bounces" etc." -- Esko Luontola

"More on TDD, test-driven design. It can be a little confusing, because it requires a really different way of thinking about the low-level coding process; that's why I prefer to think of it as an extensional definition of a specification rather than "design." ("Extensional" in the sense of an extensional vs intentional definition of a set.)

"We're used to thinking of unit testing as "code then test." With TDD, it's really "test, then code." You write a test for code that doesn't yet exist; by doing so you define a specific bit of the behavior of the code. For a real bumpo example of this, let's code a routine that tests a number for being an even number. You start knowing the definition of an even number, ie, n is even if n ≡ 0 mod 2." -- Charlie Martin

Friday, February 6, 2009

Cher is a moron, or why I hate group-think hate on other groups

Cher is a moron.

"You know what? I have so – I try to be charitable and there are some really good Republicans, but I just don’t understand how anyone would want to be a Republican. I just can't figure it. I don’t understand," she said. 'If you’re poor, if you're any kind of minority – gay, black, Latino, anything. If you’re not a rich – I don’t know. If you're not a rich born-again-Christian, I don't get it."

I have a comment for you, musicians shouldn't discuss politics. How does that feel? Doesn't make a lot of sense, does it?

I feel that there isn't such thing as a "Republican", the same way that I feel there is no such thing as a "black" person or a "white" person. Race is also unscientific and difficult to categorize people.

You have genealogy which can be traced from generation to generation, but that doesn't mean anything. For example, people say that Barack Obama is black. Clearly Barack Obama is not black at all. Based on the genetics of his parents, it is just as easy to say he is white.

There is no such thing as a Republican or Democrat, especially in modern day America. It is a non-concept and we should do best by getting rid it. We put way too much importance on these dirty party affiliations. It isn't in the scientific journals. There is no DNA associated with a Republican. It is just a coined up term as a quick and cheap way to lump people into groups. But all it does is to add to the confusion and not get us anywhere as a country. The worse culprit of these oh so scientific terms is the media. And don't get me wrong, Barack Obama is a smart person but he didn't win the election completely by himself, especially early on. The media certainly gave him an needed extra push. Let me say it another way before you disagree with me. The media lost the election for McCain and Hillary Clinton. An Obama speech is just a little bit more exciting than a McCain one, regardless on what was said. Just remember, Obama was behind when facing Hillary Clinton, John Edwards and the others. But after a couple of wins, the media focus was clearly on him. And give credit to Obama for not slipping up when it was. YES, I know that there is an actual 'Republican Party' and 'Democratic Party', liberal and conservative. But is it useful to call somebody 'a Republican, a Democrat'. My use of the term 'Republican' is probably much different than Cher's usage. Cher may not discount 'Republican' so easily. People are going to study the work of Bill Bennett, Newt Gingrich, David Gergen (yes, I picked popular TV personalities). I would love a political debate between Cher and Newt Gingrich and see which comes out looking half-way intelligent.

"There is no such thing as a Republican"
How so?

Take Rush Limbaugh. Rush Limbaugh has been making money by disagreeing with the left. Who knows what he is really like. I guess that he wants to put on this republican front to get rich.

Sean Hannity does seem like a die hard republican. Especially with his love for Reagan and all.

Even if you vote Republican (when I mean 'you', I mean idiot America), doesn't even really mean you understand the Republican principles. You probably voted Republican or Democrat for a variety of different reasons. Probably very little to do with your devotion to core Republican or Democrat ideology.

Please, I would love for someone to dispute these facts:

1. Barack Obama is not 'black'. Race is stupid, so I say this. Obama's dad is from Kenyan. Obama's mom is from Kansas and has European ancestry. Is this a fact?
2. There are 300 million people in the country. 131 million people voted. Is this a fact?
3. 46% of the country voted for John McCain. That comes to 59 million people. Is this a fact?
4. Are all the 59 million people that voted for John McCain a republican. Is this a fact? Answer, can not be determined.
5. George Bush is 62 years old. Is this a fact?
6. George Bush went to Yale and got all C's in History. Is this a fact?
7. "He was the 46th Governor of Texas from 1995 to 2000 before being sworn in as President on January 20, 2001." Is this a fact?
8. Before 1991. George Bush really didn't have a political career. Is this a fact?
9. Is George Bush a republican?

Here is my point with the questions.
I believe George Bush is a poor leader and listening to a whole staff poor leaders, but I don't believe that ALL Republican leadership IS bad. E.g. I don't believe all Republicans in Washington are bad. For example, Ron Paul has given me the best insight into politics and economics that I could have ever imagined. He is a Republican.

People are stunned when I tell people about Austrian Economics and go "hey that makes a lot of sense".

I don't really believe George Bush is a republican. I believe Barack Obama is more "Democrat" the Bush is Republican. Why? Because Barack got into politics and Democratic agendas early on and has kept Democrat associations.

Why do I care? I just hate it when people lump people into groups and then hate the group. People hate Christians. People hate Muslims. People hate Black people. People hate Republicans. People hate Democrats. People hate Iranians.

But there really isn't a way to determine if a person is a Christian, Muslim, Black.

Here is why I care and isn't to defend the Republican brand. It is to defend the 59 million people that Cher seems to hate.

Take the Muslim Americans. The reason why you are not a victim of Muslim extremists is because of the Muslim American community. Local police officials regularly get tips about bad Muslim activity and the good Muslim Americans normally report these issues. This is according to former CIA operative Robert Baer. He believes there hasn't been another 911 because the Muslim American community loves America and has done a lot of good.
With that being said, the right-wing (and left) media seems to spread the idea that 'Muslims' are bad people. For example, if you see a discussion about 'Muslims', it is normally going to be about terrorism.

I have a couple of big pet peeves, the mob and mob mentality is one of my big ones. I hate when people arbitrarily lump a population of people into a group. If it isn't for scientific purposes, maybe census purposes than it is completely bogus. (Yea, maybe the census does loop people by race and political affiliation but I guarantee it isn't for anything meaningful). I especially hate these categorizations when they are completely wrong. For example the meme, "Barack Obama is black". Since 'black' doesn't really mean anything I guess that is the way we operate these days. Half truth gets rounded up to truth. I noticed that Barack was quick to correct people when they said he was Muslim but didn't hesitate to accept that he was black. I found that kind of convenient or more importantly, a very good political play. When 95% of the 'black' population votes for you, I guess it would be kind of hard to say, "Hey hold on guys, I am not really black". And lets not forget that Bill Clinton was the real first Black president. I can't help but roll my eyes at that one. Since we are discussing religion, I want to believe that Barack Obama is an Atheist. Not in he what he has on his resume but I am basing my hypothesis on how much attention he has really given to a 'Christian' message. Barack is a smart guy and not afraid of science. He has discussed green energy plans, wants to appoint a CTO, knows how to use a blackberry, and even knows about the Bubble Sort (see the recent Google video with the Google CEO). But at the same time, he probably wants to go along with the tribe, attend church every once in a while (don't forget the blackberry), as to not piss a lot of people off. I want an Atheist, scientific leader; great for me, not so much for the American tribe.

Sunday, February 1, 2009

Outline for "Enterprise Development with JVM languages"

I am creating a book, "Enterprise Development with JVM languages"

I am an "Enterprise" (I know people don't like that term) developer and about to start a book that deals with JVM languages for use on larger enterprise applications, especially geared towards Web and Database driven systems.

I want to cover a lot of the JVM languages that are used today, I don't feel that a book has truly covered these topics. If I am an Enterprise developer and am considering a JVM language, I would want to see how the language compares with other languages, possibly with the Java language. How could this language be used? What are the advantages, disadvantages, etc?

I have also noticed that some developers are religious about the programming language they use. I am not partial and just want to find the most productive tool for the job.

I want to cover the following languages, I know there are a lot, but a developer may decide to investigate all of them or use a combination of them.

Scala, Clojure, JRuby, Jython, Groovy, SISC

Smaller Sections: Rhino, ABCL Lisp, JScheme, Bytecode Engineering, Kawa, Antlr write your own own language

Clojure proposals for the JVM?

Other topics:
Mono, Factor

I will not cover beanshell and not cover groovy with too much detail.

Topics covered for each language:

  1. Overview
  2. Hello World applications, Socket Client Apps (RSS reader?) and general syntax possibly
  3. Syntax, Algorithms with the JVM language, Project Euler examples
  4. Approaches for Java interoperability
  5. File I/O
  6. Database driven web development (Spring, Hibernate, JDBC, etc etc), Online Security
  7. Web services
  8. GUI (Swing/SWT) development
  9. OpenGL and game development
  10. Benchmarking a JVM language
  11. Terracotta and the JVM
  12. IDE setup with Textmate, VIM, Emacs, JEdit, Eclipse
  13. XHTML to PDF/Image conversion with XHTMLRENDERER.

I know there are a lot of topics and it could be a huge book, but I refuse to leave out any of those sections.

Hmm, can you cover 12 languages in one book.

Edited-1: I am probably going to remove the 'enterprise' bit. If I cover 'games' in here, then that is not really enterprise.

Edited-2: I really want to cover a lot of topics, including programming basics and advanced features. My mini scheme is an implementation of scheme with pure java code. A alpha JVM language, that covers interpreters. But, I also want to cover compilers as well. I am just not that well versed on the subject. Should I go for it anyway. Maybe covering a java forth? Hmm.