In more projects than I care to think about, I’ve seen a pattern that I dislike the more I see it. It appears quite innocent, but it brings about cost with no discernible benefit.
Including all of the application’s source code on each and every call being made to a page. It may be that one file is included that lists all files the app brings along; it may be a list of require_once statements in every entry page it has. But what is the benefit? Even if the page loads only those files that it might eventually need, it probably includes more than is warranted given the task it is currently given.
One solution is to get into using an autoloader; let the interpreter figure out by itself whether it has already seen all that it needs to execute a given piece of code. The other option is to require external files only at the point that you’re certain that you need them. Does your code do input sanitizing and validation before it loads the classes that then work with the sanitized values? Probably not â€“ it’s much more common to first load all the code, and only then start to work with what you are given.
Lazy loading means that the interpreter only runs on those code parts it needs, helping to contribute to better application performance (because we’re not spending time on code we don’t really need, anyway). This also means using less resources on the server, which means better use of resources â€“ ultimately, having your code use less electric power. But there is even more benefits: code that has not been loaded cannot be causing any kind of interference; you’re certain that you don’t have to look into those files when you’re debugging. And then, code that has not been loaded also cannot be used for security exploitsâ€“so you have less side effects there as well.
It’s not that this is a particularly complex intellectual challenge, it’s more a matter of perspective and maybe writing infrastructure code for your app. But to me, the benefits are worth the few more minutes spent whilst thinking about your code.
Schreibe einen Kommentar