code

17 posts under this tag.

Guard & Default 2
0
0
6
Aug
14

This is from Douglas Crockford’s Survey of Javascript (never program JS without your Crockford!). I thought it quirky at first, surprisingly helpful later. (Emphases added.)

The && operator is commonly called logical and. It can also be called guard. If the first operand is false, null, undefined, ”” (the empty string), or the number 0 then it returns the first operand. Otherwise, it returns the second operand. This provides a convenient way to write a null-check:

var value = p && p.name; /* The name value will
only be retrieved from p if p has a value, avoiding an error. */

The || operator is commonly called logical or. It can also be called default. If the first operand is false, null, undefined, ”” (the empty string), or the number 0, then it returns the second operand. Otherwise, it returns the first operand. This provides a convenient way to specify default values:

value = v || 10; /* Use the value of v, but if v
doesn't have a value, use 10 instead. */

Short-circuit logical operators are a well-known, simple idiom in several languages, but they can sometimes be confusing to read, specially when nested. What I want to point out here is that next time you have to go through code that uses them, try reading them as guard or default, as the case may be. You’ll grokEE them immediately, trust me.

Isn’t it striking, the power of names?

Star
Gmail Matrix 2
0
0
6
Aug
11

I don’t know exactly when or how the thought came into my mind but this morning the epiphany was there: wouldn’t it be cool to see Gmail’s half MB Javascript source1 a la matrix code viewIY? Indeed it would, and so for the next half hour I became a man posessed. It was amazingly easy (“ya sabiendo es facil”) to hack it up in JS and it makes for an interesting screensaver.




When I finished I realized it would be really easy to make my makeshift Matrix code generic and so here’s a quick stab at it. Type whatever text you want matrixified and a new window will (hopefully) popup with it. (Though be warned, it’s pretty rough, unpolished code and it’ll surely be too slow if you don’t have a fast computer.) Anyway, enjoy.



Next time you see Gmail,


think,


fn1. Which I downloaded thanks to Firefox’s wonderful Webdeveloper extension.

We have met the enemy, and it is us 2
0
0
6
Jul
02



[Vernor Vinge] added a third [future] trend: “The great conspiracy against human freedom.” As novelist Doris Lessing has observed, barons on opposite sides of the river don’t need to be in cahoots if their interests coincide. In our case, defence, homeland security, financial crime enforcement, police, tax collectors and intellectual property rights holders offer reasons to want to control the hardware we use. Then there are geeks, who can be tempted to forget the consequences if the technology is cool enough. Vinge quotes the most famous line from the comic strip Pogo: ”We have met the enemy, and he is us.”

Vinge’s technology to satisfy these groups’ dreams is the Secure Hardware Environment (She), which dedicates some bandwidth and a small portion of every semiconductor for regulatory use. Deployment is progressive, as standards are implemented. Built into new chips, She will spread inevitably through its predecessors’ obsolescence.

This part is terribly plausible. It sounds much like the Trusted Computing Platform, implemented in Intel chips and built into machines from Dell, Fujitsu-Siemens and others. Most people don’t realise their new computer contains a chip designed to block the operation of any software not certified by the group. Now enhance that and build it into RFID chips, networked embedded systems, shrink and distribute as “smart dust”. All are current trends or works in progress.

Geeks are willing to fight Trusted Computing on the grounds that it could be used to block open-source software or to enforce draconian digital rights management. But what if accepting it meant less visible security, less bureaucracy, even slight profit? She automatically sends taxes, enables much less noticeable surveillance and gets you through security checkpoints with no waiting. There’s less crime, because legislative reality can be enforced on physical reality. Fewer false convictions. Make regulation automatic, and it seems to go away. New laws can be downloaded as a regulatory upgrade.

It's one of those moments 2
0
0
6
Jun
26

It’s one of those moments when my head spins, twirls, swirls, and whirls. I’ve been seriously reading JS, CSS, and UI, since yesterday but it was just a couple of hours that it all came together. Let’s begin this Bushean trail with Ashley Pond V’s mindblowing, free web-book Developing Featherweight Web Services with Javascript. Then hop on to Sergio Pereira’s excellent Developer Notes for prototype.js. (Prototype.js, if you must know, is the JS framework.) Glen Murphy (recent googler) has a lot of interesting JS projects up his sleeve (say, this clock), and if you want clarity in this muddleheaded webworld, read everything you can find from Douglas Crockford (recent Yahoo)—all he’s written on JS is gobble-up-worthy, specially recommended are Prototypal Inheritance in JavaScript (it’s so short and yet it will change completely how you write JS) and Private Members in JavaScript (a wonderfully clear and short overview of JS object-orientedness). Did you know about JSON (Javascript Object Notation)? One last word on JS coding (and learning), please don’t do it without an HTML Real-Time Editor, a Javascript Shell, and a Javascript Development Environment—just don’t.

Yahoo! has a pretty nice UI blog going on (a couple of days ago, for instance, they did a nice post on the Patterns Behind the Yahoo! Home Page Beta) and they recently released an awesome Pattern Library (Yahoo! is becoming pretty cool lately… at least for developers). UI patterns seem to be all the rage these days and deservedly so. Jenifer Tidwell recent O’reilly, Designing Interfaces, looks set to become a classic (and some very worthwhile excerpts are available online). Out in the wild web, there’s even a pattern of how to build patterns, an interesting conversation on patterns here (intro, 1, 2, 3, 4), and Nine Tips for Designing Rich Internet Applications to which I wholeheartedly agree.

Doesn’t it just floor you how smart and fast things are becoming?

OK, back to work.

Wisdomous 2
0
0
6
Jun
25

I set out to serve me. Rails is a very selfish project in that respect. It gained a lot of its focus and appeal because I didn’t try to please people who didn’t share my problems. Differentiating between production and development was a very real problem for me, so I solved it the best way I knew how.

It’s hard enough to solve your own problems with eloquence. Trying to solve other people’s problems is damn near impossible—at least to do so to the level of satisfaction that would make me interested in the solution.

That’s why we hold the notion that ”frameworks are extractions” so very dear in the Rails community. Frameworks are not designed before the fact. They’re extracted when you’ve proved to yourself that an approach works. Whenever we get ahead of ourselves and try to leap over the extraction process, we come back sorely disappointed.

I believe that’s why Rails just feels right for so many people—because it’s been used by real people for real work before we dished it out for others to reuse.

I may be besotted with infatuation right now, but I believe there’s true wisdom—hard, distilled, endlessly applicable wisdom (well, what is wisdom if not particularly broad and useful patterns?)—up there.

And as a sidenote, I propose a new dictum based on the quote’s last paragraph: Use before you reuse.

Star
Blackboxing is how you do things with computers 2
0
0
6
Jun
20

to blackbox could be to reify thru interface. To suggest or implement a conceptualization thru interface. A basic strategy for synthetizing reality, it stems from an active rewriting of the famous duck test: “If I make this look like a duck, and quack like a duck, I may as well be able to conceptualize it as a duck”. The conscious, deliberate, “I make” part is crucial; to blackbox is not just to simply conceptualize, is to wilfully conceptualize something by painting an interface on it.

(Contrived) Usage Examples:

  1. “In modern programming, we blackbox our way out of complexity thru functions, objects, aspects, macros, and the like.”
  2. “Money is our society’s blackboxing of wealth, that is, of ‘what people want.’ We ought to remember it when trying to ‘make’ money.”
  3. “With the magic of silicone, you too can blackbox yourself a pair of massive pointy hooters!”
  4. “At this point, perhaps a better title for this essay is probably ‘An easy way to blackbox your own file-extension.”
  5. “The Kuratowski definition of an ordered pair as {{a},{a,b}} is pure blackboxing.”
  6. “In defining the class PlanePoint, from the stored attributes xPos and yPos you can (and probably should) blackbox Distance from them thru the distance formula.”
  7. “Let’s wrap these almost-expired candies with this cute cellopane bag and this lace bow, and blackbox them into a ‘Super Saving Kit’.”
  8. “I’m dying for someone to blackbox reputation, population, authority—the whole memetic shebang—thru some kind of social software.”
  9. “Don’t you find it amazing how blackboxing lanes and pedestrian crossings on the street thru mere painting can be so useful?”
  10. “Let’s blackbox operating systems away thru browsers!”

The word comes, of course, from the technical meaning of blackbox: “a device or system or object when it is viewed primarily in terms of its input and output characteristics.”

An each function for JS 2
0
0
6
Feb
18

Since Javascript 1.2 and later there has been a cool and very powerful literal syntax for functions:

var sum = function(x, y) {return x+y}

A couple of weeks ago I found an interesting use of this syntax. Missing Ruby’s wonderful each function, I decided to implement something similar in JS, and, after some experimentation, ended up with this:

function each(a, f) { for(var i=0, l=a.length; i<l; i++) f(a[i]) };

The function syntax comes in handy when you use this each:

each([1, 2, 3, 4, 5], function(e) { alert(e) });

It may not be as satisfying as Ruby’s each, but it’s quite useful.