49

A class action

Embed Size (px)

Citation preview

Page 1: A class action
Page 2: A class action

Luciano Colosio

Good morning, my name is Luciano,

Page 3: A class action

@unlucio

you can google me as unlucio.I am a nerd and today I’ll be playing the prosecutor in this case

Page 4: A class action

A Class Action

It is my pleasure to represent the people of this language as we’ll discuss a controversial matter dividing opinions in the last years.

Page 5: A class action

ES6ES2015

class

In 2012 a new keyword was proposed as a new addiction to what it will became better know as the 6th version of the ecma script standard

Page 6: A class action

classI beg you a moment to contemplate this word.How can, such a small and simple word, be considered so harmful by many?

Page 7: A class action

Ladies and gentleman of the jury, I will call three witnesses to the stand.

Page 8: A class action

The FunctionMy first witness will be the Function.We all here know the function, we are well aware of her meaning and powers.It’s well known how her been higher order is an empowering feature in our daily job.Did we really need a shorthand for manually defining a constructor Function?

Page 9: A class action

The PrototypeAs my second witness I call to the stand the Prototype.This gentleman deeply influencing the nature of the language will see his daily job getting even more miss understood and hidden.

Page 10: A class action

The ClassAnd at last I’d like to call to the stand The newly introduced Class.Does she really add something new to the language? And most of all: will she simplify the life of everybody use to other languages and to the classical object oriented programming?

Page 11: A class action

So this was the idea: investigating a new feature through the scene of a trial.And showing how our “class” character would turn out to be essentially conman.

Page 12: A class action

it’s able to declare a constructor sure, just like the Function is able to do.Adding properties and methods to the constructor’s prototype, just like we already use to do.No matter how you look at it, hardly class can be defined as any kind of addiction to the language.

Page 13: A class action

class

No matter how I looked at it but the class case was pretty simple

Page 14: A class action

Sugar. Syntactic sugar.This is how we quickly cut it off.Almost as pointing out that is not such a big deal and it doesn’t change much: it’s suppose to make our life sweeter.

Page 15: A class action

and It’s easy to get excited for stuff that seems to make our life sweeter.Right?

Page 16: A class action

I mean, think about promises.They’re a great tool, I love them but it’s easy to get carried away.

Page 17: A class action

not always:I remember reading of promises as the solution to the Xmas tree hell

Page 18: A class action

Yeah, but OOP is good for you… at least that what they say.

Page 19: A class action

Don’t get me wrong: Classical oop is great, except when is not:- Huge dependency trees can get messy and cause over structuring and over engineering

Page 20: A class action

new GlassOfWater();

and all the sudden you find a reef in you drinking water because some how it ended up depending from the Ocean.

Page 21: A class action

and any way Dispise you can create instances for “class” defined objects only using “new”, Class still doesn’t create a blueprint.Applying the classical oop, often struggling with the prototypal inheritance, we want familiar instruments.

Page 22: A class action

(it’s better to look at the graph from right to left)Under the hood this is still happening and Object.create() is a way better and more expressive way to imply this kind of behaviour.Just think about what’s happening here and what are the side effects: calls traversing the hidden links and going up in the chain to fulfil a request.

Page 23: A class action

What is Prototypal inheritance?

but wait a second: there seems to still be some kind of confusion about prototypal inheritance?Is it classes?Is it blueprints?Is it super?No, none of these

Page 24: A class action

Code Reuse

Isn’t it all the point? This is what we really care aboutWrite code once, use in multiple situations(sometimes the most used way is still copy&paste)prototypal inheritance makes this easydon't think about new, super, and extendsdon't even think about javascript's prototype propertyPrototypal inheritance is about fallbacks

Page 25: A class action

var base = { firstName:"Luciano", nickName:"unlucio", getFullName: function() { return `${this.firstName} (${this.nickName})`; }, }; // base is the fallback

var obj = Object.create(base); // where the magic happens obj.alertName = function() { alert(this.getFullName()); };

obj.alertName(); // Luciano (unlucio)

// this is code reuse! console.log(“getFullName" in obj) //true console.log(obj.getFullName === base.getFullName) //true

Page 26: A class action

it doesn’t seems to be that hard :D

Page 27: A class action

This is pretty nice but actually I don’t remember the last time I used it.actually i don’t remember the last time I really used inheritance anyway (in javascript).

Page 28: A class action

On the other side as javascript widespread it’s impossible not to note how functional programming gains more and more traction.Ramda, lodash, the introduction of promises, map, reduce in ES6

Page 29: A class action

Is it classical oop still a thing?

Page 30: A class action

Functional VS

Object Oriented

So Which one should we use?

Page 31: A class action

Functional -> minimised Object Oriented -> segregated

It’s about State

Since the beginning as programs got more and more complex the “state” problem rises.Pure functions shouldn’t deal with state, so we deal with the problem minimising the state.In Object Oriented we package the state in units called objects.

Page 32: A class action

And if you think about minimising the state and segregating what’s left over is something we do in distributed systems as well since dealing with lots and shared states goes towards madness.

Page 33: A class action

Functional VS

Object Oriented

Page 34: A class action

Functional +

Object Oriented

What about using them together?

Page 35: A class action

do we really need class?

But…

Does class and classical oop really help in all this?Well, minimising the state is something only our brains can really do at design time.What about segregating what’s left?

Page 36: A class action

What about modules?- they have their own isolated scope- they’re easily reusable (which is mostly what we care of)- has its own scope and expose a convenient api (kind of like a service in a soa)- seems pretty easier than what happens in, let’s say for example java ;P

Page 37: A class action

Let’s just try to not go nuts on modules now ;)I mean, the mind blowing growth of the javascript ecosystem is well known and very positive, but sometimes the frenzy for pushing and using the latest module can be potentially harmful.unpublishGate? Do we really need an pm module for 15 lines of code?

Page 38: A class action

A D V I S O R Y

Controversial

Contentany way:You might strongly disagree from this point on.

Let’s talk about coffee for an instant.

Page 39: A class action

The espresso, as we enjoy it in Italy.When coffee is good you don’t need sugar…

Page 40: A class action

Some others might actually like it sugared.I use to, until I tried and learned how more enjoyable coffee without sugar can be.In the same way I understand why “class” was an highly requested feature, for years I used the classical oop and I still do in those languages where it makes sense.

Page 41: A class action

And really, to me “class” seems really more like aspartame more than sugar.The difference is that aspartame can kill you.

Page 42: A class action

@sandropaganotti

4 years ago a friend come back form a conference in london telling me:“Hey, you know what? Objects are just a side effect of functional programming” and he blew my mind starting my journey away form the classical OOP.

Page 43: A class action

So by the end it’s always important to understand our tools, their meanings and how they work.

Page 44: A class action

It’s not always easy, nor it’s quick and there are a million variable all around.I changed many languages in my professional life. I was taught that the good computer science professional should learn at least 1 language per year.

Page 45: A class action

But in case of javascript I always felt more like the general approach, rather than proving the necessary paradigm shift, it’s more about turning it in something more and more familiar because for one reason or another more and more people need to deal with it and it’s easier to stay in our comfort zone.

Page 46: A class action

In our career we all build up a pretty significant baggage of experiences

Page 47: A class action

dedicated to @cirpo

But sometimes is good to let go, exit out confort zone and let it go.Re-mesh all what we learned and what we know.

(please tweet to @cirpo the frozen song in your native language)

Page 48: A class action

Because the real building blocks are simply all our experiences, and the real reusability comes from our ability to turn and compose around all what we know.

Page 49: A class action

Thank you very much for listening,

and happy coding.