Challenge your initial ideas
Recently I was working on a small command-line tool. The tool was supposed to take some user input and then render this information into a file using a predefined template. Let’s skip the user input handling and focus on the template rendering because it reveals an interesting discovery.
First solution: Take a file path as an argument and render the template into that file
While thinking about the implementation, I discovered the following (“classic”) questions:
- Shall the tool create the file if it doesn’t exist?
- Shall the tool override the file when it exists? Shall it ask for permission to override the file? Does that make sense to add
- How to inform the user that the given file path isn’t writable?
- How to test all these cases above?
That sounded like a lot of questions to answer and many cases to handle. I wanted this tool to be awesome 😊, so I decided to ask myself another question, instead:
“Is it really necessary to go this way?”
Simplified solution: Render the output into the standard output stream
While still satisfying the requirements, the solution would give the following benefits:
- Automatically dismiss all questions from the first solution as no file handling should be implemented. Also, it wouldn’t be necessary to test all that
- User would have an option to preview rendered information without the need to create a file
- User would be able to redirect output to any file and have full control over that (destination, permissions, etc)
- The tool would play well with other CLI tools
This writing isn’t suggesting to send the output of every command-line tool the standard output, but trying to encourage to challenge initial ideas. Sometimes it’s possible to achieve more by actually doing less 🙂
Happy coding! 🖖