Jump to content

Archived

This topic is now archived and is closed to further replies.

abdfahim

[SOLVED] Which procedure/code is better ?

Recommended Posts

hi friends, i need a little information. Here I am writing two codes that can be used in same purpose.
[code]$new=$_GET['new'];
if ($new==0){
      //Do something
}else{
      //Do something
}[/code]

an alternative is

[code]$new=$_GET['new'];
//$params= something needed inside functions;
if ($new==0){
      func_1($params);
}else{
      func_2($params);
}
func_1($params){
      //Do something
}
func_2($params){
      //Do something
}[/code]

Now my question is which is better and most importantly faster and why ???

Share this post


Link to post
Share on other sites
First one. However you can use a function if you wish. But first one is fine. Although I'd do this:
[code]$new= isset($_GET['new']) ? $_GET['new'] : '';
if ($new == 0){
      //Do something
}else{
      //Do something
}[/code]

Share this post


Link to post
Share on other sites
The first is going to be faster as there is always an overhead with function calls as the params and the return address have to placed on the stack. However we're talking nonoseconds here and you are not going to notice in practice.

As far as I am concerned, then, time to execute is not an issue but rather code readabilty and maintenance (that's what costs the money). If "Do something" is just a few lines of simple code I'd use the first. If we are talking something more complex I'd go with second and use meaningful function names so it's obvious what is happening
[code]
switch ($_GET['action']) {

    case 'New' :
            insert_new_record($params);
            break;
    case 'Edit':
            display_edit_form($params);
            break;
}
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.