Does it matter how concise or "smart" my code is?

I just earned my algorithms & data structures certification, at long last! This certification was much more challenging than the previous one; the intermediate algorithm challenges and the projects in particular took a long time for me to finish.

My solutions are probably not the most elegant or efficient, to be honest, with some of them reaching or exceeding 70 lines. So, usually, I’m just happy I could find a solution in the first place. But my question is: does it matter how concise or smart my code is? how can I improve?

With time you learn how to make your code DRY(Dont Repeat Yourself) but the most important thing is getting it right

1 Like

Code shouldn’t be “smart”, that’s not really something to aim for, it should be as dumb and obvious as possible.

What this tends towards in practice though is that the more skilled someone is with programming, the more the “dumb” solution looks clever and concise to someone not as familiar with the language. And from the other perspective, long solutions created by those less familiar with programming will often be extremely complex and very difficult to follow and debug.

Concision is a virtue as long as it doesn’t obfuscate meaning: less code === less code to debug and maintain === less bugs. No code at all is ideal, as there will be no bugs caused by it. But failing that, as little as possible is good.

Edit: as an addendum to this, as noted in a couple of other answers, efficient code is not necessarily concise either. Be wary of optimising for efficiency straightaway (computers are plenty fast, and premature optimisation/second guessing the compiler is a fool’s errand in many cases), but understanding how to write efficient code is an important skill. Memory-intensive programs like calculating primes are normally a good thing to practise in this regard (as is learning a language that needs you to manually handle memory allocation, like C).

It’s all experience though – it takes time to memorise useful patterns, to build up the knowledge necessary to make good judgement calls about simplifying code, so don’t worry too much: read a lot of code, take what you can from it, come back to algorithm-like challenges regularly to test out ideas that are new to you. Don’t get hung up on principles like DRY (in particular don’t try to prematurely abstract things just for the sake of avoiding repetition), they are important, but getting things working first and understanding how they are working is the most important bit at first as @jameskomo says.

1 Like

I agree with @DanCouper that it takes time and experience and especially reading a lot of code. As you’re learning programming by yourself it’s really really hard to know if you’re writing the “smartest” and “concisest” code you can or if a better approach exists. So you just have to learn from experiencing other people’s problem solving skills.

You can find someone else on here doing the same problems, check out their approaches and discuss the pros/cons of both of your solutions. Also definitely check out the other solutions on the hint page. Go through each one (basic/intermediate/advanced) and make sure you understand everything that’s happening. Take notes about things you haven’t seen before or an approach that you think is good. Try to use these concepts in new projects that you take on. Then in a while, you can try to solve these JavaScript projects again. You will come up with a different solution which is probably a lot better than your original!

I recently finished the JavaScript certification too and then started doing problems on codewars.com for more practice with problem solving. They are quite challenging, but lots of fun. The best part is that after you solve a problem you can see everyone else’s solutions. So you can see just how bad yours was… :sweat_smile: But you can also learn a lot too. It’s good for a little more practice, but there is a danger of it becoming demoralizing-ly difficult or a time suck. Just have fun with it. :slight_smile:

2 Likes

When I started codewar and didn’t know anything about algorithm I also felt that way (how bad my code was and how smart others answers were).

But as you get better you will see that a lot of the top upvoted ‘smart’ or ‘best practice’ answer, as cool as they look, definitely aren’t always the most efficient way to solve the problem.

So @asemarian don’t try to have the smartest answer but try to get readable and efficient code:
step 1: Find a solution that works.
step 2: Try to refactor your code to make it more readable and efficient.
step 3: Search for other people solution and study them.
step 4: Refactor your code with what you have learned in step 3.

If you do that for every algorithms challenge you do. It’s just a question of time, you WILL improve drastically.

2 Likes

Yeah you definitely shouldn’t blindly trust random people on the internet to come up with the best solutions. You should try to understand what they’re doing and think about what the pros/cons of the solutions are. Just because something is written in very few lines of code does not mean it has a fast runtime, is space efficient, or is even easy to test/maintain.