Posts

Showing posts from 2012

Testing Existing Code

When beginning Unit testing it is hard to find a good starting point. There is a lot of theory and some very good points of reference, but what about actually getting down and dirty & hands-on as so many of us love to do? Here are some ideas, and pointers on how you might want to think about starting out with TDD and Unit testing an existing system. I am a PHP Developer, primarily working on an Apache/MySQL stack, however I would assume these principles can be carried over to any project, if you substitute the appropriate technologies. I will assume you have been through a few basic tutorials on PHPUnit or are at least familiar with a Unit Testing framework of some description. I won't go into a step by step set up but might assume you know a few things. 1. I would begin by getting a local development copy set up and running. Using XAMPP and MySQL, get a copy of your web application running off of localhost, even if it only has a few test rows of data and is stripped down

More Than a Dot

No one knows if it was a man, or a woman, or a child that first did it, but we do know that about 40,000 years ago, someone put a faint red dot on the wall of a cave in Spain. Humankind has always felt a burning desire to record information and transmit it into the future. What that dot meant, no one really knows. Perhaps it was the first expression of binary? Perhaps it was just a dot. 7,000 years later, and our ancestors were expressing themselves in the form of horses, panthers, cave bears, mammoths, and much more inside different caves, this time in the south of France. It’s the first example of recorded history, the first transmission into the future. It took many thousands more years until we discovered writing. The Sumerians started scraping sigils into clay tablets and baking them — an act which preserved them almost forever, and because of it we, know so much about their society and the way it worked. Now, of course, we have the Web, and the world has been transformed. So

Writing Readable Code

http://annafilina.com/blog/writing-readable-code/

Introduction to TDD

Image
1.  What is TDD ? The steps of test first development (TFD) are overviewed in the  UML activity diagram  of  Figure 1 .  The first step is to quickly add a test, basically just enough code to fail.  Next you run your tests, often the complete test suite although for sake of speed you may decide to run only a subset, to ensure that the new test does in fact fail.  You then update your functional code to make it pass the new tests.  The fourth step is to run your tests again.  If they fail you need to update your functional code and retest.  Once the tests pass the next step is to start over (you may first need to refactor any duplication out of your design as needed, turning TFD into TDD). Figure 1. The Steps of test-first development (TFD) . I like to describe TDD with this simple formula:    TDD = Refactoring + TFD. TDD completely turns traditional development around. When you first go to implement a new feature, the first question that you ask is whether the existi

Why Hiring a Slapper works

http://hackthesystem.com/blog/why-i-hired-a-girl-on-craigslist-to-slap-me-in-the-face-and-why-it-quadrupled-my-productivity/

Dev owns testing, Dev owns quality, really?

http://blogs.msdn.com/b/billliu/archive/2012/09/03/dev-owns-testing-dev-owns-quality-really.aspx

Treb - A simple framework for PHP

Introducing: Treb – A simple framework for PHP http://eliw.wordpress.com/2012/08/31/introducing-treb-a-simple-framework-for-php/

Object Scoping

http://blog.ircmaxell.com/2012/08/object-scoping-triste-against-service.html

Validating Emails in PHP

A very quick snippet today because I've told two people to use this approach in the last few days and both of them told me they didn't know about it. How to check if an email address is valid in PHP: use one of the  Filter functions , like this:   $email1 = "nonsense.something@dottiness" ; // not a valid email $email2 = "dotty@something.whatever" ; // valid email   $clean_email1 = filter_var ( $email1 , FILTER_VALIDATE_EMAIL ) ; // $clean_email1 = false $clean_email2 = filter_var ( $email2 , FILTER_VALIDATE_EMAIL ) ; // $clean_email2 = dotty@something.whatever The Filter extension was new in PHP 5.2, but is one of the unsung heroes of the language. It's rare for me to ever describe one approach as the "right" way to do something - but for validating data, Filter really is excellent, offering both validating and sanitising filters and generally making it super-easy to clean up incoming variables. Many of the framework

Are PHP Developers functophobic?

I do feel this is the way things are going, although it seems they may already be this way!.. http://nikic.github.com/2012/08/10/Are-PHP-developers-functophobic.html

Coding Standards

I've recently come across http://pear.php.net/manual/en/standards.php , which got a few ideas jumping about my head, and reminded me of a tool I use from time to time for curiosity purposes more than anything - phpcs or PHP CodeSniffer ( http://pear.php.net/package/PHP_CodeSniffer/ ). I occasionally run phpcs against some of the codebases I work on, just to see if there seems to be any structure or standard being followed throughout the system. The results are usually verbose and stressful to even begin trying to fix, and sometimes wonder if coding standards are used effectively anywhere. With the many techniques and ways of completing a task in PHP and with PHP developers becoming more and more OOP oriented, developers are beginning to play around with inheritance and abstraction, namespaces and interfaces, getters and setters - All the buzzwords and terms which lead to more and more overly complex code for no reason other than - "we used all of the above", and as a resu

include, require, include_once or require_once

Well the question Today is which is the best or correct one to use? There are a few reasons to choose between include and require I suppose, based on the importance of the file you are 'including'.  If I was including a config or settings files with critical information in it, or a bootstrap loader file which all my files need, I'd be inclined to use require over include - as the file is Required, and not an optional file that the site can load without.  Take on the other hand if I was pulling in a file with some adverts on it, or a banner on the site from another template file, I'd be more inclined to use include, as this isn't mission critical to the site loading - Ok I'd really like it there, but if there is an issue for whatever reason, I still want the rest of the page to continue loading. Next comes the question of include/require vs include_once/require_once . I guess the age old discussion of performance comes to mind, if I know for a fact (and I s

Portable PHPUnit

So looking into PHPUnit again this morning, and would be great to have a "portable" set up.  The usual way to set up PHPUnit is to install the PEAR package.  However it turns out it is possible to have a more portable set up which can be moved between my XAMPP usb pen, WAMP laptop setup and LAMP stack at work, without all needing to have PHPUnit installed under all the separate hosting accounts. Found this post the other day:  http://stackoverflow.com/questions/4801183/php-is-there-a-portable-version-of-phpunit .  Well in actual fact I found the process on another site, but that one sums it up pretty well I think.  And I don't think you'd need to use the git clone's either, you could I suppose download the source from github in an archive and extract it. The main thing to remember is to update your include_path() to include all the paths to the modules you've downloaded though. I will have to have a play around with this, as I have PHPUnit PEAR package

Me and TDD

OK, so I'm looking into Test Driven Development (TDD). Now, I've looked into working with it before within the PHP environment I use daily, but sometimes feel a bit overwhelmed by it all. Every time I read up about it, I always get a different slant on things, and always learn something different or new from the time before, and feel I am getting to grips with the basic principles now. The big question is whether to start from scratch or attempt to write tests for the existing codebases I have. I have a personal codebase, albeit pretty dated, and then there are the (many) styles and codebases used at work for me to choose from.  Part of the dilema is if I use any work code, I can't post it online or use it in my own work, although, to be honest, when work finishes, usually the last thing I want to do is come home and be glued to the laptop writing code.  So that explains why I haven't made much progress over the past 3 years, and from time to time dip into blog a

MySQL SELECT COUNT(*) FROM issue with LIMIT

Recently working on my core database class I found an issue with my num_of_rows() method. I was parsing my query to do various things, which basically boiled down to running a query like: [code]SELECT COUNT(*) FROM `table_name`[/code] To return the number of rows generated by this query. Now, when I started introducing pagination in my front end, I wanted to utilise LIMIT/OFFSET in my queries, like so; [code]SELECT COUNT(*) FROM `table_name` LIMIT 30, 10[/code] Which I would hope returned 10 (or less if I had less than 30 rows in my table. Anyway, this was throwing a wobbler in my PDO class, I wasn't getting any error or feedback that there was an issue, and in phpmyadmin I was simply getting " MySQL returned an empty result set (i.e. zero rows) ". Baffled, I took to google and found this work around: [code]SELECT COUNT(*) FROM (SELECT * FROM `projects` LIMIT 30,10) AS subquery[/code] Happy coding

How to Promote a New Product on Your Ecommerce Site

Retailers frequently add new product lines, brands, or even seasonal items. When these fresh items are introduced, marketers call it a product launch. Product launches are important and frequent events in ecommerce marketing that may be managed with a promotional recipe, removing uncertainty and ensuring measurable results. This sort of marketing is common and, therefore, is best managed in a uniform way within each retailer. This product launch recipe does not imply some lack of creativity. Rather, it suggests the marketing tactics that each of these events should include, allows for consistent and comparable data from launch to launch, and it makes it possible improve common resources like email lists. A Common Thread A good product launch recipe begins with a common strategic goal for each launch event. This should be a standard goal — or type of goals — for all new product or brand introductions. For example, a certain number of sales of the product, the acquisition of some number

Most webmasters don't know how their websites got hacked, report says

According to a post on ne tworkworld.com,  63% of webmasters whose websites get hacked don't know how the compromise occurred! The leading cause of website compromises appears to be outdated content management software (CMS). This was indicated as a reason for their websites being hacked by 20 percent of respondents. Twelve percent of webmasters said that a computer used to update their website was infected with malware, 6 percent said that their credentials were stolen credentials, and 2 percent admitted logging in while using wireless networks or public PCs. However, 63 percent of respondents didn't know how their websites got compromised. The CMS platform most commonly installed on compromised websites was WordPress, as indicated by 28 percent of respondents. However, WordPress accounts for over 50 percent of the entire CMS market according to data from w3techs.com, so the rate between hacked WordPress websites and the platform's actual install base is better than that