Abstract Classes In Javascript

Modern Javascript is a lot different to Javascript of 2010. We have considerably more methods and means of doing things that we did not have previously. Some of those include classes, generators, arrow functions and a few other high-profile additions.

One aspect of Javascript that not even ES2015 nor ES2016 covers is the concept of abstract classes. The ability to specify a class that defines how child classes should look, more specifically ensuring they specify certain methods.

Recently whilst working on an Aurelia project, I was building a widget based system where a base widget class defines a few core values and methods, then the child class which is a certain type of widget extends and defines the contents of the widget.

One such requirement was the base widget class cannot be instantiated. Another requirement is all child widgets must implement a schema method which returns the schema for a particular widget.

Preventing base class from being instantiated

Due to how classes are implemented in ES6, we can check the constructor knowing that any class that extends the Widget class is going to have a different constructor because it’s a new class and therefore a new instance. We just check in the base widget class if the constructor is “Widget” or not. If it is, we can assume someone is trying to instantiate the widget class directly via: let myInstance = new Widget() which our code will stop.

class Widget {
    constructor() {
        if (this.constructor === Widget) {
            throw new TypeError('Abstract class "Widget" cannot be instantiated directly.'); 
        }
    }
}

Enforcing implented methods

To require an inherited child class implements our required method, we can check for its existence via this which allows us to see if the required method has been defined. We will take the above example and add in another check for our child class method.

class Widget {
    constructor() {
        if (this.constructor === Widget) {
            throw new TypeError('Abstract class "Widget" cannot be instantiated directly.'); 
        }

        if (this.schema === undefined) {
            throw new TypeError('Classes extending the widget abstract class');
        }
    }
}

class PieChart extends Widget {
    constructor() {
        super();
    }

    get schema() {
        return {
            "name": "PIE_CHART",
            "data": []
        }
    }
}

Conclusion

We might eventually get a native solution, but for the moment we can easily implement our own fake abstract class implementation in Javascript using classes and it will work for most purposes. I haven’t encountered any issues with the above solutions.

 

Developing A Multilingual Website? Develop In A Different Language

This isn’t a tutorial but rather a public service announcement. If you’re building a website that is going to be viewed in multiple languages, then you should be developing the site in a language other than the native language (in most cases English).

It seems like common sense, but you would be surprised how often different languages and responsive development are not considered at the same time.

Whilst working with an American video streaming platform startup last year the audience was going to be English and Spanish speaking countries. Naturally the designers only bothered to design everything in English, so when it came to building the site a lot of issues were encountered thanks to many single English words being two or more different words in Spanish.

Sometimes the issue of a Spanish word or sentence being longer could be solved by making the overall font size smaller. For some aspects however where space is constrained, it got to the point on a couple of occasions where the designers had to redesign parts of the application to account for different languages (buttons and promotional banner images with text mostly).

Even if you don’t offer native language translation on your site, if a user visits your website and is using Chrome, Google will offer to translate the page into the visitors native language (sometimes automatically if the setting is enabled to do so). This means you can no longer assume that the language you build the site in is the language it is being viewed in (even if you don’t offer the option to change). This can negatively impact the layout of your site or application without you even realising.

What actually prompted me to resume writing this post which has been in a draft format since November 2014 was a submission I saw on the Web Development subreddit. A user who viewed the Windows 10 promotional site in both Italian and English noticed a few responsive aspects were inconsistent and broken because the developers obviously only tested English.

I don’t develop multi language websites that often, but based on previous experience I know that assuming the site will look and function the same way for all languages is a recipe for trouble. I found Spanish to be somewhat troubling especially for buttons, so if you offer that language as an option, develop in Spanish first.

 

Green Smoothies

For about six months or so I have been consuming a homemade green smoothie every morning for breakfast. Some people are on the fence about them which is understandable, from the outset they look like cups of fruit and vegetables.

The reason I decided to start having them was due to the fact I did some contracting for a startup in the US and I ate a lot of bad food. The startup lunch culture is intriguing, nobody brings their own lunch, you all go out and eat out as a team. I am guilty of having Mexican food a few too many times for lunch, not the healthiest option either.

Maybe it is just a coincidence but I have noticed a few things that I would attribute to having a green smoothie everyday. It could just be in my head, I don’t want to go all crazy on you and proclaim that green smoothies are the cure for cancer like some would have you believe.

About 1 month ago, everyone at work had the flu. I literally mean over the course of about two weeks, the flu spread throughout the office. It wasn’t just any flu, it seemingly had enough impact to knock people out for an entire week, the other developer I share a cubicle with was out almost two weeks.

Weirdly enough, I was the only person who didn’t get sick. I felt like perhaps I exhibited a few flu signs (mostly sneezing), but I didn’t miss a day of work and I never felt like I actually got sick at all.

Then my fiance got sick, she had the flu for about a week. Being pregnant, she only occasionally has a green smoothie with me. I didn’t go out of my way to stay away, we still slept together and stayed close: I didn’t get sick. I did wake up with a minor sore throat, but I think that was due to sleeping with my mouth open and the fan on the high setting.

Besides the potential boost to my immune system, I feel like I have more energy and I am definitely more regular thanks to the fibre in the skin of the fruit and other various additions.

Green Smoothie Recipe

If you’re interested, here is what I put into my smoothie. Some people put coconut milk in and other needless things, this is what I put in.

Also keep in mind I only wash the fruit and vegetables, I don’t take the skin off anything.

  • 2 cups of spinach (minimum)
  • A few pieces of kale (it is expensive, so it usually isn’t available and can be optional)
  • 1 carrot
  • 1 banana
  • Half a lemon squeezed (including the seeds)
  • 1 apple (red or green)
  • 1/2 orange (if we have them)
  • 1/2 cup pineapple (if we have it, fresh only)
  • 1/2 cup watermelon (if we have it, include some of the green/white part)
  • 1/2 cucumber (optional, waters it down a little more)
  • 1 teaspoon ginger
  • 1/2 teaspoon turmeric
  • A handful of grapes (if any available)
  • Some ice
  • Half a cup of water (add more of less depending on desired thickness)

Blend it all up and enjoy. Keep in mind the first few days your body will get a shock, so you might experience a few constant trips to the bathroom (not diarhea, but just a few sudden urges like you have had too much coffee).

As always, make informed choices about what you consume. This is my opinion and I only ever have one per day for breakfast, usually with a coffee and a slice of toast with butter and Vegemite.

I’m not a health nut or condone diets like Paleo, but I also acknowledge I don’t consume the recommended amount of fruit and vegetables everyday, the green smoothie helps substitute that.

 

Australia Is Powerless Against China

You might know there is a bit of a conflict happening in the South China Sea. China are currently (and some would say aggressively) reclaiming parts of the reef to build man-made islands supposedly part of which will be for military purposes.

The US government have been quite vocal about their disdain for China’s activities and up until recently Australia was quiet on the subject. Until recently when Australian defence minister Kevin Andrews tactfully and carefully voiced his concerns over China’s activities at the Shangri-La Dialogue talks taking place in Singapore.

The laughable thing is Australia needs China more than it needs the US. Even though we do trade with the US, China is our biggest trade partner. If China were to stop accepting our exports, it would kill the Australian economy.

It is for this reason that Australia would never truly tell China to stop what it is doing in a forceful or threatening way unlike the US. We are caught between a rock and a hard place: our biggest ally and our biggest trade partner. We most likely have the US in our ears telling us to speak up, then we have obligations to ensure China remains our dominant trade partner.

We won’t see a war (hopefully) but if Australia doesn’t tread quietly China can legally damage us more than we can damage them without firing a single missile.

 

Node.js Fork: Io.js Is Joining The Node Foundation

Some great news for the Javascript community today. The popular and controversial fork of Node.js, Io.js today announced they had voted and agreed to joining the Node Foundation.

This means that we have avoided what I was worried could have been a serious fragmentation train-wreck in a year or two when both projects were so misaligned that we started seeing module incompatibilities and one or both of the projects suffering as a result of the incoherence.

Fortunately the Io.js fork is still quite young and I am just happy that we will eventually once more just build Node apps and not have to worry about any potential issues (especially modular authors). I also hope this serves as a warning not only to Joyent, but any other company maintaining an open source project that ensuring you keep the community happy and keep things democratic is an essential component of open source and the very ethos of which it was built in the first place.

In my opinion both Joyent and Io.js contributors who jumped ship because of the way things were are both winners in this situation. Lets hope the convergence process goes smoothly and Joyent have learned from their mistakes.

We will finally be getting some missing features from the out-of-date current Node version like promises without needing to use third-party modules hopefully quite soon after the convergence of the two projects is completed.

 

Galaxy S6 Edge Night Clock Screen Staying Lit Bug

Purchased a shiny Galaxy S6 Edge and you were excited for the night edge display feature which allows you to display a clock on one of the edges of the screen while the rest of the screen remained off? You want a night clock, not a night light!

In theory it is a good idea, but if you are like me, your Edge didn’t do that. In-fact, your whole screen would glow, the night edge clock would show but the whole point of it was lost. This means your battery if your phone is not charging over night would drain quite a lot.

This is in-fact an issue that seems to affect only a certain amount of Galaxy S6 devices. My fianc√©es Edge doesn’t have the issue, but mine does.

Apparently Samsung are working on fixing the issue as it is software related and in some circumstances, is related to particular carrier variants of the S6 Edge firmware.

Over at the XDA Developers forum there is a fix for certain firmware versions. But the fix requires flashing your device, thus tripping Knox and voiding any warranty on the device.

The best course of action is to wait for the fix to be pushed out in your latest carrier update. Speak with your carrier about when the latest firmware update might drop.

 

Writing Inspiration: Write About What Annoys You

Being a better blogger is something many of us aspire too (even I aspire to that), but coming up with things to write about on a regular basis can be difficult. Leading off the previous post I explicitly wrote called Writing Is Fucking Hard I want to share one of the ways I come up with things to write about.

Many articles will tell you to write about things you love or are passionate about. I do that, I write a lot about Javascript and the web in general because it is what I love, but writing about the latest Javascript framework or language can be difficult. A post about Javascript generally requires thought, research and takes days to write.

While I try to keep my complaining down to a minimum, sometimes I like to write about things that annoy me. One of my most passionate subjects is inefficient and unrealistic hiring practices implemented by many tech companies hiring developers.

Not only is writing a post about something that annoys you a great therapeutic exercise, it also helps you practice restraint and discipline. Anyone can rant about something, but it takes a certain degree of writing to adequately get your point across without starting a war.

Anyone can be positive, but being negative in a positive way is more challenging. Doing this can actually be a great writing exercise to broaden your writing toolset beyond tutorials and positively glowing pieces on things you love.

A great example is one of my most popular posts in terms of consistent traffic not reliant on a site like Hacker News or Reddit driving traffic. This actually surprises a lot of people who assume my Javascript articles are the most popular (while they are still popular, you’ll see one post not about the web at all is more popular).

Last year I kept getting telemarketing calls from a company representing an Australian telco Telstra, this company kept calling from the same number and would call everyday. They would never leave a voicemail message, they also would keep on calling and it was frustrating (not to mention, distracting).

So instead I used it as an opportunity to write a post. I figured I probably wasn’t the only person they were bothering, after a Google search came up empty, I answered the call and then wrote a post telling people who it was and why they were calling. The post in question is here.

The point is, I didn’t write about why I don’t like a particular food or brand of coffee (not to say you shouldn’t), I wrote about something that was annoying me. I turned it into a post, which just so happens to get thousands of hits from Google daily (not a glowing endorsement for the company calling).

If there is one thing you should take from this it is to explore alternative things to write about. Don’t feel as though you need to stick to writing things you have come to be known for. The above post isn’t about the web, but who cares? If it can benefit someone else and better you as a writer, why not?

And what better way to illustrate my point than to highlight the very fact that this post is not about anything web related (except loosely blogging)?

 

The Death of AngularJS

For quite a long time developers have been using AngularJS. Coming at a time when the alternatives were the likes of Backbone.js (while powerful in its own right), which just didn’t tick all of the boxes a modern web application needs ticked, we thought we had reached developer nirvana.

We got two-way data binding, controllers, directives and templating syntax. For a good while things were peachy, the sun was shining and everyone was sipping from the golden chalice of AngularJS kool-aid. We put it on t-shirts, wrote books, online training courses, bumper stickers and they sold baby, they sold.

As modern web application development evolves at a steady pace, the number of Javascript frameworks out there to choose from is staggering. It seems everyone has a Javascript framework, 10 new Javascript frameworks will probably come out by the time you finish reading this article.

While AngularJS is far from dead, its eventual demise is coming. A slew of core problems with the framework, more notably dirty-checking and transclusion was enough to rally the detractors to write blog posts about why you shouldn’t use AngularJS. They were all right in their own special way, but some were missing the point.

Not too long ago there was no viable alternative to AngularJS. I am not just talking about feature-set or community participation, I am talking about the lack of actual backing by anyone other than a few neck-beards in their basements contributing to their beloved open source project.

While there are some great Javascript frameworks out there, not many can guarantee with almost absolute certainty that they’ll be around tomorrow (not even Angular could do that, but the Google backing was reassuring).

This is where AngularJS succeeded. The everyday developer loved it, people were using it with WordPress and pretty much anything they could stuff Angular into. And then the enterprise started using it, all of a sudden you were seeing listings for local council and government jobs that listed AngularJS as a desired skill.

But why? If other choices were out there, why did Angular succeed? Was it because it was the first one out of the gate to offer almost everything you would find in a server-side framework or was it the fact it was backed by Google? In part it was both. But there was one other aspect of Angular that made it appealing.

I think it is safe to say that many took comfort in the fact that Google was behind Angular and if Google was behind it, then it must be good. This kind of trust in Google surprises me given their poor track record sunsetting a whole lot of products with their backing, more notably Google Reader.

Instead of trying to replace HTML, Google strengthened it. Decorating HTML with custom attributes, elements and control directives was a hit with everyone. Instead of having to create nested objects in a Javascript file, you were working in HTML and then using said decorations to make things happen in Javascript when you were ready.

It was Lego (AngularJS) vs Meccano (every other framework).

While the disdain for Angular was strong, the love that people had for it remained the strongest. For every hate-piece on Angular, there was a retort that would follow soon after.

The fighters and lovers were on equal footing, kind of like a war. One side spews propaganda and the other side does their best to dispel it: the PHP language is all too familiar with this ugly side of development.

“This ain’t a scene, it’s a god damn arms race” – Fall Out Boy.

When things go wrong

A little while ago talk of Angular 2.0 came about. Instead of it being a natural evolution of the framework, it was revealed that it would be a complete rewrite. The backwards compatibility, transclusion and most hated of them all: dirty-checking would either be removed or rewritten in a more efficient manner in 2.0.

But the Angular team did something that no open source project in a highly competitive battlefield should do: they announced a new version of their highly popular framework that would have no backwards compatibility with the 1.x branch and that there would be no upgrade path whatsoever.

gasp

When the Angular team announced this, you could practically hear a record skip right across the Internet. The concern started relatively contained with the Hacker News comments section and then it escaped, soon everyone was writing blog posts defending the new syntax and lack of backwards compatibility, but many were decrying such a move.

Not only did they announce there would be no backwards compatibility or upgrade path, they also revealed a controversial new syntax for decorating your HTML markup.

Why were people so angry? The Angular team announced they would be addressing all of the shortcomings in version 2.0, that transclusion you hate so much? Gone. Dirty-checking? Completely rewritten to be more memory efficient and use browser object-observe when supported.

Even though at its core 2.0 would arguably be a much better successor to Angular 1.x, removing a large chunk of backwards compatibility and starting from a clean slate, for a version 2.0 the Angular team shocked by changing too much, too soon.

It all comes down to the fact that it is a universally accepted downside of Angular that it has a steep learning curve. The basics of Angular not so much, but the more nitty gritty aspects of the framework were arguably harder to learn due to confusing documentation and confusing blog posts that would go against some of the things the documentation would tell you to do.

You could say that AngularJS 2.0 was a message: every developer that had spent their time learning the framework were being told mostly everything they have learned would be invalidated in possibly 12 months time when 2.0 is released.

It’s like learning French only to discover that in 12 months time that all French speaking nations would be switching to Italian, sure there might be some similarities but it is a completely new language you have to learn.

The biggest controversial aspect of Angular 2.0 was not just the lack of upgrade path, it was the new syntax which looked ugly. There were concerns it would also throw HTML validation (some people still run their sites through the W3C validator surprisingly).

It would take a little bit of time after for the Angular team to sit back and realise they had made a big mistake. Their stubbornness had already caused Rob Eisenberg (of Durandal fame) to abandon the Angular 2.0 team and build a successor to Durandal in the form of Aurelia. While the Angular team might not have realised it, losing Rob was actually a big blow to the project.

Now we are seeing Angular 2.0 components being ported back to 1.4 and while the 1.x branch will continue to be supported for a little while, the damage has been done. Even though there will now be an upgrade path (sort of), it is too little, too late.

Death is near

Even though AngularJS 2.0 has yet to be released, many have abandoned it already. There are a few great choices out there for starting your next Javascript framework like Aurelia or React.js. Those still using AngularJS are supporting legacy applications or junior developers completely unaware of the changes taking place around this framework and the framework space in general.

Having said that: not everybody will abandon AngularJS and support for the 1.x branch will continue for some time. However, once 2.0 lands, it would make very little sense to use the older and more troublesome 1.x branch.

While AngularJS 1.x will remain popular for some time yet, make no mistake React.js alone in its relatively short lifespan has taken an axe to Angular’s market share quite significantly and we will see those effects ten fold when Angular 2.0 is released.

This coupled with other frameworks like Aurelia and EmberJS which has seen a massive uptake by developers, Angular is not the only kid in the playground anymore.

Angular is not going anywhere in the interim, but I think there is now enough competition in the space that simply being backed by Google is not enough to convince developers to stay and trust their decisions are the right ones.

 

SBS Struggle Street: The Truth Hurts

As Australian’s most of us are pretty lucky. We are often dubbed the “lucky country” by ourselves, even though we are lagging behind other countries in many areas.

SBS’s newest TV program called Struggle Street details residents of Western Sydney in an area called Mt. Druitt, which has a pretty notorious reputation for being a rough area because of the public housing it contains.

The show before it even aired its first episode drummed up a heap of controversy, all solely based on the limited glimpse we had through the promos. After watching the first episode I have come to the conclusion that the media and those opposing the program were overreacting.

Rather than being a comical parody of the downtrodden part of society, the program is an accurate depiction of Mt. Druitt life for many. It is a bleak and unfiltered look into public housing families struggling to make ends meet and substance abuse. It is raw and at times, emotional.

While highlighting the cracks in a broken system Struggle Street also highlights something else: the truth hurts. The government and Blacktown mayor are in denial, anyone who tells you this show was exploiting anyone obviously hasn’t seen the show at all, just the promos which I think didn’t paint an accurate picture of what the show is really about.

We’ve been conditioned to write off people on Centrelink as bludgers, people who don’t want to change and happy living off the taxpayer. This show on one hand highlights this point-of-view perfectly, there are indeed those who waste their lives away and there are those who want to try and do better, but feel trapped.

Sadly what we see on Struggle Street is not just limited to Mt. Druitt. There are countless suburbs right across Australia with the same issues. You only have to go out to areas like Caboolture on the Brisbane Northside, or areas like Park Ridge over on the Southside of Brisbane to see similar suburbs filled with people in similar situations.

Given the outraged over Struggle Street, I had expectations of it being a comedy-riddled look at the lower class of Australian society, instead I think we got an honest and unedited view at just how far from the “lucky country” of our residents are. No exploitation, just brutal honesty and I think many are taken aback by how honest the show was (even I was a little).

The mayor of Blacktown Stephen Bali has painted the program as “publicly funded poverty porn” which is easy to say when you’re paid a decent salary and aren’t struggling yourself. He even arranged for 10 garbage trucks to head to SBS’ North Sydney headquarters over the airing of the show in protest. The irony is these garbage trucks are funded by taxpayers as well.

Instead of protesting using publicly funded garbage trucks, Bali could have spun it in a positive light and announced support for people in areas of Blacktown like Mt. Druitt. Most of the characters in the show seemed legitimately decent, but dealt a bad hand (especially couple Ashley and Peta).

I am looking forward to the conclusion of the series and I congratulate SBS for going ahead with it despite the media, the mayor of Blacktown and others trying to stop it from being shown. We need more programs like this, less editing and more honesty. If you want positively filtered entitlement reality TV, watch My Kitchen Rules or The Block.

 

LinkedIn Premium: Not Worth Buying

When I found my contract with an overseas company ending at the beginning of the year, I went into job search mode. I had some runway in the bank, but I hate sitting idle and prefer to be working so I started looking for a job (a full time permanent position).

I already had a LinkedIn account, so I decided to purchase a LinkedIn Premium subscription, the job search tier to be exact. I kept my premium subscription for 3 months and then I cancelled it. It didn’t take me three months to find a job, it took a couple of weeks, but I kept it out of curiosity and kind of forgot about it as well.

With LinkedIn Premium you get a few “premium” features, one of those is being able to see who has viewed your profile in detail as well as being able to be completely anonymous and lurk on other profiles without the other person (even if they’re premium) seeing who is looking.

For the job seeker subscription you also get access to a premium only job seekers discussion board. While it is nice being able to speak with other “job seekers” the board was filled with threads of people complaining they haven’t got a job yet and wanting tips.

I think LinkedIn are a little misleading, you don’t really get any kind of job seeking advantage with a premium subscription. I would argue when you’re looking for a new job, the idea is to conserve your money, not spend it on superfluous subscriptions that don’t better your job prospects.

If you’re debating whether to get a LinkedIn Premium subscription for bettering your job prospects: don’t. The only benefit a premium subscription has is the ability to spam people with InMail, stalk people and a few other features that only benefit recruiters.