Jump to content

Leading Community for Usability, Search Engine Marketing,
Social Networking, Site Planning & Web Site Development, Since 1998


Photo

Using Functions


  • Please log in to reply
3 replies to this topic

#1 fisicx

fisicx

    Sonic Boom Member

  • 1000 Post Club
  • 1821 posts

Posted 10 January 2013 - 03:18 AM

Back again after a loooong absence. I've missed you all.

Anyway here's the question. I've spent the last year learning and working with php, mainly to write wordpress plugins.

I undestand that if I have repeated actions I can put this action in a seprate function and call it when needed.

Is there a lower limit that says it's quicker to leave in the parent than create a seperate function. For example if i want to do something 10 times I use seperate function but if I only want to do it twice I leave it in the parent function.

I like to make my code as efficient as possible but it seems very clunky to always create seperate function, call it and return the result when you can do the same thing with less code insitu

Is there a rule of thumb I can aply?

#2 bobbb

bobbb

    Time Traveler Member

  • 1000 Post Club
  • 1430 posts

Posted 10 January 2013 - 12:32 PM

I would think more of separate functions if it is called from many places.
Sometimes if there are a lot of if-then-else statements and your tabs expand really far I may make some functions to sort of clean up to make it more readable or less complex to debug.
function tester{
this
and
that
}
if (condition){
{
do something
do something more
do this and that - I don t think a function call to tester is any better here
even if this is the main routine and it loops a gazillion times processing data.
}
}

Edited by bobbb, 10 January 2013 - 12:38 PM.


#3 joedolson

joedolson

    Eyes Like Hawk Moderator

  • Technical Administrators
  • 2869 posts
  • Twitter:http://twitter.com/joedolson
  • Facebook:http://facebook.com/joedolson

Posted 10 January 2013 - 01:44 PM

The best rule of thumb is your own management gut feeling: e.g. -- will it be easier for you to maintain the code by shifting this into a function? Then do it! If not, don't.

It doesn't really relate directly to how many times you're going to use it -- if it's a complicated batch of code, and you're going to use it twice, then it's absolutely worthwhile to make it a function. If it's really simple, and you'll use it 5 or 6 times -- probably doesn't help you at all. But it's really a balancing act about what is actually going to save you time in the long run.

And yes, you'll make mistakes: you'll turn some things into functions and later say to yourself that it was a waste of effort, and you'll desperately wish you'd made other things functions. The difference between the two, however, is if you regret turning something into a function, it's because it cost you a little extra time at the beginning, and then never helped you. If you regret *not* using a function, it's because it's costing you time every single time you need to use that method.

On the whole, using functions is better than not. But you need to use your own judgement on the specific cases.

#4 fisicx

fisicx

    Sonic Boom Member

  • 1000 Post Club
  • 1821 posts

Posted 18 January 2013 - 04:58 AM

So I took the plunge and functionised all sorts of things. Took a bit of planning as I'm not the brightest programmer in the box but it's made the code a lot more tidy. If got rid of loads of 'if else' things and made the plugin much simpler to manage.

Now had over a 1000 downloads and getting about 10% adoption so i'm quite chuffed.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users