Middle School QBasic vs. Killer Coding Ninja

May 21st, 2009 technical

Just got code-pwned by my coworker yesterday. Below, you’ll see the difference between a mechanical engineer and a computer scientist’s approach to the same problem. The elegant solution is to use recursion: a function that calls itself.

The Brute Force Middle School QBasic Programmer Approach:

-	function getPath($selected) {
-
-		// search entire menu structure for key matching $menu_name
-		// return array with each level of menu path
-		// a recursive function would be cool here, beyond my ability -dw
-
-		$level[0] = getMenu('all');
-
-		foreach ($level[0] as $key[1] => $level[1]) {
-			$path[1] = $level[1];
-			$path[1]['key'] = $key[1];
-			unset($path[1]['sub']);
-
-			if (false) {
-				// first level is not allowed to be a match
-			} elseif (isset($level[1]['sub'])) {
-				foreach ($level[1]['sub'] as $key[2] => $level[2]) {
-					$path[2] = $level[2];
-					$path[2]['key'] = $key[2];
-					unset($path[2]['sub']);
-
-					if ($selected == $key[2]) {
-						break 2;
-					} elseif (isset($level[2]['sub'])) {
-						foreach ($level[2]['sub'] as $key[3] => $level[3]) {
-							$path[3] = $level[3];
-							$path[3]['key'] = $key[3];
-							unset($path[3]['sub']);
-
-							if ($selected == $key[3]) {
-								break 3;
-							} elseif (isset($level[3]['sub'])) {
-								foreach ($level[3]['sub'] as $key[4] => $level[4]) {
-									$path[4] = $level[4];
-									$path[4]['key'] = $key[4];
-
-									if ($selected == $key[4]) {
-										break 4;
-									} else {
-										unset($path[4]);
-									}
-								}
-							}
-						}
-					}
-				}
-			}
-		}
-
-		return $path;

The Killer Coding Ninja approach:

+
+	// {{{ getValues()
+	/**
+	 * Returns the properties of a specific path segment, minus any children.
+	 * @param	{string}	id		The unique id of the path segment
+	 * @param	{array}		values	All properties of the path segment
+	 * @return	{array}
+	 */
+	function getValues($id, $values) {
+		$retval = array('key' => $id);
+		foreach ($values as $key=>$value) {
+			// don't include any submenus
+			if ($key != 'sub') {
+				$retval[$key] = $value;
+			}
+		}
+		return $retval;
+	}
+	// }}}
+
+	// {{{ getPath()
+	/**
+	 * Recursive function to return the path of a section.
+	 * @param	{array}		menu		The menu to iterate over
+	 * @param	{string}	selected	The id of the ending path segment to find
+	 * @return	{array|false}
+	 */
+	function getPath($menu, $selected) {
+
+		foreach ($menu as $path_id=>$path_vals) {
+			if ($path_id == $selected) {
+				return array(self::getValues($path_id, $path_vals));
+			} else if (isset($path_vals['sub'])) {
+				$path = self::getPath($path_vals['sub'], $selected);
+				if ($path != false) {
+					array_unshift($path, self::getValues($path_id, $path_vals));
+					return $path;
+				}
+			}
+		}
+		return false;
+	}
+	// }}}

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


  • categories

  • recent comments