-1

Is there any way to manipulate the program flow in C# from within a function?

I mean in C++ it was possible to place something like

#define Verify(x) if(x==null) return;

or

#define Verify(x) if(x==null) goto _exit;

into a macro and place Verify(x) anywhere into a function. In this example the macro could exit the function if some condition is met. But in C#, you can place the verification into a function, the function might be inlined, but you always have to write something in the form of

if (Verify(x)) return;

. So the question is, is there any way besides exceptions, to manipulate the control flow of a calling function be a called function?

klashar
  • 2,519
  • 2
  • 28
  • 38
be_mi
  • 529
  • 6
  • 21
  • 5
    I severely dislike seeing control flow statements in macros or otherwise hidden. Don't do it. Exceptions are the way to go. – Jonathon Reinhart Feb 18 '17 at 15:45
  • But exception have to much overhead. – be_mi Feb 18 '17 at 15:47
  • 2
    What do you mean overhead? Exceptions are not used to control flow, but to indicate an error condition beyond which there is no way to execute a function. You don't throw exceptions just to move execution someplace else. – Zoran Horvat Feb 18 '17 at 15:52
  • The idea is to test some condition at the beginning of a function and skip the rest of the function, if the conditions are not met. And I was wondering, if there is a shorter form than using `if() return;` construction. – be_mi Feb 18 '17 at 15:55
  • I mean you can always use a delegate to a to get this behavior right? – Aaron Feb 18 '17 at 15:59
  • I'll give you references in answer below. – Zoran Horvat Feb 18 '17 at 16:05
  • @Aaron, calling return from a delegate will exit the delegate but not the outer method, it won't do what the OP is asking. – Tim Feb 18 '17 at 16:07
  • Avoid using macros in C++. And if you have to use macros, then at least put parenthesis to avoid problem with complex expression. And also, it is always a good idea to write macro in a way that you are required to write a `;` at then end and that the macro works correctly inside a conditional statement. Assuming that you really want a macro, then it should be: `#define Verify(x) if((x) != nullptr) { } else ((void)0)` or something similar. If I were you, I would not write macros if you don't know why I put extra `()`, add an `else` and a final expression to force the use of a final `;` – Phil1970 Feb 18 '17 at 16:07
  • 3
    Just use the c# way and return. This is how it's done in c#. And do NOT use exceptions for control flow. – CodingYoshi Feb 18 '17 at 16:10
  • 1
    Certainly no "safe" way to do it, hehe. – IS4 Feb 18 '17 at 16:16
  • It is really a bad idea to hide return statement inside a macro. Expert C++ developer avoid using macros as much as possible and also avoid using `goto` and avoid names that start with an underscore. It would be a good idea for you to read **Meyers, Sutter and Dewhurst** books before you write you next line of code as you seems to write poor code. – Phil1970 Feb 18 '17 at 16:18

1 Answers1

3

Usual way to protect from invalid conditions is to install a guard clause at the beginning of the method body. You can find full explanation about why do we need guard clauses and how to implement them in this article: Why do We Need Guard Clauses?

I have also held a webinar on defensive coding and you can find recorded session here: Advanced Defensive Coding Techniques

Zoran Horvat
  • 10,924
  • 3
  • 31
  • 43