Tuesday, January 24, 2012

My Dozen Lines of Code

Today's ProgrammingPraxis challenge is this gem:

A high-school programming teacher recently asked for examples of short programs with a high “cool” factor, the idea being to get his students interested in programming computers. I’m not sure the suggestions would work; today’s high-school students have been surrounded by computers their entire lives, and it takes a lot to make them think a program is cool. Being from a different generation, I can remember when I thought it was cool that a program properly skipped over the perforation on a stack of green-bar paper — many programs didn’t! Your task is to write a cool program in a dozen lines of code. You can define cool in any way that you wish. Try not to abuse the definition of “line of code,” at least not too badly; to be concrete, we will say that your solution must not exceed 12 lines, and each line must not exceed 80 characters including white space. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

The ideal intro-to-programming example should meet two needs: (1) it should be relevant and interesting to students and (2) it should lay the groundwork for learning good habits.

Meeting those two goals in 12 lines of code is tricky. But, I think doable. In fact, I believe I've done it below.

Using AutoHotKey, I've written a simple implementation of the classic Boss Key. While not exactly some exciting 3D game, I'd say that this sort of example is relevant and at least interesting to students. And while it may not be obvious, there are a number of best practices taught, including:

  • Use the right programming language for the job. AutoHotKey makes scripting trivial, so embrace it.
  • Learn about regular expressions
  • Learn how to read language reference documentation
  • Learn about loops
  • Learn that programming is all about solving your problems and making life better
  • Learn about variables and the advantages of not hard coding values throughout code
  • Many opportunities for growing this example

OK, enough talk, here's the code:

;; A simple Boss Key app. My contribution to the dozen-line program contest.
NeedToHide = .*(Mozilla|Explorer|Chrome).*
WantToShow = .*emacs.*
#b::
  SetTitleMatchMode, RegEx
  WinGet, id, list, %NeedToHide%
  Loop, %id% {
    this_id := id%A_Index%
    WinHide, ahk_id %this_id%
  }
  WinActivate, %WantToShow%
  Return,

You can run this by downloading autohotkey (it's free) and running:

  autohotkey bosskey.ahk

Once you run the above code, hitting Windows+b will hide all your browser windows and bring emacs to the front. The windows aren't just minimized, they are hidden so they don't appear in your start bar.

You'll probably want this command available (Windows+w) too, as it restores all your browser windows:

#w::
  DetectHiddenWindows, On
  SetTitleMatchMode, RegEx
  WinGet, id, list, %NeedToHide%
  Loop, %id% {
    this_id := id%A_Index%
    WinShow, ahk_id %this_id%
  }
  WinActivate
  Return

And yes, I know the syntax and semantics of AutoHotKey are hideous. But, given how powerful it is, I think it's a platform worth learning on.

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails