This blog is highly personal, makes no attempt at being politically correct, will occasionaly offend your sensibility, and certainly does not represent the opinions of the people I work with or for.
Welcome to India

This entry is the concatention of three original entries.

Part 1

It is very unlikely that I will accept to work again on a website that was originally written by indian developpers. Everytime I start one of those projects consisting in taking over a website written by them, I always think that it's going to be alright this time, and I have been wrong everytime.

This entry is about the site I am currently deadling with and it's so insanely bad that this has to stop once and for all...

Passed the fact that their idea of versionning and source control was to add timestamps to files (meaning that index.php would come with index-new.php, index-12thNov.php, index-backup.php, index.backup.php, index-mohammed-new-to-replace-old.php index.php-test, etc -- and this for *every* other file in the source code), the models are ok, just kilometres and kilometres of boiler plate for SQL queries because they didn't know how to ORM (object-relational mapping -- despite it being natively supported by the framework they were using). The controllers are horribly painful (several hundred lines, up to 1000 lines, for one single function) because they followed the principle "logic, any kind of it, should be in the controllers rather than embedded in the models class methods", but I can still handle that. The thing that makes me want to kill myself are the views. Hundred of HTML files, literally, I am not even exaggerating, organised in the file system by a true psychopath (and all of them backed up several times using the aforementioned convention) and without any (!) understanding that views can be reused and can even be composed. They seem to have been living by the mantra "indiscriminate copy pasting is the correct way to write programs"; also known as "You need to use this long piece of code somewhere else ? Don't abstract it as a function and then call it (!), this is way too fancy and might reduce your line count, just copy paste (!); and tell your manager how many new lines of code who've written today. Higher order programming ?! Don't be silly (!) if they had wanted *that* they would not have put *you* on that project."

And because they're Indian (I have seen that many times over the years), they never delete any dead piece of code or any useless/obsolete files. To them the number of lines/files in a project must always increase with time.

The strategy that I am using is simple. I created a v1 folder, put all the existing view files in it, performed a rather aggressive search-and-replace across the entire code base to point at the v1 folder, then created a v2 folder, started from the front page and I am rebuilding the views structure in v2 by essentially building the smallest set of files complete by the relation of one file needing another. It's painful and slow, has to done file by file, but is somehow satisfying. What keeps me going is the idea of sending v1 to the bin in a couple of weeks. I can't wait. Then I will start optimising the views by merging similar files together.

On this, I grew convinced that all those problems are just the consequence of one single thing. They seem to be allergic to the general concept of abstraction. The idea that sometimes two things, despite being different, have so much in common that they are just two instances of the same thing. Lacking this basic mental reflex makes you a copy-paste-driven code monkey, and this despite "years of experience" (I was told that the code cannot be that bad because the main guy behind it has "years of experience"...). And in the same vein, I don't think that "code refactoring" has ever been translated to those people.

ps: And don't even get me started on the security flaws. Don't even. That would just make me angry...

ps: Of course, just deciding to stay away from trouble is not enough (it would be like tolerating religion because, after all, most religious people are peaceful). We need to pursue PHP Shaming.

Part 1.5

The above entry spawned an interesting discussion out of which I discovered something about Indian devs that I didn't know before. A mistake that I made to project onto them a relatively simple, but in this case incorrect, assumption about motivation.

In the West, people go into technology out of love for the subject. Either self taught script kiddies or university educated middle class folks are both in it for the love/fun of it. In particular I have never heard of any start up geek, sitting in a corner, the Macintosh on his/her lap, diving into a problem, whom didn't want to be there.

In India, things are... different. Imagine an over populated country, with a saturated job market, you need money to support your family, I mean you _badly_ need money to support your family. Not having gone through formal education makes your chances of being hired in any proper job very unlikely and you hear an opening for a junior PHP dev job with a relatively attractive salary. PHP are you asking ? Don't worry about it, it's easy to learn, they have templates and you can just use them (the technical term is copy-paste). The senior folks will help you. So do it! It's that or cleaning dishes at the burger joint.

The main effect of IT being something that some people do out of desperation, is the complete and utter lack of motivation. I mentioned yesterday that I had never seen any of them refactoring code and I remember clearly over all those years constantly telling myself while looking at their code "This was written by somebody who has no pride! It's like they do not care..." I never expected how right I would be.

Anyway, at the end of the discussion I was asked "So now that you understand this, do you have any empathy towards them ? Are you doing to complain less about the quality of the code ?" My answer was "Absolutely not! I don't care which stupid story people have gone through, in the end the code is still rubbish and that still pisses me off."

ps: replace "reviewing/{working with} somebody else's source code" by "reviewing a technical paper for publication" or "reviewing the safety of medical instruments" and see that I have a point. The stupid personal circumstances of whomever did the job before, should not be taken account when evaluating the final product. There are cases where you might, but in my field we should not.

Part 2

So, the last time I was talking about it, I said that I was following some sort of a strategy to deal with the views. Well, that turned out to go slower than I thought for a relatively simple reason. I tried all day to find the good way to express it and this evening it struck me as being very simple: the closing tags of HTML elements are often not in the same files as their opening tags...

Here is what it looks like. Imagine that you have the following simple HTML document:

So far, so good. And now imagine that it is spread in the three following files

File 1:

File 2:

File 3:

... but you cannot really put them back together because each of them is individually used in different parts of the website as fragments of other pages. This is the shit I am dealing with. But this is not all! Turns out that I found the rest of the logic. The first part is in the controllers (as I mentioned in a previous entry) and the second part is located in the views. An unholy insane mix of HTML and PHP. As you can imagine, trying to sort garbage like this is likely to turn somebody crazy. So I did the only thing somebody in my position should ever do...

Yes. I decided to go full functional with the rendering.

Nowadays the new PHP kids on the block (I guess the word "idiot" would suffice here) are using template inheritance to make websites. For instance, Blade which is the one used in Laravel, makes you work with structures like this (from the documentation)...

... because the worse aspect of OO, inheritance, has not done enough damage in code, now it has contaminated document structure as well. To me this is pure unmanageable shit, so obviously I am not going that way. Instead I am going to adopt the Prime Directive that HTML is function output. Each element on the page, and by this I mean nodes of the DOM tree, is going to be the output of a function, from a picture markup to the entire page itself, of course the latter will call the functions generating smaller components, recursively. For this, I need to move away from Code Igniter's own view system, which invites abuses by its very presence (above all in the hands of inexperienced, "quick and dirty" lifestyle, programmers). In essence this means re-implemeting React.js in PHP, the irony...

I reviewed some PHP template systems and most of them allow mixing PHP code and HTML markup to some degree (sometimes by introducing their own DSL, which makes things *much* worse), and in the end I am going to use something that totally doesn't allow it: Mustache, the logic-less template system.

For instance, here is a single cell of a much larger gallery of images

After having isolated the HTML+PHP mix standing for it, I extracted it, replaced all the PHP code occurences by Mustache markup and put the resulting HTML piece into its own file called "gallery-element.html". Now, the code generating the final HTML piece is

It reads the HTML template file, computes the variables it needs, applies them to the template and returns HTML. The signature of this function is (User,Portfolio) -> HTML. Simple, no fuss. And yes, the function that generates the gallery, of signature () -> HTML, collects the relevant users and their portfolios and calls get_gallery_element as many times as needed and itself returns the resulting HTML. (You can see those functions are "pure" if you add an additional implicit argument: the database). In total the processing needs are exactly the same as the original code, but the correct approach is *significantly* more rational, and has lots of interesting side effects that are going to be useful in the future; not the least that the same template files can be consumed by code from another programming language. Now, if I later on need to perform a change in the way gallery looks like, it can either be, a change in the HTML template, a change in the way the template is consumed or a change in the way individual cells are put together to make the entire gallery, the change is easily mentally conceptualised and the code related to it easily identified.

At this point, if PHP was a real language supporting higher order logic, that would make this entire project feel like Disneyland, but I will happily take what I got so far :)

While being at it, I have started to use Composer for package management and also I have started to move away from the object oriented model/logic layer to a functional layer. Because, in the end, here is the thing: I like the controller aspect of Code Igniter, mapping methods to URLs in a nice and simple way works for me (even though if it could do it like Laravel does that would be perfect). What I don't like is the OO based model system as well as the view system (which is an invitation for trouble). I never start projects in PHP so it won't be for me, but I am thinking that somebody should take the Code Igniter code base, modify it to suit a functional mind and release the result as a competing framework for people who have to use PHP but feel like me that, first of all, MVC doesn't have to be OO based and, second, would benefit from a logic-less template system.

Part 3

Last episode of my story of taking the website I am working on currently away from its original developers. Below is an email I have just sent to my colleagues.

Now, I need to build the picture handling code as a separate service (probably using Rackspace's CDN), because storing user pictures on an application server, right there in the middle of the source code, was already insane 10 years ago...

Part 4

Welcome to India. A place of history, spirituality, arts and the moron developers who write code where the delete function needs to try all possible file extensions and all possible file system locations because of the stratospherically stupid way the save function was written (and because "refactoring" and "abstraction" are words not yet translated to the local dialects...)

My screen was not high enough to show the code in its enterity....