Cutting the Gordian Knot

“Gordius, the King of Phrygia, once tied a knot that no one could untie. It was said that he who solved the riddle of the Gordian Knot would rule all of Asia. So along comes Alexander the Great, who chops the knot to bits with his sword. Just a little different interpretation of the requirements, that’s all… and he did end up ruling most of Asia.”

So begins chapter seven of one of my favourite programming books of all time.

Finding the answers to impossible problems is all about asking the right questions. So, here you have them ;-)

Problem solving questions

  • Is there an easier way?
  • Are you trying to solve the right problem, or have you been distracted by a peripheral technicality?
  • Why is this ting a problem?
  • What is it that’s making it so hard to solve?
  • Does it have to be done this way?
  • Does it have to be done at all?

TPP promises; “Many times a surprising revelation will come to you as you try to answer one of these questions.”

One of the original books on heuristics in problem-solving was G. Polya’s “How to Solve It” (1957). Although his book is about problem solving in mathematics, it can just as well be used for software engineering.

Here’s a summary of Polya’s problem solving approach:

    1. Understanding the problem. You have to understand the problem. What is the unknown? What are the data? What is the condition? Is it possible to satisy the condition? Is the condition sufficient to determine the unknown? Or is it insufficient? Or redundant? Or contradictory? Draw a figure. Introduce suitable notation. Separate the various parts of the condition. Can you write them down?
    2. Devising a Plan. Find the connection between the data and the unknown. You might be obliged to consider auxiliary problems if you can’t find and intermediate connection. You should eventually come up with a plan of the solution.     Have you seen the problem before? Or have you seen the same problem in a slightly different form? Do you know a related problem? Do you know a theorem that could be useful?     Look at the unknown! And try to think of a familiar problem having the same or a similar unkown. Here is a problem related to yours and solved before. Can you use it? Can you use its result? Can you use its method? Should you introduce some auxiliary element in order to make its use possible?     Can you restate the problem? Can you restate it still differently? Go back to definitions.   If you cannot solve the proposed problem, try to solve some related problem first. Can you imagine a more accessible related problem? A more general problem? A more special problem? An analogous problem? Can you solve a part of the problem? Keep only a part of the condition, drop the other part; how far is the unknown then determined, how can it vary? Can you derive something useful from the data? Can you think of other data appropriate for determining the unknown? Can you change the unknown or the data, or both if necessary, so that the new unknown and the new data are nearer to each other?     Did you use all the data? Did you use the whole condition? Have you taking into account all the essential notions involved in the problem?
    3. Carrying out the Plan. Carry out your plan. Carrying out your plan of the solution, check each step. Can you see clearly that the step is correct? Can you prove that it’s correct?
    4. Looking back. Examine the solution. Can you check the result? Can you check the argument? Can you derive the result differently? Can you see it at a glance?     Can you use the result, or the method, for some other problem?

    Woh! That’s a lot of problem-solving questions for you – it’s almost like finding the “unsub” in an episode of the CBS show Criminal Minds isn’t it?

    Also, notice how similar this is to design patterns? Those problem solving questions are almost like a design pattern for finding solutions! Go ahead – print them out, and refer to them when needed.

Code checklists

The best things in life are free smart and simple! As code checklists for example. I’m currently reading Bob Walsh‘s recent book Mirco-ISV – From Vision To Reality, which is a book about running your own Micro-ISV (independent/internet software vendor). Micro-ISV’s are often one-man companies, for example single developers producing and selling their own software. Early in the book Walsh talks about ‘developing the Micro-ISV way’, and this is where he gets into code checklists:

Using a code checklist is the easiest method of the lot. Go into Microsoft Word, write the half-dozen coding mistakes you’ve last made, print the document, and look at the list as you review the code you wrote today. See any familiar faces? Write down other ways you miss the mark as they crop up, and revise your code’s checklist.doc periodically. What you’ll notice over time is coding is just like spelling. You make certain coding mistakes repeatedly, but if you focus on them, you’ll make them less often. Just the process of checking my code against my private coding checklist identifies about half of the coding mistakes I make, especially those nasty increment-by-zero blunders and other simple errors. Give it a try!

Strange I haven’t thought about this before, or done it before – but I haven’t. At least not that I can remember. I’ve just started doing it, so I don’t know how much it will pay off, but it looks like a good idea at least :) .. I’ve decided to use one checklist for each language I code in (e.g. java.txt, php.txt, javascript.txt, etc.) – as I code in multiple languages each day. In addition I think it could be a good idea to include functions and things I often forget that I have to look up way too often (like various string functions in JavaScript!).

The Java community retaliates – "PHP is …"

Ok, it’s time to let the people of #java on Ef-net let us know what they thought of PHP in the period from July to December 2005 (read: yes, I’ve done a grep -iH “php is” on my #java irclogs). Of course, we all know the PHP developers had strong opinions on Java, so this will be interesting.

< @Tenchi> php is terd < @aut> straight php is faster than using php to transform xml < waz> don’t forget “PHP is slow!” < pr3d4t0r> waz: “PHP is slow! Use Perl.” < AstroIvan> and php is slow < jjava> and php is slow < tomh> so php is faster when you dont use clusters? < @joeblowgt> and php is fine for many types of websites < @jwzrd> BoZo, no you are not. Php is not something you program. < @jwzrd> deigo, php isn’t coding < @top^> a bunch of if statements? now that’s why php is not java < SouljaGoD> PHP is fairly easy for this kind of thing < mele> php is nice ;) < pr3d4t0r> kinabalu: PHP is like a gun. < mele> Tenchi: php is fun :) < @GnarlyBob> php is more scalable < eth-> you think PHP is understandable? hahaa < @boxed> you know, php is pretty much like java except with a chaotic standard library and a billion $’s thrown randomly in the code < jjava> but php is able to defy gravity < @jwzrd> hahaha php is such a configuration shithole < @jwzrd> PHP isn’t allowed < Jippi> i know – php5 got oop as a ‘bonus’ hehe.. php isnt orignally a oop language < @jwzrd> PHP is annoying though :) < CnfsdKid> php is forever horrible. < CnfsdKid> DeanC: php is retarded. < Number-6> php is quite retarded. < tonicxt> Php is nice as it has build in user-input sanitization automatically enabled on all user-input -=- < CnfsdKid> geerd: maybe php is more your style. < gripe> geerd: php is for semi-literate high school kids to write blogs with < porc> php is simply not an enterprise class language < geerd> and PHP is for the web, you dont listen for any keys

Well, we see they have humor; “PHP is slow, use Perl” is of course a reference to the idiotic “Java is slow, C is better” which these developers hear too often. So, they throw it over to the PHP community, where it, of course, is just is stupid.

Also, notice how few opinions about PHP in #java there are compared to the number of opinions on Java in #PHP in the same time period; Java coders are too cool to care about PHP. I mean; they’re coding Java, enterprise applications. They don’t kid around. It’s real OO, not “We kind of .. uhh, well, got some OO in the latest PHP5 release guys, uuh..” (Zend).

In case you’re wondering I consider myself a Java developer foremost, and a PHP developer second ;) .. but “times, they are a-changin'” as Bob Dylan sings.