• March 9th, 2010

    JavaScript Naming Conventions For People With Beards

    When developing in a team environment, coming to an agreed-upon set of naming conventions is incredibly important. The speed at which we as developers can come to comprehend the thought-processes and intentions of our peers greatly depends on our ability to look straight to their intentions, rather than having to first read the code line by line, mentally translating it into our own dialect, before truly understanding.

    In short, Aaron Cruz, here is a quick summary of the naming conventions which I adhere to:

    var MyClass = Class.create({
      initialize: function(arg1, arg2) {
        this.publicProperty = "'Public' property";
        this._privateProperty = "'Private' property";
        var localVariable = null;
      },
    
      publicMethod: function() {
        return "'Public' method";
      },
    
      isBooleanMethod: function() {
        return true;
      },
    
      _privateMethod: function() {
        return "'Private' method";
      },
    
      __eventHandlerMethodClick: function(e) {
        return "Click-event handler method";
      }
    });
    
    MyClass.classProperty = "Class-level property";
    
    MyClass.classMethod = function() {
      return "Class-level method";
    }
    
    MyClass.MY_CLASS_CONSTANT = "CONSTANT's should never change";
    
    MyClass.ClassEnumOrSubType = {
      value0: 0,
      value1: 1,
      value2: 2,
      value3: 4,
      value4: 8
    }
    

    While the above example uses the Prototype class structure, none of the principles themselves are Prototype-specific.

    Posted in Code/Projects, JavaScript | No Comments »

  • My Very Own GIT Manual

    August 11th, 2009

    GIT confuses the hell out of me. I’m used to Subversion and Vault, and the translation isn’t always clear. Thankfully, there’s GitHub which makes it super easy to jump into using GIT.

    Unfortunately I constantly forget how to do simple tasks, not to speak of complex ones. This post is going to serve as my very own GIT manual so I can make these translations easily and not have to remember the intricacies.

    Updating your local repositories with remote changes: git pull

    To get the latest changes from your remote repository merged into your local master, run git pull. This is pretty much the same as running git fetch then git merge origin/master. Think of this as the GIT version of svn:update or Vault’s “Get Latest”.

    git pull
    

    Pushing local changes to your remote repository

    git add 'your file name'  #do this for each file or folder with changes
    git commit -a -m 'Your commit message'
    git push origin master
    

    Rejected!

    If a push to your remote repository gets rejected, it’s probably because remote files have been changed since you last used git pull. Run git pull then git push origin/master again. With any luck you should be in business.

    Push master to origin/master

    git push origin master
    > To git@github.com:<username>/<reponame>.git
    >  ! [rejected]        master -> master (non-fast forward)
    > error: failed to push some refs to 'git@github.com:<username>/<reponame>.git'
    

    Damn! Shit’s broke! Pull again then re-try your push!

    git pull
    > Merge made by recursive.
    
    git push origin master
    > Counting objects: 10, done.
    > Compressing objects: 100% (5/5), done.
    > Writing objects: 100% (6/6), 814 bytes, done.
    > Total 6 (delta 1), reused 0 (delta 0)
    > To git@github.com:<username>/<reponame>.git
    >    65a1527..79239e9  master -> master
    

    It worked!

    Updating your fork of another user’s repository

    get fetch origin
    git fetch upstream
    git merge upstream/master
    git push origin master
    

    There’s probably an easier way to do that, but I haven’t figured out as of this writing.

    Posted in Code/Projects, git | 2 Comments »

  • Hatin’ on textContent and innerText

    June 1st, 2009

    It’s surprisingly annoying to get just the text content of an html element in a cross-browser friendly way. I wrote Element#getTextContent() to take care of this simple operation for me.

    The Prototype Version:

    Element.addMethods({
      /**
       *  Element#getTextContent(@element) -> String
       *  Cross-browser means of getting Element#textContent or Element#innerText
       **/
      getTextContent: function(element) {
        if (!Object.isUndefined(element.textContent)) {
          return element.textContent;
        }
        return element.innerText;
      }
    });
    

    The Procedural Version

    /**
     * Gets the text content of the specified element.
     * @param element {HTMLElement} The html element
     * @return {String} The string content of the specified element.
     */
    function getTextContent(element) {
      if (typeof element.textContent != "undefined") {
        return element.textContent;
      }
      return element.innerText;
    }
    

    Posted in Code/Projects, JavaScript | 6 Comments »

  • Fluent: PHP Data Types as Fluent Objects

    October 28th, 2007

    Introducing Fluent, a set of PHP 5 classes that make writing code happier. Fluent enables you to deal with data types as objects in the fluent api style, like you can in most other modern programming languages.

    // JavaScript
    "Hello There".toUpperCase().split(' ');
    
    // C#
    "Hello There".ToUpper().Split(new Char[] {' '});
    
    # Ruby
    "Hello There".upcase.split(' ')
    
    # Python
    "Hello There".upper().split()
    
    // PHP
    explode(' ', strtoupper('Hello There'));
    
    // PHP using Fluent
    S('Hello There')->toupper()->explode(' ');
    

    Read the rest of this entry »

    Posted in Code/Projects, PHP | 6 Comments »

Un-Dumbify