Regular expressions are like broccoli…

November 10th, 2008 by mgkimsal Leave a reply »

This was the analogy I came up with when teaching a PHP class recently.  I needed to review the topic of regular expressions, but it’s one that neither I nor the students seemed to have much interest in.  The examples that I had were adequate, but explaining the examples was still unnatural.  The students didn’t want to spend much time on regex, but I pressed ahead.  I suggested that regular expressions were like the broccoli of programming – something that left a bad taste in your mouth, but was ‘good for you’.  Now, usually the broccoli I eat is drenched in ranch dressing or balsamic vinegar, so it doesn’t leave a bad taste in my mouth.  And I know I’m not joined by 100% of the population out there – there are people who *enjoy* eating broccoli (and enjoy the taste as well).  They are a minority, just like people who enjoy writing and using regular expressions are a minority in the programming community.

Given my premise that regular expressions are like broccoli, what comparisons would you draw to other programming concepts or functions?

Closures are like carbohydrates?  Or chocolate?

Classes are like ________?

Functions are like _______ ?

Unit testing is like _______?

What catchy similes can you help me come up with?  Are these similes?  Or metaphors?  I used to say that an analogy was to a metaphor as a simile was to a __________ (this was one of those comparison fill-in-the-blanks you get on standardized tests).  Can you fill in that blank for me?  :)

 

Share and Enjoy:
  • del.icio.us
  • DZone
  • Facebook
  • Reddit
  • StumbleUpon
  • Digg
  • Simpy
  • Technorati
Advertisement

14 comments

  1. Ezku says:

    Unit testing is like love. It’ll keep you going when everything else fails. :D

  2. Thomas says:

    Regular expressions are like whiskey: Very effective, tastes like crap to most people, and you shouldn’t overdo it.

  3. stefan says:

    Unit testing is like an airbag. It saves you from disaster when bad luck strikes.

  4. Thomas says:

    Classes are the Atkins Diet: Good in theory, but you really can’t just eat meat.

  5. Thomas says:

    Unit testing is a condom. You should wear one, but it could break.

  6. Tom says:

    What on earth? No wonder you don’t have much interest in RegExp if you’re comparing them to green veg.

    Why not just get them to do something using standard string manipulation and then show them how to do it with a RegExp in half the time with a fraction of the code? In fact, why not get half the class to do it with a RegExp and then once they’re finished get them to throw Broccoli at the remaing half whilst they try to complete the task!

    The problem with regular expressions is unless you force yourself to use them regularly, you can always choose to ignore them and opt for a slower more painful route. The learning curve with RegExp is very steep, but once you get to a certain point it’s like running down hill with the wind behind you.

  7. Bill Karwin says:

    Unit testing is like flossing your teeth. Essential for good hygiene, hard for some people to get into the habit, but once you do have the habit it’s easy to do.

    By the way, you shouldn’t ignore regular expressions. They’re really not that hard. Regular expressions are just a very compact notation for a finite state machine, which is like a flowchart.

    I like Tom’s comment above. Make them code a state machine the long way, and then show them how a regular expression works the same way. If you think of a regular expression as the alternative to writing screenfulls of code, they don’t seem so hard.

  8. As if my magic, this appeared on Slashdot today. :)

  9. elzapp says:

    I’m surprised that you don’t have a regexp guy in your class. There’s always someone with a bizarre liking for broccoli. Like me for instance. The problem is, my first solution is always a regexp, even if there is a lot better and more elegant ways to solve the problem.

  10. Martin Fjordvald says:

    Classes are blueprints and functions are actually called methods when used with classes. :)

    Unit testing is wasting money since you really can’t justify a huge yearly “maintenance” free for support and “upgrades” if you do them. They’re horribly, horribly evil I say.

  11. Simon H says:

    Also by coincidence, thedailywtf.com today supplied this quote from Jamie Zawinski:

    Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems.

    I’m not saying I agree with that!

    Broccoli? You’ve just got to cook it right.

  12. fa says:

    RegEx is like chocolate.
    I want more :)

  13. Philip Graham says:

    Closures are like quantum mechanics, hardly anyone understands them but they’re always working in the background.

    Unit testing is like getting up to go to the gym at 5:00am. It’s painful initially, but once you’ve done it a few times you feel better :) .

Leave a Reply