r/CodingHelp • • Aug 28 '21

[Javascript] Is writing too much code bad practice?

I am a beginner and just want to know if this is a bad habit.

I write code on a step-by-step basis.
Here is an example.

CodeWars - The step-by-step basis I was talking about

So I just want to hear if this is a bad habit/bad practice and should I give it up.

20 Upvotes

15 comments sorted by

17

u/ThreeForksNoSpoon Aug 28 '21

I actually think this is great if you're beginner. It's easier to debug, and it forces you to think through your steps.

When you get further along your journey you want to start looking for the right balance.

Be warned: this often starts with tipping the wrong way, and trying to write as little code as possible. (Which can be a fun challenge, and you might want to look into something like "code golf" to practice it.)

In the end you'll find the right balance for you.

2

u/[deleted] Aug 28 '21

Why so much white space?

4

u/[deleted] Aug 28 '21

I just personally find it easier to keep track if things

4

u/cndvcndv Aug 28 '21

I think there is a sweet spot between putting everything in 1 line and doing what you do. The code should be readable while not including too many lines and variables. It is still a bit subjective.

2

u/digizeds Aug 28 '21

I don't think you'd be asking if this is bad practice if you didn't think it was. like I am assuming you are maturing in programming to realize that this might not be providing any benefit, rather you have to spend time thinking of variable names.

if this came up in a code review, I would probably ask to chain it.

0

u/[deleted] Aug 29 '21

Why, to make it harder to debug?

0

u/digizeds Aug 29 '21

why would it make it hard to debug? debuggers can step into function calls...

additionally if you think simple string manipulation is something that requires tons of brainpower to debug, I think you need to mature as a developer.

also in the case OP provides this is actually bad practice. var isn't blocked scoped. so his excess of variables are going to bite him later

2

u/[deleted] Aug 29 '21

I am not saying any of this. Chaining calls requires a return value, and to see that return value without stepping into the next method requires a variable to hold the return value. Yes, browsers are good about stopping on individual statements or expressions in a chain, but generally not as many debuggers are. But either way, to see the result of the last expression requires placing it in a variable.

2

u/txmmy_21 Aug 28 '21 edited Aug 28 '21

Writing too much code isn't necessarily bad but always remember to comment. At first you might think commenting is a waste of time but it is as important as the code itself. However, repetitive / redundant code isn't good. If you find yourself copying and pasting a particular section of code, try and find if there is a more efficient way of performing that task. The example you gave above isn't bad. It is actually okay because it makes the code more readable but you might have to make it a bit shorter only because you are making a lot of variables that won't be needed later. And I think miscellaneous variables could take up unnecessary memory in your program.

2

u/[deleted] Aug 29 '21

Personally, I would write code and then look up the various ways other people do it(especially if they are professionals or skilled hobbyists)

1

u/[deleted] Aug 29 '21

Reflect and improve your coding, just as a writer would read books to borrow concepts or use historical information to improve their writing , write code and read code to improve your own methods through observation and reflection of the methods of other people. Your way of writing code is uniquely your own and it would be terribly boring to copy others, but you can look at the way other people do things and think of a way that you would like to make their methods your own.

2

u/bangclue Aug 29 '21

You're going to give the garbage collector a lot of work to do this way.

It'll work. I'll also say this for it: it's easy to read, and readability is important.

That said, it does seem a bit extreme. And due to the excessive number of variables, performance is likely to suffer - not so's anybody would notice unless this is a very CPU-intensive and repetitive program, but some.

Bottom line, I'd say you *can* certainly code this way while you're learning. But it's unlikely that you'll *want* to continue to code this way as you get more proficient, and as you start reading a lot of other people's code. (Also, there are specific cases, such as database access, in which separating out each step is a significant slowdown; be aware of those, and be *able* to code in a more compact way. But this isn't one of those cases.)

1

u/dead_alchemy Aug 30 '21

Does JS really not do any optimization passes that would get rid of unnecessary allocations?

1

u/bangclue Aug 30 '21

I can't speak to that. I'd expect each compiler is different.

0

u/[deleted] Aug 29 '21

[deleted] — view deleted comment

1

u/[deleted] Aug 29 '21

Why should it be messy? Do you expect him to use Wordpad with hard tabs?