Global Functions and How to Not Use Them

When I develop in PHP and code functions, I always either place them in a file or place them at the top of the script. The force of habit comes from C++, and its been only a year ago that I realized that you can call functions at the bottom of the page. So trained was I at placing functions before I have to call them that I didn’t realize you can place functions anywhere and still use them.

Well, this is more about C++ and how I totally freaked out my teacher, by breaking his paradigm of thought. You see, you can have a definition of a function at the top of the page and then code the body of the function later. It was this ‘magic’ that I used to call a function from after the function body, when it was actually defined already. Of course, this is basic C/C++ knowledge and everyone knows this.

The Point?

Well, the script that I mentioned earlier left me looking for the functions in multiple different places, until I finally found them at the bottom, where I never expected them to be. What jackass places a function after he needs to use it?

Even if you have a foot and a gun, why go through the motion of shooting yourself in the foot, when your head is closer and therefore easier to aim at.

If there is something at least somewhat common about popular scripts that people use and extend, is that the functions are placed in organized manner. Reforming otherwise is a bitch and I remember rewriting the entire script anyway. You can’t reform when you can’t reuse anything.

Possibly Related Posts:


7 Comments.

  1. There’s actually a logical reason to place functions at the bottom of a single file script: not having to go through this heart-known code anytime the man edits the file.

    (but yeah I know that it works only for the original author (subsequent maintainers do not necesseraly know the code), and in a maintainability point of view, it completely sux.)

  2. I do this. But, I only do it when I am writing a single file script like geoffrey mentions above. Its it best coding practice? Probably not, but it keeps the functions out of the way in a single file script.

  3. If there is something at least somewhat common about popular scripts that people use and extend, is that the functions are placed in organized manner.

    That’s where stuff like ctags or PHPXref can be really useful.

  4. placing functions at the bottom is good, because you have a chance to first look at the main workflow, before digging into details..

  5. Alexey says it best I believe, however, in which case you want to place the functions in a new file, which is far better.

    The power of C++ and in part PHP is that you can do the same thing, by having the Main at the top you can better see the flow.

    My point however is that it is somewhat novice approach. Once you learn how to do functions, you can start moving functions to files and including them, so that you can use the functions in any number of applications.

  6. Gotta agree that it’s useful and much more readable to start with the high level (main) and get into the details later (helper functions). I think this is another case of the author needing to “let go” of their C++ baggage. Just because the C++ compiler authors were too lazy to allow the function to be declared anywhere doesn’t mean it was the right thing to do. Header files and forward declares are for zealots only and serve no other purpose. They denormalize the very definition of a function and defeat the purpose of computers in general (how may times do I have to tell this compiler this functions signature?!?).

  7. C++ compiler authors “lazy”? Do you have any idea how difficult it is to create a C++ compiler? For people even remotely aware of compiler theory shakes in fear of the thought of creating their own C++ compiler.

    You only need to declare the function definition once in either C or C++.