Stack Overflow: over 1k and back again

A month ago, I checked into stack overflow to see if I could help someone out – you know, be a good boy and give back to the community.

A person had a basic question about string concatenation in SQL. I promptly answered and provided a code snippet example. The OP was overjoyed! Angels heralded from the heavens, baby kittens were born. I broke the 1,000 point barrier when he checked my answer.

The next day I hopped on and my credits were back down below 1k. Wait, what!?

He unchecked my answer. Was it wrong? Nope. And neither was he.

It turns out someone posted an answer he liked better from which I learned a few things, which is awesome!!

  • it’s not about me or my points. I was disappointed that I lost my lovely 1k status, but that just reflected a selfish motive. I learned that I need to work on my character.
  • SQL 2017 has a new function: STRING_AGG() which finally performs what people have been wanting and hacking in SQL scripts since 2008. (Oracle 11gR2 had this ability in 2009 with the LISTAGG function.)
  • StackOverflow doesn’t have an alert for when your answer is declined. When someone accepts your answer, promotes it or demotes it, a little red or green badge appears over your score. If someone unchecks your answer, you don’t see a red badge. It would be nice if they drew the user to the question so, like in this case, he can learn some new tricks.

In this industry, one of the properties that sets apart a jr. developer from a seasoned one is the variety, value and vastness of knowledge built from experiences and experimentation. A little adjustment and this could have been an automated lesson. Why doesn’t StackOverflow take its vast knowledge base and perform a medical analysis on people’s posts (questions) to find similar questions that have been answered? StackOverflow has more opportunities to be mined for those who look for the potential.

(Image by Pawel Janiak on Unsplash)

(Image by Pawel Janiak on Unsplash)

My Favorite JavaScript Language Feature

Recently on one of the social network feeds, there was a long thread of developers putting their input into the question: “What is your favorite JavaScript feature.”

Most people were putting in things like object destructuring, closures, map-filter-apply, reduce, spread operators, promises, arrow functions, first-class functions, eval, async and await. One person even answered with a code sample and another mentioned the use of closure with a block declaration as a model for scope. A block declaration is the use of the ({ … code … }) structure.

function pwrGen() {
    var i = 0;
 
    return {
        next: function() {
 
            var ret = Math.pow(i, 2);
            i++;

            return ret;
        }
    };
}

var gen = pwrGen();

console.log(gen.next(), gen.next(), gen.next()); // -> 0 1 4

Something that I’ve grown to appreciate is the Truthy and Falsey properties, which I think only one other person among hundreds gave mention to.

Here’s why: because JavaScript is a loosely typed language, it handles certain types of conversions in context of the statement they’re in. This provides a way to do a unary collates, quickly identify value, and quick boolean conversions.

Unary Collation

console.log(true && 'all prior conditions are true');

console.log(false || 'all prior conditions are false');

Is There Value?

if (!!thisVarHasValue) { ...

In this example, we can use the bang operator to quickly convert any variable into a boolean. However, there are some caveats.

Can you handle the Truthy?

As you may have noticed in the prior example, there are two bang operators. The first bang operator changes the variable to a boolean type. The second one reverses that. As a result, if the variable “thisVarHasValue” has a value, it results in a True response … with a few exceptions.

The values undefined, 0, and 0.00 and NaN all produce false values. Empty strings also produce a false value. However, empty arrays and strings of zero values produce a true value.

Quickening

Efficiency is on the mind of any good coder. I like to run small benchmarking programs to get numbers when I haven’t come across other posts that do it. Below is a short program that you can run on JSFiddle. It takes a million rows then iterates through them using a single-bang (falsey) and a triple bang (double-falsey) operator compared to a typical undefined-or-zero comparison.

var a = [];

var div = document.getElementById("answer");

div.innerHTML = !!a;

for (var x = 0; x < 1000000; x++) {

  a.push(x);

}



// [Call to doSomething] took 17399.999999965075 milliseconds.


var t0 = performance.now();


for (var x = 0; x < 1000000; x++) {

  if (!a[x]) {
    a.push(x);

  } else {
    a.push(x);

  }
}


var t1 = performance.now();

div.innerHTML = "Single-bang took " + (t1 - t0) + " milliseconds.";





var a = [];

t0 = performance.now();


for (var x = 0; x < 1000000; x++) {

    if (!!a[x]) {
      a.push(x);

    }
}
t1 = performance.now();

div.innerHTML = div.innerHTML + "<br/>Double-bang took " + (t1 - t0) + " milliseconds.";





var a = [];

t0 = performance.now();


for (var x = 0; x < 1000000; x++) {
    if (!!!a[x]) {
      a.push(x);
    }
}
t1 = performance.now();

div.innerHTML = div.innerHTML + "<br/>Triple-bang took " + (t1 - t0) + " milliseconds.";





var a = [];

t0 = performance.now();


for (var x = 0; x < 1000000; x++) {

    if (a[x] === undefined || a[x] === 0) {
        a.push(x);
    } else {
        a.push(x);
    }
}
t1 = performance.now();

div.innerHTML = div.innerHTML + "<br/>Condition checking took " + (t1 - t0) + " milliseconds.";

The result is somewhat consistent. The double-bang is the fastest, followed by the triple-bang, the single-bang, and finally, the typical condition check:

Single-bang took 3.2000000355765224 milliseconds.
Double-bang took 2.899999963119626 milliseconds.
Triple-bang took 3.000000026077032 milliseconds.
Condition checking took 3.2999999821186066 milliseconds.

But remember, this is across a million rows. a tenth of a millisecond doesn’t amount to much. This was an exercise to see if there was something substantial to use in optimizing code and to peek into the JavaScript engine’s behavior.

What I find interesting is how the triple-bang, which is the same value as the single-bang, is somehow just slightly faster. I’ll have to do a follow-up later as to why this is the case.

On Robots, Automation, APIs Opencaching and Groundspeak

I Am Not The Droid U R Looking 4

Groundspeak is shooting itself in the foot, and with the new opencaching.com website, sponsored by Garmin, it’s time they stop being selfish and open up their API … and they better do it quickly.

dor.ky blog, by Scott Wilcox, has a great explanation and a few good comments on why groundspeak needs to open their API.

The forums at groundspeak on the topic pretty much ended with a comment from Jeremy (doubtlessly employed at Groundspeak) stating:

There is a private API and it is available for trusted partners. We have no plans to offer an open public API at this time. It was considered in the past but the current decison is to keep it private.

Sounds short-sighted, if you ask me – especially considering that this comment was made August 2008, nearly two and a half years ago and they still haven’t opened their API.

Why does this interest me? Support tools by fans would be an enormous benefit to Groudspeak. I would like to develop an app or even a web-page that helped geocachers manage and automate their virtual caches. Imagine, as a geocache owner, that you are able to provide a form for people to simply fill out. Once done, it allows the geocacher to log that they found the virtual. (Better yet, it even automates their log post if they have the right answers!)

Imagine, then, the service growing from that idea, allowing earth caches and history caches along with the virtual caches … all automated.

Currently, Groundspeak not only doesn’t provide an API, but their site license prohibits the use of spiders, robots, scraping, etc. That’s understandable, but they should create an API. Google’s done it and see how they’ve grown. Flickr’s done it, too… they’re going strong despite the competition. It appears that any strong data-driven company has an open API. People want to automate process, they want to enhance the experience.

As @williamtm commented on the dor.ky blogpost Why GeoCaching.com needs an API

If an API was made available, and a company/developer like atebits, TapBots, or any number of the excellent developers out there decided to make a Geocaching app, doubtless it would be brilliant. For starters, you can dump the ugly custom UI, improve the speed of it, and, as you said, improve the reputability of it over 3G/EDGE/etc, and bam, you might have an app that’s actually worth £5.99.

Fact is, the app as it is now is just terrible. It rarely seems to work when I’m out and about and want to find nearby caches, and often simply visiting the website using Safari and finding caches that way is faster.

At such a critical time when new geocachers are nudged into other offerings, it’s the combination of a variety of good tools and access to data that will win the race. Opencaching.com already has a public API out and ready for anyone who wants to develop a kick-a$$ iPhone app for them (and I would be one of the first ones to buy it)… Is geocaching.com up to the challenge?