[]RSS

[ Here: About | Archives+Tags | Artwork | Resumé | Contact ] [ Elsewhere: Comic | Projects | Philosophy | Work ]

Optional arguments in PHP

[Comment]

July 16th, 2008 in Micro Blog

Named, optional function arguments are a commonly used calling convention in Perl modules, used for constructors and heavily overloaded functions. They’re usually implemented with hashes in Perl, and while somewhat more work to iterate in the library than fixed arguments, they clearly document the call for the caller (and tend to be far less fragile).

They’re also easy to implement in PHP. Here’s an example (based on some CodeIgniter code I had laying around):

class bar extends Controller {

    protected $data = array();

    function foo($name, $extras = array('desc' => 'default1', 'on' => false)) {
        $this->data = array_merge($this->data, $extras);
    }

}
  1. The optional parameters are defined as the array elements of the $extra default array parameter value.
  2. The optional parameters are merged with the class data $this->data.

Calling the function is simple:

$b.foo('test', array('on' => true));
$b.foo('test 2' /* uses defaults */ );
$b.foo('test 3', array('desc' => 'Here is a description'));

The caller’s syntax isn’t quite as clean as Perl’s, but applying the defaults is cleaner (and merging the structures is about equivalent). Despite the slightly more complex calling convention, it’s still useful for setup functions with multiple optional parameters.

Javascript object member bindings

[Comment]

July 2nd, 2008 in Links

A great article on the often misunderstood topic of Javascript member binding. Javascript is one of those powerful languages that is treated like a Basic-ish Java. It’s not. It’s more like a Perl-ish Lisp.

XP: The upside

[Comment]

June 28th, 2008 in Micro Blog

What’s the best thing about your favorite operating / windowing system? What’s the best thing about your least favorite system? I was thinking about it this morning, considering the most inspirational design in each of the systems I’ve used. While not every vendor finds that balance of excellence, releasing a functional system is itself a difficult problem.

  • Windows XP/Vista: I love how the login/desktop locking system works. It supports multiple users properly, making switching between them trivial.
  • Gnu/Linux + xorg + Gnome: multiple desktops + Compiz. It’s a developer’s dream, like a desk the size of a large room. Enough room for a dozen editor windows, without having to navigate a mess of windows or tabs.
  • Apple’s OSX: It’s a brilliant looking desktop, with the best font rendering, colour and monitor management (nailing multiple monitor support). It does other things well, but I’m always impressed with its affinity to photo/video/music productivity.
  • *nix: I love how the unix philosophy wreaks of pragmatism. It’s simple, decoupled, and completely bent toward scaled production uses.
  • Nintendo’s Wii/DS/etc.: In a word, casual. Simple, predictable, and fun to the bone.

So what do you love about the systems you’ve used?

C skillz: The Underhanded C contest

[Comment]

June 15th, 2008 in Links

Want to hone your mad C coding skills? Try your hand at the Underhanded C Contest. It poses two challenges: to solve a semi-interesting programming problem, while hiding something devious in the resulting code. Sound fun?

The passive-aggressive programmer

[Comment]

June 15th, 2008 in Links

The story of the passive-aggressive programmer. It’s funny because it’s true.

A better Markdown editor

[Comment]

June 2nd, 2008 in Links

The best Markdown editor plugin ever. Includes live preview, smart editing, and a smack of other brilliant features.

PNGs, Javascript, and compression

[Comment]

May 10th, 2008 in Links

Hack of the week: compressing javascripts into PNGs and back again. Horribly excellent, isn’t it?

Quote: Raymond on Artists and programming

[Comment]

May 9th, 2008 in Quotes

Computer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter. –Eric Raymond

HOWTO: Directory recursion in Boost (and other tips)

[Comment]

April 25th, 2008 in Micro Blog

Boost’s Filesystem library is an incredible library: it abstracts paths, directories, and stat results. It simplifies coding shell problems in C++, it’s portable, and is maintained by a large community of contributors. The one downside of Boost is that some of its newer libraries are poorly documented.1 Until I have time to get involved in the Boost project, I’m going to post examples here.

Traversing a directory tree

This is the coolest feature I’ve found in boost::filesystem so far. It treats directory elements like iterators, and has a convenience iterator that flattens the problem of iterating through a directory tree recursively. The only examples I found for it were in their extensive test sources,2 which are a bit light on comments.

#include "boost/filesystem.hpp"
#include <iostream>

for ( boost::filesystem::recursive_directory_iterator end, dir("./"); 
       dir != end; ++dir ) {
       cout << *dir << std::endl;                                    
}

The example starts in the current working directory, and prints all of the file names (and directories) in inode order.3

Notes:

  1. I don’t alias the namespace here (but I recommend doing so in production code, see below)
  2. Creating an instance of recursive_directory_iterator sets it to the .end() element by default.
  3. The path type supports all standard string paths, including relative paths.

Aliasing Boost namespaces

Here’s how Boost’s own source recommends aliasing their namespaces:

namespace fs = boost::filesystem;
namespace sys = boost::system;

Other cool bits

I’ll write more about these later, but for now here are a few things you’ll find in the library:

  • fs::exists( boost_root / "libs" ), a static function to check if a file exists (-e)
  • fs::current_path() that returns the application’s cwd
  • fs::create_directories( "xx/yy/zz" ) is equivalent to mkdir -p xx/yy/zz
  • fs::is_directory( "xx" ) is the same as Perl/*sh’s -d
  • fs::change_extension("a.txt", ".tex") does the obvious
  • fs::extension("a/b.txt") == "txt" is used to check file extensions
  • fs::remove_all( "x/" ); deletes everything in "x/"
  • ifstream file2( arg_path / "foo" / "bar" ); shows the overloaded / operator!

Things that are great about boost::filesystem’s approach:

  • Static functions are used for ‘helper’ stuff. Take note C++ devs: this is a great balance for types, it keeps the types noise-free while still providing a great deal of utility.
  • It mirrors Perl/*sh functionality, something most developers should know well.
  • It throws errors (filesystem_error), allowing for some really clean, transactional code.
  • And, it abstracts paths that work on Win32, OSX, Unix, and Linux variants.
  1. Something I want to contribute to.
  2. See convenience tests and operations tests for more examples.
  3. Later I’ll try to post an example that uses the stat features to dump extra information.

Introducing Boost’s filesystem libraries

[Comment]

April 20th, 2008 in Links

An introduction to the Boost filesystem library, from the IBM developerworks archive.

Next page [>>]