frontend-horror icon indicating copy to clipboard operation
frontend-horror copied to clipboard

Should codes in the solutions as short as possible?

Open bcinarli opened this issue 10 years ago • 14 comments

I thought after @gartz's pull request when we providing a solution for the bad practice or the ill code, the "How it should be?" part contains the most shorter code solution or not?

What do you think?

@v0lkan

bcinarli avatar Feb 19 '15 06:02 bcinarli

Well I will give a pice of work to think about :)

functions isntTrue(){
    if( true ){
        return false;
    }
    return true;
}

vs

functions isntTrue(){
    return !true;
}

:)

gartz avatar Feb 19 '15 06:02 gartz

Haha mind-blowing, I like these kind of tricks :)

bcinarli avatar Feb 19 '15 06:02 bcinarli

my deep love for this one:

functions isntTrue(){
    return true !== false ? true : false;
}

gartz avatar Feb 19 '15 06:02 gartz

I'll just stick with

var that = that || this

bcinarli avatar Feb 19 '15 06:02 bcinarli

that's awful! You are declaring that is that, but if that isn't something than that is this.

I mean, this usually is the reference to the context of a function, and that is used to store the context to be used in the scope of a closure. (according Douglas Crockford, some people like to use var selfinstead that. Maybe because PHP, I doubt that is because python :P)

gartz avatar Feb 19 '15 06:02 gartz

Choose your destiny:

return Boolean({});
return {} == true;
return !!{};

gartz avatar Feb 19 '15 06:02 gartz

Usually I'm assigning this reference in a method to _self or _this, and in some cases referencing the Object itself as in this sample.

btw way, that that sample is just a funny sample for visualising a mind-blow :D

bcinarli avatar Feb 19 '15 07:02 bcinarli

In the example you are using your _this as a var in the closure scope for the methods created inside the object with methods that reference _this. You can use this inside the method checkCommand, because inside it this === _this (the context of the method it's the same object that you reference with the variable _this).

If you wan't to discuss more about it my hangout is [email protected]

Here is a mind blowing for you:

alert((![]+[])[+![]]+ (!![]+[])[+!![]]+ ([]+{})[+!![]]+ ([][{}+{}]+[])[+!![]]+ ([]+{})[!![]+!![]+!![]+!![]+!![]+!![]]);

now you can finish :+1:

gartz avatar Feb 19 '15 08:02 gartz

Because of the context changes, I rarely use this reference, mostly in $(this)

JavaScript though very interesting language. Probably you already saw this

I've added your email.

now you can sleep peacefully :D

bcinarli avatar Feb 19 '15 08:02 bcinarli

nice questionary, I missed one by attention lol.

Well it depends on what you are doing, have a dynamic context can allow you to do such things as prototype methods and mixing and decorator.

And if you want to glue than in the context, you can always use bind:

var a = {
  foo: 'bar',
  zaz: function(){ return this.foo; }
}
var b = {
  foo: 'traz',
  zaz: a.zaz.bind(a),
  traz: a.zaz
}

a.zaz(); // 'bar'
b.zaz(); // 'bar'
b.traz(); // 'traz'

gartz avatar Feb 19 '15 17:02 gartz

Shortest ways generally suck :sweat:

We should consider readability and expressiveness before its length when deciding a code sample is good enough to suggest as the solution to a bad practice.

scriptype avatar Feb 19 '15 19:02 scriptype

@scriptype I also prefer readability and ease of understanding. But there are some case as in the samples below. I'd rather go with the shorter one.

// longer
if (e.which !== 8 && e.which !== 0 && (e.which < 48 || e.which > 57)) {
    return false;
}

// shorter
return e.which === 8 || e.which === 0 || (e.which >= 48 && e.which <= 57);

@gartz I haven't use bind() method before. Usually tend to use .apply() and .call() method since I'm firing the function immediately in most cases. I learned new things today, thanks to you :+1:

bcinarli avatar Feb 19 '15 19:02 bcinarli

I have used bind mostly in three cases: jQuery success/error callbacks, in React where we iterate over the child components in render table to preserve this, to create partial functions. That is also how jQuery re-binds this to the dom el itself, so a good interview question.

ustun avatar Feb 19 '15 19:02 ustun

@bcinarli you are welcome, just keep in mind that bind it's ECMAScript 5 specification, it won't work in IE before 9, well there is a simple shim at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind and underscore and similar users can go for something like _.bind()

gartz avatar Feb 19 '15 19:02 gartz