23 May 2012

Naming, Grouping and Sorting

We name things on our devices almost every day; saving a contact to our phone, naming a file on our computer and so on. Sometimes, these names are just for single stand-alone objects but for the other times these objects are part of a generic list, the assigned name can be helpful in grouping and sorting the list. This is because these names form the basis of sorting and grouping on related devices.

A simple example - contacts on a cellphone.

When we want to save the contact of an acquaintance on our phone, we tend to associate them with something we can relate them with. That's why we have contacts like "Ope department" and "Doyin CDS". I bet that looks familiar. To save a new contact for another department acquaintance, I may be tempted to want to go by "Nedu dept". Now, there are two contrasting terms - department and dept. This makes it difficult to sort and group related contacts. I should rather stick to "department" or "dept". Say I go with "department", I should then have

  Ope department
  Nedu department

But that is not just all. The main reason for an effective sorting and grouping method is to be able to access contacts easier and faster. Sometimes you just can't remember the exact name you stored the contact with. Sometimes you can't even remember the contact's name. If I have both contacts like that on my phone, it will still be slight problem trying to find contacts with department. A better way will be to store it like this

  Department Ope
  Department Nedu

(You may decide to add a comma like this: "Department, Ope" though). This way, simply typing the first few letters of "department" on the contacts screen on my phone will simply bring up contacts associated with the group.

The developer side, files and codes

One of good design/development practice is properly naming and arranging files. This starts as simple as having files of the same type in separate folders. For a website for example, you could have a js, images and styles folder for these file types. However, within a folder of the same file type, the way the files are named helps to further arrange the files. Let's take an example. Consider an "ajax" folder that contains php files that handle all ajax requests on a contact management site. There are around 30 files here with different functions. Some are for the normal site interaction (drag and drop, inline editing, autocomplete, pagination), some for contacts management (add, remove, delete, merge), and some for groups (add, delete). Now consider the naming convention:

  delete.php
  edit_group.php
  merge.php
  remove_group.php
  rename_group.php
  ... on and on for 30 files.

While it may be easy to find your way and easily pickup a file among 30, doing so among say a 100 won't be easy. Notice remove and delete? Why not stick to one? But that's just even a part of the problem. A better way to sort will be to name like these:

  contact_delete
  contact_edit
  contact_merge
  group_delete
  group_rename

This way, finding files having to do with contacts or groups are way easier. And no, this is not an argument of underscores, hyphens or camel-casing. It is just about using the right prefix for the file name based on how we want the files to be arranged. If I want them to be sorted by their operation type and not by target, I can simply name this way instead:

  edit_contact
  edit_group
  delete_contact
  delete_group
  merge_contact
  rename_group

This way, I know where to find files performing delete operations and ones performing edit operations.

Where this gets useful in code writing is with writing interfaces and classes that contain multiple methods. Consider a class of over 50 methods (about 2,291 code lines). Finding methods can be difficult. Even the best of us forget what we name them once or more. But if the methods are well grouped by their actions, it is a lot easier.

  project_add()
  task_delete()
  task_add()
  project_members()

So I know methods that deal with projects starts with a project_ and same for tasks. Unfortunately though, unlike as with our phone or normal files, the methods are not alphabetically sorted by the device so I have to take the extra step to order the method names alphabetically.

  project_add()
  project_members()
  task_add()
  task_delete()

As unimportant as it may seem, proper naming conventions can really be helpful in our every day life.

My name is Opeyemi Obembe. I build things for web and mobile. You should follow me on Twitter (@kehers).