Functions

Looking back at the last 10 years, I can say with great confidence that programming is creating a list of functions that perform a task. Those functions become your portfolio, toolkit, or resources. They never grow stale because you can always improve them over time.

It doesn't always start like that. In the beginning, the new programmer rewrites every single project from scratch. Whether it is a script to reverse a string, or one to scrape a website, the process starts with a blank page and is procedurally filled with code. But the more a specific task is repeated, the more the programmer sees the need to encapsulate it in a module, or function that can be reused in the future.

Something that I always find hard to remember, is how to connect to a database and properly extract the data. It's not that I don't know how to do it because of incompetence, but it's because over the years I have stopped worrying about it. If I want to fetch data from the database, all I do is write a query and pass it to a function:

// How I fetch this blog post. 
$sql = "SELECT * FROM blog_post
        WHERE url = :url
        AND is_published = true";
return $this->query($sql,["url"=>$url]);

Just like that, I don't have to think about how the code connects to the database, or how it handles SQL injection. All I know is to write my query and pass it to the query function. Overtime, I may find out that the query function is slow, or that it does a lot of redundant stuff. I don't have to redesign my code to fix it. Instead I can improve that function alone and leave the rest of the code intact. When I start a new project and need to run a database query, I don't have to think about the implementation, I can simply copy and paste the query function from a previous project.

Other than querying, I have a timeAgo() function that I don't remember when I created. But every time I need to know how long ago an event was, I can pass the date to timeAgo() and it will give me back a nicely formatted string to tell me how long ago it was.

timeAgo("2016-12-31");
> 2 months and 15 days ago.

timeAgo("2017-02-17 14:30:00");
> 15 minutes ago.

timeAgo(date("Y-m-d H:i:s"));
> Just now.

Every developer keeps a repertoire of functions that can be copied and pasted into a different project. A repertoire is not always a nicely labeled folder with search functionality or a git repository, although this could help. Mine is usually the phantom of the thousands of projects that lie dormant in my different machines. They are hard to find but when I find myself in a situation where I am trying to reimplement something that I have done before, I go through said repertoire to find the function that does it.

Sometimes my functions are not something that I have written, but simply thought about. I may be having a technical discussion with someone and describe a solution. When I find myself in the same situation, the repertoire is in my mind and I can simply do what I have recommended in the past.

I often find myself searching through this blog for implementations that I have recommended in the past. At the end of the day, there is almost always a function I wrote in the past that has used in today's problem.

The more experience I get, the wider the variety of functions get, and the faster it is to write code, or reused code. This is what the experience of programming is, Keeping a bunch of functions somewhere handy.


Comments

There are no comments added yet.

Let's hear your thoughts

For my eyes only