Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Must-follow RULES.

Status
Not open for further replies.

Sleidia

Technical User
May 4, 2001
1,284
0
0
FR
Good Morning you all.

Each time I am writing some PHP code I can't help
hearing a little voice saying to me: "aren't you
sure there isn't a much more beautiful way to do this?".

That's why, by opening this thread, I hope good
programmers will participate and teach others
the GOLD RULES to make shorter and beautiful
code that runs faster without loads of variables
and if/else arguments.

If you know some successful rules to optimize your code
and if you can show some self-explanatory examples, please, feel free to participate.

Thank you for your understanding.
Have a good day!
 

Here is a way for avoiding else...if statements
in my code.

instead of:

if ($var == "TW") {
$var= "TWIN";
} else if ($var == "SI") {
$var = "SINGLE";
}

I use:

$var = str_replace("SI", "SINGLE", str_replace("TW", "TWIN", $var));
 
The most important rule for coding:

Choose a method and BE CONSISTENT, at least for each distinct project. Changing methodology halfway through is a recipe for insanity. If you find you have started a project wrong, and there is NO way to stick to your original method, then try Refactoring: and see
There are several good coding rules, but here are my two favorites:

PEAR standard -- Rewrite of the C++ standard for PHP --
This is only the beginning of setting up a system of rules for how you program. Next, you have to decide which overall development methodology you want to use. And this doesn't just mean whether you want to be Object Oriented or Procedural. It also means how you structure applications, and how you organize your whole development effort.

Some examples of accepted standards are
1. Model-View-Controller (such as includes/templates/main )
2. Fusebox -- 3. Cascade model --
There are several more important ones, but memory escapes me at the moment. Perhaps others can point out some of these. -------------------------------------------

"Calculus is just the meaningless manipulation of higher symbols"
                          -unknown F student
 
hello

i think naming convensions are as much important in your code as other things.
be descriptive in your variable names and stick to some specific rule. something like preceding your sql stament variable with sql (sqlftechaddress,sqlinsertsal etc)

spookie
 

Good Morning Rycamor and Spookie.

In fact, in order to avoid misunderstanding, I was
talking more about code optimization (size and speed)
than code readability.

Looking forward to hearing from you all soon.
 
The GOLDEN RULE about code optimization (size and speed) is:

Write the best structured, most object oriented, easily understood, clearest code...THEN optimize.

Why? Because 95% of the time is spent in 5% of the code. And it is a complete and total waste of time to optimise beforehand.

Of course the underlying algorithm (e.g. if writing a sort, whether to do an exchange sort, or a heap sort, or a B-tree sort etc.) is NOT part of optimization, but comes even before writing the code.
 
OK, JavaSprout again (now logged in)...

Stick a part of your code here, and we could comment on it.

Alternatively, send me a ZIP of your code, and I will let you know what I think (to javasprout at (@) priorwebsites.com).

 
Sleida,

Just a note. Doing

if ($var == "TW") {
$var= "TWIN";
} else if ($var == "SI") {
$var = "SINGLE";
}

if different from
$var = str_replace("SI", "SINGLE", str_replace("TW", "TWIN", $var));

And another thing, is more readable that you have the first way than the replace way.
The best way to do this was using a switch

switch($var){
'SI' : $var='Single';break;
'TW' : $var='TWIN';break;
}

My opinion about the subject is simple. Just do as you think it's right. If you find not easy readable, jut comment a few lines with text explaining what you are doing.



Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Well, it seems suddenly everyone thinks PHP should be faster ;-). IMHO, PHP's core functions are about as fast as you could ask for, so looking for ways to optimize based on whether you use print or echo, or case switching instead of if-else, is mostly a waste of time. If you want some more speed, look into the Zend optimizer, or even the full-blown Zend accelerator. It doesn't get much better than that.

The performance of your PHP application depends far more on overall architecture than on specific code tweaks, which is why I mention my piece above. If your code is not structured in some logical, modular way, you will never be able to identify your bottlenecks and replace them with better-performaing structures. That's what it's all about.

Overall, though, the things that affect your application's speed the most will be filesystem access, and database usage. By this I don't mean whether you use a database or not, but how you use it. I have seen people retrieve a whole recordset from a "SELECT *" just so they could sort through it in PHP to get a single row, or even a single value from a single row. This is an extreme example, but it highlights how your structure affects performance. If you have structured your application so as to do unnecessary processing at the wrong point, you have just lost the performance game. Using the database to it's full potential is one step, so learn database design, normalization, and SQL. Also, I have seen people do such things as load all include classes and libraries globally, for every file, whether they are needed or not. Better to think about structure a bit and set up a system that can react to the demand by only loading the libraries needed at any one situation.

Most importantly, though: build your application logically first, and then worry about optimization. To quote one of the great names in computing:

"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil."
- Donald Knuth -------------------------------------------

"Calculus is just the meaningless manipulation of higher symbols"
                          -unknown F student
 

Good Morning Rycamor!

I agree 100% with your very interesting post.

If we forget about the speed optimization part,
do you never try to find more elegant ways to do
things sometime? I mean, the less I write lines
of code, the more I feel "smart".
Even if it can make code a little bit more difficult
to read, it is more compact at least.
That's why I like to see small code that does big things.

Don't you feel that way some times?
I don't know why, but I always feel ignorant and ashamed when
I see too many if..else stuff and too many variables
inside my code.
Should I go out and see a doctor?

See you!
 
>>Should I go out and see a doctor?

Hehe, no... only if you want to be cured of that disease known as being a geek ;-). I know exactly what you mean.

We had a similar, but more detailed discussion on another forum:

and my answer at -------------------------------------------

"Calculus is just the meaningless manipulation of higher symbols"
                          -unknown F student
 
well
rycamor and sledia

the more u optimise ur code the more it will be unreadable
and difficult to understand by others.
so i think there shd be a balance in code optimisation and readability.
i mean when a perl code written by some geek
is very difficult to understand but when the same code is written without optimisation its understandable.

wht do u think ?

spookie
 
I think that's exactly what I was saying, if you read my comments above. Optimizing is the last thing you do, and you only do it when necessary, and apply it modularly, so that any piece of the code that has a bottleneck can be replaced without affecting the rest.

"Speed of coding matters more than speed of code" ;-). -------------------------------------------

"Calculus is just the meaningless manipulation of higher symbols"
                          -unknown F student
 
Rule #1:
Keep It Simple Stupid (K.I.S.S.)

Rule #2:
See Rule #1
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top