SReject   -  Jan 26, 2013

So everyone, to get a little discussion going, what is everyone's most favorite snippet of code. Doesn't matter the language or it's functionality. Just what is everyone's favored code snippet.

To start, I guess I should tell mine. It's in javascript, and it's not so much a snippet but a technique. Personally, I don't like having many variables that are only used once then never touched again, so I do something of the following:

function getValues() { return ["A","B"] }

var a = getValues(), b = a[1], a = a[0];

// Now, variable a equals "A", variable b equals "B"

I like this for a few reasons. The first being the single call to getValues(). the 2nd being that there isn't a "touched-once" variable

BlueThen  -  Jan 26, 2013

Mine's more of an equation.

// Inertia: objects in motion stay in motion.
velX = x - lastX
velY = y - lastY

nextX = x + velX + accX * timestepSq
nextY = y + velY + accY * timestepSq

lastX = x
lastY = y

x = nextX
y = nextY

It might seem trivial and silly at first, but it lets you do really amazing things really easily. Things like THIS and THIS!

Sorasyn  -  Jan 27, 2013

I am a personal fan of recursion. Makes programmatic error detection and correction very easy.

public void Recurse() {
    try {
        //Code to attempt
    } catch (Exception) { 
        Recurse(); 
    }
}
FelicianoX  -  Jan 27, 2013

I didn't make this one, or never had to use it but I've always liked it:

If both a and b are integers, it will swap them.

a = -(b = (a += b) - b) + a;
SReject  -  Jan 28, 2013

Very nice!
@Sorasyn I like recursion alot aswell. Not only with errors, but something like searching/listing files in a directory + subdirectories

@FelicianoX very interesting, I can see it's uses.

SReject  -  Jan 29, 2013

@Hawkee as much programming as you do, I'm surprised you haven't made a mention yet :P

Sorasyn  -  Jan 29, 2013

Indeed it does. I'm almost disappointed that I don't get to use it more often. Last time I really used it was in an ASCII distortion snippet which would, given an invalid depth, correct it to the maximum valid depth, and recurs.

    public static String encode(String str, int depth) {
            depth = Math.abs(depth);
            int err = 0;
            String out = "$" + (char)depth + "$";
            try {
                for (int a = 0; a < str.length(); a++) {
                    if ((int)str.charAt(a) + depth > MAX) { out = "INVALID"; err = (MAX - (int)str.charAt(a)); break; }
                else { out += (char)((int)str.charAt(a) + depth); }
        }
            } catch (Exception e) { System.out.println(e); }

            if (out.equalsIgnoreCase("INVALID")) { return encode(str,err); } else { return out; }
    }

Please excuse the horrid indentation mismatching.

Hawkee  -  Jan 29, 2013

I don't really have a favorite snippet, but I do like print_r quite a bit. It's very useful when debugging PHP code.

Sign in to comment

Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.