One Liners and Overengineering

Sometimes being a developer is hard work

And that is because you have to study and understand the code other people write. Now, this has many aspects in terms of understanding. There are many people who write code. Some of them are good at it, some are not. It all comes down to two categories in my opinion.

“I’m bored of this thing” people

These people  write code just enough to finish the feature. Not very descriptive code, just enough to get the job done, nothing more. Their variables are often misunderstood, they do not have the proper names or sometimes they have a twofold meaning. Something like:

$thisVariableMightContainAStringOrATraitedSuperObject

Their code structure nonetheless is pretty much the same like :

function addOrSubtract ($addOrSubtract, $firstOperand, $secondOperand)
{

if ($addOrSubtract == '+') {

  return $firstOperand + $secondOperand;

else {

  return $firstOperand - $secondOperand;

}

Their tests also are somehow like the above example. They tend to irrationally use the same structure, using the “feature” of loosely typed languages.

One Liners

Now these people I love. They are trying to do the same thing with the above people, only, they delve into it. They are bored also at first, but when they see that their code can be optimized, they are “one-lining-em”. They do not use the wrongly inferred type as the above people, and their code is “a mystery” yet “it gets the thing done” in one line. Supreme elegance in my opinion.

example :

[].forEach.call($$("*"),function(a){
a.style.outline="1px solid #"+(~~(Math.random()*(1<<24))).toString(16)
})

Now these One Liners are writing the best optimized code.

The above line was taken from this guy’s blog who explains the exact function of what it does. It is amazing how much you can learn by just one line of code.

What it does is to apply a single pixel outline to all the elements of a page, randomized with a different color. Simply paste the above line in your browser’s console and see what happens. The purpose of this line is to debug your css layout in the page.

If you have a look at the above page you will see that this line is taken from a simple gist and there are a lot of people who are suggesting revisions on how to write that single line.

I will conclude by saying that programming

is something that is quite complex at times. People who do it for a long period, tend to do the above things in order to “escape” the routine of writing plain code. Over-engineering and trying different things (in all the acceptable frameworks of programming, like optimization, maintainability, debug free code) is something that is being constantly done, to keep oneself from doing something different even if one does not have the capability of doing so.

What I mean is that, after you have seen all the technologies, you have used all the “regular path” ways of a construct, you are always trying to do something in a different – peculiar way so as to “escape” the straight-forward, more “boring”, more “effort consuming” way.

And believe me, I have seen this from many, many people that are indeed very good engineers.

After all, Lazy people make the best inventors…
Doing a thing as simple as touching one's left ear, could prove quite complex...
Doing a thing as simple as touching one’s left ear, could prove quite complex…

The Fermi Paradox

I read a rather interesting story concerning intelligent life

And I think I should post it here since, I really really enjoyed it.

The basic concept revolves around the great Italian Physicist called Enrico Fermi.

He lived during 1901 and 1954 made a lot of breakthroughs in modern physics (the only thing I remember from school though was the Fermi Level of ferrite metals — more here)

The Fermi level is the total chemical potential for electrons (or electrochemical potential for electrons) and is usually denoted by µ or EF

But this is irrelevant to what he said when he looked at the stars:

Yeah, summer, friends, camping, you will end up looking at them at some point…

Where is Everybody?

His point exactly. Based on many assumptions and theories, a simple paradox appears. We cannot see anyone apart from us in the universe (although our observation scope is rather small).

But according to this article, there is a high chance that some other life form ahead of us in technology and advancement that has colonized half his galaxy (which eventually must and will be visible? from us). This is explained thoroughly in the blog post.

But more on those thoughts to the following link :

The Fermi Paradox

What I also liked, and personally believe in part

is the saying by  Oxford University philosopher Nick Bostrom that “no news – is good news” 

 

Credit to P.K. – from the website : www.waitbutwhy.com 

WordPress SEO Sitemap and Heroku

Maybe you have read at a previous post about Heroku and WordPress

that we have been using PostgreSQL as a persistent storage for this current blog.

Aside from the problems with our hosting as it is, from the fact that we could not (and still cannot) run add-on PG4WP effectively with Heroku and WordPress, we found another rather serious problem.

Last night I was browsing Google’s Webmaster Tools

and I found out that my sitemap was not working properly.

Despite the fact that the link was loading (http://www.must-feed.com/sitemap_index.xml) if you got in and tried to load the posts sitemap (http://www.must-feed.com/post-sitemap.xml) it responded with a Not Found (404) page.

Post Sitemaps was Not Found (404)

Looking around the Heroku logs (in terminal write heroku logs) and I found out this really interesting error :

[error] WordPress database error ERROR: date/time field value out of range:
"0000-00-00 00:00:00"\nLINE 1: ...ssword = 'xxasdf' AND post_author
!= 0 AND post_date != '0000-00-0...\n
^ for query 
   SELECT COUNT(ID) FROM wp_posts
   WHERE post_status IN ('publish','inherit') AND
   post_password = '' AND post_author != 0 
   AND post_date != "0000-00-00 00:00:00" AND post_type = 'post' 
made by require('wp-blog-header.php'), wp, WP->main, 
WP->query_posts, WP_Query->query, WP_Query->et_posts, 
do_action_ref_array, call_user_func_array, WPSEO_Sitemaps->redirect, 
WPSEO_Sitemaps->build_sitemap, WPSEO_Sitemaps->build_post_type_map

I digged the code a bit and found out

that the problem was being caused by a query which was ran from WordPress SEO Yoast Plugin.

This query :

SELECT COUNT(ID) FROM $wpdb->posts {$join_filter} 
WHERE post_status IN ('publish','inherit') AND post_password = '' 
AND post_author != 0 AND post_date != "0000-00-00 00:00:00" AND post_type = %s

Had an invalid date for PostgreSQL database.

Not to mention that also, the guy who wrote PG4WP

the module that connects Postgres with WordPress (to be honest that is heavily resource consuming) had thought of sanitizing the query from these cases only at INSERTS:

$sql = str_replace( "0000-00-00 00:00:00", "'now() AT TIME ZONE 'gmt'", $sql);

EDIT: I found out that the above line while writing this post
was SQL Injecting the code which was escaped by  the back slashes of gmt and caused again Postgres to fail so I changed it properly. The ajax action of post creation was calling the wp_insert or wp_update method which in turn was trying to insert a new post. I will come back with another update since I cannot escape single quotes somehow…

And not on SELECTS and UPDATES.

So all I had to do was add a new line at 290

of file : /wp-content/pg4wp/driver_pgsql.php

$sql = str_replace("0000-00-00 00:00:00", "1977-01-01", $sql);

to sanitize the SELECT and the posts sitemap page (and xml) was being loaded properly:

EDIT: I have also found out that the same bug applies with the press_this.php functionality and you cannot use it (you need to sanitize this as well).

EDIT 2: There are numerous errors during rewrite from different modules. I will come up with my patches at a new github repo. Recently I found out that Jetpack also had a problem, more on that later.

Not Found error disappeared and the Posts entries were generated in the xml.
 Finally after all those I have to say

that the way WordPress uses the Database is the least RIDICULOUS.

Having worked with many systems on web, I suggest to move to a more database agnostic framework, such as pdo_mysql.

Coding and Peculiar Tech Blog of a Web Dev.