17

I'm working with/on an extremely antiquated site that I wouldn't put my name to. 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?

3
  • 3
    Yes it is. Maybe he was testing something. Commented Jun 25, 2011 at 13:49
  • 2
    I appreciate the need to find out WHY the previous developer did something baffling and idiotic. Too often we find ourselves simply shrugging and moving on... until it bites us weeks later, once we've forgotten what we did. Commented Sep 16, 2012 at 1:36
  • 1
    Cheers - I know right? The amount of times I had facepalmed during that project was crazy Commented Sep 16, 2012 at 6:10

3 Answers 3

19

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@chris: affordable private service? use a dvcs (e.g. git or mercurial), both are open source software. they also allow you to have version history locally without the need for a server
@chris: no you don't. those systems are distributed and work on a single pc as will (even without internet) for sharing you need to setup your own server though (or use any of the services found on the net)
Just to mention. Try-catch-finally and code after return could be executed (PHP 5.5+)
4

The code beneath the return statment is useless, it will not be executed.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.