Jump to content


Photo

[SOLVED] Which procedure/code is better ?


  • Please log in to reply
3 replies to this topic

#1 abdfahim

abdfahim
  • Members
  • PipPipPip
  • Abd Fahim
  • 535 posts

Posted 26 August 2006 - 01:49 PM

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

an alternative is

$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
}

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

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 26 August 2006 - 02:24 PM

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


#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 26 August 2006 - 02:29 PM

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
switch ($_GET['action']) {

    case 'New' :
            insert_new_record($params);
            break;
    case 'Edit':
            display_edit_form($params);
            break;
}

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 abdfahim

abdfahim
  • Members
  • PipPipPip
  • Abd Fahim
  • 535 posts

Posted 27 August 2006 - 04:20 AM

Thanx guys ... thanx v much




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users