raganwald
(This is a snapshot of my old weblog. New posts and selected republished essays can be found at raganwald.com.)

Tuesday, May 09, 2006
  JavaScript Pop Quiz


In essay or point form, compare and contrast:

    thisCell.onclick = function() { set_datetime( dt_current_day.valueOf(), true); };

and
    thisCell.onclick =
function (day) {
return function() { set_datetime( day, true); };
}(dt_current_day.valueOf());

Do not limit yourself to describing the run-time behaviour as specified in the language specification. Discuss the implications of each choice in the broader context of programming and software design.

Hint 1: Consider the possibility that these statements may be executed more than once, that this_cell may refer to different objects when these statements are executed more than once, and that dt_current_day may have different values when these statements are executed more than once.

Hint 2: SICP.

Hint 3:

Hint 4: JavaScript Pop Quiz Remarks
 

Comments on “JavaScript Pop Quiz:
Variation A is likely the one I would use as it appeals to me aestheticly speaking.
Variation B is more is nice because valueOf() gets called one time (at assign time).

If you have a page that gets opened at 11:59pm that uses this code, variation A would be useful if you wanted to accurately represent the day at the time of a click; variation B would represent the date of the instantiation of the object. Both have their uses.

The efficiency of B would be more applicable to a system that wasn't time-specific. In this example, the dt_current_day.valueOf() call defines the usage.
 
Compare and contrast? BS ahead:

Since this is javascript, it runs on the client computer. Further, it only runs when the the cell is clicked and it isn't a global function for everything to use. So the performance of the function as well as the end result of the function is not in question here.

My first knee-jerk reaction to the code was "the second block is definitely the way to go, because that code is more generic and reusable." The first block has hardcoded data (the current date time) embedded in it's function defintion. So it is inherently inflexible. I cannot change the date to tomorrow, or yesterday.

Did I pass?
 
A: Click on any cell to set the date to the last day of the week.

B: Click on any cell to set the date to that day of the week.

The problem with A is that all cell functions share the same closure and the same date value, since they all reference the same object (pass by stack?).

B solves that by creating another closue with a different date reference for each cell.
 
Ok! I got it now. You should have purchased a javascript calendar. :)

I can't understand the difference between the two code snippets. However, wouldn't you write that code differently so that people like me who come in later for maintenance don't screw it up?
 
"wouldn't I write the code differently for maintenence..."

Hmmm. First, why would I hire someone as a programmer if they couldn't maintain either style of code? Doesn't that mean they don't know their job?

Second, let's say there are two kinds of programmers, call them As and Cs. If you tell the As that they have to write programs for the benefit of the Cs, why not hire the Cs to begin with?

Oh wait, that's what most companies wind up doing, because the As chafe when told to pretend to be Cs.
 




<< Home
Reg Braithwaite


Recent Writing
Homoiconic Technical Writing / raganwald.posterous.com

Books
What I‘ve Learned From Failure / Kestrels, Quirky Birds, and Hopeless Egocentricity

Share
rewrite_rails / andand / unfold.rb / string_to_proc.rb / dsl_and_let.rb / comprehension.rb / lazy_lists.rb

Beauty
IS-STRICTLY-EQUIVALENT-TO-A / Spaghetti-Western Coding / Golf is a good program spoiled / Programming conventions as signals / Not all functions should be object methods

The Not So Big Software Design / Writing programs for people to read / Why Why Functional Programming Matters Matters / But Y would I want to do a thing like this?

Work
The single most important thing you must do to improve your programming career / The Naïve Approach to Hiring People / No Disrespect / Take control of your interview / Three tips for getting a job through a recruiter / My favourite interview question

Management
Exception Handling in Software Development / What if powerful languages and idioms only work for small teams? / Bricks / Which theory fits the evidence? / Still failing, still learning / What I’ve learned from failure

Notation
The unary ampersand in Ruby / (1..100).inject(&:+) / The challenge of teaching yourself a programming language / The significance of the meta-circular interpreter / Block-Structured Javascript / Haskell, Ruby and Infinity / Closures and Higher-Order Functions

Opinion
Why Apple is more expensive than Amazon / Why we are the biggest obstacles to our own growth / Is software the documentation of business process mistakes? / We have lost control of the apparatus / What I’ve Learned From Sales I, II, III

Whimsey
The Narcissism of Small Code Differences / Billy Martin’s Technique for Managing his Manager / Three stories about The Tao / Programming Language Stories / Why You Need a Degree to Work For BigCo

History
06/04 / 07/04 / 08/04 / 09/04 / 10/04 / 11/04 / 12/04 / 01/05 / 02/05 / 03/05 / 04/05 / 06/05 / 07/05 / 08/05 / 09/05 / 10/05 / 11/05 / 01/06 / 02/06 / 03/06 / 04/06 / 05/06 / 06/06 / 07/06 / 08/06 / 09/06 / 10/06 / 11/06 / 12/06 / 01/07 / 02/07 / 03/07 / 04/07 / 05/07 / 06/07 / 07/07 / 08/07 / 09/07 / 10/07 / 11/07 / 12/07 / 01/08 / 02/08 / 03/08 / 04/08 / 05/08 / 06/08 / 07/08 /