Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seriously, should I write bad PHP code? [closed]

I'm doing some PHP work recently, and in all the code I've seen, people tend to use few methods. (They also tend to use few variables, but that's another issue.) I was wondering why this is, and I found this note "A function call with one parameter and an empty function body takes about the same time as doing 7-8 $localvar++ operations. A similar method call is of course about 15 $localvar++ operations" here.

Is this true, even when the PHP page has been compiled and cached? Should I avoid using methods as much as possible for efficiency? I like to write well-organized, human-readable code with methods wherever a code block would be repeated. If it is necessary to write flat code without methods, are there any programs that will "inline" method bodies? That way I could write nice code and then ugly it up before deployment.

By the way, the code I've been looking at is from the Joomla 1.5 core and several WordPress plugins, so I assume they are people who know what they're doing.

Note: I'm pleased that everyone has jumped on this question to talk about optimization in general, but in fact we're talking about optimization in interpreted languages. At least some hint of the fact that we're talking about PHP would be nice.

like image 576
Dan Rosenstark Avatar asked Oct 09 '08 14:10

Dan Rosenstark


People also ask

What should be the correct syntax to write a PHP code?

The correct option is (d) <? ?> The explanation is: Every section of PHP code starts and ends by turning on and off PHP tags to let the server know that it needs to execute the PHP in between them.

Why is PHP so fast?

PHP codes runs much faster than ASP because it runs in its own memory space while ASP uses an overhead server and a COM based architecture. Less Expensive Software – In working with PHP, most tools associated with the program are open source software, such as WordPress, so you need not pay for them.

What happens when the PHP parser encounters whitespace or blank lines in a PHP script?

Comments and white space are completely ignored when the code is run. You can think of all that extra stuff as being completely wiped away once your done and the code is doing its thing. Extra white space and comments are solely there for you and fellow coders to be better able to read and understand your code.


2 Answers

How much "efficiency" do you need? Have you even measured? Premature optimization is the root of all evil, and optimization without measurement is ALWAYS premature.

Remember also the rules of Optimization Club.

  1. The first rule of Optimization Club is, you do not Optimize.
  2. The second rule of Optimization Club is, you do not Optimize without measuring.
  3. If your app is running faster than the underlying transport protocol, the optimization is over.
  4. One factor at a time.
  5. No marketroids, no marketroid schedules.
  6. Testing will go on as long as it has to.
  7. If this is your first night at Optimization Club, you have to write a test case.
like image 114
Andy Lester Avatar answered Oct 15 '22 13:10

Andy Lester


I think Joomla and Wordpress are not the greatest examples of good PHP code, with no offense. I have nothing personal against the people working on it and it's great how they enable people to have a website/blog and I know that a lot of people spend all their free time on either of those projects but the code quality is rather poor (with no offense).

Review security announcements over the past year if you don't believe me; also assuming you are looking for performance from either of the two, their code does not excel there either. So it's by no means good code, but Wordpress and Joomla both excel on the frontend - pretty easy to use, people get a website and can do stuff.

And that's why they are so successful, people don't select them based on code quality but on what they enabled them to do.

To answer your performance question, yes, it's true that all the good stuff (functions, classes, etc.) slow your application down. So I guess if your application/script is all in one file, so be it. Feel free to write bad PHP code then.

As soon as you expand and start to duplicate code, you should consider the trade off (in speed) which writing maintainable code brings along. :-)

IMHO this trade off is rather small because of two things:

  1. CPU is cheap.
  2. Developers are not cheap.

When you need to go back into your code in six months from now, think if those nano seconds saved running it, still add up when you need to fix a nasty bug (three or four times, because of duplicated code).

You can do all sorts of things to make PHP run faster. Generally people recommend a cache, such as APC. APC is really awesome. It runs all sorts of optimizations in the background for you, e.g. caching the bytecode of a PHP file and also provides you with functions in userland to save data.

So for example if you parse a configuration file each time you run that script disk i/o is really critical. With a simple apc_store() and apc_fetch() you can store the parsed configuration file either in a file-based or a memory-based (RAM) cache and retrieve it from there until the cache expired or is deleted.

APC is not the only cache, of course.

like image 34
Till Avatar answered Oct 15 '22 14:10

Till