I'm working with/on an extremely antiquated site that I wouldn't put my name to.开发者_Python百科 There seems to a re-occuring pattern in the existing code, along the lines of:
function foo() {
$a = 'a';
$b = 'b';
return;
$c = 'c';
$d = 'd';
}
I'm very relunctant to delete existing code from a function that I didn't write, and everything is working as is. But I would like to know why?
Everything I ever learnt (with the exception of a goto line call) tells me that the code following the return statement is useless. Is it? Why would the previous programmer do this?
That code after the return
statement will never be executed. most probably the original developer wanted to quickly test other values for the variables and left his old experiments in the function. Use version control and you can delete obsolete code and still recover it, in case you ever need it again.
The code beneath the return statment is useless, it will not be executed.
My first thought would be that the person who wrote the code originally added the return because the code after it no longer needed to run, but didn't want to delete the other code for some reason.
An example of where this might be the case is in a situation where the author rewrote the code to implement it in a different way. They put the new implementation above the return statement, but left the code below it in so that it was there as a "reference" for how it used to be done. That way, if they realize something isn't working in the new code, they have the old code to look at easily.
Admittedly, version control and unit tests make this a lot less useful, but it's not an uncommon thing to do anyways.
精彩评论