Debug Node .. Finally (It shall be Simpler)

Node-Inspector … https://github.com/node-inspector/node-inspector

  1. npm install -g node-inspector
  2. node –debug-brk web.js (web.js here the node script)
  3. node-inspector & (open in new tab caonsole)
  4. http://127.0.0.1:8080/debug?port=5858

Debug like you do in Chrome (browser side)

Node code debugged

Tagged

Control your feelings .. Mr. Browser , Just debounce

This is something new for me , so thought – something like this might be a news for some of the crowd. And I was immensely helped by this concept, so sharing.

Here I was- working once again on a project that required vast usage of Ajax requests. Not a problem – i thought. Have been working on similar projects, and it’s unlike the initial days of my developer life, where I was scared and wary, looking to the Javascript man in the team each time something related to Ajax came up.

Jumping off the context a little – I still remember the first time I heard of  ‘Ajax’. It was an job interview, where after I did reasonably well on the Java quizzing, I was asked – ‘What do you know about Ajax?’, and I made an honest admission that I have never heard about it. Well, after the interview, I came back and did a google on ‘Ajax’, and I realized – oh well it’s a football club from Amsterdam. But why the hell was that being asked in a programming interview? :-) .I went on to next results .. and came to learn about the JS Ajax(the one which would be of more value/interest to me in the coming years !!).

Back on to the debounce and throttle needs when making Ajax requests. How much Ajax has been used since it’s inception needs no enlightening. What has been my learning lately though, has been a case of an unnecessary overuse of Ajax, and the tricks which can be used to avoid this misuse.

The use case involves  any repeating user Behavior acting as a trigger for a server side request made via Ajax from the UI - (Some examples that I could think of)

  • on each keypress you need to update and display a list of words (which come from and Ajax request), Think an autocomplete example based on server side data.
  • on each drag of the scrollbar of an endless-scrolling application page you need to get results from the server, corresponding to the area/page to which the scrollbar is being dragged. See virtual scrolling examples here . Remember Twitter/Facebook/LinkedIn UI and you got it.
  • On each mouse move event you need to display the current position of the cursor and use it in an Ajax request. Think of a GIS application using latitude/longitude data corresponding to cursor coordinates.

So, what’s the big deal here I asked. (btw .. Mine was the infinite scrolling examples where paginated data was to be retrieved from an Ajax request). The big deal is that Mr. Browser thinks that you are done scrolling when you are in the process of scrolling. I mean you want to go from page 1 to 100 by dragging the scrollbar down and Mr. browser believes you’re done at 10, again at 13 again and 17 and so on… And each time Mr. Browser says you’re done, the bound event gets fired and your written code ditches you on Mr. browser’s word to go ahead and make a useless Ajax request. Useless because eventually all the intermediate requests are going to be futile and only the data corresponding to page 100 is going to be displayed. So, effectively you end up making ‘N’ server requests instead of just 1 or maybe 2 or 3(in case the drag is too long). So, this is a Javascript and browser limitation. It’s a browser bug, you might say, and your supervisor may just stare at you and move on to the next scientist. So, DON’T TRY THAT AT WORK.

The workaround is in Javascript and it is the simple concept of debouncing(and a related concept of throttling). I won’t present the dictionary meaning of both, although I did need to look myself.

But effectively the solution is to tell the browser in someway that – “Please treat the last activity made in a period of ‘n’ duration as the final one, and fire an event for this last activity only(ignoring the earlier ones)”.  This technique is termed as debouncing.

A probable alternative solution could be throttling –  to limit the execution time of the function on the event(triggered by user activity) by providing a duration limit( in miliseconds) . This approach again may or may not solve the problem you have at hand (depending on the use case). For example for a toggling of a flag var state, this might not be useful, and debouncing will be required.

While the description may go all over your head( :( I’m not good at explaining), the implementation of these is pretty simple. Thanks to jQuery plugin by  “Cowboy” Ben Alman .  A five minute read would be more than enough for implementing the required and getting your next month’s pay-cheque from your supervisor instead of a nasty stare.

The examples at the shared link  (again courtsey Ben Alman) demonstracte just how helpful this concept could be. How hardly you might be hit in abscence of this fix, will depend on what happens in those Ajax requests, How time consuming they are ?,  Do they modify concerned state also ? etc. factors.

Another recommended read to those interested would be http://unscriptable.com/2009/03/20/debouncing-javascript-methods/

Apparantly, the multi-utility library – ‘underscore.js’ also provides the similar functionality. See here.

Feedback welcome.

Tagged , , , , , ,

Are short-cuts always an ignorable option ?

Disclaimer:Maybe the headline is misleading. I’m talking in terms of technology, And I’ll try to present my POV on this highly debatable topic. I plan to collect input from others and expect their honest feedback with promises of answering whatever queries I am capable of answering.

We’re taught since times we get our senses – “Avoid the easy way out”, “Take the longer route, It’ll make u tough”, “There are no short-cuts in life” etc. etc .. ….

While most of it is true in context of the day-to-day life … I somehow beg to differ with notion that “There are no short-cuts in life” (in context to Technology). I believe that usually their are numerous solutions to each of the situations in day-to-day IT problems. And the ease of taking each one of these routes differs considerably when compared to each other.

I am a java developer who’s been involved in development projects for over four years now, and in my four year old journey as a coder, I’ve come across couple of characters who have been religiously following another (I won’t use the word – ‘old’) school of thought – that says “Don’t use one alien technology to learn another” which is good to hear and makes good sense too , but not always. I myself have stuck to this practice at many a times. However strict adherence to this logic is
(a) not always practical in professional work environments and
(b) not always beneficial also.

It’s one thing to be using an IDE (containing auto-completion & syntax-checking) only after you’ve learned the syntax & the compilation& interpretation of the code. Quite another – to be discarding every new & outstanding technical solution, just because you are not aware of the building blocks of the new solution.

If quickstarting one particular technology can lead to normalizing the learning curve for ten different technologies, then what’s the issue? One eligibility criteria here is that for you learning of the quickstarted technology shall not be more vital than the leanings of the other 10 technologies. The technological solutions that may fall in this category for a java developer could be – Appfuse, Maven, Dojo, DWR, Acegi etc. .

In the past, Ive got flak/taunts from my colleagues/seniors for relying too much on different third party technical tweaks/solutions for solving the problem in hand. I understand their concern.Maintainence issues are automatically increased with each inclusion of something new. but my point is – the amount of time I save by learning and using some third-party solution – I’m willing to input the same on the learning that I’ll achieve in solving any possible maintenance/integration issues instead of spending the same time on ‘Re-inventing the wheel’. Yes, it is re-invention of the wheel when something usable exists and you refuse to use it and it’s As I’ve learnt -A Programmer’s Sin.

Yes, one should not be a short-cut man. He’ll be hollow then. Real knowledge is depth of understanding. However, my suggestion is to not get into the habit of learning everything in the same manner. Prioritize your learning and take them case by case depending on how much you need them. For someone developing a business solution based on a bunch of patterns ‘Appfuse‘ may just provide a building block and it can be learnt as it comes in a leisure time focusing more on the dominant technologies that actually solve the problem. The same technology solution – Appfuse – for one of it’s source contributors will be much more important aspect – and it’ll be a start from the scratch learning for him.

I’ll try to follow this up with a personal experience (w.r.t my experience while learning/working with Appfuse).

revert for a healthy discussion.

I’m the Man

The eternal cliche’ – “I have been thinking of writing a blog for quite some time now

I ‘ve lost count of the number of times I’ve found this statement in any blog’s very first post. And I’m resuing it in my own.

Yes… I also have been thinking for a long duration on starting blogging … soon … The simplest reason for the delay (as if .. there was a fixed time) .. . could be sheer laziness … or maybe .. inhibitions .. that how bad could I get.

I would be presenting my ideas on a variety of topics that interest/disinterest me along with others. The main purpose of my writing my own blog (and I believe this is the prime purpose of blogging) is – to give me a sense of satisfaction – “that I can express myself” .. that I can raise a voice and … if I’m lucky … and if I’m any good …. could maybe get heard too.. :).

the title of the blog .. “The mighty one” came to my mind .. this morning when I was watching a movie named “the motorcycle diaries” .. the actor (don’t remember the name) says … from now on… we live as per “the mighty one” . could not think of anything better. :(

I would try to be regularly blogging. and would try to respond to all comments posted… Maybe I’ll have to hire someone for that :) ( … assuming my entire fan following turn into my blog’s followers also… ).

Dude .. don’t be turned off … I’m very modest.

And all i know is- that “I’m the Man.”

Follow

Get every new post delivered to your Inbox.