Exiting from a Method by calling another Method

RememberOfLife

So I want to exit the execution of a method or function by calling for another method. I have so far only found problems with else and nothing like I need.

Example as follows..

public static void SomeMethod() {
    // some code
    ExitMethod();
    // the next line of code will never be executed
    Console.WriteLine("Test");
}

private static void ExitMethod() {
    // if possible the execution of the Method above will be stopped here
}

The ExitMethod would work like a return statement, only that because it is a method I can more easily add an if or other conditions to it. If I were to use the ExitMethod often in my assembly I could easily refactor the conditions under which execution of the calling Method would be stopped.


This can for example be used in an obviously insecure attempt of securing a dll, so that it requires a serial key and only if the correct one is given it will enable some static bool that then is checked everytime a function is called from the dll.

Thanks in advance :)

EDIT: By using another method that can be called for the cancellation task I want to avoid something like:

public static void SomeMethod() {
    if (ExitMethod) return;
}

The goal is to only be required to call the ExitMethod Method which takes care of things.

Jonathan Dickinson

From the comments in the question:

why isn't there a better solution [...]?

Implicit flow-control is generally regarded as an anti-pattern - there are even arguments against exceptions existing at all. Languages that purposefully don't include any form of implicit flow-control exist (e.g. Go).

Here are a few approaches that you could use.

Explicit Flow Control

public bool ExitMethod() => // ...

public void SomeMethod()
{
  if (ExitMethod()) return;
  Console.WriteLine("Test");
}

Without DocComments the boolean return value might be confusing. An enum would result in self-documenting code:

public enum ExitParentStatus : byte
{
  Continue, Return
}

public ExitParentStatus ExitMethod() => // ...

public void SomeMethod()
{
  if (ExitMethod() == ExitParentStatus.Return) return;
  Console.WriteLine("Test");
}

Explicit Flow Control with State

public enum RequestStatus : byte
{
  Processing, Handled
}

public class Request
{
  public RequestStatus Status { get; set; }
}

public void ExitMethod(Request request) => // ...

public void SomeMethod(Request request)
{
  ExitMethod();
  if (request.Status == Handled) return;
  Console.WriteLine("Test");
}

Use Yield Return

This provides clues to other developers that they are approaching poorly designed code, slightly reducing the chances of a bug.

public void ExecuteConditionalCoroutine(IEnumerable<bool> coroutine)
{
  foreach (var result in coroutine)
  {
    if (result) return;
  }
}

public bool ExitMethod() => // ...

public IEnumerable<bool> SomeMethod()
{
  yield return ExitMethod();
  Console.WriteLine("Test");
}

ExecuteConditionalCoroutine(SomeMethod());

Exceptions

Use this if you want to make your code impossible to debug.

public bool ExitMethod() { throw new ExitParentMethodException(); }

public void SomeMethod()
{
  try
  {
    ExitMethod();
    Console.WriteLine("Test");
  }
  catch (ExitParentMethodException) { }
}

Post-Compilation

Use something like post-sharp to automatically inject the branch. This a great approach for completely unmaintainable code.

[ExitsParentMethod]
public bool ExitMethod() => // ...

public void SomeMethod()
{
  ExitMethod();
  Console.WriteLine("Test");
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Calling a method from another method

From Javascript

Calling a method from another method in the same class

From Java

Calling method of Another class from run() method

From Java

java calling a method from another class

From Java

calling another method from the main method in java

From Java

Calling service method from another service class

From Java

NullPointerException when calling method from another class

From Java

Calling a method in one fragment from another

From Dev

Calling a method through from another controller JavaFx

From Dev

calling render method from another class

From Dev

Calling prototype method from another object

From Dev

Calling a method in thread from another thread, python

From Dev

Calling a method from another jar file

From Dev

Calling a class from another class with main method

From Dev

Calling method from another class unsuccessful

From Dev

Calling an impl method from another impl method

From Dev

Calling a method from inside of another class

From Dev

Laravel: calling controller method from another location

From Dev

Calling a method of the MainActivity from another class?

From Dev

Calling a method from another controller in rails

From Dev

Issues with calling method from another form

From Dev

calling different Action Method from another Controller

From Dev

calling method in mainactivity from another class in android

From Dev

Calling one method from another method in Python

From Dev

JGroups RpcDispatcher calling method from another class

From Dev

Calling Test Method from another Test Method

From Dev

Sinon stub method calling from another file

From Dev

calling an object method from another method not working

From Dev

Calling Javascript prototype method from another method

Related Related

  1. 1

    Calling a method from another method

  2. 2

    Calling a method from another method in the same class

  3. 3

    Calling method of Another class from run() method

  4. 4

    java calling a method from another class

  5. 5

    calling another method from the main method in java

  6. 6

    Calling service method from another service class

  7. 7

    NullPointerException when calling method from another class

  8. 8

    Calling a method in one fragment from another

  9. 9

    Calling a method through from another controller JavaFx

  10. 10

    calling render method from another class

  11. 11

    Calling prototype method from another object

  12. 12

    Calling a method in thread from another thread, python

  13. 13

    Calling a method from another jar file

  14. 14

    Calling a class from another class with main method

  15. 15

    Calling method from another class unsuccessful

  16. 16

    Calling an impl method from another impl method

  17. 17

    Calling a method from inside of another class

  18. 18

    Laravel: calling controller method from another location

  19. 19

    Calling a method of the MainActivity from another class?

  20. 20

    Calling a method from another controller in rails

  21. 21

    Issues with calling method from another form

  22. 22

    calling different Action Method from another Controller

  23. 23

    calling method in mainactivity from another class in android

  24. 24

    Calling one method from another method in Python

  25. 25

    JGroups RpcDispatcher calling method from another class

  26. 26

    Calling Test Method from another Test Method

  27. 27

    Sinon stub method calling from another file

  28. 28

    calling an object method from another method not working

  29. 29

    Calling Javascript prototype method from another method

HotTag

Archive