Archive

Archive for February, 2010

Regex-fu #PHPUK2010

February 26th, 2010 Wade No comments

Good start: don’t use it unless you need to, there’s plenty of alternatives, e.g. DOMXML, str_replace, etc. Also PHP5+ has lots of filters for email validation and URL validation etc, function calls you can make rather than complex regular expressions. Regular expressions can slow down quickly due to back tracking, pattern complexity and long strings.

Then the talk has become abstract, each point is prefixed with an odd statement such as “Only elephants remember everything” and “Not all matches are made in heaven” – people are getting it, but everything needs explaining before they get it!

One very good point I have seen ignored many times is “try not to be greedy.” For example /<(.+)>/ in the string <a href=”">fdsfsd</a> will match the entire thing. To make it ungreedy, either use /<(.+?)>/ or /<([^>]+)/ . Greedy matches can be 20+ times slower.

Categories: Programming Tags:

#PHPUK2010 Part 2 (MySQL stuff)

February 26th, 2010 Wade No comments

Just picked up a nice tid-bit on creating a unique index on a two column table where the values in each column may be either way around but you only ever want one instance of the value in that row. So what this means is, inserting 2,1 and 1,2 for example would result in only the first of the two inserts succeeding.

CREATE UNIQUE INDEX ON tablename (LEAST(col1,col2), GREATEST(col1,col2));

Also, WITH, I’ll be honest, never thought about using it to create temporary views. This is a bad example but shows the structure rather well:

WITH tempView (a,b) AS (
SELECT table1.col1, table2.col2
FROM table1
LEFT JOIN table2
ON table1.id=table2.id
)
SELECT a,b FROM tempView;

Better yet is changing this to WITH RECURSIVE tempView and then adding in a select inside the WITH that recalls tempView. The great example he gave is for getting flights from A to B with a varying  amount of stops, it would be possible to get all routes from A to B with one MySQL query, as long as the data stored all connecting routes.

Incidentally, while there is some great stuff coming out of this RDBMS talk, I think the queries are really hurting a lot of people’s heads. Good stuff though.

Categories: Programming Tags:

#PHPUK2010 Part 1

February 26th, 2010 Wade No comments

Josh began by using the dictionary definition of simplicity (as given by Wikipedia) pointing out that the word is often used as a derogatory statement. He then went onto “clarity of expression” and that striving for it while programming is something a lot of people do but never quite seem to achieve.

He spoke of an example where a user comes to a programmer asking for a report, and the usual first reaction is “ah, you need a reporting system.” He also said that’s not always the case, at the end of the day, the user just wanted a report, at this point I heard quite a few people take a breath in through their teeth (particularly the guy sitting to the right of me, he knows who he is.) That is a hard problem, particularly at Stickyeyes where we really do get a lot of people saying “I want a report” and often we have to build a system, simply because of the sheer amount of similar repetitious reports.

He made a very good point about developers having a tendency to go for the newest, shinyest tools (such as HipHop for PHP.) The reason for this is to point out that these tools exist because they solve a particular type of problem, so unless the tool actually helps you, do you really need to use it?

Categories: Programming Tags:

Photo of Ant Holding 500 times its own Bodyweight

February 21st, 2010 Wade No comments

_47340444_008791133-1This amazing picture of an ant holding its own bodyweight while upside down was taken by zoology specialist Dr Thomas Endlein of Cambridge University while researching creatures sticky feet.

This photo snatched the guy £700 in photographic vouchers from the Biotechnology and Biological Sciences Research Council.

His hope is studying the way ants feet self clean and change their size to support varying weights will help develop new adhesives.

“The pads on ants’ feet are self-cleaning and can stick to almost any type of surface,” he said.

“No man-made glue or adhesive system can match this. Understanding how animals can control their adhesive systems should help us come up with clever adhesives in the future.”

Categories: News, Science Tags:

Find Music by Humming – it really works! #midomi

February 1st, 2010 Wade No comments

We are sitting on the sofa at home at the moment discussing our holiday plans for the year and I got a song in my head that I started humming. Remembering the adverts on TV where you hold your phone up to a speaker and it tells you the song, I thought there must be one that you can hum to and it’ll find the song. So I did a quick google and found Midomi, I hummed the song (and I can’t really sing or even hum very well) and sure enough…it found the exact song, 2 results came back and it was the second! We’re both amazed by how well it worked.

So if you get a song stuck in your head and you can hum it, even if you’re quite bad at humming, give this website a go, chances are you’ll be amazed.

Incidentally, the song it found (which was even more impressive) was Kaoma – Lambada, which I’ve included below.

Categories: Random Stuff Tags: