Archive for the ‘Software Development’ Category

Don’t over-ajax

Sunday, October 9th, 2011

“When a design is more complicated than it needs to be, that’s a sign of an inferior designer.”
- Jamie Hyneman

Today I was fixing a bug with an autocomplete field. The user was supposed to be able to type a customer name into the autocomplete field and then choose from a list of customers, but for some reason the list of customers wasn’t showing up.

When I started diving into the code, I noticed that even though the page in question already had a complete list of customers, the developer had the autocomplete doing a totally separate ajax request to grab the customer list. Utilizing the existing list of customers would have meant less code and an instantaneous autocomplete field, but for some reason the field was ajaxed.

I see this kind of thing all the time. It must be that developers are all psyched to use ajax wherever they can, and sometimes they use it where it’s not needed. In this case ajax was even worse than pure JavaScript.

Just because you can use a fancy tool doesn’t mean you should.

Which query makes more sense? (a lesson in naming conventions)

Tuesday, September 6th, 2011

Which of the following two queries makes more sense when you read it?

SELECT * FROM cities WHERE cities.city = 'Chicago';
SELECT * FROM city WHERE city.name = 'Chicago';

To me, the second query is much clearer. It’s abundantly clear what I mean by city.name, but what could a cities.city be?

The two lessons here are:

  1. Attribute labels should make sense. (“Chicago” is not the city of a city. It’s the name of a city.)
  2. Table names should be singular.

How do dump a database in MySQL or PostgreSQL (mysqldump and pg_dump)

Sunday, June 12th, 2011

MySQL:

mysqldump my_database -umyuser -p > my_database.sql

PostgreSQL:

pg_dump my_database > my_database.sql

In the case of PostgreSQL I believe you have to become postgres before you run the command.

How to set vimdiff as your Git mergetool

Monday, May 16th, 2011
git config --global merge.tool vimdiff

How to get up and running with your Lisp program on Emacs

Monday, May 9th, 2011

I mess with Lisp so seldomly that, every time I open up Emacs, I’ve completely forgotten how to use it! Here’s a little guide that I’m writing mostly for future me, but maybe it can help you, too.

You may want to try this with my unfinished tic-tac-toe game.

I probably don’t need to explain how to open a file but I will:

$ emacs tic-tac-toe.lisp

Now, to get SLIME going, type M-x slime. That’ll put you in the REPL, but you probably don’t want to be in the REPL since your code’s not compiled yet.

To switch back to the editor so you can compile, do C-x b. You can use this command at any time to toggle between the editor and the REPL.

Once you’re in the editor looking at the code, you can do C-c C-k to compile your whole file. (C-c C-c will compile just the function under your cursor.)

Now you can do C-x b again to go back to the REPL. If you’re using my tic-tac-toe program, run (g 0) to start the fun!

Want to quit the REPL? Type ,quit. Want to exit emacs altogether? Do C-x C-c.

MySQL performance tip: use a numeric type with an index

Thursday, May 5th, 2011

Let’s say you have a table called customer (not customers) that has a column called ssn which is a varchar. (Side note: I don’t normally advocate abbreviations but it’s a different story when the abbreviation is a super-common one like SSN.)

Now let’s say you commonly do SELECTs and UPDATEs on customer.ssn. You can improve your performance, perhaps dramatically, by doing two things:

  • Make ssn an int instead of a varchar
  • Put an index on ssn

I had this exact situation earlier today. Results will vary depending on the size of your table and other factors, but my INSERTs/UPDATEs got almost 100 times faster after I made just these two changes. A significant difference!

The less ways there are to do something, the better

Monday, May 2nd, 2011

Some technologies – Django, for example – make a point of having just one way of doing any certain thing.

Other technologies, including Drupal, Zend Framework, YUI and apparently Dojo, seem to pride themselves on having many different solutions for any certain problem.

I think having a bunch of different ways to do something is usually pretty dumb. Think about it: if you get stuck on something and you want to compare your code to a working example, what will you find when you search for that example? If there’s just one way to do something, you’ll find that one way, but if there are 10 different ways to do the same thing, you’ll quite likely find a ton of examples that have little or no relevance to you at all. And then imagine if the thing you’re working on depends on X, Y and Z, all of which can be done different ways. It gets messy real quick.

That’s why I say the less ways there are to do something, the better. Preferably just one.

How to create a MySQL index

Thursday, April 28th, 2011

Let’s say you’d like to create an index on customer.name. Here’s how you’d do it:

CREATE INDEX index_customer_name ON customer (name) USING BTREE;

If you’re new to indexes, here’s a tip on how indexes can improve performance. If you want to see all your indexes later, here’s how to do that.

How to see all your MySQL database’s foreign keys

Monday, April 25th, 2011

Today I tried to drop one of the tables in my database but, when I tried to do it, I got a foreign key constraint error. I thought that was odd because as far as I could think I didn’t have any foreign keys pointing to that table. Apparently I was wrong and I didn’t want to manually dig through my 50+ tables to figure out what I was forgetting.

Luckily you can see all your foreign keys by doing this:

  SELECT TABLE_NAME,
         column_name,
         referenced_table_name,
         referenced_column_name
    FROM information_schema.key_column_usage
   WHERE table_schema = 'your_database_name'
     AND referenced_table_name IS NOT NULL
ORDER BY referenced_table_name

One of the reasons I decided to use PostgreSQL instead of MySQL for Snip

Thursday, April 14th, 2011
ERROR 1025 (HY000) at line 41: Error on rename of './mcif/#sql-6a0c_d1c0' to './mcif/account' (errno: 150)