Top 5 code hacks I’ve seen as a developer

In the world of web development, ‘clever solutions’ better known as ‘workarounds’ aren’t that uncommon, sometimes it seems like these hacks are the norm, rather than the one-off exception.

I’ve collected some of the best “solutions” I’ve stumbled upon from time to time, be it in a project I’ve worked in, shared by a colleague or in a StackOverflow thread.

5. Errors? pfft, who needs those.

<?php
set_error_handler(function($errno, $errstr, $errfile, $errline) {
	$error_reporting = ini_get('error_reporting');

	if (!($error_reporting & $errno)) {
		return;
	}

	
})

It’s common knowledge, in programming errors happens for a reason (unless you’re one of these old boy C devs that practices dark magic on a daily basis) onwards… long story short, all this snippet does is support the error suppression operator inside the custom error handler function, which was likely implemented to catch all errors thrown in the first place.

4. Is it ready yet…?

window.setTimeout(function() {
   
}, 10);

Notice the 10ms delay for this code execution?

While not as much a ‘hack’ or ‘workaround’ as the rest of the snippets in this blog post, I’ve included this snippet because, I couldn’t think of anything else as of writing moment, I cannot count how many times I’ve seen this done, instead of restructuring the program flow accordingly.

It’s worth mentioning there are certain rare and occassional scenarios, such as solving race conditions where this solution could be useful

3. Throwing caution to the wind.

<?php
function remove_emojis($string) {
	$string = str_replace("?", "{%}", $string);
	$string  = mb_convert_encoding($string, "ISO-8859-1", "UTF-8");
	$string  = mb_convert_encoding($string, "UTF-8", "ISO-8859-1");
	$string  = str_replace(array("?", "? ", " ?" ), array(""), $string);
	$string  = str_replace("{%}", "?", $string);
	return trim($string);
}

This might require some explanation.

  1. Replaces all regular ‘?’ with what I personally call ‘the funkyboi’ {%}
  2. Convert character encoding from UTF-8 -> ISO-8859-1
  3. Convert character encoding from ISO-8859-1 -> UTF-8
  4. Replaces all questions marks with empty spaces.
  5. Replaces all funkybois with regular question marks.

It’s import to note when encoding a string that contains UTF-8 only characters to another encoding that does not support certain special characters, e.g. emojis, they’ll be converted to question marks, in which case we’ll now know which characters and their location to remove.

Minor caveat: this will also remove much more than just emojis, as there’s a huge difference of supported characters.

2. Maybe someone had a bad day.

<?php
function strip_crap($text) {
	$search = array(
		chr(0xC2) . chr(0xA0), 
		chr(0xC2) . chr(0x90), 
		chr(0xC2) . chr(0x9D), 
		chr(0xC2) . chr(0x81), 
		chr(0xC2) . chr(0x8D), 
		chr(0xC2) . chr(0x8F), 
		chr(0xC2) . chr(0xAD), 
		chr(0xAD), 
	"\r" ); 

	$text   = str_replace($search, "", $text);

	return $text;
}

It’s like, you can almost feel the frustration put into this one.

I’d love to elaborate further on this one and give it a good rundown, but the fact is I haven’t yet memorized the whole ASCII table.

1. Rounding decimals is hard.

div {
    --shf: 4.9406564584124654e-322;
    width: calc(((100% / 11) - 9.09px) * var(--shf) / var(--shf));
}

I’m not even sure I want to know how this guy came up with this solution, or how much time was invested in doing the maths here. Needless to say, it’s a very clever solution.

More honorable mentions.

If you just can’t get enough of finicky solutions like this, I’d recommend you check out wtfjs.com and follow r/programminghumor on reddit, of which I am also a frequent visitor.