Monday, February 28, 2011

Javascript tip of the month

I was reading an article I saw linked to from a blog I follow:

http://devblog.blackberry.com/2011/02/thanks-for-the-open-letter-to-rim-developer-relations/


Ironically, it's a response to an open letter to RIM about how difficult it is to use their PlayBook developer tools. I was doing other things when I noticed that page will still loading, so I took a quick peak into the html, and saw this:


<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js" type="text/javascript"></script>


This should go without saying, but don't do that! That's not the only offense, but certainly the most costly in terms of loading a page. There is actually another section that reference jquery locally, so I'm not sure how that got in there, but it has a pretty huge negative impact to have to download the unminified version of jquery from jquery.com.

Thursday, February 10, 2011

Case Sensitivity: MySQL and Spring Batch

I've been having some fun lately with multi-platform development. Personally, I develop on a Mac. However, I also have an Ubuntu workstation that runs a MySQL instance among other things that I can connect to at home. It's a nice setup, since production is ultimately running on ubuntu server and I have always advocated developing on an environment as close to production as possible. (Our build server is Ubuntu as well) However, one of the other devs uses Windows and has MySQL installed on his Windows machine as a development environment. This can create interesting case-sensitivity problems. Anyone using the default Mac setup (HSF+) or Windows have case-insensitive file systems. (Although, I run a case-sensitive variant of HSF+ on my mac) This can be important if you are also running MySQL on that system, as described below:

In MySQL, databases correspond to directories within the data directory. Each table within a database corresponds to at least one file within the database directory (and possibly more, depending on the storage engine). Consequently, the case sensitivity of the underlying operating system plays a part in the case sensitivity of database and table names. This means database and table names are not case sensitive in Windows, and case sensitive in most varieties of Unix.


This can be especially interesting in Spring Batch. By default, SB uses all uppercase table names. An example being BATCH_JOB_EXECUTION. If for example, your windows colleague creates the tables in lower case on his Windows system, they will work fine for him/her, since the underlying file system isn't case sensitive. However, if you take a data dump from his system and put it into your linux mysql instance, running batch jobs will result in the following error:


com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'dbmaster.BATCH_JOB_INSTANCE' doesn't exist


Or something similar. What could get even wackier is if you change the prefix of your Spring Batch tables. Setting the prefix to something like 'awesome', would result in SB querying for a table such as 'awesome_JOB_EXECUTION', which again, would work fine on Windows but blow up anywhere else. (Unless you happened to create the table in alternating caps)

There are some options, however. There is a property in MySQL called 'lower_case_table_names' that allows you to configure how MySQL deals with table name resolution:

  • 0 - tables are stored on disk using the letter case defined in your create table statements and all checks are case sensitive. (This setting could be problematic on non-case sensitive systems)
  • 1 - all table names are stored on disk as lowercase regardless of how you define them in your create statement, and any table names in queries will be converted to lower case
  • 2 - table names are stored as defined in the create statement but mysql converts them to lowercase on lookup. Which obviously won't make a difference on a case-insensitive system.
Note: lower_case_table_names was introduced in MySQL 4.0.17

On my ubuntu install of MySQL lower_case_table_names is set to 0, which I assume to be the default. However, you can check your system easily by entering: 'show variables;' into mysql.

On my local ubuntu instance I create a file called: "/etc/mysql/conf.d/lower_case_table_names.cnf". The .cnf ending is important, as that is what my.cnf is setup to look for, at least in my setup. In the file is the following:


[mysqld]
lower_case_table_names=1


The [mysqld] is important as there are numerous engines which are configured in the same file. For example, you can all create settings for mysqldump specifically as well. Next time you bounce mysql, if you make another call to 'show variables' you should see the updated setting. After this change, even though Spring Batch is making SQL statements in all caps and the tables are actually stored in lower case, the queries will still work.

Amazon RDS

It's worth noting that if you use Amazon's RDS service that you must use their web console (or command line tools) to modify this setting. It's under parameter groups. The following guide from amazon should help you: http://aws.amazon.com/articles/2935

Thursday, February 3, 2011

Sharing overload?

First: Watch this hilarious VW spot on YouTube: http://www.youtube.com/watch?v=R55e-uHQna0

Second: Press the share button below the clip.



On the top we've got how many people shared this link on twitter, facebook or buzz. Do we really care about that?

Then the most important bit, the actual link, which we can shorten. Although I don't know why, doing so cuts the size down by 25%. I've always thought YouTube links were some of the simplest out there: youtube.com?watch?v=SomeRandomNumber. "Shortening" it turns it into: youtu.be/SomeRandomNumber. Go Belgium I guess.

And then there's no less than 9 places I can share it. Half of them I haven't heard of. And really, Myspace?

Can I just have a link?

Tuesday, February 1, 2011

Speculative Generality

In my last post I talked about Velocity Anchors, and how applying metaphors such as technical debt takes away from the real problem, that developers want to get stuff done, and there's things slowing them down. I also mentioned a couple of causes, WTF and Code Jenga. WTF representing code that developers waste time having to figure out before they can get stuff done, and Code Jenga being the fear that even the smallest change will cause cascading failures and a lot of wasted time figuring out the problem. Time that could better be spent getting stuff done. To continue the series, I'd like to talk about one of the biggest causes of WTFs: Speculative Generality*. What speculative generality means is the inclination to speculate that some piece of functionality will be generally useful. I found a great illustration of this concept recently, even if it wasn't named that, from the chaosinmotion blog: How (not) to write factorial in java. Its a great post that uses some solid coding examples to illustrate the point, and I think his conclusion really nails it:

The biggest complaint I have with many Java developers is that they develop a whole bunch of really bad habits. Specifications are unclear, or they think someday the code may need to be extended into a different direction. So they write a whole bunch of overblown architectural nonsense, sight unseen, thinking that the additional crap someday will help out and make things easier. And Java as a language lends itself to doing this very quickly and easily, so that (as the theory goes) it’s easy for us to build architectures that will someday make it easier on us in the future.

But the future never gets easier, does it?

Later, in responding to comments, he presents his theory on why this tends to happen so often in programming:

Java is simple to program. With a good IDE like Eclipse, Java is a pleasure to write programs in. It’s also quite easy to write some very complicated algorithms, and building a client/server stack is absolutely trivial.

But programmers like complexity. We like things that are complicated: the flashing complicated lights on the control panels on Star Trek, or the joys of figuring out a complicated calculator with nearly a hundred buttons or playing with cell phones that have dozens of modes and menus and buttons. Programmers are puzzle solvers, and we love puzzles. We love taking things apart, putting things together again: many of us are the ones who were punished at 8 for disassembling the family computer or television set or radio, despite the fact that we nearly put it together in working order.

We love complexity.

His essential theory is that Java is easy to crank out code and that deep down developers love complexity. There's probably something to that. However, with the exception of assembler, there hasn't been a real restriction in programming that prevents developers from writing too much code, at least programmatically. Perhaps years ago when you had to code in punch cards and mail them off to be compiled, or wait for cpu cycles on a mainframe, then your development environment was limiting you. Nowadays, I think any language could be guilty of making it easy to slap around a lot of architecture or abstractions you may not need. I also agree fundamentally with his argument about certain programmers loving complexity. There is certainly some nerd pride there, and in some cases legitimate attempts by developers to make themselves too necessary to fire. However, for the sake of argument, let's assume all developers are acting with integrity. That they're not doing something malicious to guarantee themselves a job, but are honestly doing what they think is right.

In my experience, what has caused the majority of the type of code he describes in his blog is the statement I've heard every-time I've questioned overly complex code: 'I know that we're going to need to do X in the future' There's always something that the developer just knows for 100% sure will need to be done. Next month we'll need to be able to swap in a new implementation of this algorithm. There might be a performance issue, so we'll definitely need to be able to swap out this strategy. I know there could be a lot of references in response to this that: You ain't gonna need it. However, I don't think that gets to the heart of the issue. I think the more important question is what's causing them to think that they are going to need it? Why aren't they thinking about the complexity cost they're putting into the code? The technical debt they're adding. Or WTF count, or whatever. The fact that the next developer in 6 months has to figure out why you made something simple into a pluggable architecture, figure out how it works, and then try and bolt code onto it. Why?

Architecture Astronauts

One motivation certainly comes from the architecture astronaut camp. You can spot these types in a few ways. Sometimes they'll come out and say: "I prefer providing frameworks, not mucking around with business rules". They'll usually also exist in some kind of special architecture group that delivers 'frameworks' to 'business groups'. They're also at varying levels of the organization. Some 'enterprise architects' make decisions to use things like SoA or to split your web application into 10 war files or a host of other decisions that lead to complexity. Their heart is usually in the right place, and they honestly believe they're making things better, but they're usually so separated from delivering actual functionality, to actual end users, that they don't realize how much pain and complexity they're causing. They don't have to deal with the outcomes. There are also architects actually writing framework code. But when you're an architect, architecture is the solution to every problem. You look for additional places to add architecture. You also almost never have to actually use it, so you don't feel the pain. And finally, there's the cause I've heard most often: 'The average developers here aren't capable enough to work with [insert technology here] so we need to provide an architecture to make it easier for them". Even though it sounds like it, in most cases they aren't trying to belittle their coworkers. In many organizations architects are the senior developers and 'application developers' are the junior developers, at least from an experience perspective. There could even be a case where architects are on-shore and application developers are off-shore, which is a whole other level of organizational pain. However, my main point is that they live outside of delivering business value. Their job is to deliver complex frameworks. I'll leave out the organizational challenges that often lead to these positions being created, but in my experience its one of the biggest causes of overly complex code. I could talk for a long time about this particular subject, but I'll try and stay on topic and move on to the next cause of speculative generality.

Inability to effectively refactor the codebase

Even when working with an application developer who is focused on actually delivering working code to end users and truly wants to deliver it quickly, you will find they have implemented a crazy abstraction for no good reason. As I mentioned earlier in this post, the common reasoning is: 'I know I'm going to need feature x, so I created the strategyfatoryplugabblewidget'. Ok then you say, why can't you add that new feature next month when you need it? In my experience, the true reason is generally because they know they'll be afraid to next month. The codebase is finicky, there aren't a lot of tests, the feature they're working on right now will have been signed off on, whatever. Bottom line is, they won't want to change it in the future for fear of breaking the existing functionality. The overall system makes it difficult to refactor any code at all. So, anytime they put in any new feature, they try and future proof all of it, so they just need to add one new class, or extend one abstract class or implement some interface and wire it in with spring. Whatever it is, they never change or improve the existing codebase. Their coding is just one long exercise in bolting in new classes and fixing bugs in existing code. Sometimes the motivation can be worthwhile. They don't want to waste time 'refactoring', they want to provide new functionality. Of course, this generally leads to complex and buggy code, which eventually catches up with them, causing work to grind to a halt, but they don't want to think about that now. And in their defense, without reasonable automated testing or an army of manual testers, refactoring code is really really hard. And the longer you let things go, the harder it is to refactor. It's a vicious cycle.

So what's the conclusion here? Of course, keeping hammering on the yagni principles. Continue to push to make sure you're doing the simplest thing that could possibly work. However, make sure you code is reasonably easy to refactor. Encourage refactoring in whatever way you can. Although, it's probably a separate post in and of itself about how to work refactoring into a general feature development cycle, but it can be done. And ditch the architects. Everyone is delivering business value, everyone is feeling the same pain. And don't accept complexity just because the code works. It has it's own cost.

* Credit where credit is due, I got this term from Ram Singaram, a former colleague at ThoughtWorks.