Command Line Arguments, Code Maintenance and Version control: Controlling the hidden cost of custom software

Console programs are extremely useful simple to create programs that can produce great returns for the amount of time spent creating them. To improve the usefulness of console programs, one may use the ability of the console program to detect the ‘command line parameters’ passed to the program.

What are command line parameters? These are the characters typed in  when you launch a program executable. Let us say we have a program that returns the length of a file. For example, in a prior post we created a console program that added a file named “test.txt” to a database table record with a column datatype of ‘Varbinary(max)’.

 In that post, we found out the sample file we created was in fact 37 bytes in length.  The below screenshot describes a simple C# Visual Studio console program (test.exe)  that displays the number of bytes in a file named test.txt

Visual Studio Express Console Program used to count number of bytes in a file.
Visual Studio Express Console Program used to count number of bytes in a file.

However a weakness of the above console program is that it uses a ‘hard-coded’ method of identifying the file name and file path. The fact that the program  identifies the file via a hard coded method, limits it. It is because the filename is compiled into source code, that it cannot be changed unless you recompile it.

Recompiling the program can be expensive to do because you then have to replace any copies of the prior executable (test.exe) with the ‘new’ test.exe. Problems of this type, have to do with ‘code base management’ and ‘version control management’.

Managing the source code, an aspect of ‘configuration management’ is a part of an organizations due diligence to mitigate problems arising from potentially bad software compiles.

For example, if the software were released but the file name was incorrectly identified as “mistakenText.txt”, the source code would have to be ‘checked out’ and the line of code where the filename was identified would have to be located and then changed to the correct filename. So the release manager of the organization would be required to locate the source code, the developer of the program be required to change the source code (apply a fix). During the process of creating the fix,  a new version would be created. A QA Control Manager would take the executable and test it out to make sure it meets the quality control standards. If for some reason, the test manager (Quality Control Manager) discovers a flaw or problem with the software, it must be identified as a problem and the issue resolved. After the test manager verifies the program is good, it is ready to be deployed. In this final step,  the release manager would take the new compiled executable and release it to the users.  This is why maintaining software can be so expensive in terms of time and effort and cost. This is why developers are encouraged to write code that does not need a new compile whenever some frequently changed variable requires modification.

In order to access the command line parameters of a C# console program one can use code similar to this example:

            //the first arg element will contain the file’s path
            Console.WriteLine(@”The first element contains the file’s path: {0}”,args[0]);

In the above example, the command line argument is stored automatically by the console program in an automatically created array variable named ‘args’. Because this variable is in fact an array, we can obtain the first element at position zero ‘0’ using the following expression: args[0]. Note the ‘@’ is a special character the C# uses to identify text making it easier for the compiler know how to handle the data the follows. Note also that the curly brackets surround a number. The number zero refers to the first parameter in the console writeline statement which is args[0]. If we had a second command line argument that describes the file , it would be args[1]. And you would reference it like this:

Console.WriteLine(@”The file’s path: ‘{0}’ and the description: ‘{1}’.”,args[0], args[1]);
 
The below is a screenshot showing the output when the console program is run.
 
C# Console Program: Using two command lines with console.writeline

C# Console Program: Using two command lines with console.writeline

I hope the above example provide a good background for using command line parameters in a console program. In conclusion, command line parameters can be used to make software more useful and easier to maintain (less costly). Many developers use command line parameters with console programs however the concept is not unique to console programs created in c# or to any particular programming language. One may also use command line parameters for full-fledged windows applications as well or for non windows systems using other development environments besides visual studio.