Improving Clarity for -v Variables in AWK Exercises

Hi everyone,

I’d like to address an issue that may confuse external readers of code samples published in community solutions for AWK exercises. This issue came up while mentoring the Binary Search exercise, where the value variable is initialized using the -v option.

When students start the exercise, they have access to comments and can check the test cases for guidance. However, external readers who view the published solutions might not immediately understand where such “magic variables” like value are coming from, as there’s no explicit handling in the code.

Proposed Solution

To improve clarity for external readers and ensure robustness, I propose:

  1. Add an extra test case that passes an empty parameter and checks for an appropriate error message. For example:

awk -v value="" -f binary_search.awk input_file

Expected output (to stderr):

Error: 'value' variable not set.

  1. This change would encourage students to include guard validation in their solutions, such as a BEGIN block to handle the missing variable. For example:
BEGIN {
    if (value == "") {
        print "Error: 'value' variable not set." > "/dev/stderr"
        exit 1
    }
    FS = ","
}

By making this change, we can help students write more robust solutions and make it easier for external readers to understand the code.

If everyone agrees, I’m happy to proceed with creating a pull request to add these test cases and update the example implementations for all relevant exercises.

Looking forward to hearing your thoughts!

1 Like

We could provide the guard in the stub file. That way it’s right in the student’s face when they start solving.

1 Like

I like this idea. I support it.

Do you have any idea how to do this better?

# These variables are initialized on the command line (using '-v'):
# - target

BEGIN {
    if (target == "") die("Error: 'target' variable not set.")
}

function die(message) {print message > "/dev/stderr"; exit 1}

?

PR: Improve Binary Search Exercise by rabestro · Pull Request #290 · exercism/awk · GitHub

  1. We (at a problem spec level) explicitly opted out of adding input validations to exercises. I don’t think we should be reversing that trend.
  2. The variable name comes from the problem spec. Do we want to divert from the spec?

Would a comment solve this issue without actually modifying the exercise?