Polymorphism

2011-11-24 20:22:12

Of course my first reaction to the simple question regarding Polymorphism was stunned silence.  I hadn’t thought about that word more than a couple times since college.  Hell, even then I probably wasn’t completely sure what it meant (solid use of that education, Joe).  However I knew it was important.

With some dumb luck, I was able to talk about.. Well.. Something.  Not totally sure if it made sense whatsoever but I was able to talk about what I thought it meant and how I saw it in practice.

During my personal follow up, my first inclination was to check the almighty Wikipedia and what they had for Polymorphism,

The primary usage of polymorphism in industry (object-oriented programming theory) is the ability of objects belonging to different types to respond to method,field, or property calls of the same name, each one according to an appropriate type-specific behavior. The programmer (and the program) does not have to know the exact type of the object in advance, and so the exact behavior is determined at run-time (this is called late binding or dynamic binding).

- source

Hmm.. Ok, I know I’ve seen that before.

Actually, it was something that had baffled the heck out of me from earlier learning about PHP.  I had learned in C how one could define two similarly named functions where one would take a different number of arguments than the other.  So depending on how many arguments are passed, the particular function would be used.

Well isnt possible with PHP due to the fact that it allows argument overloading.  However with the use of Type Hinting it is possible to achieve Method Polymorphism.

The example given at the Geek Files is pretty helpful in understanding a few principles all at once.

class BaseClass {
   public function myMethod() {
      echo "BaseClass method called";
   }
}
 
class DerivedClass extends BaseClass {
   public function myMethod() {
      echo "DerivedClass method called";
   }
}
 
function processClass(BaseClass $c) {
   $c->myMethod();
}
 
$c = new DerivedClass();
processClass($c);

You can follow the jump above or follow my crude interpretation:

The object $c is executed with processClass() and while the function is defined with an argument of BaseClass since $c is of DerivedClass, that public function myMethod() will be called and “DerivedClass method called” will be echoed.  I highly recommend checking out the link to Geek Files, it’s really well written and the examples are excellent.

Something mentioned a few times over was “When the decision to invoke a function call is made by inspecting the object at runtime it is called Polymorphism.”

Within my CodeIgniter-based SiteApp, I have a utility_model that contains many of my most used functions as well as several key CRUD functions - so I tend to extend this model with all my custom made ones.  In doing so, I tend to ‘overwrite’ many of the utility_model functions with ones located inside the custom model.  Not all of the utility_model functions are repeated and are still accessible in its base form.  For example, I have my utility_model function to get all rows from a table:

public function get_all($table) {
    $this->db->order_by('id','asc');
    $query = $this->db->get($table);
    return $query->result();
}

And within my custom model:

public function get_all() {
    return parent::get_all('media');
}

Now, even if I call the function with $this->custom_model->get_all(), only the new custom function will call and get all rows from the media table.

Giving some attention to the neglected

2011-11-23 06:21:03

I recently was on a phone interview where I was asked a series of questions regarding some fairly standard programming terms.  In all honesty, I personally believe I did not do very well at all.  It’s my own mistake, I haven’t even really thought of them since college (6 years and counting..).

I decided to write down each term that I had a hard time talking about, whether it was the definition to how it could appear in practice.  I looked up each term/question and began taking some notes.

My next move was to look as to how each of these terms may fit into the programming I do.  To help not only clog up the internets with more data, but to help myself with future discussions, I’m going to post each of the questions and go into what they mean and how I have personally run into each through personal or business projects.

First up: Polymorphism and PHP.

Drag and Drop uploader with CodeIgniter

2011-11-07 08:58:23

I’ve spent a little bit of time here and there trying to get a jQuery powered drag n drop uploader working well with the Upload library within CodeIgniter.  Most of the fumbling has been due to my own misunderstanding of what goes on in the background.

Fortunately, I’ve overcome that obstacle.

I cloned abailiss’ jQuery-drag-drop plugin from Github, and placed the appropriate files in their locations (js file into my /assets/js/ folder and the view in /views/).  Within the Controller function which is pointed at by the uploadUrl that is set in the .dropUpload() instantiation, I placed:

  $config['upload_path'] = './uploads/';
  $config['allowed_types'] = 'gif|jpg|png|jpeg|pdf|doc';
  $config['max_size'] = '242048';
  $config['max_width']  = '0';
  $config['max_height']  = '0';
  $config['remove_spaces'] = 'TRUE';
  $this->upload->initialize($config);
  
  foreach($_FILES as $k => $f):
   $this->upload->do_upload($k);
  endforeach;

I’ll be adding a line to pass the data to a model for Database saving - but that worked beautifully for me for uploading multiple files via drag and drop.

First Pull Request

2011-11-04 21:52:36

I submitted my first Pull Request (Link), and it was accepted.  Super sweet.

It wasn’t anything too special, but it was fun to do, I have a lot of people to thank.

For those interested, the snippets of code that changed were:

module.exports = (robot) ->
  robot.respond /(hipster|clown|scumbag|rohan|jason)( me)? (.*)/i, (msg) ->
    type = msg.match[1]
    imagery = msg.match[3]

    if imagery.match /^https?:\/\//i
      msg.send "http://faceup.me/img?overlay=#{type}&src;=#{imagery}"
    else
      imageMe msg, imagery, (url) ->
        msg.send "http://faceup.me/img?overlay=#{type}&src;=#{url}"

imageMe = (msg, query, cb) ->
  msg.http('http://ajax.googleapis.com/ajax/services/search/images')
    .query(v: "1.0", rsz: '8', q: query)
    .get() (err, res, body) ->
      images = JSON.parse(body)
      images = images.responseData.results
      image  = msg.random images
      cb "#{image.unescapedUrl}"

I threw in the if statement to determine whether an url was given for a direct image link or a string to be queried.  If it turns out to be a query string, pass it to imageMe, slam it into the google image search, and give back a random result.  Fun stuff!

Trying out some new stuff

2011-11-04 21:11:16

Specifically speaking, the awesome new toy that is Hubot.  It’s Github’s open source bot.

It takes a bit to get working, the instructions are fairly straight forward, however if you have issues, you’ll probably have them because of permission conflicts.  I hit a complete brick wall until a buddy introduced me to Dotfiles authored by Zach Holman.  The kid is good, real good.  Anyway, check out Dotfiles, follow the fairly simple instructions, get it installed and switch to the slick and clean zsh (I call zosh.0 ... yeah, that’s right).  You won’t regret it.  The default theme kinda blows (blue directory names? dear lord), but after some simple early tweaks if you choose, consolidating everything and dealing with each zsh file is nice and orderly.

Local development is fairly straight-forward, however what really was the great step forward was the understanding of Heroku, a remote deployment cloud.  Again introduced by Ben, got me going with my custom hubot, edbot working within my campfire chatroom.  Played around with some Coffeescript (another damn language), but it’s been pretty fun to learn.  I can’t tell if it’s a clean language quite yet, but it hasn’t had near the swear:line ratio that Objective C (unfair comparison, really), JavaScript, or PHP has caused.

Of course, I’m not really doing anything that far-fetched.  I’m sure if I’ll give it time I’ll ‘appreciate’ it just as much the others.

Hopefully I’ve thrown enough links over to get peoples going.  Here’s a purty good one giving quite a good description of how to get yourself started on Heroku with your Hubot:
Installing Hubot on Heroku

Some other cool extras:
Hubot Scripts

..ok I lied, I don’t have any other links right now..

CodeIgniter and Twitter

2011-10-24 14:39:30

I stumbled upon a really neat CodeIgniter Library in order utilize the Twitter API (Github).  So far I’ve did some REST calls tests, and after some major frustration due to the fact that I believe there is a slight bug with the $_apiUrl within the Tweets Library file.

$response = $this->_httpRequest(strtoupper($method), $this->_apiUrl.'/'.$this->_version[0].'/'.$path.'.json', $args);

The problem was the lack of version number within the url string.  I threw in $this->_versionl[0].’/’ to fix the issue.

Just a little fyi that may help someone in the future.

So the day finally came (and went for some...)

2011-09-30 18:38:01

Well, first of all, I’m super psyched that I grabbed the Gingerbread update while it was up.  Apparently there’s some bug with the voicemail task tray icon where it doesn’t disappear even if you have zero voicemails.

Easily remedied by a simple restart of the phone (which is healthy to do every so often anyway).

Abso-freakin-lutely worth the update that has increased my battery life and fixed a whole slew of UI bugs that were slowing down the phone recently.  I was beginning to become extremely annoyed with the Sense UI and its constant crashing and all-around non-responsive self.  Now the whole experience has been tightened up.  Fonts are smaller and sharper, icons are sharper as well, there is more functionality with the Activity Tray, the Application Tray is paged and has a cool little selection bar at the bottom..  I’m still learning about the new and updated features, but it’s been a fun adventure so far.

Typing all of that really made me feel bad for all of those HTC Thunderbolt owners that didn’t get the update before it was removed for the patch.

Future Ideas

2011-09-22 08:27:34

There are just so, so many different coding libraries and frameworks out there.

And I want to try them all.

I’ve just started getting my feet wet with Backbone.js, but am itching for so much more.  Knockout.js looks fairly fun to try out, especially given the real cool tutorial experience they offer.  At first I figured it would simply be another approach to MVC (simply speaking, a Backbone.js clone), however a rather informative post over at Stackoverflow.com offered a pretty cool breakdown of both Frameworks.  Follow the link for the discussion of Knockout.js vs. Backbone.js.

If I really feel ambitious, I may give melon.js a try.  It looks like I’d also need to check out the Tiled Map Editor as well.  So that’s why maybe I’ll push it for a crazy stormy day during this winter season.

I guess the biggest thing would be to upgrade my Ball Game to work with Backbone.js.  After that, maybe give it the ability to drag and drop - which would really give some much needed user feedback as to how the hell to play the freakin game.  I’ve learned the hard way that unless I explicitly show people how to play, most have no clue how the game works.

Well, that and get some of the crap from my old posterous account over here..  Or link through their API.  You know, if I got any spare time.

Hurry up and wait

2011-09-20 09:58:25

Soooo…..

Am I ever going to get the Gingerbread update for my HTC Thunderbolt?

Articles left and right have been saying that September is the month for 2.3, however we’re already two thirds of the way through.

Anytime now, HTC.  Anytime.

Edit: Gizmocrave.com has an included image that shows Verizon advertising the HTC Thunderbolt with Android OS version 2.3.

My Backbone.js Project

2011-09-20 09:33:41

I’ve begun working on a really cool Backbone.js Project (if you didn’t already see the title).  It serves as a personally branded Google Maps Interface.

One would use the backend to input all the various locations and the backbone.js structured frontend to view everything.  I hope to have a working version on here by the weekend, and perhaps in a week or two a version to throw up onto github!

I have to admit, Backbone is pretty fun to work with.