A common instinct among a certain type of programmers is to try to refactor, generalise and abstract away every repeated line of code. The opposite tendency is to cut and paste large sections of code, which can quickly generate long sprawling programs that are difficult to maintain.
Avoiding repetition is a good thing then? Well yes, but it has to be balanced against readability. In order to avoid repetition common functions, data structures and behaviours are packaged up in abstractions such as functions and classes. Taken too far this scatters the flow of the code across multiple functions and classes, all for the sake of avoiding a little repetition. I've come back to code I've written myself and found the layers of abstraction I've used more of a hindrance than a help. While I was writing it of course I thought I was being really clever.
Good abstractions are hard to work out and not always worth making. Now I consciously try to write my code using a few simple abstractions that can be easily explained and remembered. Repetition is allowed if it means that the information I need to understand the code I'm reading is right there in front of me, rather than n-steps away in a forgotten object heirarchy.
Rigidly defined areas of doubt and uncertainty.
Thursday, July 22, 2004
Friday, May 21, 2004
Tai Chi Talking
I was listening to Simon Mayo interviewing Derren Brown, the magician / hypnotist, on Radio 5 yesterday. Simon asked the question everyone wants to ask "Do you ever use your skills in real life?". He wouldn't be drawn on the subject of getting free meals at restaurants etc, but became more enthusiastic when he was asked if he used his powers of persuasion to defuse conflict situations. He advised to always have some song lyrics up your sleeve and explained why with a story of a time he avoided being attacked in the street one night.
Attacker: Oi you! What you lookin' at! etc etc
Derren: The wall in my garden is less than ten feet high.
Attacker: what?
Derren: The wall in my garden is less than ten feet high. The walls in Spain are ten feet high.
The totally out of context reply confused the attacker and took all the energy out of him. He later sat with Derren and discussed the problems he was having with his life.
I can see very strong parallels between what Derren did with his words and the philosophy of tai chi. In tai chi you never confront force with force, you always seek to read the intention of your opponent and through avoiding it use their own energy to uproot them, while maintaining a strong root yourself. Derren didn't seek to argue with his attacker, avoided his confrontational intention and mentally uprooted him. The presence of mind and calmness required to do what he did has a lot in common with good tai chi practice too, which is as much mental as physical.
If I find myself in an argument I'm more interested in avoiding than winning - such as a recent encounter I had with a van driver while I was out cycling - I might try Derren's approach. I just need to think of some suitably abstract and memorable phrases. Favourite at the moment is this, spoken by Ambassador Kosh in Babylon Five - "The avalanche has started. It's too late for the pebbles to vote"
Attacker: Oi you! What you lookin' at! etc etc
Derren: The wall in my garden is less than ten feet high.
Attacker: what?
Derren: The wall in my garden is less than ten feet high. The walls in Spain are ten feet high.
The totally out of context reply confused the attacker and took all the energy out of him. He later sat with Derren and discussed the problems he was having with his life.
I can see very strong parallels between what Derren did with his words and the philosophy of tai chi. In tai chi you never confront force with force, you always seek to read the intention of your opponent and through avoiding it use their own energy to uproot them, while maintaining a strong root yourself. Derren didn't seek to argue with his attacker, avoided his confrontational intention and mentally uprooted him. The presence of mind and calmness required to do what he did has a lot in common with good tai chi practice too, which is as much mental as physical.
If I find myself in an argument I'm more interested in avoiding than winning - such as a recent encounter I had with a van driver while I was out cycling - I might try Derren's approach. I just need to think of some suitably abstract and memorable phrases. Favourite at the moment is this, spoken by Ambassador Kosh in Babylon Five - "The avalanche has started. It's too late for the pebbles to vote"
Monday, May 03, 2004
Taking notes
I'm reading several books at the moment, all online, some free and some on safari. I've taken in a bit on Lisp, some XML, and some more general reading about programming , but mostly I'm trying to get a more thorough understanding of Linux. I'm OK programming under Linux and muddling through configuration with a HOWTO, but I've never taken the time to get really familiar with Linux/Unix. Using Linux, as opposed to writing it, which I'm sure is great fun, is a pretty dull dry subject, but it's the foundation that everything else I use is built on so it will be worth the effort. I'm reading Running Linux and I'm finding it a fairly pleasant read. I had a look at RUTE, but the style was a bit rambling and started from very very basic principles.
I'm a little bit annoyed that the range on Safari is too narrow. I can understand that O'Reilly themselves fill a particular niche, but Safari provides access to several publishers with a wider range of titles. I sent them an email asking if they could make Paul Graham's ANSI Common Lisp available.
I take notes when I read. I used to do it on paper, but I prefer to keep them all in text files now. If I don't take notes it's too easy to read through a chapter and take very little in. Taking notes as I go forces me to think about what I've just read in the same way as explaining it to someone else would, and almost as a by-product gives me a nice aide-memoir and index.
I'm a little bit annoyed that the range on Safari is too narrow. I can understand that O'Reilly themselves fill a particular niche, but Safari provides access to several publishers with a wider range of titles. I sent them an email asking if they could make Paul Graham's ANSI Common Lisp available.
I take notes when I read. I used to do it on paper, but I prefer to keep them all in text files now. If I don't take notes it's too easy to read through a chapter and take very little in. Taking notes as I go forces me to think about what I've just read in the same way as explaining it to someone else would, and almost as a by-product gives me a nice aide-memoir and index.
Friday, April 16, 2004
First post
I'm currently reading Dynamic HTML: The Definitive Reference. It's OK but the author's style of writing annoys me. I feel like I'm being talked down to by someone who knows less about programming than I do. Just the facts, no opinion unless it's really well argued and relevant, that's what I want in a technical book. It's taking up two spaces on my safari bookshelf, so I'll probably remove it once I've picked up the major differences between the various browsers and the specification.
Once I've learnt the basics I find that online references are often better than published books anyhow. The online PHP manual tells me all I need to know about PHP, The W3C documents are good references for HTML and CSS etc, only the MySQL manual has disappointed so far.
Once I've learnt the basics I find that online references are often better than published books anyhow. The online PHP manual tells me all I need to know about PHP, The W3C documents are good references for HTML and CSS etc, only the MySQL manual has disappointed so far.
Subscribe to:
Posts (Atom)