 |
|
Monday, June 9. 2008
At DCPHP, I saw several clever presentations about unit testing, which essentially reinforced what I already know: if I could go back in time and actually guide the development of the CMS we had built in-house, I'd structure it not to have clever self-references and three-level inheritance...which would, incidentally, make it something less than a three-year project to create unit tests for it.
But over the years, with many beginning developers coming through our doors, I've seen several things that the guys who advocate unit testing assume you, as a sane person, are doing...but mostly I've seen those things not being done. So here's an easy one:
<?php $sql = "SELECT id, name, type FROM people LIMIT 10 ORDER BY date_added DESC"; $result = mysql_query($sql); if (mysql_num_rows($result)) { // do stuff } ?>
Great, eh? Checking to see if there are actually rows returned before you do something, right?
But what if your query fails? Remember, you write code not just for right now, but for when you do things down the line. Just like you make unit tests to catch any screwups before you go to production, you can be testing any calls right inside your code. So later, when you add "just a quick fix" and screw up the query, it doesn't have to fail if, for example, it only fails when you pass a variable in and the tests you wrote originally don't cover that.
So always be paranoid about any queries you make:
<?php $sql = "SELECT id, name, type FROM people LIMIT 10 ORDER BY date_added DESC"; $result = mysql_query($sql); if (false === $result) { // handle gracefully } elseif (0 < mysql_num_rows($result)) { // do something } else { // handle the case of no data } ?>
Obviously, in the world of PHP 5 and OO, you should be using exceptions and try-catch, but it is amazing to me the number of developers who will blithely assume their queries are perfect and will remain so, and that there's no risk they'll ever see a production box. Testing is one of the first thing a schedule- or budget-pressed project will skimp on when resources go short. Most clients react less well to "we had to cut your feature, because the budget is running low" than "no problem with your feature, we'll just do some spot checks instead of the full-on testing process." That is, they will until you screw up and they discover a page is completely unusuable on a weekend when you're off paddling down a river.
So write fault-tolerant code. Make sure that even if your query fails, you handle it in a graceful way--and that you provide yourself enough information to quickly figure out what's going on on Monday and fix the thing.
Wednesday, May 21. 2008
I have a couple of projects that call for node content to be published outside of the normal center-of-the-page boundary. But the default template override system accounts for individual nodes, or even overriding the node display area for a given content type, but not the whole page. A Google search turned up some possibilities, but based on this page, I was able to find the right answer for Drupal 5.x, in templates.php: <?php function phptemplate_variables($hook, $vars = array()) {
switch ($hook) { case 'page':
if ('node' == arg(0)) { $vars['template_files'] = 'page' . $vars['node']->type; }
break; } return $vars; }
?>
This means if you have a content type called event and you want it to have a custom page layout, just copy page.tpl.php to page_event.tpl.php. Dunno why this isn't part of the default theme system, but maybe it's an un-Drupal thing to want to do.
Wednesday, May 21. 2008
Jeff Atwood takes on PHP, the language of choice around here, and gives is a good, well-deserved thrashing followed by the most insightful thing I've read in a while: You've probably heard that sufficiently incompetent coders can write FORTRAN in any language. It's true. But the converse is also true: sufficiently talented coders can write great applications in terrible languages, too. It's a painful lesson, but an important one.
I, too, initially learned programming in BASIC (followed by a smidge of Pascal). Then I didn't program for a decade or so. Then what did I do? Perl, a language possibly more derided for spaghetti code than PHP. So in a way I should be doomed to be one of those relentlessly inflicting FORTRAN on the world. I don't believe I do. Why not?
Continue reading "On Choosing to Program Well"
Monday, May 19. 2008
We've been using Drupal more and more here at Forum One. One of its nicer aspects is a web interface to some functionality we had for programmers in SyntaxCMS: the ability to set up a "view" of a content type, so that you only got, say, all blog posts tagged with "Environment." This is pretty powerful; it lets a savvy Internet user with no specific programming skills to set up lots of functionality by clicking a few widgets. One such user asked me what I thought was an odd question: "Any idea why one of my terms for the issue taxonomy isn't showing up in filter for a view?" I checked, and sure enough, the first term was missing. It happened to be the longest term, and I immediately hypothesized some sort of character limit. So I tested it by shortening the term. No joy. I then wondered if there were any illegal words, so I tried eliminating "and" from the term. Still nothing. I began to suspect it something about the position or some permission around the term. I deleted and re-added the term. Still nothing. At this point I tried adding a term that came alphabetically before the missing term. Success! All but the new term showed up. But now I had an extraneous term hanging around. Having eliminated configuration errors as a cause, I went to the Views module page, and made sure I had the latest released version for the version of Drupal I was using (5.7) (We make it a policy never to rely on beta or dev versions). I did (1.6 as of this writing). So I started looking at the bug list to find out if anyone had reported this issue--it certainly seemed big enough that someone ought to have noticed. Lo and behold, I found this bug. Turns out it had been discovered in beta testing and a patch applied. But I was using the latest release version...reading further, I found out a change in the taxonomy module itself had obsoleted the fix and a new patch issued, tested, and committed to CVS. It is in the 1.7 pre-release versions, but as I mentioned before, we don't use pre-release software if any alternative is available. However, knowing the patch is applied in the next version siginificantly reduces the effects of applying the patch now. So now I have the patch applied and committed to my local repository for the site. So if you're having problems with disappearing taxonomy terms when building out a view, the patch above should fix it for you for Drupal 5.7 and Views 1.6.
Thursday, May 8. 2008
Via Jono Smith, Director of Marketing for Network for Good:
Do you know a talented Web strategist or developer interested in coming up with new ways to use the Web for social good? Network for Good & the Case Foundation are sponsoring two $10,000 prizes for Web developers who do just that.
We are challenging developers to mash-up the Network for Good online donation processing API with another Web service to either (a) enhance the online donor experience or (b) revolutionize a nonprofit's ability to fundraise online. As if changing the world was not enough, the two winners will each receive a $10,000 prize.
For more information, you can check out the information page: http://www.netsquared.org/mashup/donatenowchallenge
Seeing as we've built our own little API for the CARMA site, I could envision something that let you look up local power plant emissions and then buy carbon offsets for the typical American's yearly usage. EULA: If you use this idea and win, you have to buy me a beer with the $10,000. And I get to pick which beer.
Wednesday, April 16. 2008
Languages are tricky, quickly evolving systems, but almost all of them in their current form predate computers. Indeed, most predate typesetting. So there are a ton of writing systems out there that are designed for writing with a trained human hand, and not optimised for discrete mechanical reproduction. Those languages persist, and computers are finally getting powerful enough to reproduce them elegantly. So how do we deal with them? First, get familiar with Unicode. Once you've done that, you'll find that regular web applications need a little more to work properly. For instance, what if you want to use full-text indexing on a script that isn't derived from Latin? Fortunately, MySQL as of version 4.1.1 makes that a lot easier...but there are some tricks. You'll need to learn about collations in MySQL. It is entirely possible in PHP to set everything you're doing to UTF-8 and have a site that can accept and reproduce lots of different scripts, but the database is storing them as something else. However, you give up a lot. First, your database dumps will be unrecoverable gibberish. Second, functions that depend on MySQL understanding how the underlying data behaves, such as full text indexes, will fail. MySQL out of the box to have latin1_swedish_ci be the default collation, which I find an extremely odd choice. OK, so they're fighting Amerocentrism, but they're not exactly promoting international standardization. So unless you specify that you want the database storing information as utf8_unicode_ci (which I've found to be the most hassle-free for the widest range of character sets), you'll need to specify that your database and all its tables and rows use the utf8_unicode_ci collation, which will also cause them to store everything as UTF-8. But wait--you're not done. You'll also need to make sure your connection defaults to UTF-8. That's right, you have to have a clean UTF-8 path through your whole application. Just one place where UTF-8 isn't respected will turn everything to unreadable gibberish or a string of question marks. You can have your application issue 'SET NAMES utf8' for every request. The best option is to make sure you have an environment where you can control the MySQL configuration until they come up with some better defaults. Once you do this, and providing you do all the things you need to do in HTML and PHP (or the scripting language of your choice) to make them UTF-8-clean as well, you should be able to do all the things you're used to in other languages.
Wednesday, November 7. 2007
The F1 tech staff will be at the DC PHP conference for the next three days, and we look forward to talking with anybody, especially if you're looking to join a PHP development team that focuses on non-profits and policy organizations. Tired of building Yet Another eCommerce Site? Find one of us. I'll be updating my status on Twitter--just follow f1tech.
I'll be there this morning and all day tomorrow.
Hope to see you there!
Friday, October 19. 2007
After the release of the Eclipse PDT (PHP Development Toolkit)'s All-In-One 1.0 build, I gave it a whirl, but found to my dismay that a Subversion client isn't included in the default install, though CVS is. It took me a few tries to figure out how to get everything working well with it, so I thought I'd share my experiences and save someone else the ramp-up time.
I'll outline a couple of wrong turns here, and then describe a way I found to make it work after the jump. Note I'm not saying it's the way, not with something as configurable as Eclipse. Also, I'm coming to it as an absolute Eclipse newbie, so some of my mistakes might be obvious to a long-time user, but I assure you aren't obvious if you are new to it. First I tried making the workspace the place I'd been working previously, assuming Eclipse would recognize and import the files with a new project. Well, not so much. It seems a new project can import files, but not recognize pre-existing ones. Then I tried setting up a new project in a new workspace, and importing the project I'd checked out with Subversion on the command line. That worked better than I feared. It pulled in .svn directories and the svn client works to an extent in the new file location. However, I got strange file locks and it turns out it didn't import all the files. That's when I stopped screwing around and turned to Google.
Continue reading "HOWTO: Use Eclipse PDT with Subversion in 13 Easy Steps"
Friday, September 7. 2007
I get this question just often enough to merit a post: "What CMS/Framework should I use for my apps?" The key is "apps"--that tells me the questioner hasn't thought through his or her problem enough. There is no one answer, but there are several considerations you should keep in mind when deciding at what level you should build a website or web application: CMS, framework, or a custom application.
Frameworks, CMSes, and ground-up custom apps have their places. But they all involve tradeoffs. The key is figuring out what aspects are key for your project and living with or taking strategies to mitigate the tradeoffs you have to make once you pick. Let's look at them one by one.
Continue reading "POPAs, CMSes, Frameworks, and All That Jazz"
|
|
Comments
Tue, 01.07.2008 11:30
Dan, You are absolutely correct and I should have stated this within my post; the described steps within the post [...]
Mon, 30.06.2008 09:45
i wouldnt recomand this at all, because if something happens and the conection is lost u will have your data lost if the [...]
Mon, 09.06.2008 13:42
PDT syntax highlighting support does not seem to work when subclipse is installed, any one else had this problem?
Mon, 09.06.2008 11:56
I didn't mean to imply that you were bashing unit tests.
Mon, 09.06.2008 11:52
My point isn't to bash unit tests, but rather to say there are a bunch of things you should be doing before you get [...]
Mon, 09.06.2008 11:43
I agree with, what I think is, the gist of your argument. That is, if you don't write code that anticipates failure, [...]
Mon, 09.06.2008 08:58
clipse is an open source IDE — or as they put it themselves: “universal toolset for development”. It [...]
Tue, 27.05.2008 12:17
Navigation links should fill their container to ensure ease of selection. A good method for that is to make them [...]
Thu, 22.05.2008 10:35
One of the better comments I've seen in a while: "Although I like PHP, I agree the language is only as good as the [...]
Tue, 20.05.2008 14:03
Oscar, Yahoo's Term Extraction service takes an entire article and returns a few of (what it thinks are) the most [...]