Jump to content

Recommended Posts

I work with another guy who is relatively new to PHP programming as I am. Neither of us have any education in programming. We've both developer different styles and I'm wondering if I should adopt his way.

 

I only create functions and objects for redundant tasks.

 

He creates extremely specific functions for organization, which I can see a benefit in. However it creates a more complex flow because it jumps from function to function more often. So his code would look something like...

<?php

if(add_widget()) {
send_widget_confirmation_email();
} else {
print_widget_form();
}

function add_widget() {
  check_for_duplicate_widget();
  // insertion sql
}

function check_for_duplicate_widget() {
  // sql for checking if it exists already
}

function send_widget_confirmation_email() {
  // mail code
}

function print_widget_form() {
  echo '<a bunch of html>';
}

?>

 

If I were doing this I would get rid of the functions and move the code from inside the functions to the if/else statement since these functions wouldn't get used in any other file. Also that way the program reads more top to bottom. Though maybe it isn't as obvious to see what each portion does.

Link to comment
https://forums.phpfreaks.com/topic/195824-style-question/
Share on other sites

I don't think that there's a clear winner in either case.  His main code is more readable, but requires you to dig through his function definitions to get the full gist of what everything does.  Yours takes away the bloat of the function definitions at the expense of overall readability.

 

The two of you should come to a consensus about style, though, if you're going to be working on code together.  It'll save yourselves future headaches.

Link to comment
https://forums.phpfreaks.com/topic/195824-style-question/#findComment-1028694
Share on other sites

I thought that might be the answer. Was hoping one method was more dominant. The example I give is actually a tad nicer than his real code. He likes to take his functions into separate files. So it's more annoying form me to follow and debug. In this example there would be a file add_widget.php which includes a file add_widget_functions.php.

 

I've been looking at the code from popular open source projects to see how they do it. I see both ways and to varying extents. Like PHPBB seems more like my style. WordPress seems to be more like his.

 

Is there a technical name for either style? I'd like to read more about the more function heavy way.

Link to comment
https://forums.phpfreaks.com/topic/195824-style-question/#findComment-1028721
Share on other sites

I thought that might be the answer. Was hoping one method was more dominant. The example I give is actually a tad nicer than his real code. He likes to take his functions into separate files. So it's more annoying form me to follow and debug. In this example there would be a file add_widget.php which includes a file add_widget_functions.php.

 

I've been looking at the code from popular open source projects to see how they do it. I see both ways and to varying extents. Like PHPBB seems more like my style. WordPress seems to be more like his.

 

Is there a technical name for either style? I'd like to read more about the more function heavy way.

 

AFAIK, there's no technical name for either style.

 

Keep in mind - what your partner does would actually make a lot of sense if you were writing OOP*.  PHP has a mechanism (well, a couple of them, actually) that allows for dynamic class loading - __autoload.  That way you don't have to manually keep track of your includes.

 

If you're just writing procedural code, the most widely used style would be to simply stuff reused functions in a library file (or several, based on theme) and include them when necessary.  Functions needed for a particular page would be written in that page's code.

 

*Stuffing a bunch of related-by-theme functions into a class isn't truly OOP.  That, however, is a discussion outside of the scope of this topic.

Link to comment
https://forums.phpfreaks.com/topic/195824-style-question/#findComment-1028789
Share on other sites

It doesn't really matter.  Whichever of the two you choose you won't be able to remember what you did 6 months from now when you need to maintain it and as you customize and add they'll both become just as unreadable.  :)

 

If you're using a debugger it shouldn't matter where the functions are located when you want to step through the program logic anyways.

Link to comment
https://forums.phpfreaks.com/topic/195824-style-question/#findComment-1028813
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.