Welcome Guest, Not a member yet? Register   Sign In
Script continues to run after loading a view.
#1

[eluser]laytone[/eluser]
Quote:I have a simple if statement and when it evaluates to true it loads an error page.

If I remove all the code in my function after the if statement, The error page loads correctly. But when I leave the rest of the code (the stuff that runs if there isn't an error, my script continues to run.

Will loading a view not stop the script execution?

Thanks in advance.

I figure out that I can use return to exit my function early. like this

Code:
if (!$person_exists_in_db) {
$this->mymodel->add_user($userdetails);
} else {
$this->load->view('error', $error_data);
return;
}
//Continue with billing code..
#2

[eluser]Dyllon[/eluser]
No, loading a view does not halt the script which allows you to load multiple views and take advantage of hooks and what not.
#3

[eluser]Colin Williams[/eluser]
You might consider moving all your code to be in the conditional block. It's better for functions to have only one exit point. Here, you're bailing in the middle of the function. It might be confusing later on. Just something to consider.
#4

[eluser]danmontgomery[/eluser]
[quote author="Colin Williams" date="1263369751"]It's better for functions to have only one exit point.[/quote]

That's a fairly subjective statement...
#5

[eluser]John_Betong[/eluser]
[quote author="noctrum" date="1263414174"][quote author="Colin Williams" date="1263369751"]It's better for functions to have only one exit point.[/quote]

That's a fairly subjective statement...[/quote]

That is what I learnt and I really think that it makes programming that much simpler.

Quote:Manage the Flow of Your Code

Use only one return statement in any function. Limit your use of break statements to the inside of switch statements. In C, do not use continue statements and limit your use of goto statements to exit conditions that branch to the end of a function. Handle error conditions in C++ with a try/catch block and in C with a goto statement that transfers control to the end of the function so that your functions have only one exit point.

In other words, control the flow of your functions so that each block has one entry point and one exit point. This "one way in, one way out" rule makes code easier to read and debug.




.
#6

[eluser]Colin Williams[/eluser]
Subjective good advice is still good advice, noctrum. And thanks for the supporting arguments you quoted there, John.

There's some quote out there that any monkey can write code a machine can understand, good programmers write code that humans can understand. Having a one exit point is just one of many good ways to achieve that




Theme © iAndrew 2016 - Forum software by © MyBB