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
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.
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.
Mine's more of an equation.
It might seem trivial and silly at first, but it lets you do really amazing things really easily. Things like THIS and THIS!