DEV Community

Cover image for What's the Wackiest Coding Hack You've Ever Seen?

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

dev.to staff on May 09, 2023

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 someo...
Collapse
 
jmfayard profile image
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard
Collapse
 
siddharthshyniben profile image
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;
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Collapse
 
tandrieu profile image
Thibaut Andrieu

The fast inverse square root, definitely my favorite one 😁

Collapse
 
tandrieu profile image
Thibaut Andrieu
#define private public
#include <SomeClassWithPrivateMembers.h>
Enter fullscreen mode Exit fullscreen mode

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).

Collapse
 
mellen profile image
Matt Ellen-Tsivintzeli

If by whacky, you mean bad:

switch(true)
Enter fullscreen mode Exit fullscreen mode

It was the source of a bug, but the developer put it back when I fixed it to if else. 🀷

Collapse
 
ibrahimraimi profile image
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.

Collapse
 
darthbob88 profile image
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.

Collapse
 
darthbob88 profile image
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"
Enter fullscreen mode Exit fullscreen mode
Collapse
 
iamschulz profile image
Daniel Schulz

Probably the classic clear fix

Collapse
 
thumbone profile image
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.

Collapse
 
kvapt profile image
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πŸ˜…

Collapse
 
adam_cyclones profile image
Adam Crockett πŸŒ€ • Edited
.background { background: background}
Enter fullscreen mode Exit fullscreen mode

Valid css and functioning too.

Collapse
 
ironcladdev profile image
Conner Ow

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

Collapse
 
ryencode profile image
Ryan Brown

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