 |
|
Friday, March 21. 2008
Jeff Vodel provides tips for writing more comprehensible code, in a very humoruous article. They don't just apply to solo coders, people who inherit or update your code down the line will also thank you. Even though they're written for C programmers, his advice applies to any programming language. For web programmers, #5 should be heeded, as we've learned that the code we run on the server typically contributes only a fraction towards how long users must wait to see your web page. You get more bang for the buck by reducing how much data browsers have to download (file sizes) and how many connection it must make (hits to download css, images, javascript files, etc...).
This means that, if I make a stinky mess, I'm doing it in my own nest. When I'm chasing down a bug at 3 a.m., staring at a nightmare cloud of spaghetti code, and I say, "Dear God, what idiot child of married cousins wrote this garbage?", the answer to that question is "Me."
I've written previously my own set of tips to write readable php code.
Friday, February 15. 2008
While reading some code, I came across a pleasantly surprising snippet that a coworker had added, and it went something like this:
Calling functions through variables
function display_output()
{
return 'This is the output text!';
}
$variable = 'display_output';
echo $variable();
Result: $variable() = 'This is the output text!';
With closer inspection, this is a pretty cool feature in PHP. It allows you to dynamically call a function by treating a variable like a function. When parentheses are after the variable name, PHP calls the function whose name equals the variable's value. Ex: if $foo = 'bar', then doing $foo() will try calling a function named bar(). Sure, the same result can be achieved using IF loops, but in PHP, it's always good to at least know of the alternatives.
Variable variables
$charlie = 'delta';
$delta = 'oscar';
echo $$charlie;
Result: $$charlie = 'oscar';
My coworker, Oscar, also informed me about "variable variables". These buggers have that extra dollar sign. In the previous example, PHP looks at the value of $charlie, which is 'delta'. It then returns the value of $delta, which is 'oscar'.
Creating variables dynamically
$when = 'future';
$variable_past = 'Welcome to the past';
${'variable_' . $when} = 'Welcome to the ' . $when;
Result: $variable_future is created, and $variable_future = 'Welcome to the future'
This is helpful for when you need to create numerous variables on the fly, such as within a FOR loop. However, it may end up making more sense to create an associative array, depending on how much data you need stored. Ex: $container_array['dynamic_name'] = 'some_value'; Again, this all depends on your given situation.
Thursday, January 31. 2008
I mentioned in the previous post that a widget is just some code that people can stick on their website. A web-based widget can be directly embedded (such as a Flash or Flex application), or it could simply be an IFRAME linking to code somewhere else. For this example, I created the file widget-photo.php and linked to it from an iframe.
The cool part of this example isn't that a page "lives" within another. The magic happens when the user searches for their local congressperson. When a last name is submitted, the code within the embedded page makes a couple API calls. Using the SunlightLabs API, the PHP script asks for a list of congresspeople with the given last name. The SunlightLabs API returns a nicely-formatted JSON result set. We have officially used an API.
A mashup is just an easy way of saying that two or more functionalities have been combined in hopes of producing something more interesting / helpful / meaningful / awesome, etc. A pretty solid mashup example is HousingMaps, which mashes together craigslist and Google Maps.
The example isn't over yet. We have just returned data regarding a congressperson or bunch of congresspeople. We're about to display them, but there's something we forgot before making this a true mashup. For each congressperson, we're also making a call to the CARMA API to try and find some other information related to that person. We make a call, enter the congressperson's name for the input, and in exchange are given an XML file of that congressperson and their CARMA statistics. How we choose to display all this information is anyone's call from here.
Mission accomplished.
Source: http://tech.forumone.com/code_examples/widget-photo.phph
Wednesday, January 30. 2008
In the next several months, we'll be rolling out several widgets that take full advantage of CARMA's Application Programming Interface.
The CARMA website, especially with the Dig Deeper tool, allows the user to sift through vast amounts of cleanly presented CO2 emissions data like never before. Plants, companies and locations can be filtered by numerous criteria, including but not limited to: future power output, present CO2 emissions, past intensity and parent location. Maps present an intuitive display to compliment the raw data.
With all this information in the CARMA database, there's no wonder that some connections were left untouched. We just couldn't add too much information to the website without it getting overwhelming for the casual visitor. If you have unanswered questions after viewing the CARMA site, then the chances are that the API might be worth looking in to.
With the CARMA API, you can create a widget to simply repackage CARMA data. Examples include:
1. Displaying the future emissions of planned power plants by location.
2. Displaying trends in emissions and carbon intensity for specific plants, companies, or geographic regions.
3. Looking up the carbon intensity of power providers by zip code in the U.S.
You can also mashup data from CARMA and other sources:
1. Using the carbon intensity of individual power companies to calculate accurate carbon footprints for individual users.
2. Mashing up CARMA’s emissions data with campaign contribution data for congressional districts.
3. Mashing up CARMA’s power company emissions data with financial information.
Remember that a lot of the plants have coordinates, so creating visually-appealing map widgets (perhaps mashing up with other types of geographic data, like asthma rates) is entirely possible.
A widget is a little chunk of code that people can call their own and place on their blog or website. For example, this widget simply grabs the latest RSS feeds from somewhere. Some other widgets are more interactive, such as for weather, allowing users to enter some inputs (in this case, their zip code).
Calling the API is relatively simple, and it returns data back in XML format. Let's say that you wanted to get the raw data for the state of California. You can simply browse to http://carma.org/api/1.0/searchLocations?name=California. To build a widget, you could make multiple API calls and even mash it up with external data sources (like Yahoo! finance).
We created the API so anyone can download the raw data and use it to form their own connections. Forum One will be creating several widgets to showcase the great potential of the API, but the real excitement comes from how you interpret and display the data.
Tuesday, January 29. 2008
Dan and I are back from the MySQL for Developers training, and we decided to share the enlightenment. Instead of boring you with a summary of what we've learned, we've decided to divvy up our notes into various "tips & tricks" posts.
Let's say that a client sends you an Access, Excel, or CSV file and tells you to stick all that information into your database. The client is oblivious to the structure of your table, and the file contains several columns that aren't needed. Some of the other columns are improperly formatted, making your life that much harder.
Your first instinct might be to write a PHP (or Perl, or Python, etc) script that squeezes and trims data to the right dimensions, then sends to MySQL. That's fine, but many don't know that MySQL can usually do this tedius work for you. It might also be worth mentioning that MySQL parses this data exceedingly fast.
LOAD DATA INFILE (or LOAD LOCAL DATA INFILE from your local machine) is an importer's dream come true. It allows for MySQL itself to load and parse a file. Once loaded, you can set the FIELDS TERMINATED BY, ENCLOSED BY, LINES TERMINATED BY, and IGNORE ... LINES parameters to split the data into meaningful chunks.
Setup:
LOAD DATE INFILE '[file.ext]' INTO TABLE [table_name]
FIELDS TERMINATED BY '[column_separator]'
ENCLOSED BY '[usually doublequote]'
LINES TERMINATED BY '[usually \r or \n]'
IGNORE [lines_to_skip] LINES
(col1, @variable2, col3, col4)
SET col2 = SOME_FUNCTION(@variable2, some_parameters);
You can use @variables instead of columns for processing. If unneeded file columns exist, then we store that column into a dummy variable (look at @skip below).
The import file uses "Apr 20 1982" for the birthday, but we want it looking like "1982-04-20". Below, we set the @birthday variable equal to the file's birthday column, then SET birthday (the DB column) equal to STR_TO_DATE(@birthday, 'input_format').
Real-world usage:
LOAD DATA INFILE 'import.csv' INTO TABLE people
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 2 LINES
(id, @skip, @fname, @lname, @skip, @birthday)
SET name = CONCAT(@fname, ' ', $lname),
birthday = STR_TO_DATE(@birthday, '%b %e %Y');
NOTE: STR_TO_DATE expects the input date format to be specified. The 2nd parameter contains the curious string '%b %e %Y'. In this example, it's looking for a date formatted like 'Jan 9 2001'. The meaning of those special characters can be found here.
I would highly recommend attending the MySQL training seminars. David Swain, our trainer, was highly competent and had helpful answers and further insight to every question we could throw at him.
Thursday, November 15. 2007
With the help of Forum One, the Center for Global Development (CGD) launched a web-based initiative aiming to reduce global carbon emissions. CGD believes that public disclosure is one of the best steps towards solving a problem, and they came into the CO2 front with some pretty heavy artillery. Kevin Ummel and David Wheeler at CGD worked tremendously hard not only to find and sanity-check the emissions data, but to sculpt it in a way that many will undoubtedly find meaninful. The database itself consists of information for 50,000 power plants and over 4,000 power companies. Google maps have been used to add some flesh to the raw data, and a colored icon is attached to every item to further reinforce the numbers.
 What's coolest about CARMA is the ability to "dig" through all the data to find stuff that you'd consider meaningful. The Dig Deeper section allows you to drill down to find plants and companies in your area, as well as the most troublesome (or greenest) ones in your region/country/continent/etc.
Users can download the raw data in CSV or XML formats on just about every page, and if that's not thorough enough, there's a full-featured API. With the API, anyone could pull out data and use it for their own website -- perfect for custom widgets. Examples and helpful tips will shortly be added to the API documentation as well.
Even though we knew from the start that this is a really cool initiative, we had no idea that it would make it to the front page of sites like CNN and NPR. On the day after launch (11/15), the first server buckled under the enormous load, causing a bit of chaos. The site was down for a good 20 minutes while Rackspace upgraded to a new server. However, all is well again, and the steps made towards correcting the problem will be on a follow-up post.
Tuesday, November 6. 2007
Less experienced PHP programmers might read 6 PHP coding tips to write less code and think dangerous thoughts like "This is what I have to do to be an l33t coder!" The assumption that less code == better code quickly leads to hard to maintain spaghetti code. Sure it saves you time in the fun, exciting build it and show it to the client phase, but six months later when you come back to fix something, or worse yet hand it off to a colleague, you'll waste more time deciphering what you meant to do. The author addresses this with his sixth point, which advises readers to "write a comment and more readable construction."
What can you do to write readable, maintainable code - especially if you're not just a one-person operation?
Let's start with a few basics. - Use descriptive variable names. There's no rule that your variable name has to be less than 8 characters, so don't omit vowels just to invent your own dialect. Instead of
$cmpnt, just use $component. You should be using an IDE or text editor that can complete words for you. Don't get carried away though, no one wants to see $Web_Service_Component_Collection either. - Function and class method names should also be descriptive. Methods which return a boolean true/false should be prefixed with is or has, ie
isToday(), isAdmin(), hasChildren(). - Prefix functions that return a value with a get or fetch, or some other verb. Most functions should have a verb component. This avoids syntactical errors between calling a function and accessing a property in classes. For example,
$user->posts, which is a property, can be confused with $user->posts(). The latter should be $user->getPosts();.
Use a code standard, and stick to it. Coding standards promote uniformity and makes it easier to understand because you don't have to parse through an individual's idiosyncrasies for naming variables, placing braces, et al.
Document functions and classes using phpdoc. Don't do it just for the auto-generated documentation, which is a nice side effect. More importantly, it will help you capture your intent for the function, parameters and their expected types, and expected return values as you're writing them, helping you when you come back to read your own code later.
Use functions to group code needed to perform a single action or test. This leads to code as in the following, albeit trivial, example:
if (is_admin($user)) { update_admin_log($user); $menu_options = get_menu_options('admin'); } else { $menu_options = get_menu_options('public'); }
Use classes to group code related to a particular set of operations or that work on an object you can model. Classes also organize your code into easier to find files, instead of monolithic files that contain many, many functions.
$page = new Page();
if (true == $user->is_admin()) { $log = admin_log::singleton(); $log->update_login($user); $page->bind_user($user); }
$page->show_header();
Finally, the best advice I can give is to read other people's code. Just seeing your own code reinforces bad habits. Two particularly well written PHP libraries I've run across are JpGraph, and more recently the Solar framework.
Thursday, October 11. 2007
Have you ever written a report or paper which took forever to complete? I'm sure you have. And like most of us, a good chunk of that time was spent rewriting unsaved work. And to think, most of that time could have been saved if we simply 'Saved often' and keep versions of our work. Well this simply theory has been embraced by multiple programs that enables data versioning. As a developer, a great program to use is Subversion.
What is Subversion?
Subversion is a free/open-source version control system. That is, Subversion manages files and directories over time. A tree of files is placed into a central repository. The repository is much like an ordinary file server, except that it remembers every change ever made to your files and directories. This allows you to recover older versions of your data, or examine the history of how your data changed. In this regard, many people think of a version control system as a sort of “time machine”.
Subversion can access its repository across networks, which allows it to be used by people on different computers. At some level, the ability for various people to modify and manage the same set of data from their respective locations fosters collaboration. Progress can occur more quickly without a single conduit through which all modifications must occur. And because the work is versioned, you need not fear that quality is the trade-off for losing that conduit—if some incorrect change is made to the data, just undo that change. (Source: http://svnbook.red-bean.com)
Working with varied project requirements and changing technology, developers are always making changes and modifications. And sometimes old modifications can serve as a foundation for new modifications after they have been removed from a project. Talk about a 'head spinner'. But this has all been simplified by utilizing Subversion and some simple practices which I will share with you.
Commit Often!
Avoid: Making changes to multiple features or functions and committing them all at once.
Depending on how complex your project is, you want to try and break your changes up by feature or function. Once you have made your revisions to a particular function/feature to satisfaction, you want to commit it to the repository. Once completed, move on to the next function/feature.
The reason for this approach is to have granular flexibility in rolling back your changes without affecting multiple functions/features. There is nothing more frustrating than creating extra work for yourself after rolling back what you think is one change when in fact it is multiple changes across the entire project. You will not only find flexibility with rollbacks, but in other tools available in Subversion.
Meaningful Comments!
Avoid: Committing changes and not knowing exactly what the changes were.
The point in using Subversion is to make your life that much easier! In addition to the aforementioned practice you want to couple your 'commits' with a meaningful description so that both you and other developers can easily understand each others revisions.
Additionally, if you use a ticketing system to document bugs and modifications you may want to refer to the respective ticket ID where greater detail can be found. (A totally separate post!)
Simple practices can make all the difference in your work processes and maintaining your sanity. Although short, this post will be followed by additional posts made available about how you can effectively use Subversion.
|
|
Comments
Mon, 21.04.2008 13:43
Hi Vikram-- Have you set up your repository in Subversive and successfully connected?
Mon, 21.04.2008 12:56
On checkout as.. dialog you asked to choose "Check out as a project configured using the New Project Wizard." That [...]
Fri, 11.04.2008 06:06
Excellent post, very detailed and I especially like the images. It helped me a lot with my set up.
Thu, 03.04.2008 01:55
How can they say that they're "not responsible" for "death, personal injury, or severe property or environmental [...]
Tue, 01.04.2008 23:29
Hi Marcel-- Make sure you follow the pre-step I have above and ensure you've installed one of the two Subversion [...]
Tue, 01.04.2008 20:21
Please help when I try to import, I don't have Other/Checkout Projects from SVN. Where is my problem?
Thu, 21.02.2008 05:58
Great, this was useful.
Wed, 13.02.2008 09:26
Thanks , I was trying hard & hard with http://www.eclipse.org/pdt/ins tall.php but success came here... thanks for the [...]
Wed, 06.02.2008 15:39
I had to find packages for e Redhat EL3 server, the following worked for me: Catdoc [...]
Wed, 30.01.2008 14:38
Yeah, it does. Just remember that a function is specific to the database in which it's created. If you're trying to call [...]
Wed, 30.01.2008 14:17
Awesome - I didn't know you could calculate and transform fields using functions that way. Does it work with custom [...]
Tue, 22.01.2008 20:49
Hehe... DNS hosting services I toyed with including in the tutorial but it's really a bit too much for the one-man (or [...]
Sat, 19.01.2008 18:51
Awesome, thanks very much for the guide!
Wed, 16.01.2008 14:21
I'd have to say that I like #2, its much more of a natural web experience to me. We don't always have to emulate [...]
Wed, 02.01.2008 16:10
It's fashionable this time of year to create some sort of year-end "best of" or "top ten" list [...]