Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

We are writing a program that does a standard "auto update software on startup". If the software finds an update ready, it will start the update process and then exit (and not run the program).

We have two different designs on how to write the logic of the update.

Design 1: Let our AutoUpdate class handle everything, including exiting the program if required.

Design 2: AutoUpdate does most of the logic, and if it wants the program to exit, it returns a value indicating this, and the Main() takes care of exiting.

code wise, the main function would look like:

Design 1 (AutoUpdate exits internally if required)

Main()
{
    AutoUpdate.DoAutoUpdate();
    RunMainProgram();
}

Design 2 (AutoUpdate returns true if it needs to exit, Main handles the exit)

Main()
{
    if (AutoUpdate.DoAutoUpdate() == false)
    {
        RunMainProgram();
    }
}
share|improve this question
3  
I like the second way better. –  Marcelo Jun 23 '11 at 17:07
add comment

1 Answer

up vote 10 down vote accepted

I prefer the second, as it's much more apparent what is going on. I'd also write it using the ! operator, but that's personal preference as == false is more to read. :)

Main()
    {
        if (!AutoUpdate.DoAutoUpdate())
        {
            RunMainProgram();
        }
    }
share|improve this answer
1  
Just to argue: I prefer the ==false because when my eyes are tired, the ( and ! run together, visually speaking. ;) –  FrustratedWithFormsDesigner Jun 23 '11 at 17:27
2  
@FrustratedWithFormsDesigner then you should maybe opt for if( !Auto or even if( ! Auto... ) –  stijn Jun 23 '11 at 17:57
    
I guess what I have the most problem with, is that the DoAutoUpdate() function requires the program to exit if its going to work. It's not like it would be nice. If you try to update with out exiting, it is going to fail. Isn't it bad to write a function that relies on the caller to "finish" what you started in order for what you want to happen to actually work? –  David Jun 23 '11 at 18:08
    
@David: Depends on what the return value of your method is. If it returns false in cases where an update wasn't required, and that's what the caller expects, then I think it's perfectly acceptable for the caller to exit based on that. –  Ryan Hayes Jun 23 '11 at 18:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.