How important is it to have concise code, and at what point does it become bad to sacrificing readability for it?

Hey everyone, JS beginner here trying to learn as much as I can to hopefully find a job later down the line.
I’m trying to make sure that I complete exercises in an efficient way and follow any important conventions for projects that would potentially end up being portfolio worthy and to just have good habits for my future job.

exercise 3 greetAliens() : Write a function that takes in an array of strings and uses a loop to print a greeting with each string in the array.

The function should return the following:
“Oh powerful [stringElement], we humans offer our unconditional surrender!” for every element.

I challenged myself to make this as concise as possible and try to get it to accept as many inputs. Ended up losing all readability but it’s pretty short. My question is as you read in the title.

const greetAliens = (…args) => args.forEach(i => console.log(Oh powerful ${i}, we humans offer our unconditional surrender!));

greetAliens(“Blorgous”, “Glamyx”, “Wegord”, “SpaceKing”);

side note: I’d really appreciate any experienced people who can share skills you have discovered that are most appreciated or tasks that you are most commonly given, Thank you and best regards friends.

-accel

You can find some helpful info on this with a Google search - these are a few things I found:

Is Elegant JavaScript Always Performant? Concise vs. Fast Code Explained

Code readability matters

Code readability vs conciseness

Readability vs Conciseness?

2 Likes

Thank you. This was very helpful

Readability should always be a primary concern while working within the framework of best practices.

Sometimes it’s not possible because the demands are that things be performant (you may need to use some special algorithms, or very case-specific patterns), then you probably want to document it well enough where it’s not obvious to yourself from the future or another developer.

When you’re on a team, code-review will help you understand what is understandable to others and what is not. I have never had a review where anybody says can you write this in fewer lines (or vice-versa). However it is common to hear, this is a bit hard to follow, can you arrange it to make it easier to tell what it’s doing?

Conciseness for its own sake is not really a metric we really go for anymore unless you’re in some sort of constrained environment where the size of the source code is an issue (think home devices, etc.). A lot of work has been done on compilers to optimize your code a lot so as long as you write things in decent time complexity, how many lines it takes does not actually affect what it’s going to in assembly instructions that much.

1 Like

Thank you very much. I appreciate your feedback. I will be prioritizing readability more from now.