Posts Tagged ‘tools’

Javascript Performance: Make the Browser Happy (and You Sad)

Sunday, November 15th, 2009

BENDERThe browser is emerging as the best platform for applications, so a large community is growing to address its final weakness: speed. Google, Yahoo and various independent programmers are all pushing a bunch of clever techniques to boost performance and please end users. That’s nice, yet as Mark Twain once said, half of the results of good intentions are evil and i see potential danger in many of the suggestions made. Here a representative short list of them:

  • Avoid for-in and forEach in favor of optimized while loops
  • Before making modifications to a DOM node remove it and then re-insert it
  • To insert multiple DOM nodes, first insert them into a Document Fragment and then add it to the DOM
  • Join all scripts into a single file
  • Load javascript files on demand

Let’s make it clear for once, execution speed is not a human problem, that’s what computers are for, they execute our commands fast. The human problem is programming speed and writing down clear, readable, maintainable commands aka programs. forEach loops make sense to me, they say “i want to do something on each item”, optimized while loops make sense to computers. If i want to add DOM nodes or modify one, i don’t care of removing it or document fragments, browsers care. To me it’s just noise. Almost all of the problems addressed by those techniques stem from lack of smartness in the browser, and that’s where fixes belong to, on the machine side. The fact that there are inept browser makers is no excuse. Fixes still belong to the machine, they’re repeatable and can be made automatic. We have a long history of programs automatically converting human friendly code to machine friendly code. They’re called compilers and the output either machine code or optimized javascript doesn’t matter.

So, learn about javascript performance since knowledge is always the way, but don’t turn yourself into a machine, you’d be an awful one. Use the tools and wait for browsers to catch up.

Vim or the Inevitable Value of Complexity

Friday, July 31st, 2009

vimIt’s becoming clear to me that a programmer is probably closer to a craftsman than a scientist. The craftsman greatest strength is mastership of his tools. So just as the carpenter masters the plane to shape wood, the programmer must master a text editor to shape programs. I’ve spent enough time wandering aimlessly around a lot of editors and IDE’s. It’s time for me to settle down, make a competent choice and take the years needed to become proficient in it. I wanted something possibly simple, light and which would leave me in control. That means an editor over an IDE, but which one? I did some deep research and after an endless stream of positive reviews backed by Gabriele “warm” suggestion, i bought “Learning the Vi and Vim Editors 7th edition”. Vim is 1991 software based on a 1970s one, and it’s great. Btw my second best was emacs, another 1970s software. This must say something about advances in text editing software industry. Anyway, now that i’ve finished the book and daily using Vim, i realize its very existance is relevant to the “simplicity in software” debate.

Is Vim amazing software? Yes. Is Vim simple? No. This could imply that simplicity is a highly overrated software value, but i think it’s not. It just implies that, as any other thing under the sun, simplicity is a relative value. Relative to what? I guess to user’s knowledge of the domain. Let me explain.

Simple software is one that provides a few objects and a few rules to compose those objects coherently into newer more complex abstractions. This makes an optimal hotbed to learn. Few objects are easy to understand and remember. Few corner case free rules give confidence while exploring the unknown. It’s important that the learner is not exposed to further possibly useful complexity which is not ready for yet. Only some to ignite inspiration, but greater power cannot come a priori, but as a consequence of greater understanding of the domain. This way user and software evolve together.

“Good” Complex software, which still tries to minimize basic objects and rules and completely avoid corner cases, may ship equipped with many levels of abstraction relevant to the domain. That’s for the sake of efficiency, so that the skilled user will be able to manage complex scenarios by quickly referring those abstractions. Give something too simple to the master and he will end up bored and unproductive. Give something too complex to the apprentice and he will run away confused.

Vim is extremely good and complex software which comes with a plethora of short keyboard commands to make any conceivable manoeuvre on text at top speed. Optimized for being efficient with keyboard, which already happens to be the most efficient input device, usually much better than mouse. That’s heaven for touch typists as i am. The nice thing about efficiency is that it can be easily quantified by time. Same goal, the faster the better.

Let’s close with a fulgid example of vim capabilities with a common editing pattern: swapping two words.
An usual hybrid keyboard/mouse approach on windows:

  • Select the first word (click/drag the mouse or double click)
  • Cut (ctrl+x or right click and use menu)
  • Move the cursor after the next word (move the mouse and click)
  • Paste (ctrl+v or right click and use menu)

I guess it takes 3-4 seconds to complete correctly

Vim keyboard based approach:

  • type dwwP

An average good typist can do 50 wpm which means it takes about 1 second to type 4 characters. That’s efficient.