views:

56

answers:

3

This is just an efficiency question really.. I'm interested to know if there is a more efficient or logical way that people use to handle this sort of scenario.

In my asp.net application I am running a script to generate a new project my code at the top level looks like this:

Dim ok As Boolean = True
    ok = createFolderStructure()
    If ok Then ok = createMDB()
    If ok Then ok = createProjectConfig()
    If ok Then ok = updateCompanyConfig()

I create a boolean and each function returns a boolean result, the next function in this chain will only run if the previous one was successful. I do this because an asp.net application will continue to run through the page life cycle unless there is an unhandled exception and I don't want my whole application to be screwed up if something in the chain goes wrong (there is a lot of copying and deleting of files etc.. in this example).

I was just wondering how other people handle this scenario? the vb.net single line if statement is quite succinct but I'm wondering if there is a better way?

+2  A: 

Normally, a function should not return true/false based on its result - that is considered old fashioned. A try/catch block is better here, where the function throws an exception in case of an error. That is, of course, assuming errors only occur at special occasions:

Try
    createFolderStructure()
    createMDB()
    createProjectConfig()
    updateCompanyConfig()
    ''// ...
Catch ioExeption as IOException
    ''// handle it
End Try
Kobi
Thanks for the reply.. the problem with the try catch approach is that all of the functions would still be run, taking the above example, if createFolderStructure() fails then createMDB() cannot work and the above would not stop that. If the try/catch was in each function itself then even if I call 'Exit Function' in the catch the next function in the chain will run?the next function will still need to query a result to know if it's safe to run or not?
Markive
@Markive, you have some things mixed up. If the createFolderStructure raises an exception, the createMDB most definitly does not run.
Lieven
@Markive - if `createFolderStructure` fails and raises an exception none of the following methods will be called. Control will immediately pass to the exception handling section.
ChrisF
haha good point how stupid of me.. Ok final question related to this.. How ineffecient are try/catches are there many overheads associated with them? I seem to use them all the time..
Markive
@Markive - I didn't mention performances in one word in my answer, this is more about best practice and readability. It also saves you the need of returning a Boolean from all methods. If you have many errors, or many cases where you `return false` - this does have an overhead, and isn't the right choice for you.
Kobi
The overhead is negligible, especially in the context of working with files.
dbasnett
+2  A: 

You could drop the Boolean all together

If createFolderStructure() Then
If createMDB() Then
If createProjectConfig() Then
If updateCompanyConfig()

but I have to agree that using exceptions is the better way to handle these situations.

Lieven
Kobi
+1  A: 

Kobi showed you the way to do it. If you really need to use the results of boolean functions then you can chain them with the AndAlso operator.

Dim ok As Boolean
ok = createFolderStructure() 
    AndAlso createMDB()
    AndAlso createProjectConfig()
    AndAlso updateCompanyConfig()

Those expressions are evaluated lazily, i.e. if one function returns false the rest of the expression is not evaluated.

Ozan
AndAlso is the short-circuit version of And.
dbasnett