Friday, December 17, 2010

OCD in CS

This man is brilliant. Brilliant because he is able to take computer science topics and turn them into comics...funny ones at that.



(alt text: "Not only is that terrible in general, but you just KNOW Billy's going to open the root present first, and then everyone will have to wait while the heap is rebuilt.")

His latest was gold, as always, but my OCD in CS wants me to edit his tree very badly! IF I had it my way, I'd whip that tree up in an AVL fashion. Everyone loves a self-balancing tree?! [and yes, opening presents with starting at the root would be quite desastrious]

Monday, October 18, 2010

Javascript patterns and antipatterns

Original post here



This most likely is just the first in a series of posts on the matter. Let me preface this post with the thought that I've come across quite a bit of code in my professional life. When I was a bit younger and happened to come across a piece of code that I knew was horrible, I'd was for the most part apathetic. But since being heavily involved in core Javascript for the past year from writing plug-ins to playing around with server-side JS and everything in-between, I've come across not only bad legacy JS, but dangerous JS that really does more damage than good. I guess this is to be expected because JS is so easy to get something going without know what is being done.

I admit that starting out I too fell victim to common bad practices, or antipatterns. It was only natural that I learned from reading someone else's solution to a problem without going into detail of what the language was actually doing; I wasn't concerned with that at the time. This is no longer the case since JS has gain tremendous traction in the development role. In order to have super premium code that will preform the way that it is expected to without doing harm to others code is to: seriously know the language inside and out along with adhering to certain design patterns that will not only be optimized, but safe, clean and easy to read.

Here is my little attempt to show little snippets that I've learn along the way that are essential for me to be 100% confident in my code.

One of the most common antipatterns that I've come across is the overuse of global variables. Yes, they are quick and easy but also potentially dangerous, not to mention annoying. But aside from my personal annoyance at the sloppy coding, JS does have some bugs within the language can have unforeseen consequences on the sloppy code. Along with the overuse of global variables, are sometimes the way the variables are defined.


// global namespace
myVar = 5; //antipattern
var myVar = 5;


Both of these statements are valid, and for the most part they will do what the author expects them to do, but they are different. The variable lacking the 'var' is called an implied global and the other is an explicitly defined variable.


// antipattern
function MyFunction( args1 ) {
myVar = 5;
if ( args1 === myVar ) {
return true;
}
return false;
}


This function is easy enough, it checks the parameter passed in with the author's intended local variable 'myVar'. Since myVar in this example is defined without 'var', it is an implied global. What author might not have intended is now 'myVar' has been added to the global namespace which can cause problems if there are variables already defined with the same name.

Corrected:

// correct pattern
function MyFunction( args1 ) {
var myVar = 5; //corrected
if ( args1 === myVar ) {
return true;
}
return false;
}


Another different between implied globals and explicit ones, or ones defined with 'var' is the ability to delete the variable if wished. It is safe to say if a global variable is necessary it most likely shouldn't be deleted and be available through the program's life-cycle. Variable that are implied globals can be deleted and explicit cannot. Seeing why this is a problem requires you to take a step back and see the true purpose of a global variable. Implied globals can be deleted which means that they are properties or elements of the global object. This is undesired for the purpose of truly using a global variable. Lastly, since we are working with Javascript and not the DOM 100% of the time, adding elements or global variables to the window object on DOM is ill-advised. To have your program less prone to bugs while still relying on using global variables no matter what environment you may be working in, create your own global object.


var globalObj = (function () {
return this;
}() );

Thursday, August 26, 2010

unix commands

Want to know the size of a directory or directories?

du -sh [directory/directories]

"du" = disk usage, -sh = "sum the disk usage of the directories and make it human readable"

ex: du -sh /var/www

(though I probably wouldn't recommend running the command due to the size of the "www" directory)

Friday, August 20, 2010

Future Development

Being a developer is an interesting as well as an extremely frustrating job. I could work two weeks on a project with an additional two or three days debugging for my five second pay-off. As crazy as that might sound, I could not imagine doing anything else (except for maybe music or art....or philosophy...or even cultural studies).

Being a programmer, and more specifically web developer, has it's share of interesting problems that I couldn't find in any other field. This is the only field that I've come across that has new paradigms, revolutions, and shifts every couple of years if not faster. This requires an extensive amount of reading and research to stay at the top of the field consistently. Due to the reinventing nature of the field, traps and pit-falls can easily happen to the best of us. Either reading too much, which only allows for a theoretical approach without implementation or experience to reading too little which puts one behind the rest of their peers is a hard thing to balance. When reading the latest and greatest technology out, how does one know whether invest time into learning or simply just skip over? I struggle with this constantly.

By trade, I'm a PHP developer. The platform that I build on is an ever-changing, fast-paced one. But there is one thing that seems to make itself extremely obvious in the future of my field: javascript. Yes, this may sound like old news to some because javascript has been out since the mid '90s, but with the trend of where the Web is going, only recently javascript has started to make huge leaps and bounds ahead of anything as a staple of future web development. Tools such as jQuery (yes, also been around since the mid 2000), node.js, and many others has A) brought development with javascript to an extremely high level, B) development possible on the front end as well as the back (yes, all ECMAscripts are agnostic in theory, but the practice of such was rare) and C) it has branched out to mobile development such as pure scripting game engines, jqtouch, phone gap, and many others.

I've always been decent enough in javascript to get done what needed to get done, but lately I've been spending the majority of my time learning the language's popular design patterns and best practices. There is a wealth of information out there to turn a novice into an experienced developer. I've been spending some serious time developing jQuery plugins because I truly believe that my work will last longer in javascript than any other technology at the moment. It is a gamble, but I think it is a safe gamble.

Friday, June 18, 2010

LiteFramePHP

is now out...

LiteFramePHP

This is a small MVC, PHP framework that is built out of a single object. It may be small but it certainly has many features that you'd find in a full-fledged MVC framework such as CakePHP.

The site hosting the files was quickly thrown up, so there still is a lot to be done. Mainly, the documentation!

More to come...

Wednesday, June 9, 2010

Open Sourcing

I admittedly look at the Open Source movement as one that parallels a spirtual or NGO cause. At the core of it, there isn't allure of money that drives people to work hard and then to share their ideas openly. Something about that tells me that the movement as a whole is greater than the sum of its parts, and this is shown every day with more and more code that is available for use. I owe an incalculable amount of my development and knowledge to this movement, and I didn't have pay a penny if I so choose not to. I've probably learned more through Open Source on development than my time in college.

It is time for me to become one of the parts of this movement. I want to attempt to give back freely what I have taken away in hopes to help out others; at the very least to support the movement. I must admit that I am quite nervous though, I am exposing my ideas and code which I usually am very protective of. They will be open to ridicule for those that choose to use my code.

I've been working on two different frameworks with a buddy at work that includes quite an extensive amount of different modules. To give an idea, we probably have roughly around 8-10k lines of code. First, a php, super lightweight framework. Yes, I realize that the world might not need yet another php framework, but after using MVC frameworks such as Cake and Zend I was blown away at the size of the framework that was needed to get up and running on a new project. The challenge was to keep this framework as light as possible while sill providing all of the necessary functionality. I think that we did a pretty good job. The time that it took to develop probably equaled the time that we talked it over and this is usually a good thing. Hopefully that'll be an indication of the quality.

Next, a series of plug-ins that are piggy-backing off of jQuery. This is probably what I'm most proud of. I believe the code quality to be fairly strong. We are using some fairly new, a bit advanced, js code development to keep the code super clean, flexible, and any other buzz word that you choose to insert. It has always been a challenge to develop javascript with the same mindset of true OOP, not just one or two objects that might have a private function/variable that is thrown in there for the novelty of it.

Some of the next major steps will be documentation. If I hope others to use, I need to provide information. At any rate, I hope to release both of these frameworks by the end of the week for those that don't need documentation.

Here is to exposing my brain. Cheers.

Monday, June 7, 2010

Web Hooks

I've been doing a lot of API integration work lately. Mainly my days have been spent trying to master Google's Adwords, Yahoo's Search Marketing, and Microsoft's Bing. On top of that, a large chunk of my time has also been involved in Mail Chimp and Paypal's APIs. Dealing with these different APIs requires an incredible amount of reading which in turn requires a decent amount of time. The purpose in dealing with all of these APIs is to automate processes which allows for incredible scale with zero overhead (other than the guy writing the software).

I am able to trigger events that these 3rd party companies have if a change comes through our system. I am limited in having these APIs, which have no idea about each other, communicate with each other based on an event that *they* might receive. Confusing?!

An example of this could be the event of a user unsubscribing from our mail list through Mail Chimp, which handles all of our mailings. Since a user unsubscribes from Mail Chimp, I have it such that that event would trigger Mail Chimp to send me the information of that event (e.g. user's name, why they unsubscribed, date they unsubscribed, etc) to a URL that I have set up (like an event listener) that processes these 3rd party events to update our system appropriately. So in this example, when I get information on Mail Chimp's event, I might change the value of the user in our database to let us know that they are off our mailing list. These are called "web hooks".

This is great. I am now processing information based on events that are outside of my system. Now you can start using your imagination on having 3rd party events starting to interact with other 3rd party systems. Continuing with the previous example, we can take the unsubscribed user that we got information on from Mail Chimp and check our database for their action based on Adwords and then update our Adwords automatically to reflect the change in our system based on the unsubscribed user. Now I have events from one system impacting how I treat another one, all automatically.

APIs were a huge step in allowing for automation, but they can only take you so far. APIs are only half of the puzzle. Web Hooks are the other half. Now APIs can be connected together through custom HTTP Post callbacks. This idea isn't new, but I'm starting to see it gain sizable traction in the web. Hopefully developers developing new APIs will keep web hooks in mind when building.

Monday, March 1, 2010

chainable generic javascript objects

yeah...is a mouth full but it is insanely good stuff.



function GenericObj(){
var ChainableObject = function(){ return new ChainableObject.fn.init();};
ChainableObject.fn = ChainableObject.protoype = {};
ChainableObject.fn.init = function(){};
ChainableObject.fn.init.prototype = ChainableObject.fn;
return ChainableObject;
}



My buddy and co-worker Mahdi (don't worry, the site has been under construction forever...i just needed somewhere to link) are currently working on a jQuery toolkit that will be module based. jQuery ToolKit (yeah, that too is under construction). This little nugget is in the toolkit and more gold like this you can expect to find.

Friday, January 29, 2010

piano

i would very much enjoy playing concerts for a living...sigh...