An Evening with Vincent Laforet – NYC Talk

Earlier this month, I attended a talk at Adorama in NYC that provided great context to Vincent Laforet‘s career. As a photographer who saw the writing on the wall, Vincent transitioned his career from still photography into motion picture.

IMG_7559

Vincent’s presentation was statistics heavy and illustrated the amount of content (image and video) that gets produced every day. The future holds even more consumer made content that reduces the role of big media as tastemakers. Also, viewer attention span keeps going down over time.

When Reverie came out in 2008, I assumed that Canon reached out to him to produce it. The reality is that Vincent was at Canon and happened to come across the 5d mk2 prototype. Vincent was rejected several times, but Canon eventually let him borrow it. And history was made.

Vincent said that he takes most (95%) of his photos on his iPhone. As someone who shot for the NYTimes and is a Canon Explorer of Light, I’m surprised he doesn’t use his DSLRs more often. It makes sense since convenience is king, and we always have our phones.

Another point that was brought up during the talk was Constant Photography. Instead of taking THE photo at THE right moment, you could just take a film of ALL the moments and cherry pick the photo that you want. Why would you need a photographer when you have a videographer that does both?

What I got out of Vincent’s talk was that passion and being open to change are important. Vincent could have kept working as a photographer, but changing to a Director/DP was a risky move that paid off in the long run. As a new DP, he doesn’t know everything, but he is able to hire great people with complimentary skills and get it done.

Code Reuse

road

I’m reading Sandi Metz’s POODR book, and it is an invaluable resource for becoming a better OO programmer.

While many people are familiar with technical debt (the idea that you cut corners today to ship code today at the expense of the future), Sandi Metz brings up code reuse as another example of code written today that affects the quality of your future code.

From POODR, page 31:

Other programmers will reuse the methods instead of duplicating the code. They will follow the pattern you have established and create small, reusable methods in turn. This coding style propagates itself.

While you should always be striving to write good code, code reuse means that your code sets an example for future developers working in your code base. Your code today acts as a guidebook for future visitors showing what is acceptable practice. Your code patterns today will propagate throughout your future code base.

What does this mean? Well, Captain Obvious, it means the code you write today will impact your future codebase in countless ways.

Github All the Things

socialite by cameronmcefee (http://octodex.github.com/socialite/)

socialite by cameronmcefee (http://octodex.github.com/socialite/)

As Github continues to take over the world, I wanted to share a couple creative ways Pull Requests have been used.

  1. Job Offer

    In January, Carrot Creative made a job offer via a pull request. When I found out about this at Flatiron School, I was ecstatic for Adam Jonas, and I was wondering if this was Carrot’s standard way giving job offers. Apparently, it’s not standard practice, but it’s awesome nonetheless.

  2. RSVP

    Gust is having a Ruby concurrency talk with José Valim in March. Their RSVP page had a simple message: send us a pull request.

    The instructions were super easy for any developer. Fork their repo, create a file with your contact info, and send them a pull request.

While these could have been handled by e-mail or one of the many channels of communication, using Github to do it is simply cool.

Setting up Sidekiq and Redis on VPS

Sidekiq is useful for background Rails workers. Instead of tying up your rails server, it’s better to handle certain processes asynchronously.

Setting up Sidekiq on your VPS is simple.

Install Redis

With a Linode guide, install Redis. Make sure to use the latest Redis version (2.6.9 for me) instead of 2.2.2 as the guide discusses. Run [cci]sudo make test[/cci] to test your Redis setup. If all goes well, you should see [cci]\o/ All tests passed without errors![/cci]. The Linode guide will also go over the Deploy Init Script so that your Redis starts up when you reboot your VPS.

Config Capistrano File

With Capistrano, you need to edit your [cci]config/deploy.rb[/cci] to include the line [cci]require ‘sidekiq/capistrano'[/cci]. [cci]For config/sidekiq.yml[/cci] options, see the Sidekiq wiki.

That’s it. Make sure to have your server rebooted so that Redis is running on the default port, and Sidekiq can access Redis upon your [cci]cap deploy[/cci].

How jQuery text method works in JavaScript

jQuery has a popular method for accessing the text contents of a targeted DOM node named [cci].text()[/cci]. It allows you to either get the text content or set the text content.

For example, using [cci]text()[/cci] on [cci]$(‘#test’)[/cci] returns [cci]foobar[/cci] below:
[cc]

foobar

$(‘#test’).text(); //=> returns ‘foobar’
[/cc]

By using [cci]text()[/cci] with a parameter, you can set the text to the passed in parameter.
[cc]

foobar

$(‘#test’).text(“cupcakes”); // sets the value to ‘cupcakes’
$(‘#test’).text(); //=> returns ‘cupcakes’
[/cc]

There is a similar jQuery method named [cci]html()[/cci]. It returns the html contents within the targeted DOM node.
[cc]

foobar

$(‘#test’).html(); //=> returns ‘foobar
[/cc]

Before we dive into the jQuery source, we can guess that jQuery uses textContent.

In the jQuery source, you can see on line 5702:
[cc]
jQuery.fn.extend({
text: function( value ) {
return jQuery.access( this, function( value ) {
return value === undefined ?
jQuery.text( this ) :
this.empty().append( ( this[0] && this[0].ownerDocument || document ).createTextNode( value ) );
}, null, value, arguments.length );
},
[/cc]

jQuery checks the passed in parameter [cci]value[/cci] using a ternary operator. If [cci]value[/cci] is [cci]undefined[/cci], then jQuery calls [cci]text[/cci]. Else, jQuery sets the value with [cci]createTextNode[/cci].

[cci]jQuery.access[/cci] on line 784 allows jQuery to determine if the value is defined or undefined.

If undefined, [cci]jQuery.text( this )[/cci] runs.

[cc]
// line 5348:
jQuery.text = Sizzle.getText;
[/cc]

Sizzle’s getText looks like:

[cc]
/**
* Utility function for retrieving the text value of an array of DOM nodes
* @param {Array|Element} elem
*/
getText = Sizzle.getText = function( elem ) {
var node,
ret = “”,
i = 0,
nodeType = elem.nodeType;

if ( nodeType ) {
if ( nodeType === 1 || nodeType === 9 || nodeType === 11 ) {
// Use textContent for elements
// innerText usage removed for consistency of new lines (see #11153)
if ( typeof elem.textContent === “string” ) {
return elem.textContent;
} else {
// Traverse its children
for ( elem = elem.firstChild; elem; elem = elem.nextSibling ) {
ret += getText( elem );
}
}
} else if ( nodeType === 3 || nodeType === 4 ) {
return elem.nodeValue;
}
// Do not include comment or processing instruction nodes
} else {

// If no nodeType, this is expected to be an array
for ( ; (node = elem[i]); i++ ) {
// Do not traverse comment nodes
ret += getText( node );
}
}
return ret;
};
[/cc]

You can see that jQuery either returns the string with [cci]textContent[/cci]

[cc]
// Use textContent for elements
// innerText usage removed for consistency of new lines (see #11153)
if ( typeof elem.textContent === “string” ) {
return elem.textContent;
}
[/cc]

or jQuery will traverse the DOM and append them into the [cci]ret[/cci] variable (initialized as as empty string) to return.

[cc]
// Traverse its children
for ( elem = elem.firstChild; elem; elem = elem.nextSibling ) {
ret += getText( elem );
}
[/cc]

If you are passing in a param value into [cci]text()[/cci], then jQuery will empty out the contents of the targeted DOM node and use createTextNode to allow your passed in param to show up.

[cc]
this.empty().append( ( this[0] && this[0].ownerDocument || document ).createTextNode( value ) );
[/cc]

If you are interested in using Core JavaScript to replicate jQuery’s [cci]text()[/cci], you could try the following:

[cc]

lion

var div = document.querySelectorAll(“#test”);
div[0].textContent; //=> returns ‘lion’
[/cc]

This targets the [cci]#test[/cci] div and get the text content.

[cc]

var div = document.querySelectorAll(“#test”);
div[0].appendChild(document.createTextNode(‘pizza’));
[/cc]

This is one way to set the div contents to display ‘pizza’.

Thanks for tuning into to another jQuery without jQuery. Diving into the source code is great for helping you understand how it all works.

jQuery without jQuery is a series that aims to open up the jQuery black box. jQuery is just JavaScript, so you should feel comfortable working at lower levels of abstraction and looking at the jQuery source code written in JavaScript.

jQuery DOM ready handlers

As you work with JavaScript & jQuery, you will quickly get accustomed to wrapping your JS scripts with a DOM ready check. You generally want to do this, because you don’t want your DOM-manipulating JS to run before the DOM is fully loaded.

The standard jQuery ready function is:

[cc]
$(document).ready(function(){
// Your code here
});
[/cc]

If you’ve seen the jQuery ready shortcut, you can also use this (equivalent to the above):

[cc]
$(function() {
// Your code here
});
[/cc]

How does this work? Diving into jQuery source, you can see on line 173 that jQuery checks if what is passed in is a function and applies [cci]ready[/cci] to it:

[cc]
// HANDLE: $(function)
// Shortcut for document ready
} else if ( jQuery.isFunction( selector ) ) {
return rootjQuery.ready( selector );
}
[/cc]

Also, I want to show how you could wrap your JS code without jQuery. Traditionally, it looks like this:

[cc]
window.onload = function(){
// Your code here
}
[/cc]

Note the traditional way using [cci]window.onload[/cci] will happen later than jQuery’s [cci]ready[/cci] event. Code snippets to run your JS upon DOM ready without jQuery are available online.

Finally, if you want to wait for both the DOM and complete page (including all frames, objects and images) to be loaded using jQuery, you can use:

[cc]
$(window).load(function() {
// Your code here
});
[/cc]

Running JS with jQuery upon DOM ready is simple. Using the jQuery ready shortcut is easy and works well.

jQuery without jQuery is a series that aims to open up the jQuery black box. jQuery is just JavaScript, so you should feel comfortable working at lower levels of abstraction and looking at the jQuery source code written in JavaScript.

How jQuery CSS selector works

Diving into jQuery source code, you’ll find attribution for [cci]Sizzle.js[/cci] near the top. What is Sizzle? Sizzle is jQuery’s spinoff CSS selector engine.

jQuery is optimized for easy DOM manipulation and allows method chaining by returning the jQuery object. This means that the [cci]jQuery[/cci] (or [cci]$[/cci] shorthand syntax) allows you to target DOM elements with CSS syntax.

You can select a paragraph with the id “dino” in jQuery:

[cc]$(‘p#dino’)[/cc]

Pretty easy, right? How does jQuery do this?

BigBinary has a great article walking through the process:

  • If querySelectorAll works, use querySelectorAll.
  • Else, use Sizzle.

To use [cci]querySelectorAll[/cci] instead of jQuery, you could use:

[cc]document.querySelectorAll(‘p#dino’)[/cc]

This goes to show that anything jQuery can do, core JavaScript can do (since jQuery is JavaScript). With that said, I would recommend jQuery over writing your own JavaScript from scratch as jQuery is highly tested for edge cases and offers amazing cross browser support.

jQuery without jQuery is a series that aims to open up the jQuery black box. jQuery is just JavaScript, so you should feel comfortable working at lower levels of abstraction and looking at the jQuery source code written in JavaScript.

Javascript While Loop Decrement Explained

While watching Paul Irish : 10 Things I Learned from the jQuery Source, his code at the 31:30 mark looks like similar to this:

[cc]
var times = [42, 28, 75, 50, 62]
times = times[Math.floor(Math.random()*times.length)]

while (–times) {
do_something();
}
[/cc]

Not accustomed to seeing [cci]–[/cci], I wanted to research and explain what this snippet does. Paul’s code picks a random number in the [cci]times[/cci] array and runs [cci]do_something()[/cci] while [cci]times[/cci] is greater than 0.

The line by line breakdown looks like this:

[cc]
var times = [42, 28, 75, 50, 62]
[/cc]
Set the variable [cci]times[/cci] equal to an array of integers.
[cc]
times = times[Math.floor(Math.random()*times.length)]
[/cc]
Set [cci]times[/cci] equal to an integer chosen at random. [cci]Math.floor()[/cci] rounds down the integer. [cci]Math.random()[/cci] returns a random value between 0 and 1. [cci]times.length[/cci] returns the number of items in the [cci]times[/cci] array.
[cc]
while (–times) {
[/cc]
[cci]–times[/cci] runs the while loop while times is greater than zero. For instance, if times was 75, you can see that the while loop runs for the numbers 74 to 1, inclusive. Try it out in the JSFiddle.
[cc]
do_something();
[/cc]
[cci]do_something();[/cci] runs the function defined elsewhere.

Paul’s screencast is helpful, and diving into jQuery source is tremendously helpful.