Skip to content

Instantly share code, notes, and snippets.

@goopi
Created December 18, 2013 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goopi/8018088 to your computer and use it in GitHub Desktop.
Save goopi/8018088 to your computer and use it in GitHub Desktop.
Do not program "defensively"

Do not program "defensively"

A defensive program is one where the programmer does not "trust" the input data to the part of the system they are programming. In general one should not test input data to functions for correctness. Most of the code in the system should be written with the assumption that the input data to the function in question is correct. Only a small part of the code should actually perform any checking of the data. This is usually done when data "enters" the system for the first time, once data has been checked as it enters the system it should thereafter be assumed correct.

Example:

%% Args: Option is all|normal
get_server_usage_info(Option, AsciiPid) ->
  Pid = list_to_pid(AsciiPid),
  case Option of
    all -> get_all_info(Pid);
    normal -> get_normal_info(Pid)
  end.

The function will crash if Option neither normal nor all, and it should do that. The caller is responsible for supplying correct input.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment