Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Multiple return points - is it OK practice?
Message
De
01/02/2013 15:53:03
John Baird
Coatesville, Pennsylvanie, États-Unis
 
 
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01564936
Message ID:
01564959
Vues:
49
>>>Hi everybody,
>>>
>>>I am wondering what are good ways of avoiding multiple return points and yet not being caught in multiple if /else complex logic.
>>>
>>>Is it OK to return early? I think in C# it's a normal practice, but what if I need to format output? E.g. at the bottom of each method I have
>>>
>>>String returnMessage = Base.GetReturnString(errorValue, statusCode, messageText);
>>>            Logging.Log("Returning " + returnMessage, 2);
>>>
>>>            return returnMessage;
>>>
>>>So, should I put these 3 lines in its own method and just do
>>>
>>>return LogAndReturn(errorValue, statusCode, messageText);
>>>
>>>every time I need to exit the procedure earlier?
>>
>>If your code checks for a bunch of conditions at the start and exits early when one of them fails, that makes the real heart of the code easier to follow. No need to artificially indent the heart of the code.
>
>I agree and I've decided to use early exit points. I've checked a few procedures coded by my colleagues - they used early exit points. I also believe this is part of ASP.NET MVC - I've seen lots of conditional return code in the Controller's code. Otherwise I was getting lost in all these if else statements although I coded a few of the methods this way - only 1 exit point.


Doesn't surprise me that you pick the non-standard approach...
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform