Page 1 of 1

Breaking up logic: "include" files or functions

Posted: Thu Jan 22, 2004 5:00 pm
by pickle
Hi all,

Just a question I want to pose to the community. When you are splitting up logic, say for example, an array transfer sub-routine, do you commonly put it in an include file and then include that file, or do you put the logic in a function then call the function?

What are the pro's and con's of doing it either way?

I've recently started doing a number of small webapps and I've experimented with both methods, but I can't seem to decide which is better.

Thanks everyone.

Posted: Thu Jan 22, 2004 5:05 pm
by qads
depends, i only use functions when i know i will be useing it in few pages, if it is something small, like a array check or something then i just put it in a file and include, overall, its up to you i guess.....kinda makes hard to choose i guess.

what i uselly do is to have a file called "functions.php" and i put all my site/app functions in there, so when i need it i can include it, now days i just make a class for the whole process, a image gallery for example :D .

having functions in one place means you dont need to look in all of your files to change something, so functions are my first choice.

Posted: Fri Jan 23, 2004 10:40 pm
by fractalvibes
If you are using that chunk of code in multiple places, then why not in a function that is in an include file? Could be an ordinary function or a method of an object that you include. Either way, if it need be changed, change in 1 file only...

fv

Posted: Sat Jan 24, 2004 12:47 am
by McGruff
Always put everything in functions (and then put all your functions in classes).

Each function should carry out just one task. Each class should similarly do one "thing".

There really shouldn't be any "bare code" anywhere (as always there are exceptions to the rule but precious few). Include a file with function defs instead.

Functions aren't created only when they can be re-used. Think of them more as ways to organise your code with chapters & titles.

Posted: Sun Jan 25, 2004 1:51 am
by JPlush76
mcgruff, you crazy java thinker guy ;)

have you lost your procedural roots? lol

Posted: Sun Jan 25, 2004 7:02 am
by McGruff
I'm on a mission these days. Can't cope with debugging spaghetti code which, with a bit of organisation, probably wouldn't have half as many bugs.

We badly need some refactoring tutorials. I might even get around to writing something. Sigh. If only I had just one spare afternoon..

Posted: Sun Jan 25, 2004 1:21 pm
by lazy_yogi
JPlush76 wrote:have you lost your procedural roots? lol
We can only hope so

What fun programming would be if everyone lost their procedural roots

Posted: Sun Jan 25, 2004 1:30 pm
by Straterra
A function...for everything? I think that would become even more combersome than spaghetti code. Just think..all 9000 users change all of their code into functions and classes...Then, we try to help them..All they show us is the line calling the function...Then, we ask for the function, then that function calls some variable from inside the class...Then, we use 10 posts just for someone to post their entire class, to just find out that they misspelled mysql....

Posted: Sun Jan 25, 2004 1:52 pm
by Gen-ik
The McGruff method is something I use all of the time now, even with small PHP based sites which don't do much. People will only grasp why using classes() and functions() is a good thing to do once they have tried it... and understand it.

The only time I use include files within functions is when I need to load a 'settings.php' page or something which contains a load of variables and/or arrays etc, or when I need to pull a chunk of HTML code into a function for some reason.

Posted: Sun Jan 25, 2004 3:57 pm
by McGruff
Straterra wrote:A function...for everything? I think that would become even more combersome than spaghetti code. Just think..all 9000 users change all of their code into functions and classes...Then, we try to help them..All they show us is the line calling the function...Then, we ask for the function, then that function calls some variable from inside the class...Then, we use 10 posts just for someone to post their entire class, to just find out that they misspelled mysql....
Not at all. Suppose they did give us the wrong function: it doesn't take a second to pass some test parameters to verify it's OK then move on to the next possible source of the problem. Single-shot functions, half a dozen lines or so, are easy to test - often there wouldn't be a question in the first place.

Passing parameters around to functions can start to get awkward though - that's when it's time to look at classes.

Posted: Sun Jan 25, 2004 5:11 pm
by Straterra
My point is..its retarted to have functions for functions that are already in PHP that only require 1 argument..Like echo, or crap like that. Don't you agree?

Posted: Sun Jan 25, 2004 5:21 pm
by ilovetoast
Echo needs a class.

sugar on toast

Posted: Sun Jan 25, 2004 5:38 pm
by McGruff
Straterra wrote:My point is..its retarted to have functions for functions that are already in PHP that only require 1 argument..Like echo, or crap like that. Don't you agree?
Re-writing native fns can be useful. I often use this to log queries in a $monitor object. __FILE__ and __LINE__ are passed in via $location.

Code: Select all

<?php

function sqlQuery($sql, &$monitor, $location) 
{
    $monitor->setArray('sql', $location . ' <br /> ' . $sql);
    
    $query = mysql_query($sql) or die('<b>sql error:</b> ' . mysql_errno() . ' - '
                                        . mysql_error() . '<br/>' . $location . 
                                        '<br />'
                                        );
    return $query;
}

?>
One doesn't just write code that a computer can understand: you're also writing code that (hopefully) people can understand. Functions provide "chapters and titles" which make a script easier to read at a glance. With chunks of code encapsulated in well-named functions, code can almost read like pure english - and well-factored code is easier to debug & maintain.

Deciding what fns to build gets you started thinking in terms of responsibilities. That's a vital skill for creating good program designs.

Posted: Sun Jan 25, 2004 5:49 pm
by McGruff
ilovetoast wrote:Echo needs a class.

sugar on toast
Lol.

What I can't figure out is if sugar is a child of toast or if toast should extend a sugar class. And how to avoid beverage-dependency in order to support multiple presentation formats: coffee, tea, hot milk...?

Posted: Sun Jan 25, 2004 6:33 pm
by ilovetoast
LMAO.

But seriously, sugar is a interface. Beverage issue remains under construction.

As to the more pertinant, albeit lesser, question of include files, etc., I strongly agree with McGruff's well explained points. Anything more from me would be repetitive.

butter the toast