r/javascript • • Dec 16 '18

Coming from Python to JS NSFW

const print = console.log

I found my safe space.

607 Upvotes

156 comments sorted by

View all comments

22

u/BloodAndTsundere Dec 16 '18

I actually kind of like this because I always manage a typo when I type 'console.log'. I get a lot of 'cosnole.log'. I think I'm much more consistent typing 'print'.

26

u/Earhacker Dec 16 '18

VS Code, Atom and the like usually have a snippet that autocompletes when you just type "log".

0

u/[deleted] Dec 16 '18

[deleted]

33

u/Earhacker Dec 16 '18

You could go to Menu -> Preferences -> User Snippets -> JavaScript and add this to the file:

"Print to console": {
    "prefix": "print",
    "body": [
        "console.log('$1');",
        "$2"
    ],
    "description": "Log output to console"
}

And now every time you type "print" in a JS file it will autocomplete to "console.log()" with your cursor between the brackets.

Safe space safely restored!

0

u/[deleted] Dec 16 '18

[deleted]

16

u/Earhacker Dec 17 '18

for loops are for dirty Python casuals. Over here in JavaScript we use Array.prototype.forEach.

(but sure, we use your fornum loop sometimes too)

0

u/linuxdropout Dec 22 '18

If you want your code to be approximately 90% slower -_-

1

u/linuxdropout Dec 22 '18

1

u/Earhacker Dec 23 '18

If you really need those milliseconds, forEach is not your only problem. Performance isn't everything.

1

u/linuxdropout Dec 23 '18

It's not a matter of milliseconds it's that it's an entire magnitude slower. Sure if you're just doing a single foreach loop then it is what it is. But if you're doing a "common pattern" of filter -> map I've seen all over code bases. And then you do that in 10 different places on a front-end application, all at page load time. All of a sudden you've added an entire second to time-to-interactive.

I get the whole maintainability thing. And there are plenty of places that it ends up being much cleaner and easier to maintain using map/filter/reduce/foreach. And in some places you can add functionality with callback chains or async-await.

But it's something to be aware of that I've seen many more developers just use Array functions as the "default" without thinking.