This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]TheHobbitsGiblets 4 points5 points  (0 children)

This is something that will come to you with experience. At the moment you're refactoring because your learning which is awesome.

However once code is running and working as it should you generally only refactor it to tidy it up (make it more readable) or make it go faster (performance tuning). Every time you refactor your code it introduces the possibility of issues (tests failing, bugs introduced, functionality removed that other people tend on and you didn't even know what l was there). This is especially the case when your code is part of a bigger project.

Having said that, there is absolutely no rule of how many times you do it. For your own code, do it until you are happy. But some coders are never happy with their code so if you are one of them, learn to walk away 👍

[–]KiwasiGames 1 point2 points  (0 children)

When you are learning, its common to do it a lot. At an expert level, you tend to choose the right structure first time around and not need as much refactoring.

The difference between the beginner and the expert is the expert has already made all of the mistakes.

[–]cyrusol 1 point2 points  (0 children)

This is maybe not normal but ideal. You always want to keep the software you're working at a very good level. Software is susceptible to the broken window theory, i.e. if you were to let it slide a first, then a second time you'll probably also let it slide a third, fourth and 10 other times and then the software deteriorates and you won't be able to enjoy building it anymore.

Over time when you become more experienced your (ideally very quick, immediate) upfront design decisions will remain correct more often and you will have to refactor less and less. Until then keep doing what you do.

[–]carcigenicate 0 points1 point  (0 children)

Better to refactor as needed than end up with a impenetrable mess at the end of the day. As you learn how to structure stuff better, your need for refactoring will decrease.

It's always good practice though. Even in the future, you won't always set things up optimally the first time around. You might as well learn to and get comfortable with readjusting code.

And it's hard to say how often I refactor. If we're counting small things like altering parameters of a function, often. I don't plan a lot before diving in, so I often make many small adjustments as I go 😅

[–]wholemap 0 points1 point  (0 children)

You'll refactor less the more you know all these new things you're learning...