How I Code
(Programming)

If you're a programmer under the age of 30, just go away now. You're not going to have good things to say about this article.

In the last twenty years, I've coded a lot of web applications, for all kinds of purposes. Mostly to run the back end of my business, but also more personal things, like a library for my movie collection. I decided way back when that PHP was the best language for this, since the platform it runs on is available to anyone. I have this problem in my house/business: I run Linux, and my wife runs MacOS and Windows. So cross-platform is a necessity. PHP is a lot like C, and I've coded in C. Plus all the infrastructure is in the browser. If I were writing C, I'd have to use some GUI framework. I've done that sort of thing, and it's a pain.

So I've written endless forms and database (SQL) code.

When i first started out, I just wanted to get something that worked. I didn't know about MVC at the time, so the code worked, but wasn't very pretty. Over time, I improved my coding. I incorporated MVC and OOP.

But in back of my mind, I always thought maybe I was doing it all wrong. Maybe I should be using some framework someone else wrote. Something OOPy.

I researched WordPress. For a long time, we designed and hosted websites built from scratch with Adobe Dreamweaver. But as time wore on, customers demanded more and more hands-on ability to edit their own websites. Thus WordPress. But you don't really design a website with WordPress. You more or less pour your content into their template. Forms? Um... yeah.

Symphony? I worked with that platform on a job I had a while back. Everything is driven by YAML configuration files. If you got something wrong (and I did), you got a 20 file stack trace. No thanks.

Joomla? I checked into it, but the learning curve was more than I could spare.

Drupal? I looked into this one a lot more thoroughly. Their engineering is just beautiful. There's quite a learning curve, but the logic behind their design choices is just phenomenal. But it's far too much code for me to drag around just to put some forms up on a website.

As I've mentioned before, I heavily researched Codeigniter. It's an extremely lightweight OOP platform. Very easy to get into. There's a lot of benchmarking and logging code in there I don't need, but the code is pretty solid and easy to use. I actually built an application with it at one point.

There are others, like Kohana, which is a derivative of Codeigniter. Although they lifted some design ideas from Codeigniter, the code is far more difficult to work through.

Let me stop here and say that I don't have a computer science degree. Everything I know about programming is purely self-taught. That's not to say I'm completely ignorant. But every time I see a new programming fad or paradigm, I find that in the long run, it's probably more useful to academics than real world programmers. Not that that matters to a lot of coders. They'll just on the fad because it's new.

I know, a lot of guys collect fads. The latest paradigm, the latest language, etc. That's not me. I want my code to be simple to write and operational.

Here's an example of my thinking. A lot of sites give you URLs like: http://mysite.com/this/thing/or/that/thing In order to make that work, you have to do a lot of fudging with the .htaccess file and Apache mod_rewrite. I worked at a company some years ago which created and maintained websites for other companies. They had numerous clients, and each one had their own rewrites in their .htaccess files. Worse, most of the coders there didn't have a clue about how those .htaccess files were set up or how they worked. When I had to set up a local testing area for code, someone handed me an .htaccess file and of course it didn't work. And of course, no one knew how to fix it. The problem is that I needed to know what source file to edit, but the way the redirects were set up, there was no way to actually know what file contained the issue.

I've said it before. URLs don't have to be "pretty". But manipulation of .htaccess files is what you do when you want your URLs to be "pretty". Waste of time.

Most systems that use this type of redirection have OOP controllers. The URL typically typically represents the name of the controller, the method to use, and possibly some additional parameters. I prefer to have a page controller for each page, rather than a front controller for the whole site. For what it's worth, Rasmus Lerdorf, the inventor of PHP, agrees with me.

The page controller deciphers POST and GET parameters, if any, and defines whatever fields need to be used. I wrote a form library which handles fields easily. Then it puts in a request to the model, which does all the interaction with the database. Then it calls in the head view, the view file for this page, and the footer view. Classic MVC.

When a user makes a mistake or succeeds, the program should tell them so. It should show them a message at the top of the page, one way for failures, another for successes. Since this messaging system may be called deep in the interior of classes, libraries or models, I found it simplest to make it an include file with a couple of functions. Because error messages need to survive page loads (PHP loses its mind every time it loads a new page), messages are stored in the session variable, and deleted once displayed to the user.

I hate date drop-downs. A lot of programs use them because Europeans and others don't format their dates the way we do in America. So I wrote a date library which has a built-in, configurable template for dates. If you're going to be using the software in America, set your template to accept American dates. Elsewhere, change the template to whatever works for your area. One thing I needed my date library to do is tell me how many days between dates, and give me dates for beginning and ending of quarters, etc. Tax related. So the date is represented as an array of values, the most important of which is the Julian day. This allows me to determine the number of days between any two dates. The functions of the class are all static, and the array can be directly manipulated by the programmer, though I advise against it.

I wrote an error handling library which sends real errors to a log file. Standard stuff.

I normally have some sort of list of links along the left hand side of pages. These are typically other things they can do in this application. So I wrote an include file with a couple of functions, which lay this out for me, given an array of navigation links.

I wrote a whole library to handle user registration and login. I use that on my Internet based sites (a lot of my apps live on my home network, when security isn't an issue).

For blog sites, I don't use a database to store content the way WordPress and others do. I consider this an abuse of databases. Instead, I store all articles in pages on the site. I use the database do store metadata.

I also hate ORM. Data in a SQL database is not hierarchical. It's not objects. It's collections of fields, one record after another. It's not necessary to jam it into some hierarchical framework, and have each record be its own object. Doing so just adds unnecessary code. Learn to write SQL. Real simple. I wrote my own library to surround PDO and make it easier to handle. Very shallow code, a thin layer. I've got SQL statements all through my code. I once had a boss tell me programmers typically didn't do database well. I think maybe he was right.

I worked for a company a while back which had a massive network of classes and controllers for the various websites they administered. Each page load needed about a 100 files in order to load. And most of those files and controllers were almost completely empty. They were there to make the whole system seem more "orthogonal" and complete. This was the OOP/MVC paradigm gone mad. The coders who worked there admitted that it was a crazy system.

I don't need 13 classes and 24 libraries to display an image on a page. That's an abuse of computer hardware. Write only the code you need. Make it sleek, slim and efficient. But also make it readable and maintainable. Comment liberally. Only include code you actually need. Codeigniter calls in a profiling library for every page, whether you want to profile your code or not. Seriously?

Perhaps a lot of my attitude on economy of code has to do with how I learned to code and my early interactions with computers. I started out on an acoustic modem and teletype hooked to a mainframe. From there, I graduated to an 8-bit Z80 based computer. No Internet. No Core i9s, no Threadrippers. CPU is not an unlimited resource. I try to use it and my memory sparingly.