A note to beginners - clever solutions

If you are a beginner and are reading the megathreads for each challenge, you may have noticed that some people are posting some really clever solutions. I wanted to tell you that the goal shouldn’t be to write the most clever or least-amount-of-lines solution, it should be to write a solution that is readable and clean. This can be a heated topic, and I don’t intend for this thread to be a debate on the topic.

My suggestion is to stick to writing code that you understand, and don’t worry if you’re not coming up with these clever lines, or that you don’t understand them. It’s ok. In my experience with working on a very large codebase at work, there is no clever code anywhere. It’s so big that it needs to stay simple and readable in order to follow whats happening and be maintainable.

Here are some articles on this topic:

Note: if you write clever solutions to these challenges, and you enjoy it, that’s great. I’m just trying to help out the beginners to not get discouraged by seeing the clever solutions and thinking that that’s what they need to aim for, or that it’s the ideal solution.

18 Likes

Thanks for this @codeOnMaster, I’m going to pin this on the forum for visibility. Great suggestions!

3 Likes

To not get off topic, I’ll simply add:

do not mindlessly follow or copy paste the answers posted on megathreads.

Plenty of answers are worse than just clever code because they pass the tests but have clear deficiencies. Keep that in mind.

Also, as someone who posts these ‘clever solutions’, I wanted to agree that you probably do not want clever one liners all over your codebase. Readability is king.

Yet, and I must end with this, part of not writing unnecessarily convoluted code is being aware of the alternatives to what you are writing.

A little learning is a dangerous thing ;
Drink deep, or taste not the Pierian spring :
There shallow draughts intoxicate the brain,
And drinking largely sobers us again.
(Alexander Pope)

2 Likes

This is great. I was also intending to post such a thread right from day 2 but hesitated a bit because of the same (high debatable) issue. Thank you so much :innocent::innocent:

2 Likes

thank you for the great words. they really are comforting and i hope those affected take heed to this advice.
may the code be with you

2 Likes

Thanks for posting this – a few days in I realized I needed to focus on basic solutions, logic that I could follow and coding that I knew now (or could easily learn) vs. being overwhelmed by the more advanced solutions posted. I know overtime my knowledge will expand but for now I’m happy when my solutions work and I understand why :slight_smile:

1 Like

So I think what would help me the most is to hear about HOW one way is better, preferred, etc… than another. The issue with JS is that there are literally multiple nearly similar ways to complete a task. I’d love a thread run by LightHouse, or just an explanation once a challenge has a substantial thread, that would explain a few of the more popular approaches in terms of what an experienced JS programmer might do and why. The video paired coding examples are great, but I don’t think they explain all the options and I think that would help in trying to figure out what approach to even try and how to see the problems from a variety of approaches.

This is highly debated, and I’m still learning this stuff myself.

One thing I can point you towards that isn’t specific to the challenges, but that is good practice, is a style guide. There are many, I like this one myself: https://github.com/airbnb/javascript

It gives some answers to ‘why’ for certain things.

One specific example for the challenges, that I’ve personally used a few times so far, is the use of switch/case. When there are more that 2 options like today’s challenge, which has 4 (north, south, east & west), it’s probably best to use swith/case instead of many if / else if statements.

This is cumbersome to read:

if (move === 'north') {
  do this;
} else if (move === 'south') {
  do this second thing instead;
} else if (move === 'east') {
  do this 3rd thing instead;
} else if (move === 'west') {
  do this 4th thing instead;
}

This is easier to read and understand:

switch (move) {
  case 'north':
    do this;
    break;
  case 'south':
    do this;
    break;
  case 'east':
    do this;
    break;
  case 'west':
    do this;
    break;
}

This is one example. And it could be debated so nothing is really absolute.

In the end, it’s best to write something that YOU understand and can follow. Over time, with experience you’ll be able to use more complex solutions that you will easily understand. In my opinion, your focus should be on this: is it readable? If it is, then good :slight_smile:

Totally agree. Readable wins over clever any day. And great example btw.

I just think especially with JS where there seem to be multiple nearly identical ways to do something, there must be a more modern take and a more basic take, but I couldn’t tell you what those would be!

The style guide you linked to is helpful. Thank you for that.

1 Like