Is the style of the advanced solutions the best?

Is the style of the advanced solutions the best?
0

#1

I guess I’m beyond “beginner” level because I am frequently able to get intermediate- and advanced-type solutions for the JS algorithm problems.

But I wonder. Would more senior devs prefer to see the beginning solutions or the more advanced ones? It seems to me the beginning solutions, while longer, are easier to read and understand (and therefore, maintain). Maybe that’s because I’m still not used to reading the more advanced style. What do the senior devs here think?

I’m totally convinced that it’s good to know how to do the advanced solutions, but I wonder if, by calling them “advanced,” freeCodeCamp is not maybe encouraging people to write more obscure but “clever” code.

Surely this has been discussed in more depth somewhere. Where could I read about this issue?


#2

The sample solutions are provided by the community, so there will be some variation in quality and understanding of what “advanced” entails. Can you give a couple of examples where you think the beginner solution is preferable?


#3

There are a lot of trade-offs in coding. But I tend to favor readability. Sometimes you need something to be fast and lean, like if it is going to be done a few million times. But compact code isn’t always the same as fast. Unless there is a reason against it, I tend to favor readability. But also keep in mind that what someone considers readable will change - before I learned more, many prototype methods with arrow functions looked insane to me.

But I agree that there is a “cult of concision”. There are a lot of coders out there that think cramming 10 lines of code onto one line with an inscrutable tangle of syntax is “cool” and “clever”.

But as lionel says, give us an example.


#4

When you become the “new guy” on the team, you’ll figure it out by looking at the existing code.

The less verbose solutions can be more readable and using up-to-date conventions doesn’t always make you a d-bag. For a simple example:

function func(item, thing, other) {
  if (item !== 0) {
    thing += item;
  } else {
    thing += other;
  }
  return thing;
}

Alternatively:

const func = (item, thing, other) => (thing += item ? item : other);

Okay, in this single, simple case, there’s not much benefit. But when you’re dealing with thousands of lines of code, it adds up.

Basically, just always write code with the best intentions.

EDIT: That being said, in most cases you’ll be seeing code that’s more like the first example.


#5

Some are legit and some are bogus.

IMO, advanced solution should either

  • legitimately make the code shorter and more readable.
  • be exemplary by demonstrating the best practice.
  • use brilliant ideas to make the algorithm simpler with practical values. e.g. Using math-magic or smart programming technique to reduce time, space complexity.

A worthy advanced solution:
One solution incorporated well-established mathematics property to improve the algorithm on every aspect.

Not worthy advanced solution:
One advanced solution simply showed that the problem could be solved in more complicated way with Regexp.

I don’t really know about professional developers’ opinions, but books, Quora and StackOverflow are good places to peek a portion of their thought.


#6

There’s a few posts about benchmarking various approaches. Shorter code does not always mean a shorter runtime.

Here is one example: Check for Palindromes [spoilers][solved]