Changes

Jump to: navigation, search

User:Lkates

3,543 bytes added, 17:55, 18 January 2007
Code Reading Exercise
First I looked at loadargc, since argc reminds me of the C/C++ standard command line variables that you use in main. However, that turned out to be a load balancing program. Next I looked at Main.h, since main seems like a good start, and headers tend to define stuff. But there wasn’t anything useful there. So I opened Main.c, and voila! Code that has to do with command line switches.
#. What are your first reactions to these files when you examine them?
'''make'''
I don’t think I can put them down, in accordance to Seneca’s Acceptable Use Policy. But the sheer amount of code and underscores and structs were a bit overwhelming.
#. How is the code for working with command-line switches organized at the method, class and project levels? (e.g. is is all in one class? broken across multiple classes? spread across many methods? etc)
'''make'''
<code>
/* The table of command switches. */
 
static const struct command_switch switches[] =
{ 'b', ignore, 0, 0, 0, 0, 0, 0, 0 },
<code>
/* Secondary long names for options. */
 
static struct option long_option_aliases[] =
{
<code>
static struct file *
enter_command_line_file (name)
char *name;
{
if (name[0] == '\0')
fatal (NILF, _("empty string invalid as file name"));
}
</code>
 
'''Ant'''
Opening main.java yeilds some gold right off the bat:
 
<code>
/**
* Command line entry point into Ant. This class is entered via the
* canonical `public static void main` entry point and reads the
* command line arguments. It then assembles and executes an Ant
* project.
* <p>
* If you integrating Ant into some other tool, this is not the class
* to use as an entry point. Please see the source code of this
* class to see how it manipulates the Ant project classes.
*
*/
public class Main implements AntMain {
</code>
 
 
Right after, there's more gold
 
<code>
/**
* Creates a new instance of this class using the
* arguments specified, gives it any extra user properties which have been
* specified, and then runs the build using the classloader provided.
*
* @param args Command line arguments. Must not be <code>null</code>.
* @param additionalUserProperties Any extra properties to use in this
* build. May be <code>null</code>, which is the equivalent to
* passing in an empty set of properties.
* @param coreLoader Classloader used for core classes. May be
* <code>null</code> in which case the system classloader is used.
*/
public static void start(String[] args, Properties additionalUserProperties,
ClassLoader coreLoader) {
</code>
 
Such nice documentation!
 
Soon after, I encountered a method that "processes command line arguments" Sweet!
 
<code>
/**
* Process command line arguments.
* When ant is started from Launcher, the -lib argument does not get
* passed through to this routine.
*
* @param args the command line arguments.
*
* @since Ant 1.6
*/
private void processArgs(String[] args) {
</code>
 
It goes on to parse each possible command line argument (and its long name equiv), check it for existence, proper structure, and throws exceptions where appropriate.
 
After that, it goes to check files.
#How are invalid or non-existent Filenames dealt with?
'''make'''
In general, a fatal flag is put up, and the program dies. This seems to be mostly done when the command line switches are processed. If a file was expected (ie: a Make file, or a file specified after a switch argument), then the errors are found when the switch is processed, or the file is looked at. This is different from what I was expecting. I was expecting it to do mandatory file checking right off the bat (start of main function, perhaps), and dying before it did any other work.
 
 
'''Ant'''
After checking all the switches (and their appropriate files, where appropriate), in the Start function, it goes on to run a bunch of if-blocks to check for file names. It checks for buildfile, etc. If it ever doesn't find an expected file, it throws an exception and dies.
<code>
// make sure buildfile exists
if (!buildFile.exists()) {
System.out.println("Buildfile: " + buildFile + " does not exist!");
throw new BuildException("Build failed");
}
</code>
 
 
.Find and describe 2 similarities between the code you examined in both projects.
 
.#Both of the programs process switches, then look at files
.#Both use a seperate class/method to process the command line switches
 
.Find and describe 2 differences between the code you examined in both projects.
.#'''Make''' uses structures and flags to error check their flags. In a way, the switches error check themselves. '''Ant''' uses a bunch of if blocks and exceptions. It has to error check the switches.
.#'''Make''' has a bunch of very obfuscated comments laced throughout the code. '''Ant'' puts all their documentation right before each function, and each piece of documentation is, like, 3 paragraphs long and is presented in very plain English.
 
.If you have time, try modifying one of the programs to add a new command-line switch that is your learn ID. The switch doesn't have to do anything, or can do something very trivial. (A prize to the first group that does this successfully!)
 
Sure, we'll see.
=Other Wiki Courses=
[[lkates:dps909]]
1
edit

Navigation menu