Archive for category web standards

6 Ways to make your PHP code more maintainable

Most of us have inherited projects from other developers, or have revisited old code that we’ve written months or years beforehand. I’ve personally seen code that looked like it was written by a 3rd grader. Hopefully none of you will have to deal with code written this poorly. Here are a few ways to make your code flexible, and maintainable so that you, or some other developer won’t be cursing you.

I’ll assume that if you’re reading this article that you’ve written PHP code before, and that you’re fairly familiar with the constructs of the language. But, for the sake of being thorough, I’m going to mention a few things.

1. Comment!

Make sure you’re commenting. I recommend a header comment to describe the file: The Author’s name, a description of the script, and a listing of your founding assumptions are all good things to include in the file description. Also if you write some code that is particularly complex or important I recommend commenting it aswell. Some sticklers will even recommend commenting every function (including function parameter descriptions). I don’t feel that this is necessary but if you’re writing a massive application or an application created by multiple developers than this will be very helpful.

2. Use Functional Programming

I can’t believe I have to say this, but, use functions. If you write a string of code more than once it should be turned into a function. But, be careful to avoid writing several functions that do similar things, if two functions could be consolidated into one in an elegant way I would recommend that. remember the less code you have to look at in the future the easier it will be to understand what the application is doing.

3. Split up your files.

If you find that your script is very long try splitting it into multiple parts. Also, if you have certain portions of your code that are used on multiple pages make sure that this part is included in an external file for easy maintenance. If one page has several modular parts make sure that they are all contained in external files.

4. Make sure you’re using directories.

If you’re working on a large project, it’s very important that you organize your files into logical directories. There’s nothing worse than a completely flat file system. I’ve seen projects where there were hundreds of scripts (with nondescript names) in the same directory. Do yourself a favor and organize these files. It’ll make finding things later much easier. I usually find that it’s best to organize my files into Models, Views, and Controllers. Model are scripts that describe data objects (classes). Controllers manage interactions between user actions and models and views give information to the user (pages, ajax responses, RSS feeds, RESTful API responses, SOAP pages etc.).

5.Don’t create variables where you don’t have to.

If a function returns an iterable object and all you need to do is perform an action on that object using a loop, don’t create a dedicated variable for it. For example say that I want to iterate over an array that is returned by a function. Instead of:

$arr = functionReturningArray();

for($arr as $element)

do some action($element);

try:

for(functionReturningArray() as $element)

do some action($element)

this is a much more concise way of performing an action.

6. Object Oriented Programming

If you’re not familiar with it already read up on Object Oriented Programming. OOP allows you to create objects that contain all the data and functions to represent a physical object or idea. This is very helpful for large project where you want to compartmentalized your code into distinct pieces (easier for debugging easier for understanding). This approach almost always yields less code, and if done correctly can yield reusable code for other projects.

7. (Bonus) Don’t Be Lazy

One of the main sources of sloppy code is laziness, remember that just because you know what you’re trying to accomplish with your code doesn’t mean that others will. Take the time to do it right, because sooner or later almost all code needs to be revised, write it correctly the first time and you’ll thank yourself later.

Regards,

Adam Haney

Post to Twitter Post to Delicious Delicious Post to Digg Digg This Post Post to StumbleUpon Stumble This Post

,

No Comments

Moving to the cloud: perks, profit and peril

In response to Cory Doctorow’s article Not Every Cloud has a Silver Lining, and a recent discussion on Slashdot. We here at Casa Nova Designs would like to comment about cloud computing and what it means for our customers, and for our customer’s users.

What is cloud computing?
Before we begin this discussion it is important to define, for this conversation, what cloud computing is. Cloud computing is an ambiguous term that can refer to everything from hosting to interface, from our standpoint we take cloud computing to mean this. Cloud computing is a software design paradigm that removes the majority of storage/computation from an individual user’s computer and instead uses a remote location to process information and store data. While some would argue that cloud computing must have a layer of hardware abstraction (such as Amazon’s S3 platform that totally abstracts the hardware that applications are running on), we believe that any software that runs primarily on a remote machine maintained by either the product vendor or a contractor of the product vendor is cloud computing (irregardless of the architecture of the remote machine). Popular examples of this design paradigm are Google Documents, Gmail, Blackboard, etc.

The bad news first
Because Doctrow’s article is primarily concerned with the risks associated with cloud computing let’s start there. Cloud computing gives all of the power of information to the software vendor. User’s do not maintain a private copy of their data and thus, if a software product which utilizes cloud computing becomes inaccessible all of the user’s data is lost. This does not necessarily mean that this is likely. Google has one of if not the best up time records on the Internet, I would argue that an individual is much more likely to lose data either to malware, hardware failure, or accidental deletion. The flaw with this argument is that, if a software vendor chooses they may forbid users to export their data. I’ll use Google documents as an example for this argument. Google docs, like many cloud applications allows users to download files from their servers to their personal machines, and in google doc’s case this information is provided in several open source formats. To contrast that Microsoft Word, which was the dominant play in this market, allowed users to store all of their documents on their personal machine, but did not allow them to access these documents in a none Microsoft product. So while users were able to store the information on their machine they weren’t free to use it as they pleased, and the potential of losing their data was much more likely than if they stored it on servers maintained by professionals.

The good outweighs the bad
In spite of these issues I believe that cloud computing is helpful to the user community and the software development community. For the user, cloud computing is often client agnostic. I, as a Linux user can access Google docs in Firefox, and if I’m at a friend’s house I can access the same information from a Windows machine. This cross platform compatibility was achieved without any extra thought from google. This is incredibly beneficial to the Linux community, as software platforms move away from Window’s dependence Linux adoption becomes a much smaller hurdle. Also cloud computing is, in many ways, architecture agnostic. I as a developer can create a cloud application in Perl, PHP, Ruby, Python C, C++ on either Linux, Mac, Windows or any other OS I choose. The only requirement is that my server can manipulate data and server is up to the web via http. This is immensely helpful for developers, as they aren’t locked into using any one language or operating system regardless of what their end client is using.

On Cloud 9
In my opinion, cloud computing’s benefits far outweigh it’s risks. Like any paradigm, software developed for the cloud is only as negative as a developer makes it. If Google wanted to control user’s data they could, much like Microsoft controlled users data with their proprietary obfuscated file types. In the hands of benevolent hosts cloud computing offers an exciting new age of computation for the average user. It abstracts their operation system, takes care of backups and requires no updates. True software vendors could create software as a service models that would continually charge users for use, but are these monthly fees going to be worse than the one time expense of purchasing traditional software? These are important issues to consider, but I believe that those decisions lie in the realm of business men. I as a developer embrace cloud computing as a paradigm.

Regards,
Adam Haney
Chief Developer
Casa Nova Designs

Post to Twitter Post to Delicious Delicious Post to Digg Digg This Post Post to StumbleUpon Stumble This Post

, , , , ,

No Comments

Simple Prototype.js slideshow

Over the last few weeks I have been looking for a simple javascript based slideshow script based upon the prototype.js library. I found a few scripts that looked rather shiny but they were difficult to integrate with a php backend (I wanted something that could use simple xhtml).

The code to create a slide show is rather simple. Simple create an element and everything that you place inside of that element will be presented in the slide show.

the XHTML:
(it is very important that the style of the parent element is set to “display:none” inline and not in the stylesheet or elsewhere in the document. The prototype library is unable to change the opacity/display of an element whose style is define in an external stylesheet).

1
2
3
4
5
<div id="slides" style="display:none" >
	<img src="http://www.sxc.hu/pic/m/l/lu/lusi/1189704_ripe_strawberries_3.jpg" />
	<img src="http://www.sxc.hu/pic/m/s/sj/sjur/1186634_kiwi.jpg" />
	<img src="http://www.sxc.hu/pic/m/l/lu/lusi/1186300_banana_diet_4.jpg" />
</div>

The Javascript:
To create a slideshow simple include prototype (either on your site or using google’s service) and call the StartSlideShow function. The first argument of the function is the id of parent element in this case slides. The second argument is the length of time (in milliseconds) that a particular slide stays visible.

1
2
3
4
5
<script type="text/javascript">
window.onload=function(){
	StartSlideShow('slides', 2000);
}
</script>

If you don’t already have Prototype.js included as a javascript library on the site you are working on it can quickly and easily be added via google’s javascript api service:

1
2
3
4
5
6
7
8
9
10
11
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
  google.load("prototype", "1.6");
  google.load("scriptaculous", "1.8.2");	
</script>
<script type="text/javascript" src="slideshow.js"></script>
<script type="text/javascript">
window.onload=function(){
	StartSlideShow('slides', 2000);
}
</script>

The entire slideshow.js file is less than 30 lines of code (not counting the comments). The script should be easy to understand and modify.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
function DoSlide(slide, index, slideLength){
	setTimeout(function(index){
		$(slide).appear();
	}, index * slideLength, index);
 
	setTimeout(function(index){
		$(slide).fade();
	}, (index * slideLength) + (slideLength / 2), index);
}
 
function DoSlideShow(slideshowDiv, slideLength){
	var slides = $(slideshowDiv).childElements();
 
	//fades each slide in and out
	slides.each(function(s, index){
		DoSlide(s, index, slideLength);
	});
 
	var intervalLength = slideLength * slides.size();
	var slideShow = setInterval(
	function(slideshowDiv, slideLength){
		DoSlideShow(slideshowDiv, slideLength)
		}, intervalLength, slideshowDiv, slideLength);
}
 
function StartSlideShow(slideshowDiv, slideLength)
{
	var slides = $(slideshowDiv).childElements();
	//hides all slides inside div
	slides.each(function(s, index){
		s.toggle();
	});
	//makes parent slide div visible
	$(slideshowDiv).toggle();
	DoSlideShow(slideshowDiv, slideLength);
}

If you are interested in a working copy of the slideshow I have a demo set up: http://scratch.casanovawebdesign.com/slideshow. If anyone has any questions or suggestions for this script feel free to discuss in the comment section.

Regards,
Adam Haney
Chief Developer Casa Nova Designs

Post to Twitter Post to Delicious Delicious Post to Digg Digg This Post Post to StumbleUpon Stumble This Post

,

No Comments

CSS3

CSS3 the upcoming W3 recommendation for Cascading Style Sheets (the current specification is 2.1) has many exciting new features that will allow common design paradigms to be implemented more easily.  Noupe  has an excellent article about some of the most powerful new features of CSS3. Check it out over at http://www.noupe.com/css3/css3-exciting-functions-and-features-30-useful-tutorials.html.

These new features are very exciting and some of them are already implemented as non-standard CSS two open source browser rendering engines. Gecko the rendering engine employed by the mozilla browsers (firefox, mozilla, thunderbird) is able to render rounded corners using CSS3 properties as well as handle object opacity. Webkit, the rendering engine that supports Safari and Konqueror (a linux browser) is also able to implement many CSS3 features. CSS3.info has detailed information about CSS3 support in popular browsers (no small surprise Internet Explorer has little to no support).

Finally for more information about the path of CSS3 and how far away the W3 is from releasing their specification check out the W3 roadmap at http://www.w3.org/Style/CSS/current-work.

I hope you find this information helpful. I for one am very excited about the release of CSS3.

Regards,

Adam Haney

Post to Twitter Post to Delicious Delicious Post to Digg Digg This Post Post to StumbleUpon Stumble This Post

,

No Comments

Free As In… ?

The free and open source software movement has gained much traction over the last few years. Users of all operating systems have begun embracing Firefox the open source and community maintained browser. And some, like myself have ventured into the world of Linux, a free and open source operating system that can completely replace Windows.

Recently members of the GNU and Linux communities have begun pondering the affect of web based applications on their lives, and questioning whether or not these utilities are truly “free”, despite the fact that they represent no monetary expenditure. The question has become, what value does the data that you have input hold? Many people have never asked themselves that question, have you ever thought: “how can I get my facebook pictures back?”. The short answer is you can’t.

There are two very interesting articles that dive further into this subject than I am willing to. “Avoiding the Javascript Trap” on the tux HQ website gives a good perspective on the issue (http://tuxradar.com/content/avoiding-javascript-trap). The original article by Richard Stallman entitled “The Javascript Trap” (http://www.gnu.org/philosophy/javascript-trap.html) is an excellent article hosting on the GNU website.

I personally don’t have an answer to this very important question. I have much of my personal data hosted in the cloud with no apparent way to get it back, and I think it’s important that we begin realizing that our data has value, and we much be the keepers of its worth.

Regards,
Adam Haney

Post to Twitter Post to Delicious Delicious Post to Digg Digg This Post Post to StumbleUpon Stumble This Post

,

No Comments

The Affects of O3D On GP-GPU Computing

Recently Google released an exciting new browser extension for Internet Explorer, Firefox and Safari. The Extension, O3D allows javascript developers to tap into the computing power of a modern user’s graphics card. This is exciting for many reasons.

First, this extension opens new doors to web application developers who want to add a new level to their current user interfaces (although I fear that some might over do it). It should be interesting to see how this extension is used to make GUIs more intuitive and easier to use.

Secondly, and most obviously this extension will allow for game development for the browser. Web 2.0 games have been on the rise for a few years now and there are already a few javascript libraries that allow for rapid javascript game development. It will be exciting to see how developers incorporate this new library into their games. I expect that in the next year or too we will so many new games that incorporate this library into their software stack.

Lastly, the affect that I’m most excited about is this libraries affects on GPGPU computing. GPGPU computing a branch of computer science that focuses on creating massively parallel programs that run on a user’s graphics card has already grealy reduced the cost of computation for many small scientific research projects. Nvidia’s nonschalant motto “Moore’s Law is for wimps” has proven true and we have seen a growth in GPU computing power that greatly exceeds Moore’s law for the last few years.

What I’m proposing is this, a distributed GPGPU computing API based upon O3D. This library would allow for GPGPU clusters to be created (not unlike ROCKS clusters that currently exist). Except this system would make adding a new node to the cluster as simple as navigating to a URL in the browser. Ideas like this have already been proposed for distributed javascript computation and now with this library allowing for javascript calls to the GPU I believe a small group could build a distributed system much faster than any centralized super computer (possibly larger than the current FOLDing at home cluster that does use GPU computing but requires the installation of a hard client on the user’s computer).

Update: I have begun work on a project that I am deeming “casanova cluster” which uses python for program development. The python library then makes SOAP calls to a PHP server that is controlling the number of nodes and their latency. Finally the PHP server uses JSON/Comet to talk to the browswer nodes running javascript and O3d. This Project is in infant stages but it should be interesting to see the ways it develops.

Regards,

Adam Haney

Chief Developer

Casa Nova Designs

Post to Twitter Post to Delicious Delicious Post to Digg Digg This Post Post to StumbleUpon Stumble This Post

, ,

3 Comments