## DEV Community

dev.to staff for The DEV Team

Posted on

# What's the Wackiest Coding Hack You've Ever Seen?

What's the most unconventional, innovative, or straight-up wildest coding hack you've ever seen? Was it something you came up with, or was it someone else's creative solution?

## The DEV Team

Jean-Michel π΅π»ββοΈ Fayard

Siddharth

There's the classic fast inverse square root:

``````float Q_rsqrt( float number )
{
long i;
float x2, y;
const float threehalfs = 1.5F;

x2 = number * 0.5F;
y  = number;
i  = * ( long * ) &y;                       // evil floating point bit level hacking
i  = 0x5f3759df - ( i >> 1 );               // what the fuck?
y  = * ( float * ) &i;
y  = y * ( threehalfs - ( x2 * y * y ) );   // 1st iteration
//  y  = y * ( threehalfs - ( x2 * y * y ) );   // 2nd iteration, this can be removed

return y;
}
``````

And here's something I wrote myself:

``````let x={},s=(S,A=30,T='black,red,green,yellow,blue,magenta,cyan,white'.split`,`)=>T.map((a,i)=>x[a+S]=t=>`\x1b[\${i+A}m\${t}\x1b[0m`)&&s;s('')('Bg',40)('BrBg',100)('Br',90)('',0,'reset,bold,dim,italic,underline,blink,,reverse,hide,strike'.split`,`);export default x
``````

It's been a while since I wrote this so it took me a hot minute to understand it :P
It's a little terminal color library I golfed, called planckcolors

Thibaut Andrieu

The fast inverse square root, definitely my favorite one π

Thibaut Andrieu
``````#define private public
#include <SomeClassWithPrivateMembers.h>
``````

For those who are not familiar with C++, the `#define private public` say that "Starting from now, each time you see βprivateβ word, replace it with βpublicβ.

Meaning, you can now access private members of following class.
I had to do this to workaround an uninitialized private variable in a 3rd part, leading to a crash.
Definitely an anti-pattern and doesn't work every time (create some low level binary incompatibilities).

Matt Ellen-Tsivintzeli

If by whacky, you mean bad:

``````switch(true)
``````

It was the source of a bug, but the developer put it back when I fixed it to `if else`. π€·

Ibrahim Raimi

The `FizzBuzz` code challenge, where developers are asked to write a program that prints the numbers from 1 to 100. But for multiples of three, the program should print `Fizz` instead of the number, and for multiples of five, it should print `Buzz`. For numbers that are multiples of both three and five, the program should print `FizzBuzz`. This challenge has led to some creative solutions, including one developer who wrote the entire program in one line of code using a ternary operator.

Raymond Price

I saw one that worked by finding the right seed for an RNG, then `for int ii from 0 to 100 { print [ "Fizz", "Buzz", "FizzBuzz", ii ][rand.nextInt() % 4] }`

Another one, that I want to do myself, used a genetic algorithm to evolve an array containing the correct sequence of strings.

Raymond Price

I honestly don't remember where, but I saw some code that used an array of function pointers, like

``````logTrue = () => console.log("It's true");
logFalse = () => console.log("It's false");
var foo = [logFalse, logTrue]
foo[+true]() //Chrome doesn't convert directly from true to 1, so have to do this to convert it.
/// "It's true"
``````

Daniel Schulz

Probably the classic clear fix

Bernd Wechner

I'm with you on that. Whackiest not by its nature so much as the fact that it fulfilled such a ubiquitous need for so long before the language deigned to provide more logical solutions, even then I don't think it's been killed off completely yet.

Igor Diev • Edited

I've used it while working as a frontend-developer back in 2009. And you know, in 2009 I really thought, that I've invented this trickπ€·ββοΈ I didn't know, that the others do the sameπ

``````.background { background: background}
``````

Valid css and functioning too.

Conner Ow

In typescript, you can use `require()` to work around a circular dependency error.

Ryan Brown

I've always been in awe of Duff's Device
Duff's Device(Wikipedia)