Monday, October 19, 2015

XPATH vs. jQuery selectors

I've always wondered if there was a good way to connect a button with an input field within a repeated list item that didn't involve generating unique IDs with index numbers. Wouldn't it be nice if you could just say, 'this button is connected to that input field a few DOM nodes away'?

Well, it turns out you can!

In the example below the 'data-control-xpath' attribute references the drop down list that accompanies the button. The selector uses XPATH rather than a jQuery selector -- the good news is that XPATH can be natively interpreted in Javascript. The XPATH query below translates to: "find some select element that exists in the grandparent of this node', where 'this node' is the button itself (passed as a parameter to the function).

HTML
<a href="#" data-control-xpath="../..//select" data-id="@Model.Id" onclick="return associateCategoryWith(this);" class="btn btn-primary">Assign</a>

Javascript
function associateCategoryWith(button) {
var $button = $(button),
$list = $xpath($button.data("control-xpath"), button),
categoryId = $list.val();
[...]
}
function $xpath(xpath, contextNode) {
return $(document.evaluate(xpath, contextNode, null, XPathResult.ANY_TYPE, null).iterateNext());
}

Other ways around this problem include building a KnockoutJS or AngularJS template and classes or, as mentioned, generating unique IDs with an index variable.

I guess you could also do it with jQuery '.parents()' and a 'filter()', but that would be no fun. ;-)

Thursday, December 19, 2013

In the Round

"For all drawing depends, primarily, on your power of representing Roundness.  If you can do that, all the rest is easy and straightforward." -- John Ruskin

Leonardo da Vinci said almost the same thing in his treatise on painting.  There is great truth in the statement that the essence of the plastic arts (painting, sculpture, drawing, etc.) resides in the ability to project roundness as it implies a discipline whereby the mind can conceive of and manipulate three-dimensional forms in its imagination -- a skill that masters like Leonardo, Ruskin, and others considered both rare and difficult.  (Worth pondering why that is.)  If the object of painting is to simply 'represent' lines and shapes that create an illusion of solidity -- then this idea is not interesting, it is simply another technical trick.  But if the object of painting is to conceive of three-dimensional forms in the mind and project them onto a plane, it is an entirely different story as we are talking about not a mechanical trick but a mental operation -- perhaps not different from the ability of the musical composer to interweave melody and harmony and to think in counterpoint.  It is the difference between the blind application of some rules ... and the very power of the imagination to think literally in several dimensions at once, to interweave and play with forms in space, to communicate awful feelings of depth; expansive feelings of volume; profound sensations of weight and mass and cohesion.  And when the object of the painter's attention is not just any old round shape but an organic form, like the human or animal body, or a landscape, the power for these sensations to move us is vastly amplified, through our innate sympathetic attachment to natural forms.  Of all these, the human body is of course the most sympathetic as its nervous system is a thing we have intimate knowledge of -- so to draw this truly in the round, where the three-dimensional counterpoint of muscle and bone and tendon and flesh is woven into a human fugue and animated with human passions, well, to draw 'in the round' in such a way is a very profound experience -- and is a long, long way from merely imitating the inclination or declination of some perspective lines.

Tuesday, August 20, 2013

The Tale of Two Revolutions

Anthropology professor David Graeber asks the very pertinent question of what ever happened to the increased leisure time that we were promised by John Maynard Keynes?  In spite of both the Industrial and Information revolutions, far from now working only 20 hour weeks we are, if anything, working longer than before.  While that may not be true if we compare the early 21st century to the late 19th century, and while in some (okay, in many) respects we live in a workers paradise, it does not necessarily follow that we are happier.  Mr. Graeber puts this down to the creation of what he calls "bullshit jobs" -- jobs that seem to serve no purpose other than to provide meaningless busy work and which have the collateral effect of merely depressing and distracting the populace.

It is hard to disagree with him -- at least with the sentiment.  I agree that it's somewhat depressing that Keynes's prediction did not come to pass, but this line of thought does verge awfully close to conspiracy thinking.  It seems to impute that there is some force beyond the market ordaining the creation of non-essential jobs in the interests of social repression.  But while this often feels real -- I've often wondered why we have to work 40+ hour weeks, and why most jobs aren't at least shift-based (which would surely maximise opportunities to consume) -- it's not conceivable that thousands of companies are coordinating a campaign of full-time employment merely for the hell of it, or to 'keep down' the people.

The conclusion is perhaps more depressing than the failure of Keynes's prediction: these bullshit jobs exist because, whether through a failure of imagination or technology or both, there is an administrative need for them.  It has been said that the invention of the computer created an Information Revolution in the same way that machines and steam power created the Industrial Revolution.  But has anyone thought to follow that parallel further?  How pleasant was the Industrial Revolution for the average worker, intellectually and aesthetically brutalised as they were by repetitive labour which they had no creative input into?  Has the Information Revolution been any better for the average information worker in terms of creative fulfillment?  Perhaps what the two revolutions have in common is the same brutalisation of the spirit by mechanisation.  Does it matter much, intellectually, whether you're a cog in the computer or a cog in the machine?  Of course I'm not comparing the quality of life in physical or health terms, or in many other ways. But as John Ruskin deduced in his famous chapter on 'The Nature of the Gothic' in 'The Stones of Venice', the happiness of a craftsman is ultimately dependent on the amount of freedom that they can exercise in their craft.  Without this freedom -- which is also the freedom to be inefficient, to make mistakes, in other words, to learn -- then the creative spirit of the worker is debased by their trade. In this sense, working at the keyboard can be just as brutalising to the creative spirit as working at the loom.

While reducing the working week from 40+ hours to 30 or even 20 would obviously be desirable, it would not necessarily fill the creative void.  More opportunities for pleasure do not necessarily lead to pleasure -- they can just as easily lead to anomie and boredom (see the plight of the long-term unemployed).  Work provides structure and consistency -- backed by the social obligation to turn up, to not disappoint -- and that does give people a sense of purpose that can often be lacking with unstructured free time (whether unemployed or merely on holiday).  What is really depressing about this is that this could simply be down to a colossal failure of imagination -- the imagination required by society to find forms of play (as opposed to work) for people that are not just as compelling as work but that stimulate more personal growth and consequently greater social benefit.

Wednesday, February 6, 2013

Negahertz

Negahertz (NHz): Noun - The unit of measurement of the amount of frustration created by unnecessarily slow software and operating systems.

My reawakened passion for vintage computing has changed my perspective on modern computing in various ways. One thing that leaped out at me recently was the discovery that old computers aren't necessarily slower than new computers.  Of course computers today are millions, even billions of times more powerful than any high-end personal computer from the 1980s.  But until recently the everyday experience of using a modern computer has usually been characterised by blissful periods of speed and productivity interlarded with teeth-grinding delays.  These delays, these periods of software- and operating-system induced gridlock, I describe with the term "negahertz."

If the number of megahertz equates to how fast a computer is in theory -- or used to -- then negahertz equates to how slow the computer actually feels in practise.  There are plenty of ways that an old computer system can feel faster than a new one.  Anyone who grew up in the 1980s will remember switching on a personal computer -- like an Atari 800XL or an Apple //c in my case -- and booting almost immediately into a usable programming environment.  Before the days of multitasking graphical user interfaces it was possible to fit a computer's operating system and a built-in programming language comfortably onto ROM.  (Although, once Apple invented the graphical user interface it was no longer possible to fit an entire operating system in ROM.) Bigger storage devices and faster CPUs spurred companies on to develop ever bigger and more capable operating systems, but at the price of turgid boot sequences and often sluggish interfaces.  Computers went from being under-powered machines that were productive in seconds, to over-powered machines that took longer to start than it took to boil a kettle.  If you think about it this is a strange concept -- strange that we, the all-powerful consumer, meekly put up with it for so long. (Of course, it's not like there were any alternatives.)

The concept of negahertz has encouraged me to look at the reality of using a computer -- and not the technical stats driven fantasy of clock cycles, revolutions per minute, or bus speeds. All of those wonderful developments in raw speed and power can be crippled by software bureaucracy and the mire of abstractions. Just as negawatts encourages you to think of where energy is wasted -- and can therefore, if desired, be saved -- negahertz has made me think more about what a computer could and should be.  A machine that puts the needs of users first -- that may not be blazingly fast but is instantly responsive; that doesn't bring interaction to a halt while the file system is busy; that has a graphical paradigm that creative programmers can expand upon, instead of being straitjacketed by; that is understanding and tolerant of errors and mishaps and that puts a smooth and stress-reduced experience first.

Of course, it is not all gloom and doom. Far from it.  In fact, we may be seeing the best of both worlds: a humane user experience driven by inhumanly fast hardware. The smartphone and tablet revolution has finally forced complacent operating system vendors to take boot times seriously and to prioritise the responsiveness of the user interface -- as they should always have done.  This betterment of experience and improvement in perceived performance has not been the direct or necessary outcome of faster hardware -- it has emerged because consumers un-purchased and un-bought slow and buggy mobile devices into oblivion.  Those who wished to survive shifted their priorities accordingly.

In the meantime, I find it a fun mental exercise to now rate slow-loading apps and websites in negahertz -- and to imagine how, with the right ideas, hundreds or thousands of negahertz could be saved.

Thursday, September 27, 2012

Hate Speech

I wonder if this tendency to characterise people as 'haters' was brought about by the increasing divisiveness of American politics. It is a little disturbing that any kind of opposition or criticism toward a product can be summed up as 'hating on it'. Rather than be a hater, why not be an adversary, an opponent, a critic, a skeptic, a detractor, a gadfly, an enemy, or an antagonist? Instead of 'hating' why not dislike, distrust, oppose, criticise, be skeptical of, or consider flawed? Or if you want to be extreme why not slander, defame, denigrate, or calumniate?
So if I take a balanced view and consider that there are good things about the new Apple Maps, for example, but on the whole think there was a laughable lack of quality control -- that makes me a 'hater'.
Words are the functions of speech -- the API of natural language. Differentiation of meaning matters. Using crudely simplified language is like using a crudely simplified API in your programme; it restricts the scope of your expression and what you can create.
If we insist on 'beautiful' APIs when building our software, why not go further and reach for the right word when it comes to criticising software?

Monday, August 27, 2007

A good haul

I went to the annual Lifeline bookfair at South Bank over the weekend. Out of the silty river bed of out of date text books (some 120 years out of date) and Maeve Binchys I chanced on some solid gold nuggets. I made the tail end of one of these fairs years ago, and found much more grit than gold last time. Lesson learnt: get there early, snatch, and run.

It also helps to have clearly defined criteria beyond the author or subject matter. I was looking for good condition antiques this time, and the haul includes the following:

Most were art books or art criticism ...

2 x Kenneth Clark (HB, 1st);
3 x John Ruskin (HB, 1890s);
2 x William Gaunt (HB, DJ, 1st);

Along with some curiosities:

A beautifully illustrated (watercolour) hardback on Belgium by Forestier & Omond from the '20s;

The real find of the booksale was probably a 1930s edition of The Rubaiyat of Omar Khayyam, 1st & 4th Fitzgerald translation, illustrated by Willy Pogany, gilt edging intact.


After all these years there is no damage to the black-and-gold bookplate stuck to the cover.

As well as a dozen beautiful tipped in colour reproductions there are these black-and-gold stuck on block illustrations. Arabesques, florilegia, fantastic urns, grotesque anamorphic forms that grin like gargoyles.

It is a strange but not displeasing mixture of chaste classic line, Arabian Nights atmosphere, and a lady who could sway out of the pages of an Art Deco fashion catalogue.

I also chanced on a catalogue of a Justin O'Brien retrospective, an Australian academy-trained modernist who painted in a loosely Siennese style.

I think this is marvellous painting in its primitive, urgent colour, and in its austere counterpoint of volume and void. O'Brien is a modern who has enriched himself with the passionate study of masters who suited his temperament. There is no straining, no reaction, no rebellion. The work has the timelessness of a Duccio or a Puvis de Chavannes. But with Chavannes I am pulled up on the question of spiritual content. Like Chavannes there is purity of colour, grandeur of form, serenity of temperament. But what is the painting's contribution to the summa of Christian art? Not having been Christian in 20 years I'm not one to offer an ultimate affirmation or denial. But it strikes me that works like this don't positively add to the Christian tradition so much as offer a lyrical interlude. Time is suspended, and you find yourself back in 13th century Tuscany, but of fresh religious inspiration, prophetic insight, I see no sign. But then, the greatest popular Christian thinker of the time was probably an ironist, G.K. Chesterton, compared to figures like St. Francis and Dante who would have inspired O'Brien's idols. At least he didn't howl to the whirlwind.

There was also a paperback annual of the art / lifestyle magazine 'Colour', whose sub-heading features the charming endorsement: 'The most fascinating magazine in the world'. At a glimpse it's a bit of a mish-mash of art reviews, anthologized (bad) poetry, serialised adventures, etc. And gobsmackingly beautiful illustrations from the Royal Academy thoroughbreds of the day. The colours stand up well today and they must have been stunning to the audience of the time. And the reproductions bear no consistent relation to the text. But they're awfully pretty ... er, 'fascinating'.

Also, two calf-skinned volumes of Wordsworth and Tennyson. So sensuous to touch it ought to be perverse.

Again, a quarto of sculpture from India, Khmer, Cambodia. The perfect companion to the Roger Fry 'Last Lectures' recently acquired ...

And lastly :

Two 1st eds (I think), hard cover and dust jacketed Frank Herberts and Douglas Adams: 'Chapter House Dune' & 'Mostly Harmless'.

A good haul this year.