From PhotoShop CS3: “Duplicate Layer” and “Delete Layer” actions appear right by each other. These two actions look very similar, and are grouped dangerously close together, and yet they perform exact opposite actions. Luckily if you hit the wrong one, you’re alerted before a layer is deleted. And there’s always undo.
The following is perfectly valid SQL (containing php variables).
INSERT INTO objects_shows (id, object_id, object_type, show_id, season_id, episode_id, scene_id, counter, depth, added_by, created_at, deleted_at) VALUES (null,$id,'$dbtype',$s_id,null,null,null, $depth, 1, '$username',NOW(), null)
But you should never do this. Why? Quick… what’s does counter get set to? It’s hard to tell. Well, it’s just before the depth variable, so it must be getting set to NULL. But this is messy, you have to think too hard to figure out what’s getting set to what. Thinking sucks. Let’s insert some line breaks to maybe make it more readable.
INSERT INTO objects_shows (id, object_id, object_type, show_id, season_id, episode_id, scene_id, counter, depth, added_by, created_at, deleted_at) VALUES (null,$id,'$dbtype',$s_id,null,null,null,$depth, 1, '$username',NOW(), null)
Better, but still a bit messy. Let’s try something else.
INSERT INTO objects_shows SET id = null, object_id = $id, object_type = '$dbtype', show_id = $s_id, season_id = null, episode_id = null, scene_id = null, counter = $depth, depth = 1, added_by = '$username', created_at = NOW(), deleted_at = null
Much better! This SET syntax works exactly the same as the previous INTO syntax when inserting one row, but it’s much more readable. It’s now very clear that counter is set to $depth, which is probably a bug.
If you are only inserting one row, please use the SET syntax. Your code will be easier to read and maintain.
Emacs is an incredible text editor. My favorite feature is macros: the ability to “program” a repeatable action. In fact, macros are such a powerful tool that this is how Emacs got it’s name: Editor MACroS. But what’s the point of macros? Read on…
Let’s say I have an abbreviated list of Top Level Domains and their accompanying nations.
.am Armenia .an Netherlands .ao Angola .aq Antarctica .ar Argentina .as American Samoa .at Austria .au Australia .aw Aruba .ax Åland .az Azerbaijan .ba Bosnia and Herzegovina .bb Barbados .bd Bangladesh .be Belgium
This is nice, but I’d like to post it on my blog, as an html table. So I need to put an td tag around each line. How should I do this? I have three options:
1) alter each line manually
2) write a quick program (PHP, C++, whatever) to input the file, manipulate it, and output the result
3) use a macro
I have 15 lines to edit. I suppose I could edit each line manually, but I might want to add a few dozen more. I could write a program, but that’s overkill for such a small number of lines. I really don’t want to take the time to write code that I’ll likely only use this one time.
You can see I’m stuck in this awkward zone where it’s too much to do manually and too little to automate. This is where macros shine. Because macros can be quick to “program”, you’re not investing a lot of time when you only have a handful of lines. And macros can be repeated quickly, so even when you have dozens or hundreds of lines, macros provide a quick solution.
Here is result I’m after:
<td>.am</td> <td>Armenia</td> <td>.an</td> <td>Netherlands</td> <td>.ao</td> <td>Angola</td> <td>.aq</td> <td>Antarctica</td> <td>.ar</td> <td>Argentina</td> <td>.as</td> <td>American Samoa</td> <td>.at</td> <td>Austria</td> <td>.au</td> <td>Austratda</td> <td>.aw</td> <td>Aruba</td> <td>.ax</td> <td>Åland</td> <td>.az</td> <td>Azerbaijan</td> <td>.ba</td> <td>Bosnia and Herzegovina</td> <td>.bb</td> <td>Barbados</td> <td>.bd</td> <td>Bangladesh</td> <td>.be</td> <td>Belgium</td>
And here’s how to get there.
Position my cursor on the first line. Start recording the macro, move to beginning of line , type “<td>”, move forward one word, type “</td> <td>”, move to end of line, type “</td>”, , move down one line, stop recording macro.
Here are the keystrokes in emacs: C-x (, C-a, <td>, M-f, </td> <td>, C-e, </td>, C-n, C-x )
At this point, Armenia’s line should be enclosed in a tag, and the cursor is on the Netherland’s line. Simply repeat the macro, C-e, and instantly one more line is complete! At this point I can type C-e until I’m finished, or simply C-u 13 C-e to run the macro thirteen times. It should be clear that if I had the full list of Top Level Domains (about 300?) it’d be trivial to repeat as needed.
If you’re new to Emacs, this might be a lot to take in. General Emacs usage can be learned (with a bit of time and effort) by anybody and pays off big time if you edit text often. Once comfortable with Emacs in general, just a bit of practice with macros will give you a lot of power, and they’ll become second nature in no time.
Macros are an incredible tool. If you manipulate text for a living and are not using macros, you’re missing out! Many editors support their own macros, though Emacs and Vi do a particularly good job.
I’ve been reading Coders At Work, which is a collection of interviews with fifteen noteworthy programmers. One recurring question is “What was your first interesting program you ever wrote?”. It’s a good question, so I thought I’d share my answer here.
My first exposure to programming was in high school; I took a class where we learned to program in BASIC. One assignment was to create a scrolling marque, where text would move from the left side of the screen to the right, and then reappear on the left in an infinite loop of textual motion. As a follow up assignment, we had to make the text “turn” downwards, and then “turn” to the right again.
But that’s not the interesting part. This being my first involvement with programming, I didn’t know what functions or methods were. Hell, we hadn’t even gotten to subroutines yet.
Back to the program, I realized that the code for both of those turns were very similar, and decided to generalize it out. I knew that if I invested a little more work into this code, then I’d be able to turn in any direction (8 directions to be exact), and have that text snaking around the screen like nobody’s business! So I wrote this generalized block of code which would make the text “turn” depending on how a few variables were set. I put this code way down in the file, like line 2,000 or something. And whenever I needed to use this code, I’d set a few appropriate variables, and then GOTO the generalized code. At the end of that code, I’d GOTO the code where I left off. I didn’t realize this at the time, but I had essentially just created a function — a block of code that takes parameters, meant for reuse.
Ironically, when I did learn about methods in C++ the following year, I had a bit of difficulty grasping the concept.
The HTML5 placeholder feature is such a good (and simple) idea, and hardly seems complicated to implement. I’m surprised that only webkit (Safaria, Chrome, Most Mobile Browsers) implements it so far.
How can you tell if a page is in quirks mode or standards mode? Javascript’s document.compatMode should tell you what mode you’re in, and should work for any browser. You can type the following into the url of any web page:
javascript:alert(document.compatMode);
If the site is in quirks mode, you’ll get “BackCompat”.
More at http://stackoverflow.com/questions/627097/how-to-tell-if-a-browser-is-in-quirks-mode.
This was really difficult for me to find without knowing the term “shy group”, so I’m posting it here so I’ll never need to search for it again.
The syntax for a PHP regular expression to match a group without capturing the group is an open paren, question mark, colon, and ending with a close paren. Like so:
/(?:product|dp)\/([0-9a-zA-z]{10})/
which will capture the ASIN out of an amazon url, for urls like
http://www.amazon.com/Single-Ladies-Put-Ring/dp/B001KR1IBW/ref=sr_1_1?ie=UTF8&qid=1262047994&sr=1-1
http://www.amazon.com/gp/product/B000W23KKS/ref=dm_mu_dp_trk1?ie=UTF8&qid=1262053531&sr=1-1
by taking the 10 digit number/letter sequence that occurs after either ‘dp/’ or ‘product/’.
Here’s a programming problem. Write a function that takes in a continuous Number to Number function, and returns its derivative (or at least an approximation of its derivative).
So by continuous Number to Number function, I mean something like
f(x) = 3x^2 +5x + 8
or
g(x) = sin(x).
And the results would be
f'(x) = 6x + 5
or
g'(x) = cos(x)
I really like this problem, because it shows off the power of lexical scope. I often find myself coding in PHP, wishing for lexical scope, because, well, it’s just so freakin’ usefull.
Assuming you remember the definition of a derivative, the solution is almost trivial using first class functions and lexical scope.
function derivative(func) {
var h = 0.0001;
var d = function(x) {
return (func(x+h) - func(x))/ h; // this line looks familiar
};
return d;
}
// test some results
for(var i = 0; i < 6.28; i=i+.785){
document.write("sin'("+i+") => Exact: " +Math.cos(i)+", Approximate: "+derivative(Math.sin)(i)+"
");
}
sin’(0) =>Exact: 1Approximate: 0.9999999983333334sin’(0.785) =>Exact: 0.7073882691671998Approximate: 0.7073529267287437sin’(1.57) =>Exact: 0.0007963267107332633Approximate: 0.0007463267248652983sin’(2.355) =>Exact: -0.7062616448200052Approximate: -0.7062970411886305sin’(3.14) =>Exact: -0.9999987317275395Approximate: -0.9999988096956306sin’(3.9250000000000003) =>Exact: -0.7085130991922728Approximate: -0.7084778131294378sin’(4.71) =>Exact: -0.0023889781122815386Approximate: -0.002338978251081869sin’(5.495) =>Exact: 0.7051332290084228Approximate: 0.7051686815739977
So that’s it. I wonder if there’s any easy way to do this without relying on lexical scope. What would you do if you had to solve this problem in C, PHP, or Java? Is it even possible?