How We Determine Product Success, The Netflix Tech Blog
I once had to work with some very poorly written code, and I found this gem of a coding horror.
Within the PHP code that printed the HTML markup, there was a section that printed JavaScript. What did the JavaScript do? It selected an element (which had just been printed in the code above) and set the height attribute to a value based on the number of items inside the box. So you have PHP writing JavaScript which sets a style. That’s three technologies to do one simple thing. And the best part of it all: it was totallly unnecessary! I couldn’t understand why the height needed to be set, so I went ahead and set it to auto (the default value), and it worked just fine. (Likely, the items within the box were originally floated, which means they’d be removed from the normal flow, requiring a set height). Needless to say, I removed the entire mess. (to give the developer credit, he was clearly a smart guy and wrote some good code; I’m not sure why he resorted to this elaborate setup)
But let’s say you did float the elements within the box. Then what? Add an empty element after all the floated elements, like so: <br style="clear:both" />. (or better yet, put it in your CSS). The clear attribute ensures the element appears below the floated elements, and so the containing box gets an appropriate height.
Talking about this stuff reminds me how nice HTML layouts are. You almost never have to set the height or width of elements (unless you’re doing something tricky). Contrast this with Flash and ActionScript (to name just one example), in which you must explicitly set a height and width on everything, which is particularly annoying when you have dynamically sized content.
Yukihiro Matsumoto, aka “Matz”, creator of Ruby
When I need help with a technical issue, Stack Overflow is the first place I go. And sometimes I head over to Server Fault. But wouldn’t it be nice to search both sites at once? Now you can. Quick Overflow combines all the Stack Exchange Q&A sites into one easy to use, super fast search page. So whether your question is about HTTP ports, Zerg strategies against the Protoss, or the ripeness of cantaloupes, Quick Overflow is the place to go!
I’ve been doing a bit of Java work lately; I built a quick prototype in PHP and needed to convert it into Java. And I was shocked at a few things that are trivial in PHP but more difficult in Java. An example: I have text which may or may not (but probably does) contain HTML, and I want to simply remove all the tags to create “unformatted text”. In PHP this is as simple as
$unformatted_text = strip_tags($text_with_html);
Easy! It’s just one line! Now how’s it work in Java? I won’t go into detail in this post, but you can read a solution on stackoverflow. It involves downloading and importing a third party package (is javax third party?) that allows you to parse the HTML, and you need to be sure to set a somewhat obscure argument that will ignore all tags. This isn’t difficult, it’s just more work, and it gets in the way of my getting things done.
Another example I came across in the same project is encoding and decoding JSON. Again, PHP has built in functions json_encode and json_decode. Java, again, you need to download and import third party packages.
I’m not complaining. These aren’t deal breakers. I’m just very surprised. When I first began working with PHP, I didn’t really like the language (Have you ever created an anonymous function in PHP? And those dollar signs in front of variables, the hell?) But now I realize how much I’ve taken PHP for granted. Web development with PHP is easy.
I came across Browse Sad the other day. Their goal is to get web developers grayscale their entire site (filter:grayscale) for IE6 users, while asking them to upgrade their browser. I can empathize with them; IE6 prevents us developers from using all the great web stuff that have come out in the past 9 years. But I feel their approach is a bit aggressive.
The following code will display a subtle message on the bottom of the browser asking the user to update to Google Chrome or Firefox. It’s small, and can be easily ignored, but is till noticable
<!--[if lt IE 8]> <div style="padding:0.5em; background-color:#ccc; border:1px solid #555; border-bottom:0px; position:fixed; bottom:0px; right:0px; z-index:9999;"> <strong>Want a better browsing experience?</strong> Upgrade to a modern web browser today, for Free! Download <a target="_blank" href="http://www.google.com/chrome/">Google Chrome</a> or <a target="_blank" href="http://www.mozilla.com/firefox/">Mozilla Firefox</a>. </div> <![endif]-->
Another note about browsesad: They want you to change your behavior because it’s inconveniencing me. That’s not how you persuade people. You need to let them know why they will benefit from this action. It’s not that people are selfish, it’s just that you need to get their attention, and get them excited enough to take action. And there are very strong reasons to upgrade your browser: better performance, better security, and more features.
Here’s a simple tip to make your code more readable. Suppose you have:
$things = get_stuff(50, 25, true);
Unless you have the entire codebase memorized, you may not know what arguments are being passed into this function. But try this:
$things = get_stuff($start = 50,
$count = 25,
$include_soft_deletes = true);
Now it’s much more clear. You’re getting 25 things, starting at the 50th in the list. And you also seem to be including things that have been soft deleted (marked as deleted, but not actually removed). In PHP, an assignment expression evaluates to the value being set, so the above is a shortcut. But for languages that don’t allow this, or for those of use who know better than to get too clever, the following is just as good.
$start = 50; $count = 25; $include_soft_deletes = true; $things = get_stuff($start, $count, $include_soft_deletes);
A more general rule is to never use a number (or any value) without “labeling” it, and you label it by assigning it to a variable with a descriptive name.
I’m a professional web developer, and yet I learned this one from my Mom, which she read in Reader’s Digest Magazine.
In a url, everything to the left of an @ symbol is ignored.
For example, if you copy/past google.com@yahoo.com into your url bar, you’ll be taken to Yahoo, not Google. Not a big deal, just be aware that the second url might be a spammer or con artist trying to make their url look like it’s from a legitimate source.
This seems to be the case in all browsers. However, if the @ symbol appears to the right of the TLD, the domain is interpreted correctly.
If you’ve never played Towermadness, the rest of this post will make zero sense. Leave now. Or better yet, buy it for your iPhone/iPod Touch/iPad; it’s one of the best strategy games I’ve ever played.
The map Endurance is crazy impossible to beat. It’s not the map that’s difficult, it’s the unending number of aliens they send at you: over 300 waves! But I’ll show you how to beat it! I don’t know if there’s a name for this strategy, but I’ll just call it The Reversal Strategy
Check out the first image above. Notice the tower layout, and notice there’s one, long winding path. If you’ve played the game at all, you’ve figured out the most basic strategy is to make the shortest path from the saucers to the sheep as long as possible in order to maximize shooting time. I’ve drawn the path out, half blue, and half purple. The blue path is that which the aliens will follow. But what’s up with the purple path? In the next image, notice the tower circled in red, just within the entrance to the sheep area. Suppose you delete it, and then rebuild it just one square to the right. What happens? The aliens turn around and head backwards! Yes, they’re retreating! Ok, not really, but what’s really happened is you’ve changed the shortest path to the sheep so that they now need to loop all the way around the map again. They’ll backtrack through the blue path, and continue on to the purple. If they’ve survived long enough to get to the end of the purple path, simply toggle the tower in the sheep entrance again. Continue reversing until all aliens are destroyed.
On this particular level, Endurance, I built a dozen fully upgraded flame throwers and a dozen nukes. I thought this was overkill (and it was for most of the game), but when the last boss aliens came around, I had to reverse back and forth many many times in order to kill them. It took several minutes kill the last ones. No matter how good of a maze you’ve built, and how much high powered weaponry you’ve built, it’s impossible to beat this level without the Reversal Strategy (if you can prove me wrong, I’d love to know how else to beat this level!)
PS - If you’re wondering how to take screenshots on your iPod/iPhone, hold the home button and then press the power button. Then email the photo to yourself.