Tag Archives: code

Who tested this sh***

One of my colleagues was testing something and found out that a cron that was consuming 100% of memory and never ended. He is not a PHP Developer so he called me to take a look.

After less than a minute looking on the code I found the issue but I was perplexed to see how a code like that went to production.

In short this was the main execute code of the cron:


while(!empty($all)) {
$row = array_shift($all);
}

As you can imagine, that array will never be empty, so the loop will never end.

Fixing it was very easy, a simple change from while to foreach took care of the issue, but a question remains for me: who tested this sh***

Obviously it wasn’t tested, otherwise this would have been caught way prior getting to production.

Sorry about my rant but I can’t avoid to get mad when I see a developer that did not even run the code prior commit it.

Advertisements

Object Calisthenics

A great slideshow to go through

btw: very hard to code in the proposed way


3 cool tools for Chrome

All of them a must have:


Developers

“Developers are tools that convert caffeine into code”

Author: Unknown


PHP Basics Series – Starting with PHP

Hi and welcome to the second article of the PHP Basics Series. The first article – PHP Basics series – coding – was about coding best practices, standards and methods. A small overview on what you need to know to be a good coder.

In this second article we are going to talk about the very beginning of PHP, how it works, how to start coding on it and a bit of history.

PHP stands for Hypertext Preprocessor and it was created based on the C, Java and Perl (used until today on extra modules) languages. With many authors, editors, contributors, etc, is hard to name one as it’s creators, therefore you can point Rasmus Ledorf (who wrote the original gateway binaries) and Andi Gutmans and Zeef Suraski (both responsible for witting the parser that formed PHP3) as the original creators of PHP3.

If we had to create a in-line structure to show how it works, it would be something like this:

The client (your browser) requests a page to the server. The server reads the page and see if there are any PHP script and it will parse that script generating the correct result that can be embed to the HTML / XML / Javascript (JS) / etc.

Off course there is much more when we are talking about the way that PHP works, and it’s history, but I will focus more on the coding techniques.

Before we create our first code and I promise that it will not be “Hello World“, you will need to have PHP installed on your development machine or in a server where you can store your PHP files to be executed.

If you are on a Windows machine you can either install PHP manually adding it to IIS server or you can install a PHP bundle that will install for you the Apache server, PHP engine and MySQL (if you desire to, otherwise you can use PHP native SQLite).

Truly I’m not the best indicated to show how to install PHP on a IIS server, but there is a very good step by step – Vista – on Bills IIS Blog: How to install PHP on IIS.

Installing the bundle, for a development machine, at this point, is the most recommended. There is a great bundle, AppServ, which is pretty easy to install and configure. It will come with all of the most usual libraries that you need and you can also choose to install some extra ones.

In other hand, if you are on a Linux machine (Ubuntu, Xubuntu – love it, Suse, etc) most likely you already have PHP, Apache and MySQL installed. Therefore you can check by using the Synaptic Package Installer, and if is not there, install it. Just make sure that you have root permissions.

Now that you have PHP installed, let’s create our first code.

Note: If you don’t have an development editor, you can use Eclipse PDT. It’s a great editor and it’s free. Very similar to the Zend Studio. If you have any other editor that you like, such as Dreamweaver, Notepad, Edit+, etc, you can use it as well.

The first thing you need to know is how make the server to understand that it will be dealing with a PHP code. For that you will be using the PHP open / close tags. There are a set of 4 open / close tags for PHP, but we will focus on only one.

PHP holds as it’s open / close tags the following:

  1. <?php ?> – Full tag
  2. <? ?> – Short tags. As one of it’s variations you will have <?= that is the same as <? echo
  3. <% – ASP tags. More as a history point, but really, not useful at all.
  4. <script language=”php”></script> – besides the first one, the only one that you can trust to work.

While coding we could be using all of the 4 sets of PHP’s open / close tags, but, for keeping a standard and full compatibility on any PHP installation, we will always use the first set. The second set have some amazing features applied to it, but on the latest versions of PHP you need to enable it on the INI configuration and, if you are dealing with embending your code on a XML file you can have some parsing errors.

As I promised before, we are not going to write a “Hello World” application and we will code 2 types of application today. A standalone and a embed one. There is no real definition as standalone or embed types, but it will be easier, for now, to identify our files as standalone or embed. The standalone is a file that is purely PHP, no other code besides PHP. The embed is a HTML, XML, or any other type of file that have PHP code inside it.

Let’s take a look on a standalone file first: standalone.php (note the .php extension, whithout it, the server can’t recongnize that there is a PHP code inside it and it would only write the code on the screen as text).

standalone.php
<?php
$string    = ‘ This is a string and the number is: ‘;
$number = 7;
echo $string;
echo $number;

If you call the file from your browser (http://localhost/standalone.php) you will see the following result:

This is a string and the number is 7

As you may noticed by now, the close tag, ?>, for the above script was omitted. When you are dealing with only PHP code on a file, you don’t need to include the closing tag, the PHP engine will do that for you and it also is a simple performance and standard recomendation from PHP.

Among other uses from the standalone file, you can call it from the command prompt / command line: php /path/to/file/standalone.php. It’s results will be displayed on the command prompt screen. At first this does not seem too useful, but when getting more advanced, you will see that you can use this with cron jobs to increase your application power.

Let’s take a look now on the embed file, embed.php. Again the extension of the file must be .php otherwise the server will not understand that there is PHP code inside the file and it will not process it.

embed.php

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html>
<head>
<title>
Page Title</title>
<!– other head data such as CSS styling and JS scripts –>

</head>
<body>
<p>

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
<p>

<?php
$string    = ‘ This is a string and the number is: ‘;
$number = 7;
echo $string;
echo $number;
?>
</p>
</body>
</html>

This is a good example of a PHP script that is embed into a XHTML file. When you call the file on your browser you will have the following result:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

This is a string and the number is 7

As you have notice, in the embed file the php close tag, ?>, is on place. Without it, the PHP engine would throw an error on the screen not letting the code to be parsed and the page to be displayed.

This is our first PHP application. For now on things will be increasing and we will focus a bit more on structures and tools that you can use while programming PHP.

For now on every time that we work with a PHP file that will be displayed on a browser or used on a cron job or for any other need, we will reference it as a application because that is what truly is.

See you on the next article, have fun.


Guide to write testable code

I have received this this morning and is a good document with examples of what should be done do write code that can be tested at Google Standards.

Just follow the link: http://misko.hevery.com/attachments/Guide-Writing%20Testable%20Code.pdf


When to give-up changing your code and start over

Lately I have been fighting with a small XSL job in a simple task of alternating rows. I was changing a code that originally wasn’t mine and trying to re-use most of the functions, but sometimes is better just to start all over again.

The schematic of the process was simple:

Get the XML nodes in the date range and for each unique day get all nodes and print. All I needed to do was to change the colors of the tables rows to have a table with alternating rows colors to give a better impression on the information that the table was displaying and to use as guide, but with the current format used (get xml nodes in the date range and for each unique day get all nodes) I could easily swap colors between days, but not between inner nodes.

This seems simple when you are not working with XSL. XSL is a great language to allow different format for the same data coming from a XML, but it’s a very simple language as well. One of the concepts of the language is that variables should be interpreted as constants, therefore you cannot reset a variable. This was one of my issues in order to use the previous methods. I could not reset a flag to tell me what was the color of the last row, before the next set of nodes started.

After trying to work around for a while and getting somehow frustrated with all of this I decided to start over.

Start over should always happen whenever you just can’t work with the current structure and you need to create a new one. The problem with starting over is that it actually takes some time to create all again and in the corporative world that we live today, time is something that you just don’t have.

For my luck I had a couple hours before placing the new format in production and I have created the methods again. The methods now are not as complex as they where before. The concept changed to loop nodes in date range and nodes inside that unique day to loop into all inner nodes inside that date range. Making the structure a bit simpler I could manage to alter the rows inside the XSL.

If you are wondering when to start over with your code, I must say, there is no easy answer, but the best moment is when you truly believe that you have exhausted your options and you still have enough time or can deal more time to rewrite the code.


%d bloggers like this: