Category Archives: Intro to Programming

#challenge: Write an API

If you manage a set of code, take some time today to improve your flexibility by replacing a certain set of code with an API.

Common characteristics of an API

  • A logical group of functions surrounding a task or set of data.
  • A reusable system that is easy to document, and a joy to use.
  • Easier bug fixes.  Fix it once, and it’s fixed everywhere.

You might have a set of tasks or functions on a website, or in a program that could really benefit from reduced code duplication. Things like…

  • Email systems.  Do you send emails a lot?  Reduce the work involved in sending emails, by writing a nice email API. $email->setSubject("Hello!"); $email->setMessage($message); $email->setRecipient($to); $email->send();
  • Formatting systems. If you have structured semantic data, waste less time formatting it by creating a slick formatting engine.  Array goes in; formatted data comes out. $formatting->printUserList($users);
  • Logging API.  Create a simple system for you to write entries to a log file.  Rather than complicated stacks of code, use a simple function call like $debug->log("There was a problem")