Shares

Writing software is 0.5 art. See, almost anything that allows you to express your creativity is essentially an art form. I personally see software as a canvas for the mind, which makes me an artisan. Since a couple of weeks, an idea has occupied my mind. It’s about grammar inspired software and naming things. I’ll ink in a few thoughts here. Read on.

I take ‘naming things’ very serious, partly because it feeds my ego of ‘me imagining myself as an artisan’, so naturally, work must be done to live up to the standards. Here’s a little story.

One day, I was refactoring my application and decided to architect my way to lighter controllers. Usually, I put a lot of my logic in the controllers for prototyping purposes, but when its ‘Prototyping Sunday’, I try to utilize dependency injection to make my controller’s free from any logic.

This brings about a problem. Since there are now multiple functions talking to each other, naming things becomes hard. It’s often a Task controller having a Task function which is calling a Task function in some part of the app/modules directory.

One does really have a hard time of naming things, such as functions, inside controllers when all you can think of the same name. This is exactly what I was doing before the revelation (of incorporating grammar for writing more verbose software).

public function fetch()
{
    $this->stream->fetch();
}


Well, that’s confusing. Although you get it, it’s not readable for subsequent maintainers. All in all, it’s loopy mess. But here’s the deal. Use “nouns” and “verbs”. Take a look at the diagram which is a mini-framework for you to stick by.

This framework has worked for me gracefully, most of the time. Here’s why it works. Recall, controllers exist to essentially trigger actions. Actions better defined by verbs. A controller may ‘update’, ‘run’, ‘initialize’, ‘dispatch’ or ‘fetch’. These are all verbs. Verbs as function names for controllers make sense.

In our example, we are fetching a stream. A stream is a noun. You can fetch an image, a picture or an article. They are all nouns and it just feels so verbose, descriptive, elegant and right to apply this grammar inspired framework in naming things.

This works well for components that require under-the-hood tasks. But what about naming things when the function names essentially impact how your “route” or URL looks like. Well, just do the inverse.

public function timeseries($articleId)
{
    $data = $this->data->crunch_article_statistics($articleId);
}

 

Now, the controller function is called timeseries, a noun, and calls upon a function with a verb prefix. This makes things more verbose, readable, and you know what’s going on. Here’s a diagram.

About Ali Gajani

Hi. I am Ali Gajani. I started Mr. Geek in early 2012 as a result of my growing enthusiasm and passion for technology. I love sharing my knowledge and helping out the community by creating useful, engaging and compelling content. If you want to write for Mr. Geek, just PM me on my Facebook profile.