Sunday, 12 January 2020

An exploration of JavaScript's variable declaration keywords (Part 3/3)

Now we come to the const keyword. Its rules are the strictest, and it should be used only in cases where the variable's assigned value is not expected to be changed.

The const keyword
It functions almost exactly like the let keyword, with one major difference - its value cannot be reassigned.

If you try this using the let or var keyword, this is legitimate and will be allowed.
let a = 5;
console.log(a);

a = 10;
console.log(a);


5
10


For const, you get an error. You can't reassign it some other value.
const a = 5;
console.log(a);

a = 10;
console.log(a);


5
Uncaught TypeError: Assignment to constant variable.


In fact, if you try to declare a variable using const, without an initial value...
const a;


...you get this error.
Uncaught SyntaxError: Missing initializer in const declaration


Needless to say, this wouldn't work either.
for (const i = 0; i < 5; i++)
{
   
}


Uncaught TypeError: Assignment to constant variable.


There is a way to get around this rule, though. Objects!

Say you declare an object variable using const. Let's call it obj, with properties a and b.
const obj = {a: 5, b: 10};

console.log(obj.a);
console.log(obj.b);


5
10


Now if you did this, there would be an error.
const obj = {a: 5, b: 10};

obj = {a: 10, b: 5};

console.log(obj.a);
console.log(obj.b);


Uncaught TypeError: Assignment to constant variable.


But if you do this, it works!
const obj = {a: 5, b: 10};

obj.a = 10;
obj.b = 5;

console.log(obj.a);
console.log(obj.b);


That's because it's the properties of obj whose values you're reassigning, not obj itself. Cool, huh?
10
5


In Conclusion

let and const introduce some rigidity into JavaScript, where it had previously been way too flexible for the comfort level of many programmers. It's possible to use var the same way too, but you'd have to be very disciplined. Maybe even extraordinarily disciplined. And when there are a lot of moving parts in your project, it may not be worth the effort.

I've been using var in JavaScript for a long time now out of sheer habit. Because I almost never use it in a way that would introduce problems, so far it hasn't given me many. But as times change, so must programming habits evolve.

Kind and constant regards,
T___T

Friday, 10 January 2020

An exploration of JavaScript's variable declaration keywords (Part 2/3)

In this next part, we'll take a look at how the let keyword is used. let was introduced in ECMAScript, tightening the functionality of JavaScript's variable declaration.

The let keyword

Like var, let is used to declare variables. In its simplest form, it's used similarly.
let a;

console.log(a);


undefined


let a = 5;

console.log(a);


5


If you declare a variable more than once using let, it'll produce an error.
let a = 5;
let a = 10;

console.log(a);


Uncaught SyntaxError: Identifier 'a' has already been declared


Unlike var, if you try to access a variable before it is declared, you'll get an error. Hoisting does not apply.
console.log(a);

let a;


Uncaught ReferenceError: Cannot access 'a' before initialization


Unlike var, a variable declared in a block using let cannot be accessed outside of that block.
let a = 5;

if (a > 5)
{
    let b = a;
}
else
{
    let b = a + 1;
}

console.log(b);


This is the error that will be produced. Variables declared using let are block-scoped.
Uncaught ReferenceError: b is not defined


Again, any variable declared using let cannot be accessed outside of the block it was declared in.
for (let i = 0; i < 5; i++)
{

}

console.log(i);


Uncaught ReferenceError: i is not defined


Where functions are concerned, let behaves just like var.
let a = 10;

function x()
{
    a = 0;
}

x();

console.log(a);


0


function x()
{
    let a = 0;
}

x();

console.log(a);


Uncaught ReferenceError: a is not defined


Now try this...
let a = 10;

function x()
{
    let a = 0;
}

x();

console.log(a);


When you try to access a at the end, you get 10 instead of 0. That's because a is now declared as a variable using let, within function x(), thus the value of that a can't be accessed.
10


What we've learned

let is block-scoped. Thus if a variable is declared inside a block, it cannot be accessed outside of it.

Any variable declared using let cannot be redeclared, though its value may be reassigned.

let is stricter than var and is subject to more limitations. This is a good thing in the sense that rectifying (or better yet, avoiding altogether) the resultant reference and syntax errors will in turn help the programmer avoid logic errors down the road.

Next

Examining the const keyword.

Wednesday, 8 January 2020

An exploration of JavaScript's variable declaration keywords (Part 1/3)

With the creation of ECMAScript, JavaScript's days of being derided as a "loose" scripting language have come to a halt. Well, not really, but at least proponents of a variable definition stronger than the traditionally-used keyword var have had their prayers answered in the form of let and const, in 2015.

This post is maybe five years too late, but what the hell. Today, we're going to take a look at those.

The keyword var

This is used to declare variables. They can be declared just about anywhere.
var a;

console.log(a);


undefined


var a = 5;

console.log(a);


5


You can access a before even declaring it. Although, I really can't think of a reason why you'd want to.
console.log(a);

var a;


undefined


If you declare a variable twice using var, it will take the most recent value assigned to it.
var a = 5;
var a = 10;

console.log(a);


In this case, it's 10.
10


Let's try this again, explicitly declaring a again, and setting the value to undefined.
var a = 5;
var a = undefined;

console.log(a);


So far so good.
undefined


But what if the second time, you didn't specify a value?
var a = 5;
var a;

console.log(a);


In this case, it's 5. JavaScript uses the most recent value assigned to a.
5



JavaScript assigns the default value of undefined to all variables that haven't been declared, or declared but not assigned a value. This is known as hoisting.
undefined


A variable can be declared using var inside a block...
var a = 5;

if (a > 5)
{
    var b = a;
}
else
{
    var b = a + 1;
}

console.log(b);


6


... then redefined outside of the block.
var a = 5;

if (a > 5)
{
    var b = a;
}
else
{
    var b = a + 1;
}

var b = 10;

console.log(b);


10


Just for good measure, try this!
for (var i = 0; i < 5; i++)
{

}

console.log(i);


You'll notice that you can access i outside of that For loop! That's because the For loop is a block.
5


var can be used to declare a variable outside of a function, then accessed within that function.
var a = 10;

function x()
{
    a = 0;
}

x();

console.log(a);


0


However, if var is used to define a variable inside of a function, it cannot be accessed outside of it.
function x()
{
    var a = 0;
}

x();

console.log(a);


This is the error that will be produced.
Uncaught ReferenceError: a is not defined


You can use var to redeclare a outside of the function and assign a value to it.
function x()
{
    var a = 0;
}

x();
var a = 5;

console.log(a);


5


What we've learned

var is function-scoped. Thus if a variable is declared inside a function, it cannot be accessed outside of it.

Any variable declared using var can be redeclared. This can lead to confusing scenarios. The looseness of JavaScript can be a boon in the sense that it's very forgiving, but in large-scale scripts, this can make errors hard to detect.

If JavaScript is the only language you have ever used, you probably won't think much about this. But if you learned other languages before this, var's behavior will seem really nonstandard and inconsistent to you. It's predictable enough; you just need to understand the rules behind it.

Next

Examining the let keyword.

Saturday, 4 January 2020

The GovTech Experience

Late last year, I went through a delightfully different recruitment experience.

Someone from HackerTrail called me up and asked if I was interested in a position in GovTech. For some background, GovTech is a Singapore Government agency that deals with software, internet infrastructure, cybersecurity and all that good stuff. It's a restructuring of the former Infocomm Development Authority of Singapore (IDA).



I was under contract to another firm (and still am!) and when you're in that kind of position, you simply don't say no to an opening like that. Besides, all HackerTrail asked me to do was take one of those online coding tests.

And, you know, I absolutely love that shit. When I have time, I actually do these for fun. And now I get to have an interview opportunity if I pass?! Son, you had me at Hello World.

Stage One

The test took three hours and it was by no means easy. The coding problems were certainly straightforward enough, and since my Apache server happened to be up, I did all of it in PHP. The first few tests ran beautifully, but my solutions probably failed on efficiency. Still, I documented my code, submitted and put it out of my mind. I probably wouldn't get called up, I reasoned; any serious algorithms practitioner could do better.

Online coding problems.

To my surprise, a few days later, HackerTrail called again and informed me that I'd been selected for a recruitment event at GovTech next week.

Wow. A recruitment event. With a table of events, opening speech and everything. It would begin at 3PM and last until evening. Stage Two would start around 3:45PM after an opening speech. This would be followed by a catered dinner and networking, and Stage Three at 7PM.

At this point, I was under no illusions that my code had been exceptional or anything. In fact, considering I hadn't heard of this event until now, one week before it was scheduled to begin, I suspected that attendance was low and I'd simply been drafted to make up the numbers. Still, other than half a day's leave, what did I really have to lose? It was a job opportunity that looked worth exploring.

Also, I liked the way they hooked me with an online coding test first. It may not have been guaranteed to bring in the finest minds, but if they had intended it to attract people who actually liked doing this, well, that was pretty damn clever. And the fact that the coding test had not been stack-specific, was a bonus. Some companies want to hire for a particular technical stack only, a practice which I consider really short-sighted. It suggested to me that GovTech was hiring based on foundational skill rather than experience in any particular language, and in my opinion that's the right way to go.

I should also mention that HackerTrail offered all candidates a 20SGD Grab Voucher for the specific destination and time of the event. Nice. These guys were covering all the bases. They must have really wanted attendees bad.

The event begins...

The day arrived and at 2:30PM, I was having a leisurely drink at a nearby cafe. The HackerTrail representative actually called to ensure that I was on my way. I'll be damned, these guys seemed even more anxious about attendance than I was.

At 2:45PM, I made my way to the lobby of the Sandcrawler Building that housed the GovTech headquarters. There, I was brought up the office to register my attendance, where I was issued a pass for the evening and told to enter my particulars in an online form.

Now, my opinion on the ultra-lame practice of filling in forms at an interview has been well-documented (or perhaps I should elaborate even more at a later date) and while their online form was well-designed, this only served to make the experience only slightly less lame. At the risk of coming off as arrogant, I didn't apply for this. They invited me. And now they wanted to act like they didn't already have my particulars? Something seemed a little off here.

The candidates took their seats. There were maybe forty of us. Later in the evening, someone told me that over a hundred online profiles had been shortlisted for suitability and invited for Stage One. Apparently, only forty of us had passed. The others had somehow failed, or more likely, hadn't even bothered.

Sweet! A ping-pong table!
Decor was pretty nice, though. Just the right mount of casualness (non-matching couches, funky fonts on the walls) to give off a hip and comfy vibe. Even had a ping-pong table! Not that I actually play ping-pong, but still.

Stage Two

After an opening speech which, if I'm going to be honest, wasn't particularly interesting and I didn't pay a lot of attention to, we were split into groups of four. Each group was sent to a room where we introduced ourselves to each other. Guy A was a Ruby On Rails dev, and sounded Filipino. Guy B was a DevOps type dude who used Docker. Guy C had an Indonesian accent and called himself a "full-stack .NET developer".

A group of
random devs.

And then there was me. No special labels to speak of.

We were set a project that was to be completed within ninety minutes - an impossibility in itself considering the scope - and graded on our cohesion, presentation and problem-solving approach, all under a tight deadline. Guy A pretty much took charge and broke the project down into tasks so we could each do stuff. Halfway through, I realized that my Git wasn't working, and contented myself by being totally useless acting like some glorified Project Lead and trimming the list of User Stories down to size.

Not my finest moment, to be honest.

Then again, I'm not sure any of us really covered ourselves in glory here. Guy A handled the database schema, Guy B failed to get Docker running, and Guy C pretty much hard-coded the front-end. As a team, we were an absolute failure.

Dinner And Networking

Then it was time for dinner. And let me just say the buffet spread catered for this event was more than just decent. It was smashing. Fish, seafood, huge chunks of roast chicken, dessert. GovTech certainly wasn't holding back.

Fancy.

It was at this time I took a look at the door gift that the reception area had given me upon registration. Oh, wow. A wireless mobile charge with the GovTech logo on it! Too bad my mobile phone was an older model. That aside, this was pretty cool.

There weren't enough assessors to conduct Stage Three for everyone at once, so some of us had to wait. As we waited for Stage Three, we got to talking. Like me, everyone here had been headhunted by HackerTrail. We started discussing, like a bunch of programming geeks, how each of us had solved the coding questions in Stage One. Surprise, surprise - just about everyone else had gotten similar results. No one I spoke to got a 100% score, or even a 50% score. This made me feel a little more optimistic about my chances.

Stage Three

I was ushered into a room with a HR Representative, who told me that we were waiting for an engineer to arrive. She looked tired. "Long day?" I ventured sympathetically, and she exhaled in affirmation.

The engineer tasked to assess me presented me with a pair programming exercise - and to my surprise, they reused the same coding question I had been posed in Stage One. The coding problem had been modified slightly so that I could reuse my earlier code to solve the problem with the updated specs. Only, this time, I was using a borrowed Mac from my ex-boss and didn't have Apache server installed, so I couldn't reuse my old code. The engineer was pretty nice about it and even offered to let me download the software onto his Mac, but seriously, it was just code. I could do it in vanilla JavaScript just fine.

Pair programming.

To heck with it, I just redid the whole thing and modified the code on the fly. Sure, my code wasn't up and running by the time limit, but he hopefully got a good grasp of my thinking process.

The engineer looked kind of tired too. I wondered aloud just how many times he'd had to walk through the same damn coding problem today, and he responded with a chuckle.

Finally, the representative from HR and the engineer sat down with me for some good old Q and A. It was the very standard "tell me about yourself" and "why do you want to join GovTech" shtick. The engineer asked me about a few projects I'd worked on in my spare time, and gave me the opportunity to brag. I have to give this guy props; he understood the dev mindset very well.

I was then given a chance to ask them some questions of my own. I'd actually prepared a few over dinner, many of which were applicable either to HR or co-workers. To their credit, they answered in adequate depth and detail and with little hesitation. I wasn't really looking at the answers themselves - I'd probably find them out myself in due course - but rather how they answered them. Did they appear confident? Evasive? If by some miracle I managed to get in, what kind of people would I be working with?

Aftermath

Finally, it was done. I shook their hands, filled up the goddamn exit questionnaire, and left. The time was 10PM. The entire recruitment process had taken seven hours. Exhausting yes, but I suspect it was worse for the event organizers.

Two weeks passed. No one from either GovTech or HackerTrail got back to me, and I have to confess here - I'm disappointed.

I'm disappointed not because they didn't want to hire me. Expecting them to choose me out of forty talented candidates would be a tall order. My ego isn't that big.

But Christ... would it have been too much to send a polite mass email to the ones who didn't make the cut? It's not like I expect this from interviewing companies normally; but the amount of work GovTech had put into the recruitment effort had let me to believe that their rejection efforts would meet the same standard.

Someone dropped the ball there. It's a blot on what was otherwise an amazing experience.

Final thoughts

No regrets. It had been fun. I was there, done that, got the t-shirt (in this case, it was a wireless mobile charger) and I had an interesting tale to tell my friends and colleagues.

I saw this on the GovTech blog.



Show us some code - open source contributions, side projects and school projects.


Since my tech skills aren't anything spectacular, I can only surmise that I was selected due to my online presence, which includes my GitHub repositories and website. And it's always nice to know the work I put into those, paid off.

Another nice thing was that I told my colleagues about what I went through, and they were not only happy for me that I got the opportunity to experience it, they didn't use this as a stick to beat me with or a knife to stab me in the back with. See what I mean about contractors? We stick together.

The fact that I gave them the wireless mobile charger to use, probably didn't hurt.

Ah, well. Another day, another opportunity!

The path of true Gov never did run smooth...
T___T

Saturday, 28 December 2019

Here's Why You Shouldn't Take Tech Career Advice From Your Friends

During the last Chinese New Year, I got this link in my news feed, an article from Robert Half, a recruitment agency. It caught my eye for the wrong reasons. Something about this article struck me as extremely problematic.

If you’re not sure where you want your career to go, don't be afraid to seek help. Ask friends and family over Chinese New Year for their advice.


Really? I call bullshit.


Surround yourself with friends...
but don't take their advice. Because
their advice sucks.

Friends are good things to have. They hang out with you when company is needed, they usually find the same things funny, and they listen when you need to rant. But friends are not infallible (big surprise, huh?) and when you need advice, sometimes they fall short. I don't mean they run out of advice; advice is primarily opinion-based, and if it's one thing human beings rarely run out of, it's opinions. I'm going to be really blunt here and say this - don't listen to your friends. Especially when it comes to a tech career. Your friends don't know shit. This goes for your parents and your siblings as well.

Now let's just be unambiguously clear about one thing. I'm laboring under the assumption that your friends care, and only want the best for you. They honestly are telling you what they think is the best way to go. They're not jealous of you, or trying to sabotage you. They have only the purest of intentions.

And their advice is still utter bollocks.

I have a lot of friends, and I've lost count of the number of times I've had to dismiss their freely offered opinion as well-meaning, but useless. That's because most of them are non-technical people and not of the career consultant variety. I'm honestly baffled at the number of people who have seen fit to advise me on how to build my career in my own industry without having worked a single day in it. Or people who give me marriage advice when they don't even have someone to hold hands with. Or people who tell me how to manage my money when they're the ones who are constantly in debt.

Advice is easy to give, and exceedingly few people will, when dishing out advice of any sort, pause to consider if they are qualified to dispense that particular brand of wisdom. That's not a blot on their character; merely an unfortunate facet of human nature.

But the advice is free!

Oh, dear. You know what else is free? Farts. Toe fungus. STDs. You don't have to pay a cent for them either, but do you really want those?

Advice is one of those things you should not value just because it's freely given. If it was worth anything professionally, you should have to seek it out, or better still, pay for it.

But my friend is a well-paid developer in a tech company!

Then yes, listen to him. Or her. But you should be listening because this friend is an authority in the career you want, and not because he or she is your friend. In fact, if you're in the habit of listening to people just because they're your friends, stop it. Pronto.

But they've hired techs before!

Oh, wow. I guess that must really mean they know something, huh?

Hey, you know who else has hired techies? Human Resource people, who (surprise, surprise) may not actually know all that much about tech careers specifically. Clueless CEOs of small outfits who don't have the sense to leave hiring to more qualified people. Incidentally, unless you're desperate, I'd steer clear of those companies.

I'm not saying that people who have hired techies don't know shit. They could actually know shit. But if they claim to know shit and don't want to get laughed at, they should also know that "I've hired techs before", on its own, doesn't cut the fucking mustard.

Actually, scratch that last bit. They don't have to know. You do.

What about my ex-classmates from my Computer Science Degree?

That depends. Are they currently making a living in tech? Then go for it. If not, fuck 'em. What could you possibly learn about the tech industry from people who haven't written a single line of code in years? I'm sure their opinions are valuable to someone, somewhere out there. Just not to you.

Why Advice Can Be Bad

The best advice doesn't necessarily come from someone in your field, but it helps.

People who are only in it for the money, will give you advice on how to earn money in the shortest time possible. People who just want to coast along in life, will naturally recommend jobs where you can take it easy. People who equate professional success with fancy titles will point you the way to companies where you can make a name for yourself and climb the ladder.

None of that is intrinsically wrong. People want what they want. But you need to be really honest and ask yourself if this is what you want. Just because what people want for themselves isn't wrong, it doesn't follow that what they want for themselves must be right for you.

Don't follow for the
sake of following.

I'll say this again - advice is opinion-based. And opinions are skewed by perspective. If you're looking for relevant career advice regarding the tech industry, do you think you could obtain it from someone plying his or her trade in, say, healthcare? Law? Journalism? Sales and marketing? Driving a cab? Running a hipster cafe? A career soldier or civil servant?

None of the above, Junior.

The point is that the person giving you the advice should have his career goals aligned to yours. He should want the same things you do, and preferably already accomplished them. Therefore it follows that someone in the industry you want to build your career in, would give you better advice than someone out of it. If you want a tech career, be advised by a tech.

It could still be bad advice, but it would at least be relevant bad advice.

And even then, bear in mind that the advice isn't good just because the one giving it comes from the same industry. There's this guy who used to intern at a start-up I worked for. He wants to be a software architect five years down the road, and the worst thing he could do is listen to me. Why? Because I'm not the kind of guy who wants to be a software architect. I want to code. I don't want to be in the position of overseeing something and then not getting my hands dirty. Any advice I give him would not bring him anywhere close to his goal. It would only bring him close to my goal. And even then, I'm hardly the ideal person to ask - I've made several mistakes during my storied career and will likely make several more by the time this post is read.

But if I'm not convincing enough, maybe this long-dead technopreneur will be.
"Your time is limited, so don't waste it living someone else's life." - Steve Jobs

The times I didn't listen...

A couple years ago, I was in the market for a job. A few people, rather amusingly, offered me tips on my job search. Apparently, I needed to:

- refuse to provide details of my last drawn pay (to give me that edge in negotiation)
- provide some plausible bullshit for leaving my last job (because the truth would blunt my edge in negotiation)
- cover up the tattoos on my fingers. (because, even though it was 2017, these things somehow still matter)

Liverpool FC pride, baby.

You know what I did? None of that shit. I was too arrogant to lie, obfuscate or provide evasive answers. I provided excruciatingly detailed salary figures from all my previous places of employment. I stated up front that the last two companies that I worked in, tanked. And I certainly didn't cover up my goddamn fingers.

I not only landed the job, but they offered me more than what I had asked for.

Strategy? Bah humbug. You need substance, not strategy. You need to have a finely honed sense of what matters and what doesn't. Without that, no amount of "strategy" proposed by people who can't tell Java from JavaScript, will help you. Trust your instincts.

In conclusion

Be very stringent as to whom you take advice from.

Whatever advice you take, remember this. This is your career. Own it. It is your responsibility. You don't get to do something stupid and then claim that people gave you bad advice. You chose to follow it!

You're the only one with skin in the game. If you're going to give your career an honest shot, I hope to hell that whoever is advising you has something a lot more substantial than "I'm older than you, so I know better" and "I mean well".

Also remember that everyone has a right to their opinion, informed or otherwise. What they don't have an automatic right to, is to be taken seriously by you. That they have to earn. And if you choose to give their opinion weight simply because they're family or friends... hey, you know what they say - there's a sucker born every minute.


You've been so advised,
T___T