Does solving algorithms help you write better code?

I’ve only been working on front end code so far but I haven’t applied anything I learned from algorithms into my code and forgot most of what the algorithms were. Are algorithms more applicable in back end code? Have any of you used algorithms in your code and have algorithms helped you?

Take a grain of salt my answer.

For me personally, no. What helps me is becoming familiar with the language and/or framework you work with and using the built in functions it may have already provided. So that means spending some time just exploring and reading the documentation. You’ll always discover something new.

In your haste to dive in, for example, you may spend hours writing your own algorithm and several lines of code later you find out there is a single line function you could have used if you just imported X library or namespace.

Algorithms are just solutions to common problems.

I’m not sure what you mean by “solving algorithms”. I assume you mean is it important to learn them and use them.

Now better code is very subjective, there’s a lot to good code than just how it solves a problem. A lot of people will say “On the front-end you don’t need algorithms” I would agree you wont be dealing with large computation times mostly, but at the same time you never need to care about algorithms until things grind to a halt.

So why should you learn it? Probably the most important is to know whats out there when it comes to common problems. You might not need to know how to implement a solution, but you should know what problem your actually trying to solve. Most “hard problems” come down to a few common hard problems.

Now personally I’ve used algorithms to solve the code “that grinds things to a halt” and that’s about it. But then I don’t work on algorithm heavy work most of the time, but when I do its usually a walk in the park.

So really getting algorithms down depends on the type of work your going to be doing, but I would never consider it to say “Naw you don’t need it” because the knowledge will be there for when you do need it. Otherwise you will end up doing “browser breaking code” like this:

var foundEl;
arr1.forEach((arr2) => {
  arr2.forEach((arr3) => {
     arr3.forEach((el) => {
      if (el) {
        foundEl = el;
      }
    });
  });
});
console.log('my element: ', foundEl);

(yes this is a terrible example, but its a real example)

I know it’s a contrived example, but what is it supposed to do? Is looping through a nested array and printing the last non-falsy value the intended behavior?

Its a contrived example.

Anytime you have data that you want to be different data, you need an algorithm. Sometimes it is simple. Sometimes it it complex. But any non-trivial app is going to involve some algorithms. I don’t think I’ve ever written any substantial program that didn’t have some algorithm in it.

It’s not like it used to be where you needed to code them all yourself. JS has a lot of nifty algorithms built in like sort and find and filter. Back in the old days you had to code them yourself.

When people say they don’t use algorithms, I think what they mean is that aren’t using complex algorithms that require a lot of thought. But part of what makes it not require a lot of thought is experience with algorithms. I just wrote an app tonight where it depended on a few algorithms.

Not all the test algorithms you do when learning are necessarily “real world”. That does not mean that they are not useful. In football practice, the players do a lot of jumping Jacks - no one ever won a game doing jumping jacks.

Even if you think you will never use an algorithm in real life, they are common on interviews and therefore are good to know.

People are always looking for excuses not to do things they don’t like. But (IMHO) you are just hurting yourself in the long run. One day (probably many days) the right algorithm will save your life.

Anyway. I think you’re talking about the algorithm challenges on FCC. The point of those is to teach you complex concepts (like advanced operations on objects) and to give you experience solving complex problems. That experience is universally relevant.

If you mean the kind of algorithms people write papers about, I’ve only used two in the course of the FCC projects (this one and this one). They probably represent 2% of my JavaScript code, but had I tried to come up with them myself, I probably never would have finished.

I don t know about others but for me algorithms taught me a lot. About helping us in general with better code, it can be. Why? Well, if you have a problem that can be solved with algorithms only and if you don t know about algorithms and you try to solve it your way, prob your code would be quite ugly and lots of unnecessary lines of code

But I think the purpose of algorithms is to help people think like developers. For me it was important, I don t have a CS degree or anything related to IT .
I watched lots of tutorials basics of Javascript I saw them talking about variables, strings, objects, arrays and methods on them and in that moment I was thinking: great now how is this gonna help me build something like a carousel I keep seeing on webpages or a toggle panel or something in general.

Later when I worked with algorithms in these challenges I saw how you can split a string, reverse it using an array and do other crazy stuff. URL is a string also you can do stuff with it u can add variables to make it more dynamic. So yeah some of them have a purpose even in real applications. They helped me understand how you can work with them and use them even tho I didn t have a CS degree and stuff.

1 Like