January 21st, 2010

Worthless Commenting

Writing comments in your code is fine and dandy but sometimes its just a fucking waste of time. Here’s an example:

<?php
class UserDataAccessClass
{
    /**
* Get the number of followers a user has
*
* @param string $userID The user id
*
* @return int
*/
    public function getNumFollowers($userID)
    {
        return valueFromAServiceCallOrQueryEtc($userID)
    }
}
?>

So, yay – that chunk of code passes PHPCS (using the PEAR standard). All the parameters are documented, there’s a line of text explaining what the function does and the docblock even states the return type… how cute!

But why? Why do I have to type all that crap? The function’s name is self documenting & its sole parameter is obvious. The return type makes sense to me but the rest is bullshit. God forbid your function takes multiple parameters; then you’d have to line up the @param’s types and descriptions ’cause PHP people have a strange hardon for lining shit up.

The truth is the only reason I do all that stuff is cuz we run PHPCS on our code at work and I don’t wanna be “that guy”. If it wasn’t a “standrad” at work there’s no way in hell id ever bother.

I’d much have the documentation go like this instead:

<?php
class UserDataAccessClass
{
    /**
* @return int
*/
    public function getNumFollowers(string $userID)
    {
        return valueFromAServiceCallOrQueryEtc($userID)
    }
}
?>

…And to be 100% honest the only reason I’d include the @return is ’cause I’m an Eclipse & it helps PDT’s static analysis (aka autocomplete gets more better).

  • Digg
  • del.icio.us
  • Facebook
  • Reddit
  • Twitter
Tagged: , ,
<?php
 
if (! isset($argv[1])) {
    echo "enter a search term:\n";
    echo 'php ' . __FILE__ . " <search_term>\n";
    exit;
}
 
$term = urlencode($argv[1]);
$url = "http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStoreServices.woa/wa/wsSearch?limit=10&entity=software&term=$term";
$json = file_get_contents($url);
 
print_r(json_decode($json, true));
 
?>

More info here.

  • Digg
  • del.icio.us
  • Facebook
  • Reddit
  • Twitter
Tagged: , ,

I stumbled upon Paul William’s plugin for embedding a Gist into a Wordpress blog.

Its a quick ‘n clean plugin but it relies on a JS <script> tag to render the Gist’s content… so, I made a quick tweak to get the plugin to actually put the Gist’s content into your HTML source. There may already be something similar but, eh, it was just a quick hackjob.

The plugin’s code and instructions for installation & usage are in the Gist below.

<?php
/*
Plugin Name: Gistson - Embedded Gist WP Plugin
Plugin URI: http://arin.me/blog/tag/gistson
Description: Use a shortcode [gist id="12345"] to embed A Gist from http://gist.github.com into your blog
Version: 0.1
Author: Arin Sarkissian
Author URI: http://arin.me
 
Copyright 2009 Arin Sarkissian
 
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
 
/*
CREDIT:
Heavily based on Paul William's plugin:
http://www.entropytheblog.com/blog/
http://www.entropytheblog.com/blog/2008/12/wordpress-github-gist-shortcode-plugin/
Main difference is that this version doesn't do a JS, <script>, embed... the code from your gist is
actually in the HTML source.
 
INSTALL:
Toss the gistson.php file into your blogs wp-content/plugins folder. Login to WP and enable the plugin.
 
USE:
Put this <LINK> tag in <HEAD> of header.php
<link rel="stylesheet" href="http://gist.github.com/stylesheets/gist/embed.css"/>
When you wanna embed a gist just type in:
[gist id="gist-id-from-gist.github.com-here"]
example:
[gist id="250709"]
You can exclude the attribution by doing this:
[gist id="250709" nometa="true"]
This is useful for when you have multiple gists. But for big chunks of code etc
I'd encourge you to keep the attribution cuz those guys have a business to run
*/
 
function gist_shortcode_func($atts, $content = null) {
$url = 'http://gist.github.com/' . trim($atts['id']) . '.json';
$json = file_get_contents($url);
$assoc = json_decode($json, true);
 
if (isset($atts['nometa'])) {
        // you'll end up with 2 1px borders at the bottom =(
$assoc['div'] = preg_replace('/<div class="gist\-meta">.*?(<\/div>)/is', '', $assoc['div']);
}
 
return $assoc['div'];
 
}
add_shortcode('gist', 'gist_shortcode_func');
 
?>
view raw Gistson.php This Gist brought to you by GitHub.

Oh ya – I named it Gistson ’cause it grabs the Gist’s data via an HTTP GET to a JSON doc. Ya, I know, not too creative.

  • Digg
  • del.icio.us
  • Facebook
  • Reddit
  • Twitter
Tagged: , , ,

Ever since I started doing Flex development my installation of Eclipse has been pretty pissed off. I guess having a big ass PHP project and a Flex project open at the same time is a recipe for disaster.

Eclipse Out of Memory Internal Error

I’ve change the Eclipse ini file to allot more memory but that still doesn’t do the trick. My MacBook’s one of the earlier ones so I can’t expand beyond 2GB of RAM either :(

I guess I’m gonna have to get Jeff to pony up and buy me a new machine. Getting a new machine is 1/2 way exciting 1/2 annoying though but it’s easily better than having my IDE crap out 3 or 4 times a day.

  • Digg
  • del.icio.us
  • Facebook
  • Reddit
  • Twitter
Tagged: , ,
April 28th, 2008

Weird PHP/Flash/Flex Problem

The last little while I’ve been writing a new MP3 player for Fuzz using Adobe’s Flex Framework.

OK – first off… before I discuss the problem I gotta say Flex is pretty awesome. I’ve tried Flash before and have always been confused by the whole thing. Timelines, drawing tools & all the other designer-oriented stuff just stumped me. I’m a programmer so getting my head around Flash was just not happening… I don’t think that way. But Flex is fucking cool. The prologue of O’Reilly’s Programming Flex 2 pretty much addressed all my frustration with Flash and explained why I’d like Flex… Well, they’re right. It’s just like writing an app. You get handlers, listeners and all that crap that programmers understand and there’s no timeline or Illustrator-style, insane pen tool. It reminds me a lot of Swing and other shit like that.

Anyways… ya, Flex is cool. But, let’s get to the problem…

Here’s the problem:

The MP3 player streams songs off Fuzz, obviously. Also, downloading a song is quicker than listening to it real time. So a song downloads in 20 seconds, but takes 3 minutes to play… makes sense right. OK but here’s what was going on:

If you hit “play” on the player the song starts downloading and once it has enough it’ll start streaming. But, if you click on a link before the DL finished the request for the new page would not register until then download was complete.

A different manifestation of the same issue comes up when you have 2 browser windows open. Window 1 is playing music while you use window 2 to cruise around the rest of the site. Again, same problem. If you clicked on a link in window two to navigate to a new page nothing would happen until window 1 finished downloading the current song.

Fucked up huh?

I started fucking with every imaginable header in the proxy which serves up the song. Nothing seemed to work. Keep-Alive etc… I tried everything. I started tweaking Apache settings to no avail. Totally had me stumped.

I finally figured it out; The solution was to close the PHP session in the proxy before the code started outputting the MP3’s contents. Why’s this fix things? No idea but it works and I’ll look into “why” tonight at home.

A partial fix was to ignore_user_abort. In the single window case this worked fine but not in the 2 window situation.

Anywho… If anyone’s streaming MP3’s via PHP and wonders why links don’t respond just close your PHP session before spitting out the binary. I’ll check into why this is an issue and update when I understand the technicals.

  • Digg
  • del.icio.us
  • Facebook
  • Reddit
  • Twitter
Tagged: , ,

Search This Blog