I mainly use C #, but I think this question might be suitable for other languages โโas well.
Also, there seems to be a lot of code here, but the question is very simple.
The attachment, as I understand it, is a compiler (in the case of C # - a virtual machine) that replaces a method call, inserting the method body in every place from which the method was called.
Let's say I have the following program:
static Main() { int number = 7; bool a; a = IsEven(number); Console.WriteLine(a); }
... body of the IsEven
method:
bool IsEven(int n) { if (n % 2 == 0) // Two conditional return paths return true; else return false; }
I understood how the code would look after embedding the method:
static Main() { int number = 7; bool a; if (number % 2 == 0) a = true; else a = false; Console.WriteLine(a); // Will print true if 'number' is even, otherwise false }
Obviously a simple and correct program.
But if I slightly correct the IsEven
body to include the absolute return path ...
bool IsEven(int n) { if (n % 2 == 0) return true; return false;
I personally like this style a little more in some situations. Some refraction tools may even suggest that I am modifying the first version to look like this, but when I tried to imagine what this method would look like when it was built in, I was at a dead end.
If we introduce the second version of the method:
static Main() { int number = 7; bool a; if (number % 2 == 0) a = true; a = false; Console.WriteLine(a);
Asked question:
How does the compiler / virtual machine deal with nesting a method that has an absolute return path?
It seems extremely unlikely that something like this will really prevent the use of the method, so I am wondering how this happens. Perhaps the attachment process is not as simple as this? Perhaps one version is likely to be built into the VM?
Edit: Profiling both methods (and manually inserting the first) showed no difference in performance, so I can only assume that both methods are embedded and work the same or similar (at least on my virtual machine).
In addition, these methods are extremely simple and seem almost interchangeable, but it can be much more difficult for complex methods with absolute return paths to change versions without absolute return paths.