HomeХобби и стильRelated VideosMore From: Robin Pokorny

Random hex color – a one-liner in one minute in JavaScript

29 ratings | 286 views
One liners are fun and they can teach you so much. Let's go through this one for generating a random color in hex format. This is the tweet that inspired me to make this video: https://twitter.com/jaffathecake/status/1038030104566358016 *** Check out the list of gear I use to create YouTube videos: https://kit.com/robinpokorny *** Music Downtown Funk by Dj Quads https://soundcloud.com/aka-dj-quads/downtown-funk
Html code for embedding videos on your blog
Text Comments (6)
Paras Arora (4 months ago)
i like one liners...they are quite smart works with language...but we should provide a good comments for them for readability
Robin Pokorny (4 months ago)
Paras Arora Yeah, you are right. Good comments are important 😉
Riki Fridrich (5 months ago)
I used to love these smart one-liners. They make you feel like a 1337 h4x0r, because others often can not figure out how the hell that one-line works. And then I figured out, that this is exactly why these one-liners are, in fact, stupid. First, as a programer, you should not strive to write code readable by computers. Computers are good at reading messy code. You should write code readable by other programmers. Including your future self. Second, these one-liners often do not handle various edge cases. Handling these cases would turn the one-liner into messy multi-liner. There was a thread on Quora about a year ago about exactly this - how to randomly generate hex colour: https://www.quora.com/How-would-you-write-a-JavaScript-function-that-returns-a-random-hexadecimal-color Most of the smart one-liners failed to handle the edge cases. Here's my answer, which is intentionally stupid and multi-line: https://www.quora.com/How-would-you-write-a-JavaScript-function-that-returns-a-random-hexadecimal-color/answer/Riki-Fridrich So I agree that while one-liners can be fun as a learning exercise, you should never ever use them in real world scenarios. Unless you're writing code for some crazy competition, like https://js1k.com/ or http://js13kgames.com/
Robin Pokorny (5 months ago)
Totally agree. Just sometimes it is not clear what is still a one-liner and what not… Also, I have realised it depends on the team and it's experience.
Lukáš Frýdek (5 months ago)
Nice. More one-liners! :)
Robin Pokorny (5 months ago)
Thanks, Lukáš! 😊

Would you like to comment?

Join YouTube for a free account, or sign in if you are already a member.