Home > other >  When to make a program a command versus a subroutine?
When to make a program a command versus a subroutine?

Time:08-15

The "UNIX Programmer's Manual" [1] by Ken Thompson and Dennis Ritchie says this:

Commands are programs intended to be invoked directly by the user, in
contradistinction to subroutines, which are intended to be called by the
user’s programs. Commands generally reside in directory /bin (for binary
programs).

How do you decide whether a program should be made into a command and run at the shell versus made into a subroutine that is called by other programs?

[1] https://www.bell-labs.com/usr/dmr/www/manintro.pdf, page ii.

CodePudding user response:

The audience. It is rather something to be used by system admins (which devs also play the role of from time to time) or an API to help developers build systems.

But they are not exclusive. Many times you have the same software packaged as both. For example, OpenSSL is mostly an API that is largely used by the industry for encryption affairs but the package also includes commands that can be run on the shell. XZ is a compression tool that you can run on the command line but it also provides an API for programs to call to compress their data.

So you can definitely package your code as both an API and a command. It comes down to how much extra work you are willing to put to format and maintain both. In some cases it does not make sense to do both like a math library like Armadillo for example - you just don't run math commands on a daily basis.

  • Related