Documentation CS Thinking Example 3

Follow the full solution, then compare it with the other examples linked below.

Example 3

medium
A function has no comments or documentation. Reverse-engineer its purpose from the code: FUNCTION mystery(text): SET result = ''. FOR i = LENGTH(text)-1 TO 0 STEP -1: result = result + text[i]. RETURN result. Write appropriate documentation for it.

Solution

  1. 1
    Step 1: The function iterates through the string backwards, building a new string character by character. It reverses the input string.
  2. 2
    Step 2: Documentation: // FUNCTION reverseString(text): Reverses the input string. // Parameter: text (String) - the string to reverse. // Returns: String - the input string with characters in reverse order. // Example: reverseString('hello') returns 'olleh'.

Answer

The function reverses a string. Documentation should include: purpose, parameters, return value, and an example.
Good function documentation describes purpose, parameters, return value, and examples. This allows other developers to use the function without reading its implementation.

About Documentation

Software documentation is the collection of written descriptions that explain how a system works and how to use it, including inline code comments, user guides, API references, design documents, and README files. Good documentation makes software understandable, usable, and maintainable by both current and future developers.

Learn more about Documentation โ†’

More Documentation Examples