Well as a programmer all these aspects I'm going to be talking about are common sense to me but it has been brought to my attention that the aspects I'm going to cover here are not all that well known among less advanced programmers.
That mixed with no sticky thread in this section and no real talk about this important aspect on WickedFire is why this thread is going to be created, by the end of this thread you'll know how to better optimize your code and to avoid bad coding practices. Also if you have any coding questions after words please let me know and I'll do my best to answer them and if you want to contribute some more then please do so.
I'll simply start off with the basics of any programming language, it doesn't matter what type of language you are using these are common among all languages and those are common coding practices. Just like in the medical field where doctors need a standard form of communication and diagnosis the programming world is much the same.
The first common practice are comments in your code, lots and lots of meaningful comments are helpful for both you and others reading your code, the comment structure varies depending on which language you are using but for PHP see the code below for an example of proper comment practice.
Looks easy right well actually Example A isn't required and as you get better in coding you'll realize why, first off Example A has a line comment (because of the //) but the comment isn't required because its a very simple line that almost every PHP programmer will know what it does when they look at it, so no need to waste your writing time with commenting everything.
Now Example B on the other hand isn't so obvious and requires a comment because not everyone looking at it is going to understand why your doing the expression. Also note that I used a block comment in this one which is depicted by the opening /* and closing */.
Easy and boring stuff right? Well it can save you hours on a big project so over commenting isn't required but complicated blocks of code or complicated lines of code need to be commented for both you and someone else looking at it, if you don't comment your code I can guarantee you that going back to it a month later you won't know why the hell you did what you did.
So thats beyond basics just so everyone knows but lets move onto some better stuff like block format and organization then I'll move onto code optimization.
First off in PHP and many other languages whitespace doesn't mean anything and should be used correctly, for instance which block of code can you read better?
In the computers eyes all these code blocks will execute at the same amount of time and it doesn't matter but to human eyes Example B is just stupidly hard to read and Example C is also way too spaced out to read correctly but Example A is just right, so again use whitespacing in moderations and don't get carried away, again very simple stuff.
Ok enough simplicity if you need simple answers then ask later on and I'll respond but what I want to get into right now is PHP code optimization which a lot of noobs seem to fail so badly and the worst part of it is that they won't ever realize it until they begin to write larger programs because lets face it you can code a small program as shitty as you want and it will still run pretty damn fast but continue to build on that shitty code and you'll spend months going back over it trying to speed it up.
The first example is when using Functions in PHP now functions dramatically decrease the speed in which your scripts will run, however functions are a powerful and are still needed, if I remember correctly functions will actually slow down your code by 33% overall. So for each function you clean up you'll speed up the execution time of your code by 33% that is huge.
One common mistake I see people making is using functions too much look at both these below examples.
That seems clean, organized and nice right? Well no its not if your using these functions over 100 or so times in your code then yes by all means do this but even still I recommended you don't. The reason for this is simple, it only takes you at most 10 seconds longer to write out these few lines of code when needed in your application then it is to just call the function.
But by taking an extra 10 seconds to write out a few lines when needed (Even though it may seem repetitive), it will speed up your code by 33% each time you don't use the function. Also remember that in PHP things like the example below are also functions, they are just pre-build by PHP and you never see the full function code for them.
So when do you use functions? Well this changes with personal preference but I like to only use additional functions when there are more then 20 or so lines of code which need to be called several times over in an application, this speeds up the time I need to write out the code, it cleans up my code, and even though I still sacrifice a bit of speed its not enough to justify not using the function. So with practice and experience you'll begin to find a balance between execution speed and the time it takes to write the code.
The next optimization tip I want to talk about is incrementing items in your code, this is done for so many reasons in almost every application a person could write, there are basically two ways to increment, one is known as pre-increment and post increment, look at the example below.
Now this is something PHP specific so don't try to use this on other languages but the pre-increment is a tad bit faster then the post increment and the reason for this is because that post increment actually create a phantom variable which stores the new value but pre-increment does not and it changes to value of the variable directly. You should also be careful when using the pre-increment in loops because incrementing something too early will cause the formula to be off by 1 as where post increment will be exact, so this is just something to keep in mind.
Here is yet another thing that becomes complicated for noobs and that is using print(); over echo(); well guess what they both do the exact same things right? They print out code to the browser or screen.
The answer is yes and no but for optimization sake echo(); is faster then print(); because the print function can return to you a status of success or not while the echo function will simply display the text and nothing more.
All this is something that comes with practice and there are a ton of more stuff that I'll have to add to this at a later date but by keeping some of these tricks in mind you'll be able to build much bigger and much faster functions.
P.S. Regular Expressions (REGEX) are extremely slow and are not recommended if a string alternative is available but I'll talk about that later on.
That mixed with no sticky thread in this section and no real talk about this important aspect on WickedFire is why this thread is going to be created, by the end of this thread you'll know how to better optimize your code and to avoid bad coding practices. Also if you have any coding questions after words please let me know and I'll do my best to answer them and if you want to contribute some more then please do so.
I'll simply start off with the basics of any programming language, it doesn't matter what type of language you are using these are common among all languages and those are common coding practices. Just like in the medical field where doctors need a standard form of communication and diagnosis the programming world is much the same.
The first common practice are comments in your code, lots and lots of meaningful comments are helpful for both you and others reading your code, the comment structure varies depending on which language you are using but for PHP see the code below for an example of proper comment practice.
Code:
[B]Example A:[/B]
// Creates cURL bot
$bot = new bot();
[B]Example B:[/B]
/* Stores number of pages in $pages */
$pagesExp = "(?<=Page [\d] of )[\d]*";
$pages = preg_match_all("#".$pagesExp."#",$source,$feeders);
Now Example B on the other hand isn't so obvious and requires a comment because not everyone looking at it is going to understand why your doing the expression. Also note that I used a block comment in this one which is depicted by the opening /* and closing */.
Easy and boring stuff right? Well it can save you hours on a big project so over commenting isn't required but complicated blocks of code or complicated lines of code need to be commented for both you and someone else looking at it, if you don't comment your code I can guarantee you that going back to it a month later you won't know why the hell you did what you did.
So thats beyond basics just so everyone knows but lets move onto some better stuff like block format and organization then I'll move onto code optimization.
First off in PHP and many other languages whitespace doesn't mean anything and should be used correctly, for instance which block of code can you read better?
Code:
[B]Example A:
class bot
{
function setup()
{
$cookieJar = 'cookies.txt';
curl_setopt($this->curl,CURLOPT_COOKIEJAR, $cookieJar);
curl_setopt($this->curl,CURLOPT_COOKIEFILE, $cookieJar);
curl_setopt($this->curl,CURLOPT_AUTOREFERER,true);
curl_setopt($this->curl,CURLOPT_FOLLOWLOCATION,false);
curl_setopt($this->curl,CURLOPT_RETURNTRANSFER, true);
}
function get($url)
{
$this->curl = curl_init($url);
$this->setup();
return $this->request();
}
function request()
{
return curl_exec($this->curl);
}
function kill()
{
curl_close($this->curl);
}
}
Example B:
class bot
{function setup(){
$cookieJar = 'cookies.txt';
curl_setopt($this->curl,CURLOPT_COOKIEJAR, $cookieJar);
curl_setopt($this->curl,CURLOPT_COOKIEFILE, $cookieJar);
curl_setopt($this->curl,CURLOPT_AUTOREFERER,true);
curl_setopt($this->curl,CURLOPT_FOLLOWLOCATION,false);
curl_setopt($this->curl,CURLOPT_RETURNTRANSFER, true);}function get($url)
{$this->curl = curl_init($url); $this->setup(); return $this->request();
}function request()
{return curl_exec($this->curl);
}function kill()
{ curl_close($this->curl);
}
}
Example C:
class bot
{
function setup()
{
$cookieJar = 'cookies.txt';
curl_setopt($this->curl,CURLOPT_COOKIEJAR, $cookieJar);
curl_setopt($this->curl,CURLOPT_COOKIEFILE, $cookieJar);
curl_setopt($this->curl,CURLOPT_AUTOREFERER,true);
curl_setopt($this->curl,CURLOPT_FOLLOWLOCATION,false);
curl_setopt($this->curl,CURLOPT_RETURNTRANSFER, true);
}
function get($url)
{
$this->curl = curl_init($url);
$this->setup();
return $this->request();
}
function request()
{
return curl_exec($this->curl);
}
function kill()
{
curl_close($this->curl);
}
}[/B]
Ok enough simplicity if you need simple answers then ask later on and I'll respond but what I want to get into right now is PHP code optimization which a lot of noobs seem to fail so badly and the worst part of it is that they won't ever realize it until they begin to write larger programs because lets face it you can code a small program as shitty as you want and it will still run pretty damn fast but continue to build on that shitty code and you'll spend months going back over it trying to speed it up.
The first example is when using Functions in PHP now functions dramatically decrease the speed in which your scripts will run, however functions are a powerful and are still needed, if I remember correctly functions will actually slow down your code by 33% overall. So for each function you clean up you'll speed up the execution time of your code by 33% that is huge.
One common mistake I see people making is using functions too much look at both these below examples.
Code:
[B]Example A:
function stripUrl(){
$urlExp = "";
preg_match_all("#". $urlExp ."#",$url,$strippedUrl);
return ($strippedUrl[0][0]);
}
function LinkingArrays(){
for($i=1; $i<($pages + 1); $i++){
$appendedUrl = $url."?p=".$i;
array_push($linkArray,$appendedUrl);
}
}[/B]
But by taking an extra 10 seconds to write out a few lines when needed (Even though it may seem repetitive), it will speed up your code by 33% each time you don't use the function. Also remember that in PHP things like the example below are also functions, they are just pre-build by PHP and you never see the full function code for them.
Code:
[B]count();
array();[/B]
The next optimization tip I want to talk about is incrementing items in your code, this is done for so many reasons in almost every application a person could write, there are basically two ways to increment, one is known as pre-increment and post increment, look at the example below.
Code:
[B]Pre-Increment:
++$i;
Post Increment:
$i++;[/B]
Here is yet another thing that becomes complicated for noobs and that is using print(); over echo(); well guess what they both do the exact same things right? They print out code to the browser or screen.
The answer is yes and no but for optimization sake echo(); is faster then print(); because the print function can return to you a status of success or not while the echo function will simply display the text and nothing more.
All this is something that comes with practice and there are a ton of more stuff that I'll have to add to this at a later date but by keeping some of these tricks in mind you'll be able to build much bigger and much faster functions.
P.S. Regular Expressions (REGEX) are extremely slow and are not recommended if a string alternative is available but I'll talk about that later on.