It seems like you are using an ad blocker. To enhance your experience and support our website, please consider:

  1. Signing in to disable ads on our site.
  2. Sign up if you don't have an account yet.
  3. Or, disable your ad blocker by doing this:
    • Click on the ad blocker icon in your browser's toolbar.
    • Select "Pause on this site" or a similar option for lancecourse.com.

Mistakes which could kill your app in time

This could probably sound very strange to many, but I have noticed recently with my experience in developing phpocean that there are some errors and bugs we might not notice/discover while coding in earlier ages of our applications.

I have struggled a lot to get a clear way of bringing them out of the shadows. Every time I tried discussing it with people they usually get confused about what exactly is the matter.

Coding is complex and requires a lot of attention and focus. When you code you define complex systems from their tiny pieces. Then those pieces, with their singularities, have to be assembled to define the main function(s) of the entire system. The more the pieces are, the higher the risks of mistakes and misconfigurations:

  • You may miss a piece
  • A piece can be of a bad quality
  • A piece may have a short lifetime
  • A piece can be misplaced during maintenance
  • A piece could be updated without considering to balance its co-relation with the rest
  • etc

In case a piece is faulty, the effect is more or less felt in the entire system. Indeed, programming paradigms, like Object Oriented Programming(OOP), are there to help leverage such situations. In most cases, they may be considered and handled as exceptions

This is how developing a website too is and has always been. It's made up of many things:

  • The design
  • Functional structure
  • User experience
  • SEO
  • Administration
  • Management
  • Maintenance
  • Etc.

That problem can attack/appear in any of these aspects of your website/application regardless of the type of the website and its age. They have no real ending. They can't really be PERMANENTLY fixed because they depend a lot on TIME. The more time goes on, the more obvious and clearer they become. They are like worms and can metamorphose at any time. When you roam on the Internet you can notice them here and there from website to website. Sometimes, most users or even website owners don't even notice them as such. Note that these problems are mostly the human failure, due to misunderstandings or other factors related to the human mind.

In this article, I will try to illustrate some of those problems and share with you, my idea of how they could possibly be handled.

Loops and data size

Loops in most programming languages are used to make decisions. While they are used for computational decisions, the programmer's decision matters a lot in how those loops will behave in the software.

Application logic

If the programmer doesn't have a deep understanding of the situation and of the context to better format the loops, they may become a source of bugs and behavioral application.

Let me illustrate this with an example:

$posts = [1,2,3,4,5,6,7,8,9];

foreach($posts as $post) {
    // do something
}

At first look, this seems normal and harmless, but in time the number of posts will increase and there you will notice the problem. The size of the array and the logic in the loop always impact the time of execution. Let's look at some facts:

<?php

$start = microtime();

$posts = [1,2,3,4,5,6,7,8,9];

foreach($posts as $post) {

}

$end = microtime();

$elapsed = $end - $start;

echo "<br>Elapsed time: {$elapsed}";

With this, the execution time on my PC varies from 0.000001?s and 0.000003?s

When I add a simple logic in the loop like this:

<?php

$start = microtime();

$posts = [1,2,3,4,5,6,7,8,9];

foreach($posts as $post) {
    echo $post;
}

$end = microtime();

$elapsed = $end - $start;

echo "<br>Elapsed time: {$elapsed}";

The execution time varies from 0.000002?s and 0.000006?s

Now when I increase the array size to 10000 elements without the loop body the execution goes up to 0.001152?s. When I add the loop body it goes up to 0.011447?s.

This is obvious proof that loops plus the size of your data can create a considerable execution time in your application. I do understand that most of us are aware of that. My warning here is for you to know that while you are creating your applications, you usually work with a few data samples for tests. You can't rely on such data to prove at 100% how your application is going to react in time.

Design

Meanwhile, this is what concerns the application logic. In the case of a website where the content display matters a lot, it's important you think of your design based on these facts.

This may happen in your forum, pagination system, commenting system, post listing, etc.

Database

Databases are used in most websites which need to persist and store data for further use. As mentioned in above, the bigger the data used to feed your application, the slower it runs. It's, therefore, important you take care of how to fetch data from your database. Always think of having a maximum or a limit to your SQL queries in charge of fetching data.

SELECT * FROM table LIMIT 100;

SELECT * FROM table LIMIT 0, 100

I recently noticed this problem with some tutorials on phpocean.com. When I was coding the website I did not expect to have so many people visiting and commenting. So I just did it the working way. Then there is this tutorial I wrote last year which has about 200 comments. At first, I used to display all the comments at the bottom of the tutorials. But with 100+ comments, in time this tutorial started to load very slowly(very). Besides that, it was breaking the site design by tearing a lot of grids. The browser's vertical scrollbar was becoming so tiny that you couldn't handle scrolling fast down. So, I refactored the display by using some infinite loading principle. Then the comments start loading if only you scroll to the end of the tutorial. That one too, just one comment at a time.

Bottom Line

This may be a huge topic, not only the code design or user interfaces but also can become a huge concern for your application security. Here I have given you an example of these mysterious problems that occur and become more obvious in time. I am sure from now on you should be able to consider them in your next application. You will definitely discover more in various forms, always address them as possible as you can before they kill your application/website.

One advice, always fix bugs at the moment you meet them. Ignoring bugs can really increase the size of those problems.