3

I have a software application. Currently at the start of each Controller it loads in only the Models it needs for that Controller. I recently tested and found that including all the models appears to takes around a microsecond. If I always loaded all Models, it would simplify the application and make things a bit more DRY. However, I see many large frameworks do not auto-load non essential parts of it's code.

Are there reasons other than performance to avoid auto-loading non essential code?

Here is my basic performance test below. (Update: This performance test is flawed, see my comment below)

    echo round(microtime(true) * 1000); // 1474295691851
    echo '<hr>';

    require_once 'DB_Model/Translate_Model.php';
    $GLOBALS['translate_model'] = new translate_model;

    require_once 'DB_Model/Import_Model.php';
    $GLOBALS['import_model'] = new import_model;

    require_once 'DB_Model/Sku_Entry_Model.php';
    $GLOBALS['sku_entry_model'] = new sku_entry_model;

    require_once 'DB_Model/Group_Entry_Model.php';
    $GLOBALS['group_entry_model'] = new group_entry_model;

    require_once 'DB_Model/Event_History_Model.php';
    $GLOBALS['event_history_model'] = new event_history_model;

    echo round(microtime(true) * 1000); // 1474295691852

Side note: Please keep answers and comments on topic to my question. I am aware of other design problems, such as using $GLOBALS. I am aware and working on this.

7
  • 2
    Actually, I think loading only the essential modules is DRY. Sep 19, 2016 at 15:52
  • @RobertHarvey Good point. I meant that in physical lines visible to developer, it is code repeated in each Controller as well as more to manage and think about.
    – Goose
    Sep 19, 2016 at 16:31
  • 1
    Consider looking into the repeated code in your controllers and see how much of that can be moved out to get rid of the repetition.
    – JeffO
    Sep 19, 2016 at 17:01
  • @JeffO, if I am loading only the code I need, then I can't really leave out any of these requires, but if I load it all in, I can just require them all all the time, and not need to spend several lines of code in each Controller, depending on which Models I need.
    – Goose
    Sep 19, 2016 at 17:46
  • Where lazy-loading makes sense is where it takes many seconds to load "everything", or if all the things you're not using hog the RAM. Sep 20, 2016 at 12:12

1 Answer 1

4

Performance is something best worried about once it's a problem. Lazy loading techniques shouldn't be resorted to without good reason.

However, you also shouldn't be preloading up everything humanly possible just because you can.

Between these extremes is a sensible balance. If you load up some more than is always needed it's ok.

In fact performance would be improved after the initial load. No waiting the first time something is needed. This is why splash screens became a thing.

It sounds like you've already tried it and haven't yet seen a reason not to do it this way. Until you see one, do it this way.

Far more important is making the code readable. Readable code is easy to make performant. Performant code is rarely easy to make readable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.