views:

629

answers:

7

Certainly there are valuable programming techniques yet to be discovered, or that have been discovered but are not well-known.

I'm interested in hearing about techniques that may be counterintuitive or seem strange, but have real benefit.


Edit: It was suggested that I give an example.

Just to show what I mean, my personal favorite is something I discovered in '86, that I called differential execution. It's a control structure in the same way that recursion or backtrack is a control structure, and just as hard to understand for anyone who doesn't know it yet. But it has a benefit in building UIs with time-varying structure, in reducing source code by about an order of magnitude.

It is hard to understand, no question about it, but once it is understood, it pays off in productivity.


Edit: Another favorite I've seen is a way to do propositional resolution theorem proving with bit patterns. It doesn't get much use, but when you need it it's pretty nifty. Each proposition corresponds to a bit-position in a word. Suppose you have two propositional clauses A and B stated in conjunctive normal form. Let word A0 be the set of propositions that are false in A, and A1 be the set that are true in A. Similarly for B0 and B1. Then you can resolve A and B into a new clause R by the bitwise operations:

// find the propositions in which A and B disagree
int temp = (A0 & B1) | (A1 & B0);
// if they disagree
if (temp != 0){
    // make a new clause by combining them
    // and removing the proposition where they disagree
    int R0 = (A0 | B0) & !temp;
    int R1 = (A1 | B1) & !temp;
    // do something with the result R
}

So, for example if A = (!X | Y), and B = X, then R = Y.

+3  A: 

Duff's device is an optimized implementation of a serial copy that uses a technique widely applied in assembly language for loop unwinding.

some
That's beautiful. It reminds me of Jon Bentley's unrolling of a binary search algorithm.
Mike Dunlavey
That's horrendous. A real code-stench.Edsger Dijkstra must be rotating in his tomb.
Brent.Longborough
Heh. As they say on Wall Street when there's disagreement "It sounds like you guys could do some business!"
Mike Dunlavey
+5  A: 

One thing that surprised me the first time I saw it was an "automatic" variable in a C++ method that was never used. Like

  Foo &foo;

But when I took it out, things failed badly. It turns out the constructor for the variable grabbed a mutex lock, and the destructor released it, so as long as it was in scope, it had the lock.

I don't normally condone such "jedi mind tricks" in the code, but it was pretty cool.

Paul Tomblin
That is really subtle.
Mike Dunlavey
A good comment might take care of the "mind tricks" objection.
Mike Dunlavey
English being a foreign language for me, I didn't realize that "subtle" and "error prone" are synonyms...
Kaniu
No, comments are bad, especially on repeating tricks. Just name the type, "AutoScopeLock" or something.
Brian
Isn't it the same without "
kcwu
+3  A: 

ternary operators. They are second nature to some, a glaring run-time error to others. I feel the real power of them come from the fact you can assign variables to the evaluation of ther ternary operators.

var welcome_string = "good " + (Time.now > noon ? "evening!" : " morning!")

Pardon my pseudocode

Mostly I use them to avoid an "if/else" with two returns at the end of a method. "return arrayList.size() == 0 ? null : arrayList;" seems clearer than "if (arrayList.size() == 0) return null; return arrayList;"
Paul Tomblin
It is like functional code in that it guarantees you will handle all cases.
Mike Dunlavey
Paul - Yes, great example.
A: 

String Mapping is an odd thing I've encountered for VB, where one overwrites the headers of an array in VB with the address of a string and accesses the string like an array. Sure, you could just copy a string to a byte array, but this is faster. And crashes VB if you don't untrash the headers before the array is free.

Brian
It would have to be in pretty time-hogging code to be worth the risk.
Mike Dunlavey
+2  A: 

Monte carlo unit testing. If you have some really complex algorithm in your code that would be hard to test deterministically because of the huge number of code paths, also implement a naive algorithm that's hard to get wrong and is supposed to have the same observable behavior as your complex algorithm. Then, generate a bunch of random input, give it to both your complex and naive algorithm, and make sure the results match.

dsimcha
Great answer. (Sheepishly - I've had and used the same idea.) It is a real significant enhancement to unit testing. If you can't prove your program will never hit an assert, and you can't try all possible inputs, this is the next best thing.
Mike Dunlavey
(continued) Once I tried to look into using quantum computers to test software with all possible inputs. http://arxiv.org/PS_cache/quant-ph/pdf/9807/9807026v1.pdf
Mike Dunlavey
@dsimcha - do you have any references for this monte carlo unit testing?
Russell
@Russel: Never seen an article about it or anything. I invented it myself, though several people seem to have independently invented the same thing.
dsimcha
@dsimcha Would not it be nice to always have the same random number sequence (for the whole test to be reproductible, not just the "broken" cases) also?
mlvljr
+2  A: 

Try-Finally blocks:

I like these for functions that have to do a lot of setup and cleanup, and have multiple return paths. They let you consolidate your cleanup logic, while keeping your returns simple and direct.

private string Test(int arg) {
 try {
  //extensive setup

  if (arg > 5) return "too big";
  return "normal";
 } finally {
  // extensive clean up
 }
}
recursive
+1  A: 

I know one. Deliberately slowing (throttling) down a system to make it perform better and scale higher.

William Louth
I'm confused. Do you mean developing on a slower system to force you to address performance issues, so that when users run it on a normal system, it goes like blazes? Yeah, that's a good trick.
Mike Dunlavey
I once had a customer tell me that when he installed a particular profiling tool that their system got marginal faster. Naturally I was curious to how this could be the case as most profilers are not dynamic optimizing compilers. When I looked at what was instrumented and what wasn't I quickly realized what was actually happening. The instrumented code largely in the application process layer was slowing down the workload that was being placed on the [saturated] database resulting in short contention windows are the resource transaction level.
William Louth
By the way IBM used this once following my reporting when they claimed that GC was actually good for your Java application and that it could indeed speed things up.
William Louth
@Willian: On the DB-saturation issue, that's pretty subtle. When I've looked at cases like that (years ago) I did it the hard way, by recording, merging, and analyzing time-stamped message logs. I'm sure you have tools to improve on that sort of thing.
Mike Dunlavey