Linux Basic — Clean & Readable Code Practice
Practice Exercises — Clean & Readable Code
Refactor existing scripts to make them maintainable.
Warm-up
- Take a script with single-letter variable names and rename them to descriptive
snake_casenames. - Add a header block with description, usage, dependencies, and exit codes.
- Extract a repeated 10-line block into a function.
Naming
- Convert all environment variables to
UPPER_CASE. - Convert all local variables to
lowercase_snake_case. - Rename functions to verb-first:
userCount()→get_user_count().
Commenting
- Find 3 “what” comments and rewrite them as “why” comments.
- Add a comment explaining a magic number (e.g.,
sleep 5). - Remove redundant comments that just restate the code.
Organization
- Refactor a monolithic 200-line script into 5–7 functions, each with a single responsibility.
- Move all
readonlyconstants to the top. - Add a
main()function and move script body into it.
ShellCheck
- Run
shellcheckon a script and fix all quoting warnings (SC2086, SC2046). - Fix
cdusage (SC2164) by adding|| exit. - Fix array expansion (SC2128) by using
"${arr[@]}". - Achieve zero warnings at default level.
Full Refactor
- Take a legacy script with no header, cryptic names, and no functions. Apply all techniques from this chapter to make it production-ready.
Verification Checklist
- All variables quoted unless splitting is intentional
- ShellCheck passes with zero warnings
- Functions are < 50 lines each
- Script has a complete header
main()is at the bottom- Constants are
readonlyand at the top
Next chapter → Optimizing Scripts