Technology


4
Sep 09

Using OSS in projects: beware of the passionate liars

This post summarizes a lot of what I have learned about using OSS in the projects I have had.

(Update June 2010: Just read a Hacker News thread about MongoDB, and remembered this post. I didn’t write it for any specific technology, but it applies pretty well.)

Do what is usually done – avoid unusual setups

Open source software, whether it is an MVC framework (like Ruby on Rails, CakePHP or Codeigniter), backend software (such as Apache or a particular library) or end-user software (like Gimp or OpenOffice) is generally built scratch an itch felt by the author. There is nothing bad about this. However, expect to run into problems if your intended use is different from whatever the software is usually used for – despite whatever “counterpoints” offered by enthusiasts of that particular software.

As long as you use OSS for things that are commonly done, you will generally save time and money using it. However, if you step outside the common use, you will probably run into issues that are a pain to solve. As someone said, Linux is free if your time is free. I love having Linux on my netbook as a cheap and lightweight server,  but I have quit searching for solutions to anything that doesn’t work out of the box (such as in my case, the WLAN drivers). It just isn’t worth the time when one can dual-boot Windows with little effort.

Now if anyone were to actually read what I write in this backwater blog, they would blame me for being a lazy moron, or tell me that this issue in particular is due to the hardware vendor not providing adequate support (or only providing proprietary binary drivers). Well, I don’t care whose fault it is. If you want me to use it (you probably don’t), then get the damn problem solved. Knowing that I could muck around for 2-3 days trying different solutions doesn’t really solve the problem. I appreciate the work (I really do!), but I will only use it if it works for me. (There is no law saying that just because you can do something with OSS, you must do it using OSS.)

Passionate liars

This applies to MVC and other development frameworks (and libraries) as well. They are built to match whatever their authors have been doing the most. A lot of web development involves simple CMS-or-blog-or-event-site type websites where having conventions and backend support such as ORM (object-relational mapping) is really useful.

The problem is that when evaluating different MVC frameworks, you will generally find someone who will insist that it can solve whatever key problems you need to solve in your project. They are lying.

As John McCarthy (Stanford) said: “Personal dishonesty is not needed to produce a dishonest business plan or research proposal. Wishful thinking suffices.”

GIMP really is not the same as Photoshop and OpenOffice isn’t Microsoft Office, despite arguments to the contrary. Enthusiasts aren’t really lying, but they are blinded by their enthusiasm for whatever technology they are using to the extent that they cannot imagine why someone might have completely different needs. It works for them, which says nothing about whether it will work for you in your particular situation.

The smart and conservative approach to evaluating the alternatives is to give a lot more weight to perceived concerns (such as technology performance, platform support and maturity) than to whatever optimistic assurances the enthusiasts of any community offer you. If you see a number of side-mentions of concerns such as “X does not scale very well”, or “X is hard to deploy”, or “X is very unstable”, or “X is not commonly used for Y”, take note. More likely than not, it is true to some (significant enough) extent.

Someone (can’t find quote) said that you can judge an organization by the problems people concern themselves with. Non-problems are generally not discussed nor are they debunked by passionate enthusiasts. Think about Java performance, or the usability of Linux-based OS installation. They were talked about a few years back, now they are essentially non-problems.



12
Aug 09

Semantic CSS naming best practices

I went through a number of articles as well as my own CSS files, and here are my suggestions for semantic CSS layouts (names of ID’s and classes) as an image.

semantic-css-conventions

There is an article that could be written on the information, but I can’t be bothered. View the full-size version here.


11
Aug 09

My favorite Xperia software & tweaks (with screenshots)

From the “this-might-help-some-random-stranger” department, here is a quick catalog of some of the applications I use on my Xperia (Windows Mobile) phone. With screenshots!

All of these are free except Syncplicity and Pocket Informant.

Update: Check out Opera Mini 5 as well – the beta is free, and is a ton better than Opera Mini 4 (which is included on Xperia). To install, visit http://m.opera.com/next on your mobile phone.

Entertainment

Slide2Play (S2P)

s2p

A simple but useful mp3 player: the best feature is, it activates automatically when you plug in the headphones and exits when the headphones are removed. Available for free from A_C.

Xperia Facebook Panel

facebook

Access to Facebook using an interface optimized for mobile use. Available for free from Sony Ericsson via the More Panels icon on the phone.

Productivity

Syncplicity mobile (m.syncplicity.com)

syncplicity

Syncplicity is a online backup and syncronization tool for computers; it keeps files in sync across computers and is awesome. They have a mobile interface (http://m.syncplicity.com/) which allows me to access files that I have on my computer on the phone (with no extra effort). Available as a subscription service from Syncplicity.

Internet Sharing (installed by default)

netsharing

Shares the internet connection via USB cable with a computer, allowing you to browse the web on your laptop using your phone as a GRPS/3G modem. Already installed.

NuevaSync (Google Calendar syncronization over the air)

Syncronizes my Google Calendar with the calendar on the phone, no docking needed! Their website may look really bad, but the free syncronization service is really good. No additional software installed, since they use Microsoft Exchange protocols to sync the data. Worth the extra effort of installing once; after that you get all changes on your calendar synchronized back to your phone. Available for free from NuevaSync.

Gmail Mobile (http://mail.google.com/mail/h/)

By adding “h/” to the address when making a bookmark, you get the mobile version of Gmail by default, which works great. (Works with Google Apps for Your Domain as well.)

WordPress Mobile Package (WordPress plugin)

wpmobile

After looking at the various blogging apps available for the phone, I found that it is much nicer to use a web interface. This is a mobile adminstration and reading interface for WordPress blogs. Installed on the blog, not on the phone, but works quite nicely on the phone via the Opera browser. Available for free from WordPress.org.

Pocket Informant

informant

Awesome calendar and to-do list for your phone, synchronizes with Outlook. Commercial software from WebIS.

Tools

KeePassSD

keepass

Encrypted, protected storage for passwords and other information. Synchoronizes with the desktop version of KeePass. Available for free from KeePass/SourceForge.

G-Watch

gwatch

A useful and pretty timer application. Available for free from Ageye.

HTorch

led

Controls the LED light on the back of the phone – makes for a decent light at the summer cottage. Available for free via Xda forums.

Tweaks

FingerKeyboard2

fingerkbd

Enlarges the keyboard so that it can be used without the stylus. Available for free via Xda forums.

FingerMenu

fingermenu

Enlarges the menus so that they can be used without the stylus. Available for free via Xda forums.

Slide2Unlock (S2U)

s2u

An awesome automatic keyboard lock, which can be opened by sliding the bar on the bottom. Also allows you to answer calls by sliding the bar, and shows appointments/SMS. Available for free from A_C.

XperiaTweak

xperiatweak

Tweaks a large number of settings on Xperia. Available for free via Xda forums.


7
Jun 09

6 great books on software development and programming

If you happen to know me, then you might be able to borrow these books from me, I am quite sure I have all these in my bookshelf – since they aren’t generally found in Finnish libraries and I prefer to own my books.

Software development

1. Rapid Development by Steve McConnell. A solid book on rapid development practices and best practices. It covers the “oh so common” errors well, and provides both hard data and further reading. Great!

2. The Art of Agile Development by Shore & Warden. A comprehensive and practical guide to Agile and a great reference for each of the different Agile/XP practices. I like that it is short, to the point and not bound by a particular approach.

3. Test-Driven Design by Kent Beck. A practical approach to Test Driven Design. For any developer who wants to learn why and how to do it right, this is the book.

Also good:

Practices of An Agile Developer by Subramanian & Hunt. A great book focused on the programmer’s side of Agile, the particularly nice parts are the ” how it feels” sections which also present the benefits of agile for the programmer. Could be good for training people for Agile in a new company, for instance. Does not address Agile from a project managers perspective though, and it probably shouldn’t.

Programming

1. Agile Software Development – Principles, Patterns and Practices by Robert Martin. Don’t let the word “Agile” fool you into thinking this a “soft topics” book. It contains a good discussion of principles and patterns and the focus is on programming rather than management. The principles of OO design are articulated very well. A great read.

2. Design Patterns by the Gang of Four. A classic on design patterns. The book covers a lot of basic patterns which any developer ought to know. The only negative is that the patterns are somewhat dated: many things that were very problematic in C++ are not as problematic with newer languages which support closures and dynamic typing. However, the patterns described here are truly universally applicable to OO design.

3. Patterns of Enterprise Application Architecture by Martin Fowler. A book which catalogues common patterns in enterprise applications. While I expected to find more new patterns than I did, this is a good book because being able to name the patterns that are in use in an application is very useful, and probably you will find a few patterns which are unfamiliar.

Also good:

Data Modeling Essentials by Simsion & Witt. A book on database model design. This book complemented with a book on performance in the specific database back end (e.g. High Performance MySQL) should give anyone a good basic knowledge of the DB principles that need to be taken into account when designing a database model.

Artificial Intelligence – a modern approach by Russell & Norvig. A fun book! Discusses alternative algorithms to artificial intelligence. If you plan on reading it, make sure you leave time to play around with the different approaches you will learn from reading it.


27
May 09

How to install trac, mercurial and trac-mercurial on Fedora Core 10

Here is a quick, up-to-date (as of 05/2009) guide to installing and troubleshooting trac, mercurial and trac-mercurial on FC10. I started with a fresh VM install for this guide.

1. Get the dependencies and tools

yum install wget
yum install python mercurial trac trac-mercurial
yum install python-devel python-setuptools python-genshi python-docutils python-pygments mod_python

2. Get easy_install to work on FC10

wget http://peak.telecommunity.com/dist/ez_setup.py

Here is the first (minor) pitfall. You need to force the update of easy_install. If you only run “python ez_setup.py”, then you will not get the easy_install command (bash will not recognize it). Run:

python ez_setup.py -U setuptools

3. Install the mercurial plugin

From: http://trac.edgewall.org/wiki/MultipleRepositorySupport

Check out the mercurial plugin:
svn co http://svn.edgewall.com/repos/trac/sandbox/mercurial-plugin-0.12

Build the mercurial plugin. From within the checked-out directory, build the egg file (it will end up in the dist subdirectory):

python setup.py bdist_egg

Check out the multirepos branch:

svn co http://svn.edgewall.com/repos/trac/sandbox/multirepos

Install the multirepos version of trac (from within the multirepos directory, as root):

python setup.py install

4. Get the development branch of Genshi

Here you will run into the error “No local packages or download links found for Genshi>=0.6dev-r960″. To fix this, you need to force an update of Genshi to the latest dev version:

easy_install -U "Genshi==dev"

5. Create your trac environment directory

For example:

trac-admin /path/to/trac/myproject initenv
chown -R apache:apache /path/to/trac/myproject

6. Setup Apache for Trac

nano etc/httpd/conf/httpd.conf

Add something like:

<Location "/trac">
SetHandler mod_python
PythonInterpreter main_interpreter
PythonHandler trac.web.modpython_frontend
PythonOption TracEnvParentDir /path/to/trac
PythonOption TracUriRoot /trac
</Location>

And if you haven’t added mod_python already:

LoadModule python_module modules/mod_python.so

7. Continue with the setup

From: http://trac.edgewall.org/wiki/MultipleRepositorySupport

Copy the mercurial plugin egg file to the <trac-env>/plugins directory. Make sure the following is somewhere in <trac-env>/conf/trac.ini, adding all the desired repositories to the [repositories] section:
[components]
tracext.hg.* = enabled[repositories]
my_repo_a.dir = path_to_a_repository

[trac]
repository_dir = path_to_default_repository
repository_type = hg

Note that when the repository .type is not specified, the value of [trac] repository_type is used.

8. Restart apache and test

service httpd restart

Navigate to http://localhost/trac .

Some possible errors

If you get something like:

Available Projects
    * attachments: Error
      ([Errno 2] No such file or directory:
'/path/to/trac/env/attachments/VERSION')

Check your TracEnvParentDir in httpd.conf, it is probably incorrect. (And restart apache.)

If you get the error:

"Unsupported version control system "hg": Can't find an appropriate component, maybe the corresponding plugin was not enabled? "

Check if you copied the TracMercurial-0.12.0.6dev_r7902-py2.5.egg file to the correct location. (And restart apache.)

References:

http://trac.edgewall.org/wiki/TracOnFedoraCore

http://trac.edgewall.org/wiki/TracMercurial

http://trac.edgewall.org/wiki/TracModPython

http://trac.edgewall.org/wiki/MultipleRepositorySupport

http://www.smallroomsoftware.com/articles/2008/2/14/trac-0-11-installation-on-centos-fedora


12
May 09

Internet Explorer and JavaScript code

Update: Added the song “IE is being mean to me” at the bottom of the post.

I don’t usually write “raw” JavaScript code, that is, JS code that does not use an existing framework -- mainly because using code that has been tested in multiple browsers is better in most cases than writing your own code from scratch and testing it with different browsers.

However, every now and then it seems a project is small (and non-commercial) so that using a framework would be overkill. These are the times when the differences between Internet Explorer’s JavaScript support and that of other browsers is painfully obvious. Here are two relatively recent examples and solutions:

1. The IE innerHTML and “Unknown runtime error”

The JS engine in IE is notoriously unhelpful when encountering errors. One thing in particular has bit me in the ass so many times it is not funny anymore. This is the occurrence of a “Unknown runtime error” when attempting to assign a value to an element in IE via the innerHTML attribute. For example:

document.getElementById('containing_element_id').innerHTML
    = '<a href="http://something">A link text</a>';

Looks innocent, doesn’t it? However, in IE this code may or may not fail with an unknown runtime error depending on the context. For example, if another link is already applied to the containing element, then this code will fail on IE. Firefox, Opera and Safari will perform some code cleanup and allow this to work as expected (the content of the element is changed to that link). IE, on the other hand, fails with little warning. The point is, IE is very strict when it comes to nesting elements -- in the specifications, nested links are not allowed.

This is not a bad thing in my opinion -- standards ought to be followed. The problem is, IE will not give you any clues as to what went wrong. If the code is more complex than in the example, and it mostly is, then this will take a long time to correct.

There is one way to diagnose whether this problem is due to a previous tag which ought to closed per specifications: in IE8, you can use the debugging tools (F12) and set a breakpoint at the offending line of code. If the element itself can be retrieved via getElementById, then the problem is probably with a runaway tag somewhere earlier on the page. Once you find the offending tag (or the logic that generated it), you can fix the problem.

2. The IE extra comma error

A very painful bug that took me long time to hunt down was related to how IE handles object initialization. In any complex application, you are most likely generating JavaScript code or setting JavaScript variables from the server using some other programming language, for example by generating JSON data structures via PHP:

foreach ($a as $k =&gt; $v) {
   $result[] = json_encode($k).':'.json_encode($v);
}
   $data = '{' . join(',', $result) . '}';

At some point this data is combined and outputted to the browser as JavaScript code. Sometimes there will be cases in which an additional comma (“,”) is left on that JSON string because of some data not being included. In other browsers, this is not a problem: Firefox for instance can handle code like:

var js_arr = {"name1":"value1","name2":"value2","name3":"value3",};

See that extra comma at the end? It will cause IE to fail silently and prevent the rest of the code from being executed! You can imagine how much fun it is to spend a few days debugging complex AJAX UI code to find that a single additional character was the problem.

And that can take more than a while, particularly if one does not know what to look for. And once you find the problem, you will not know whether to scream or to cry. My advice? Stick to tested code and be aware of these errors when you start debugging to avoid going on a wild goose chase.

IE is being mean to me again

Somebody linked to this, I think it’s awesome and probably appropriate if you arrived here via Google:

“I tried Jquery, Moo tools and Prototype,
but IE still won’t display my widget right

and have you experienced the horror
of debugging in Internet Explorer?

I’d like to say that I simply do not care,
but how can I ignore so much market share?”


10
May 09

Creating a book review site from your Amazon book review RSS feed

I figured the first thing to do in order to get my book reviews online was to set up the functionality to get them from Amazon and display them on my site. This way I can write reviews on Amazon and have them automatically added to my own site.

Amazon offers an RSS feed of your reviews under your profile, see the link in the “Public Reviews Written by You” heading on “Your Profile”. What we will do is use this RSS to create a new site.

Get Simplepie (and CodeIgniter, if you want to)

I am going to set up the site using the CodeIgniter MVC framework and the SimplePie RSS reader library. You don’t really need to use CodeIgniter, but I wanted to give it a try. First download SimplePie and move the simplepie.inc file under /application/libraries and rename it to “simplepie.php”. Set up CodeIgniter as instructed in the user guide.

Controller code

In your controller:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  function index($start = 0, $length = 5)
  {
      $this->load->library('simplepie');
      $this->simplepie->set_cache_location('./system/cache');
 
      // amazon uses class and id attributes in their review, which are useful for styling the content inside CDATA - so allow those two, and the rest is default.
      $attribs = $this->simplepie->strip_attributes;
      // unset class and id
      if (($key = array_search('class', $attribs)) !== null) {
          unset($attribs[$key]);
      }
      if (($key = array_search('id', $attribs)) !== null) {
          unset($attribs[$key]);
      }
      $this->simplepie->strip_attributes($attribs);
      // change this to your Amazon book review RSS feed address
      $this->simplepie->set_feed_url('http://www.amazon.com/rss/people/A31F72N3CBFDG2/reviews/ref=cm_rss_member_rev_manlink');
      $this->simplepie->init();
      $data['feed'] = $this->simplepie;
      $data['start'] = $start;
      $data['length'] = $length;
      $this->load->view('show_rss', $data);
  }

View code

After that, create the view (show_rss in my case). Create code to process the feed. I used the pagination code from the SimplePie documentation as the basis so that you can have separate pages for the items:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  foreach ($feed->get_items($start + 1, $length) as $item) {
      $feed2 = $item->get_feed();
      echo '<div class="latest-post"> 
            <h1 class="title"><a href="' . $item->get_permalink() . '">' . $item->get_title(true) . '</a></h1> 
            <p class="meta">Source: <a href="' . $feed2->get_permalink() . '">' . $feed2->get_title() . '</a> | ' . $item->get_date('j M Y, g:i a') . '</p> 
            <div class="entry"> 
              <p>' . $item->get_content() . '</p>
              <span class="comments">' . $item->get_date('j M Y, g:i a') . '</span>
            </div>
          </div>';
  }
 
  // Let's do our paging controls
  $next = (int)$start + (int)$length;
  $prev = (int)$start - (int)$length;
 
  // Create the NEXT link
  $nextlink = '<a href="' . site_url(array('welcome', 'index', 'start' => $next, 'length' => $length)) . '">Next &raquo;</a>';
  if ($next > $max) {
      $nextlink = 'Next &raquo;';
  }
 
  // Create the PREVIOUS link
  $prevlink = '<a href="' . site_url(array('welcome', 'index', 'start' => $prev, 'length' => $length)) . '">&laquo; Previous</a>';
  if ($prev < 0 && (int)$start > 0) {
      $prevlink = '<a href="' . site_url(array('welcome', 'index', 'start' => $next, 'length' => $length)) . '">&laquo; Previous</a>';
  } elseif ($prev < 0) {
      $prevlink = '&laquo; Previous';
  }
 
  // Normalize the numbering for humans
  $begin = (int)$start + 1;
  $end = ($next > $max) ? $max : $next;
 
  echo '<div class="latest-post"> 
  <p>Showing ' . $begin . '&ndash;' . $end . ' out of ' . $max . ' | ' . $prevlink . ' | ' . $nextlink . ' | <a href="' . site_url(array('welcome', 'index', 'start' => $start, 'length' => '5')) . '">5</a>, <a href="' . site_url(array('welcome', 'index', 'start' => $start, 'length' => '10')) . '">10</a>, or <a href="' . site_url(array('welcome', 'index', 'start' => $start, 'length' => '20')) . '">20</a> at a time.</p>
  </div>';

CSS stylesheet

Finally, add a stylesheet. I used one from Free CSS Templates. I also made the following minor tweaks:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
span.amzRssByline {
  /* Adds newline before the by line in reviews */
  display: block;
}
 
div.entry img {
  /* Floats the review picture */
 float: left;
 display: block;
 
}
div.entry table div {
  /* Adds a margin after the review heading */
 margin-bottom: 10px;
}

Here is the result: http://books.mixu.net/


5
May 09

How to watch Hulu videos via SSH tunneling

hulu

Update 2 (Oct 2009): Since this is reasonably popular content, here are some additional findings:

First, Flash does not always use the same settings as your browser – try the proxy settings in Internet Explorer/Chrome. These seem to work better. The vast majority of problems are a result of Flash using different settings than your browser, revealing your actual IP to Hulu.

Second, if this does not work you can always use Proxifier (commercial product with 30 days trial), a TCP wrapper which forces all connections on the computer to use a particular proxy. This can be used in conjunction with a Hulu downloader, such as Hulu Video Downloader to download the videos.

Update: Still works even after Hulu tightened their geofiltering. As long as one isn’t using a mainstream service like Hotspot Shield, I doubt Hulu will be able to fully block this since they are in essence removing access from legitimate US computers. Probably the best thing would be to have a server set up in a legitimate, large institution such a US university or have a home-based server on a consumer ISP in the US. Both would make it hard to block the IP without causing legitimate users to be blocked as well. Then again, for me this is more of an interesting thing to do rather than a necessity. Mileage may vary depending on how proactive Hulu gets, in the end all anyone needs is one non-blocked legitimate IP address in the US.

If you haven’t heard of Hulu, it’s basically a website like YouTube but with full episodes of recent TV series – legal and with short commercials inserted into the video streams. For instance, the HBO show CSI is available on the site very soon after it has been shown in the US and you can watch it directly as streaming video. There is one caveat – the website blocks non-US viewers from watching any of the videos. So, no luck for us Finns.

However, this is rather easy to get around if you happen to have a server based in the US with SSH access. What you need to do is set up a SSH tunnel to your US server, then instruct your web browser to connect via that tunnel. This will cause all connections to Hulu to go through your US server, and will make it possible to watch Hulu videos.

Setting up using the SSH command/PuTTy

SSH allows you to connect securely to the server and create a secure tunnel from your computer to the US-based server. You can set up a local SSH client program to do this.

ssh -D 8080 -p 22 -f -N username@yourserver.com

As the SSH manual page states the -D option:

-D port. Specifies a local "dynamic" application-level port forwarding.
 This works by allocating a socket to listen to port on the local side, 
and whenever a connection is made to this port, the connection is
forwarded over the secure channel, and the application protocol is then
used to determine where to connect to from the remote machine.  Currently
the SOCKS4 and SOCKS5 protocols are supported, and ssh will act as a SOCKS server.

You can also do this in Windows using the PuTTy SSH client. Look under Connection -> SSH -> Tunnels. Add a dynamic port forward, use port 8080 and no need to specify the destination.

Setting Firefox to use the tunnel

Then set up Firefox to use the local SOCKS server (localhost:8080). The relevant settings can be found under Tools -> Options -> Advanced -> Network -> Settings … -> Manual proxy configuration.

Testing

This works very nicely, because you are now connecting from an US IP. To verify this, open up Internet Explorer and check http://www.whatsmyip.org/ and do the same in Firefox. You should see two different IP addresses. Just remember that any videos you watch will be transferred twice (from a bandwidth usage perspective) – once to your US server and then back. A single video is only about a hundred megabytes or so, so this not particularly bad for the convinience though.