twitter
    Find out what I'm doing, Follow Me :)

Your Language Shapes Your Thought

I recently finished reading “Outliers the Story of Success”, the latest book from Malcolm Gladwell. This is not a book review. But there are a couple of thought provoking ideas in this book that struck me. It made me think how it is applicable to this world of programming.

In the “Rice Paddies and Math Tests” chapter Gladwell try to present answers to this stereotype: Why are Asians good at math?

Gladwell started by quoting example from Stanislas Dehaene's book "The Number Sense” . He asks reader to try to memorize this sequence of numbers: 4,8,5,3,9,7,6 in twenty seconds. He states that English speakers would have 50% chance of getting it right. However if you speak Chinese, you're almost certain to get it right every time. Why so?

This is where things getting interesting. It’s a fact that people have about two seconds memory span. We can easily retains information that we can fit in two seconds. Here cultural or linguistic background doesn’t matter. What does matter is how many numbers you can comfortably fit into those 2 seconds. Chinese speakers get that numbers sequence right every time because—unlike English speakers—their language allows them to fit all those seven numbers into two seconds.

I’ve observed this with my Chinese friends. They can recite and write down 10 digit phone number easily while I always need the last half to be repeated.

Gladwell further argues that, the numbering system in Chinese is much more logical than in the western languages. It matches nicely with the decimal numbering system that we use in Maths. Chinese, for example say two-tens three for 23. While English speakers say twenty three. Ten two for 12 vs twelve. Math is therefore thought of, spoken and written all in the same way.

It makes learning math a much more logical and much less daunting process. Same thing with the concepts of multiplication, division, and fraction, it fits perfectly with the Chinese language.

How does it relate to programming? I’ve observed, while reading other people code, that their programming language background define the kind of abstractions they will use. It influences how they will solve a particular problem.

For example, while reading Java code written by people with functional programming language background, I’ve found they used anonymous inner classes repeatedly. Which I rarely found in code written by programmers which solely program in Java.

I found myself start to use some anonymous inner classes in Java or lambda expression in C# after learning Ruby, Python, and LISP. I started to use higher order function more in my Javascript code. Why? because the concept of passing a function as parameter by using anonymous inner classes in Java or delegate in C# pre 3.0 is a very remote concept for me. The syntax is clunky. Hence, it doesn’t come intuitively to use this abstraction in my code.

However, when I learned Ruby, or especially LISP, it is used everywhere. The syntax is so much more intuitive. Thus, I started using it more and more.

Joel Spolsky in his article “The Perils of JavaSchools” states:

“Without understanding functional programming, you can't invent MapReduce, the algorithm that makes Google so massively scalable. The terms Map and Reduce come from Lisp and functional programming. MapReduce is, in retrospect, obvious to anyone who remembers from their 6.001-equivalent programming class that purely functional programs have no side effects and are thus trivially parallelizable.”

The point is, your language defines your thought. And real world experience is a big influence to both your thought and language.

As the book itself. It is a great book. It is informative, entertaining, and thought provoking.

Programmer Life wasn’t meant to be easy, but take courage: it can be delightful

I spoke to a developer the other day. I was very surprised when he said that a growing bug lists is just normal, that daily firefighting againsts bugs is just a fact of life. No I don't accept that.


Probably, I am just so infected by all the agile values, that I cannot accept them anymore. My first encounter with Agile (Extreme Programming) was during a user group day in Melbourne around 2002. That time I was just recently graduated from Uni. I attended a presentation about Extreme Programming by Steve Hayes. Steve and his co-presenter (sorry, I can't remember his name), showed us how to do pair programming and how to unit test our code using JUnit.


Me and my flatmates were very excited, when we get back we started doing pair programming and we unit tested most of our code. We were developing a project scheduling application using genetic algorithms at that time. Our software development philosophy back then were pretty much: code and then test it by clicking the GUI of our application. What a waste of time.


And last week on 7 May 2009, I attended another Steve's presentation during JAOO conference in Sydney. It was a very inspiring presentation titled “How your choices influence your agility”. In the talk Steve quoted Former Australian Prime Minister Malcom Fraser most famous quote "Life wasn’t meant to be easy". And then Steve said " but that is b...s...". I couldn't agree more.

It turns out that Malcolm Fraser was quoting George Bernard Shaw's, “Life wasn’t meant to be easy, my child, but take courage: it can be delightful!” It is rather unfortunate that Fraser hijacked it.

Then, Steve moved on and said “A characteristic of high performing agile teams seems to be how much they value ease of work”.

If your bug lists keep growing and your job is dreadful, do something about it. Make it at least somewhat enjoyable and gradually improving it. Uncle Bob talk about this during his keynote talk titled Clean Code at last year JAOO conference. He mentions the boy scout principle: "Leave the campground cleaner than you found it".

So next time you check out the code, please check it in in a slightly better shape. How about writing unit tests for a start! Do it over some period of time, you'll find the code much easier to work with.

Don’t be a Blub

I like to learn new things. As a programmer I have this affinity toward shinny and cool things. I remember while still at Uni, I used to tell my friends that we should learn both J2EE and .Net, then we wouldn’t have to worry about getting a job.

However, not until after reading The Pragmatic Programmer: From Journeyman to Master book by Dave Thomas and Andy Hunt, my affection for learning programming languages goes to the next level beyond Java and C#. Among other excellent recommendations/advices in this book , Dave and Andy taught us in their "Your Knowledge Portfolio" section that in order to be well-rounded developers, we need to set a goal to learn at least one new programming language every year.

They also suggests to treat your skills like an investment portfolio so you should:

  • Invest regularly
  • Diversify portfolio
  • Balance portfolio for risk
  • Buy low/sell high (be an early adopter of new technology)
  • Rebalanced you portfolio periodically.

Why a year, what can you learn in year you would ask. Won’t we just turn into a jack of all trades? No, the idea is not to be a master in all the languages. No one can do that. But the ideas is to open your mind to different way of thinking through and solving programming problems. So it is much more than just learning different kind of weird syntax. Learning the syntax is one of the most easiest task.

Instead try learning different concepts and abstractions in these different languages. Learn about lambda expression in language such as LISP, Python. Learn about mixins in Ruby. Learn about actor/agent in Erlang. Learn about curry function, monads, fixed combinator in Haskell, or OCaml. Learn about macro in LISP.

In fact, you are in dangerous becoming a blub programmer otherwise. Blub programmer is a concept coined by Paul Graham in his article Beating the Averages.

Paul argues that some languages are more powerful than others and there exists a hypothetical mid level power language called Blub. He describes the Blub programmer as:


“As long as our hypothetical Blub programmer is looking down the power continuum, he knows he's looking down. Languages less powerful than Blub are obviously less powerful, because they're missing some feature he's used to. But when our hypothetical Blub programmer looks in the other direction, up the power continuum, he doesn't realize he's looking up. What he sees are merely weird languages. He probably considers them about equivalent in power to Blub, but with all this other hairy stuff thrown in as well. Blub is good enough for him, because he thinks in Blub. “

Although It is debatable that there exists such a continuum in programming language power. But the concept of Blub programmer succinctly described the dangerous of becoming a narrow focused programmers.

Most people without the experience of a programming language can't see that language features as really useful things. If the only language I know were C# and the only web framework that I know were ASP.Net probably I would still thing that ASP.Net is the best web framework in the world. I wouldn’t know it is such a leaky abstraction of web programming. I wouldn’t know there are better alternatives. And if all other .Net developers thing the same way, Microsoft wouldn’t have to fix it and created ASP.Net MVC.

I also found learning different kind of abstractions in different languages increases my knowledge and understanding of the main languages that I used(Java and C#). It is not until I learn the concept of pattern matching that I really understand the Visitor pattern.

The other day, I showed my program that does screen scraping(written in Ruby using Mechanize and hpricot) to a Java developer. I used Ruby for this app because I think it is the right tool for the job at hand.

To my surprise he screamed what is that weird looking syntax and hard to understand code, you should have used Java. What, Ruby syntax is weird and hard to understand? That is the weirdest things I've ever heard. Beside that, how long it would take to write the same app in Java. Three or five times at least and with much more noise. Or could it be that I’m the Blurb programmer. What a paradox :)).

Departing thought, don’t think in Blub!

Hibernate EntityManager and ApplicationException

Last year we had successfully migrated our legacy web applications to EJB 3 on JBoss. EJB 3 has simplified Java Enterprise development greatly. However, the learning curve is still high. Among other things that we have learned, you really need to understand how the EntityManager works in different settings.

EJB 3 let you to declare EntityManager dependency using annotation @PersistenceContext. Then the container will inject the EntityManager to your class. This is all good but we have to be mindful, there are two types of EntityManager i.e: Extended and TransactionScoped. By default when we annotated the EntityManager dependency, what we will get is TransactionScoped EntityManager. It means all the EntityManager operations, including persisting new beans, removing beans and updating beans, are cached in memory. They are synchronized to the database in a single batch when the current thread completed, or before the next database query is issued, whichever comes first.

But for some users, it might be desirable to manually control and optimize exactly when the database is synchronized inside a thread. For instance, you might want to send in the changes earlier to reduce memory use and avoid large cache replication in clusters. Inside a transaction, you can always flush the current in-memory changes in the EntityManager to the database by calling the EntityManager.flush() method.

The second type is Extended PersistenceContext EntityManager which allow us to cache all database updates from multiple method calls (i.e., multiple threads), and only commit the changes in a batch when the application finishes a logical session (e.g., at the checkout time of a shopping cart). To get this type of EntityManager, we could annotate our class with @PersistenceContext(type=PersistenceContextType.EXTENDED). The Extended EntityManager will commit the changes only when it has been removed.

We also need to tell the JTA transaction manager NOT to commit any updates to the database at the end of each thread. We do this by giving each database related method the NOT_SUPPORTED transactional attribute. The EntityManager should only update the database when the stateful session is destroyed. We do this by annotating the method with @Remove.

The write behind strategy of the EntityManager doesn't allow us to throw a checked exception to the client. There is a code inside org.jboss.ejb3.tx.Ejb3TxPolicy class.

    public void handleExceptionInOurTx(Invocation invocation, Throwable t, Transaction tx) throws Throwable {
if (t.getClass().isAnnotationPresent(ApplicationException.class)) {
ApplicationException ae = (ApplicationException) t.getClass().getAnnotation(ApplicationException.class);
if (ae.rollback()) setRollbackOnly(tx);
throw t;
}
if (!(t instanceof RuntimeException || t instanceof RemoteException)) {
throw t;
}
setRollbackOnly(tx);
if (t instanceof RuntimeException && !(t instanceof EJBException)) {
throw new EJBException((Exception) t);
}
throw t;
}




As we can see to throw a checked exception we need to:-


(1) - manually Flush the EntityManager at the point the transaction is committed and any exceptions will be gratefully received


(2) - When throwing a checked exception inside your session bean ensure that you annotate it with @ApplicationException. This allows JBoss to recognise that this is an application checked exception and not an unchecked runtime exception. Jboss will then deal with the exception accordingly. For more details read this forum thread.

Back to Basic


Most developers know all the fundamental database design stuffs like Primary Key, Foreign Key, Unique constraints, etc. But surprisingly not many of us really put much attention into it. It may not be the coolest thing. It is so 1960s. It ain't Web 2.0. It ain't ESB. But it is the simplest tools that can save you from future pains. Ignore this at your own perils.

Recently, I have been involved in a project to migrate our main web application to EJB3. The architecture of the existing applications is quite advanced for its time. It uses all the good design patterns stuffs like ORM, Dependencies injection, and SOAP webservice. It is full on design patterns. It externalizes all the dependencies into an XML configuration file. Just like Spring but in a much painful way :)). It had so many layers of indirection for any simplest use cases. It was an overengineering gone mad.

It doesn't achieve its goal of flexibility. The flexibility is there, but it is so complex, that makes it so painful to change anything. Just the opposite of what the intended effects are. What can I say. KISS (Keep It Simple Stupid) is the most beautiful thing.

Yet these weren't the worst problem of this application. It is the data integrity or lack thereof. Most of the tables have neither primary key nor foreign key constraints.

The database were full of duplications. It violates every business rules. Mind you, this database supposedly to control authentication and authorization of our main websites.

Lessons learned:

  • Never design a table without primary key.
  • Put all the constraint directly into the database or just don't use relational database.
  • Strive for the simplest solutions. That is often all you need.


ASP.Net Failed to start monitoring directory changes

If you run ASP.Net ASP .NET application that is stored in a remote file share and attached via Universal Naming Convention (UNC) paths, you might intermittently get the following error:
"Failed to start monitoring directory changes".

ASP.Net uses the Server Message Block (SMB) protocol to monitor files changes. So that it can reload the application if you made changes to your aspxes, asaxes, or dlls.

However, when more SMB requests exist than the Web server can process, your Web application may unexpectedly restart or you may even lose the session object. To fix the problem,
increase the number of connections that you can have by resetting the registry values of the MaxCmds and the MaxMpxCt keys.

Locate and then click the following key in the registry:
HKLM\SYSTEM\CurrentControlSet\Services\LanmanWorkstation\parameters
If MaxCmds and MaxMpxCt keys do not exist, it will use the default value for the client (MaxCmds) which is 50.

So you need to manually adding MaxCmds registry key. The recommended values are 253, 1124, and 2048 to the maximum of 65535. And you have to do the same things with MaxMpxCt keys.

Repeat the above steps on your remote file share server.

For more information refers to:
http://www.kbalertz.com/kb_843584.aspx

Server failed to load application

This week we rolled out Windows XP migration to all our PCs. I had to reinstall all the tools that I have been using since it is not part of the SOE. Everything was fine , eventhough reinstalling Visual Studio .Net took almost half a day , until I tried to run one of my ASP.Net project.

It just doesn't want to run. After turning off the "show friendly error" in IE, it displayed the error message "Class is not registered".

Next, I checked the IIS extension mapping, there is no ASPX registered there. So, I reinstalled ASP.Net by running the command "aspnet_regiis -i". Still doesn't want to run.

After googling for a while, I found this link. It told me to recreate IIS Package, so did I. Indeed, it start to work again.