r/AskComputerScience • • Feb 06 '20

If writing code is like making art – can you name an example where this famous Picasso quote holds: “Learn the rules like a pro, so you can break them like an artist.”?

https://twitter.com/dnmjd/status/1225530961095516161
80 Upvotes

26 comments sorted by

34

u/DrunkHacker Feb 07 '20

John Carmack's 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;
}

Carmack was expert enough that he knew when it was okay to break "the rules."

11

u/Jerome_Eugene_Morrow Feb 07 '20

From the wiki Carmack doesn't take credit (I've read that thing a few times - one of my favorite code comments ever). But it's clear whoever did write it knew what they were doing.

The source code for Quake III was not released until QuakeCon 2005, but copies of the fast inverse square root code appeared on Usenet and other forums as early as 2002 or 2003.[1] Initial speculation pointed to John Carmack as the probable author of the code, but he demurred and suggested it was written by Terje Mathisen, an accomplished assembly programmer who had previously helped id Software with Quake optimization. Mathisen had written an implementation of a similar bit of code in the late 1990s, but the original authors proved to be much further back in the history of 3D computer graphics with Gary Tarolli's implementation for the SGI Indigo as a possible earliest known use. Rys Sommefeldt concluded that the original algorithm was devised by Greg Walsh at Ardent Computer in consultation with Cleve Moler, the creator of MATLAB.[22] Cleve Moler learned about this trick from code written by William Kahan and K.C. Ng at Berkeley around 1986[23] Jim Blinn also demonstrated a simple approximation of the inverse square root in a 1997 column for IEEE Computer Graphics and Applications.[24][25] Paul Kinney implemented a fast method for the FPS T Series computer[26] around 1986. The system included vector floating-point hardware which was not rich in integer operations. The floating-point values were floated to allow manipulation to create the initial approximation.

15

u/TransientVoltage409 Feb 07 '20

Duff's Device. "This code forms some sort of argument...but I'm not sure whether it's for or against."

10

u/argh523 Feb 07 '20

... He then realized that the unrolled version could be implemented by interlacing the structures of a switch and a loop:

    register n = (count + 7) / 8;
    switch (count % 8) {
    case 0: do { *to = *from++;
    case 7:      *to = *from++;
    case 6:      *to = *from++;
    case 5:      *to = *from++;
    case 4:      *to = *from++;
    case 3:      *to = *from++;
    case 2:      *to = *from++;
    case 1:      *to = *from++;
            } while (--n > 0);
    }

Shocking though it appears to all who encounter it for the first time, this device is actually perfectly valid, legal C. C's default fall through in case statements has long been it's most controversial single feature; Duff observed that "This code forms some sort of argument in that debate, but I'm not sure whether it's for or against" -- The New Hackers Dictionary, p. 165

22

u/[deleted] Feb 06 '20

Inheritance is the root of all evil.

5

u/[deleted] Feb 07 '20

Why

11

u/Sqeaky Feb 07 '20

It is intended to represent a relationship where some types can be used interchangeably. For this it is fantastic when you actually know the set of types that might get substituted or have some other mechanism for guarantee safety.

It actually get used for "all" code re-use in some projects. This anti-pattern is why so many functional programmers declare OOP bad despite functional programming being a subset of OOP.

2

u/[deleted] Feb 07 '20

But isn't that like javas whole design scheme

Also this is what I am getting taught is best practice in my software class sequence

6

u/Sqeaky Feb 07 '20

Yes

Oh no

3

u/[deleted] Feb 07 '20

Honestly I regret taking that class anyways because I'm in computer engineering and think I'm going to switch to electrical engineering since CE seems so niche and now I'm a semester behind if I do that because I haven't taken PDE's yet

I don't know why I'm telling you all this actually but I will hit post anyways

3

u/Sqeaky Feb 07 '20

Sometimes venting helps. I am glad to hear you out.

9

u/fragmede Feb 07 '20

All sorts of hacking techniques - buffer/stack overflows, shellcode, XSS, SQL injection, for starters. In order to exploit the bug, you have to understand the rules very well.

The two C programming contests (not to be confused with each other): http://www.underhanded-c.org/ and https://www.ioccc.org/

Refactoring a particularly messy bit of code, especially if a layer of indirection can be removed. In order to do the refactor and have things work afterwards, the rules of the code have to be especially well understood.

6

u/dmazzoni Feb 07 '20

This is a great question! So many examples.

First of all, nearly all security vulnerabilities are an example of this. Someone figures out a way to do something that's against the rules anyway.

Side-channel attacks, for example - the "rules" say that one process isn't allowed to access what another process is doing. This rule is totally central to processor design and operating system design.

Except....it has a flaw. Processors try to be smart and they cache things they've accessed. And you can figure out whether a memory address is cached or not by measuring how long it takes to access. So if you're clever, you can get clues as to what another process is doing by paying attention to what's cached. (The details are a lot more complicated than that, of course - look up Spectre and Meltdown.)

The second thing that comes to mind is the The International Obfuscated C Code Contest. The entire point of the contest is to write a program that appears to do one thing but actually does another. How is that not art?

Both of those are silly and impractical. Just like a lot of art, right?

But this is true even when it comes to production code. The Legendary Fast Inverse Square Root is one great example.

9

u/roman_fyseek Feb 07 '20

There's an old challenge called Vampire Numbers. It has to do with numbers that make multiples that include the original digits of the multiplicands: for instance, 3 * 51 = 153. Both sides have one 3, one 5, and one 1.

The challenge was to be faster than the professor's simple inefficient C routine (for which we were not given the source code) to find the first 1,000 vampire numbers (or however many it was) using x86 assembly.

The solution involves putting the digits into a bin of size 10, doing the multiply, and extracting the digits from the bin. If the bin is empty, that's your vampire number. Dump it to the screen and increment the loop.

I realized that if you don't actually bin the numbers in an array, you can keep the whole process in register memory, and as long as the result wasn't zero, I could increment and move on. Only if the end result was zero did I actually have to go and do the binning work to be sure.

The result was exquisite.

4

u/SftwEngr Feb 06 '20

I would agree. It's very much like writing music that way. Learn all the rules so you know when you're breaking them and can use that to your advantage.

4

u/zultdush Feb 07 '20 edited Sep 20 '25

no thanks

4

u/-MHague Feb 07 '20

Look at debates over patterns vs anti-patterns. It's usually phrased in absolute terms but there's a time and place for different things. In my experience with C / C++, you somewhat frequently come across "dos and don'ts" that essentially go: don't ever do this thing, except when you need to do this thing. To me, that directly translates to "learn all the rules, so you know when to break them". I think it's especially true for programmers forced to cut corners -- you really want to know which corners to cut.

7

u/mqduck Feb 06 '20

I would agree to a limited extent: you should know the conventions and why they exist before you break them. But coding isn't like painting or music or whatever, where breaking the rules is sometimes the point.

5

u/SftwEngr Feb 07 '20 edited Feb 07 '20

But coding isn't like painting or music or whatever, where breaking the rules is sometimes the point.

I don't think breaking the rules is the point, at least in most popular music, it's to create an effect (or in programming lingo, solve a problem) that can't be obtained by following the rules. For instance, to create tension in a trumpet solo, Miles Davis would play in between the diatonic notes, essentially playing microtones. He didn't do it to make a point about rule breaking, he did it because it sounded so good when playing a diatonic note immediately after (I'm presuming), despite the fact it broke the "rules" of diatonic harmony. I've certainly broken "pattern" rules to suit the problem I was dealing with which is a similar thing. Instead of a strict MVC pattern you might break the rules and split the M into different parts, or combine V and C together, etc.

3

u/psikosen Feb 07 '20

I agree 100%. I've used some patterns, but there are times when its crunch time and you just have to make things work. I call that art lol.

1

u/[deleted] Feb 07 '20

Yeah the reasons for many programming conventions are quite different from the reasons for conventions in art.

3

u/wackyvorlon Feb 06 '20

Ever programmed in Perl?

1

u/magpiesarenotcrows Feb 07 '20

pH.d. or research in general.

1

u/InconspicuousTree Feb 07 '20

React as a whole was a good example of this. The separation of concerns argument went against a lot of what React did, but "breaking the rules" led to one of the most popular web frameworks

1

u/Jerome_Eugene_Morrow Feb 07 '20

I think you could make a case that most novel language development starts this way. People try something and they get bored or frustrated with some process and decide to totally reinvent the wheel. Then you end up with Rust or Python or Brainfuck.

1

u/MugiwarraD Feb 07 '20

hard coding.