views:

5271

answers:

4

I'm not sure why I'm getting this error, but shouldn't this code compile, since I'm already checking to see if queue is getting initialized?

public static void Main(String[] args)
{
    Byte maxSize;
    Queue queue;

    if(args.Length != 0)
    {
        if(Byte.TryParse(args[0], out maxSize))
            queue = new Queue(){MaxSize = maxSize};
        else
         Environment.Exit(0);
    }
    else
    {
        Environment.Exit(0);
    }

    for(Byte j = 0; j < queue.MaxSize; j++)
        queue.Insert(j);
    for(Byte j = 0; j < queue.MaxSize; j++)
        Console.WriteLine(queue.Remove());
}

So if queue is not initialized, then the for loops aren't reachable right? Since the program already terminates with Environment.Exit(0)?

Hope ya'll can give me some pointers :)

Thanks.

+7  A: 

The compiler doesn't know that Environment.Exit() does not return. Why not just "return" from Main()?

Brian
I prefer to use Environment.Exit with a non-zero error status in case the program is called from a script. That way the script can know whether the program was successful or not by checking the exit status.
tvanfosson
Change the return type of main to int and return the status.
Scott Langham
+14  A: 

The compiler doesn't know that the Environment.Exit() is going to terminate the program; it just sees you executing a static method on a class. Just initialize queue to null when you declare it.

Queue queue = null;

tvanfosson
A: 

The compiler only knows that the code is or isn't reachable if you use "return". Think of Environment.Exit() as a function that you call, and the compiler don't know that it will close the application.

Nelson Reis
I can think of two other sources of unreachable code:Break or continue in a loop, and throw :)
Bjarke Ebert
That's true. I was referring to this specific question, but thanks for the clarification, it could lead to some misinterpretation.
Nelson Reis
+2  A: 

A couple of different ways to solve the problem:

Just replace Environment.Exit with return. The compiler knows that return ends the method, but doesn't know that Environment.Exit does.

static void Main(string[] args) {
    if(args.Length != 0) {
       if(Byte.TryParse(args[0], out maxSize))
         queue = new Queue(){MaxSize = maxSize};
       else
         return;
    } else {
       return;   
}

Of course, you can really only get away with that because you're using 0 as your exit code in all cases. Really, you should return an int instead of using Environment.Exit. For this particular case, this would be my preferred method

static int Main(string[] args) {
    if(args.Length != 0) {
       if(Byte.TryParse(args[0], out maxSize))
         queue = new Queue(){MaxSize = maxSize};
       else
         return 1;
    } else {
       return 2;
    }
}

Initialize queue to null, which is really just a compiler trick that says "I'll figure out my own uninitialized variables, thank you very much". It's a useful trick, but I don't like it in this case - you have too many if branches to easily check that you're doing it properly. If you really wanted to do it this way, something like this would be clearer:

static void Main(string[] args) {
  Byte maxSize;
  Queue queue = null;

  if(args.Length != 0 && Byte.TryParse(args[0], out maxSize)) {
     queue = new Queue(){MaxSize = maxSize};
  } else {
     Environment.Exit(0);
  }

  for(Byte j = 0; j < queue.MaxSize; j++)
    queue.Insert(j);
  for(Byte j = 0; j < queue.MaxSize; j++)
    Console.WriteLine(queue.Remove());
}

Add a return statement after Environment.Exit. Again, this is more of a compiler trick - but is slightly more legit IMO because it adds semantics for humans as well (though it'll keep you from that vaunted 100% code coverage)

static void Main(String[] args) {
  if(args.Length != 0) {
     if(Byte.TryParse(args[0], out maxSize)) {
        queue = new Queue(){MaxSize = maxSize};
     } else {
        Environment.Exit(0);
        return;
     }
  } else { 
     Environment.Exit(0);
     return;
  }

  for(Byte j = 0; j < queue.MaxSize; j++)
     queue.Insert(j);
  for(Byte j = 0; j < queue.MaxSize; j++)
     Console.WriteLine(queue.Remove());
}
Mark Brackett
Thanks for the very informative post.
jkidv