Small personal victories for a developer

Even a developer can have his own personal victories when it comes to coding.

If you look at the world of IT development from the outside, you've probably already said to yourself that it's a crazy world, with all those hieroglyphics visible on a screen that are indecipherable to the average person.

Except, that's just a new language, so just as you can't read a foreign language, you can't read the code written by the developers.

But like a writer, serious developers are always looking for the best sentence, the one that will deliver the desired result in the most beautiful way.

Normally, all developers are looking for this syntactic optimization to make their text perform as well as possible while consuming as few resources as possible.

But for some, results are all that counts, and that's why so many of us criticize the work of our colleagues.

Just as a fine sporting gesture loses all meaning if the result obtained is nil, for a professional developer you need to strike a balance between beautiful code and efficiency.

Recently, as part of the development of one of my modules, I had to find a solution to a seemingly simple, but complex IT problem.

I simply wanted to create a function that would return the start and end dates of the quarter preceding today's date.

Even mentally, you'll think for a few seconds to get these two dates.

Then imagine reproducing this in computer language.

Some developers will tell you that all you have to do is ask the latest artificial intelligence and you'll get results fast.

So I asked the AI to code this function for me to save time, and here's the result:

function trimestre_precedent($date) {
    $date = new DateTime($date);
    $mois = $date->format('n');
    $annee = $date->format('Y');

    if ($mois <= 3) {
        $trimestre = 4;
        $annee--;
    } else {
        $trimestre = ceil($mois / 3) - 1;
    }

    $premier_jour = new DateTime("$annee-" . (($trimestre - 1) * 3 + 1) . "-01");
    $dernier_jour = new DateTime("$annee-" . ($trimestre * 3) . "-" . date('t', strtotime("$annee-" . ($trimestre * 3))));

    return array(
        'premier_jour' => $premier_jour->format('Y-m-d'),
        'dernier_jour' => $dernier_jour->format('Y-m-d')
    );
}

I haven't tested it, as I'd already coded mine, but it seems valid.

Now, here's what I've coded to meet my need:

public function medGetTrimestreDate()
{
    list($year, $month) = explode('-', date('Y-m', strtotime(date('Y-m-01') . ' -3 Months')));
    $firstMonth = 3 * floor(($month - 1) / 3) + 1;

    return [
        'from' => date('Y-m-d', strtotime($year . '-' . $firstMonth)),
        'to' => date('Y-m-t', strtotime($year . '-' . ($firstMonth + 2))),
    ];
}

Personally, I find the beauty of my code far superior to that produced by AI.

But since beauty is highly subjective, it would be best to test the performance of both functions to find out which is really the best, because only performance can decide between two computer codes, in terms of resource consumption and speed of execution.

Unfortunately, I didn't carry out this test, sticking to my personal victory of producing a code, reduced to its simplest expression and giving the expected result.

Because yes, a developer can jump out of his chair simply by coding what he wants, and in the most beautiful way.

So here I am, just sharing with you my little personal victory over the last few weeks with this simple feature that will certainly be of use to other developers who took the time to read me.

Feel free to comment on this article if this feature has helped you in your developments, it's also nice to know that knowledge is shared on the Internet.

Comments