Randy_Johnson's avatar

My code is a đŸ€Ź mess

Do I

a) keep pushing forwards until I have a complete product
b) stop now and take a week sorting out everything
0 likes
6 replies
Tray2's avatar

This is what I do.

  1. Write code to a make a feature work.
  2. Refactor/ clean up the code of said feature.
  3. Start with the next feature and repeat.

If you push on and finish the product, it is likely that you never clean up the code.

So I'd say C. clean up some code, write a feature, clean up some more code until you are done. Make small improvements of the code in iterations, don't try to clean it up all at once, it will likely be overwhelming.

2 likes
jlrdw's avatar

I agree just plug away a smaller section at a time and try to refactor as you go.

Can you show some code you are referring to?

On so many of Jeffrey's videos he will "bang out" what works first, but prior to moving on he cleans it up and refactors it so it's clean code.

I basically do the same, a segment at a time, look it over, and refactor as needed.

shez1983's avatar

if its not some topsecret work - share repo/ some code samples for feedback

martinbean's avatar

@randy_johnson No code is “perfect”. It’s all written with various constraints (time, resources) in an ever-shifting landscape (language and library updates, advancements in external fields such as AI, etc).

Instead, just make a list of what is actually “messy” and why. Write down discreet, actionable tasks (like, “improve uploading logic”) rather than something ambiguous and open-ended like “fix messy stuff”. Then, chip away at the list, and keep re-evaluating the items on it. For example, if you can’t proceed with X without first refactoring Y, then do so. But limit the refactoring to just Y, otherwise you’ll drive yourself crazy, will have touched nearly every file in your application, and now have a massive pending merge that massively deviated and increased in scope from what you were initially intending a la Hal from Malcolm in the Middle trying to fix a lightbulb.

simotion's avatar

Not exactly a black and white answer to that. A week could be time well spend to fix technical debt before interest adds up.

Does the pending work require you to interact with that code? For example, does the code you have now contain service classes that you need to use in your pending work?

If so, take time to refactor, because delaying it will increase your technical debt.

If it's very loosely coupled, works and doesn't have an impact on your pending work; I would focus on finishing the MVP and refactor later on.

No code is ever perfect, and perfection is the enemy of good. Striving for perfection will delay your product and many projects have never been finished that way. Trust me, we've all been there.

Get your MVP ready. Once there's cashflow start refactoring.

My strategy while coding is:

  • Abstract where needed
    • form validation always in formrequest classes
    • business logic in service classes immediately
    • keep controllers (c)lean
    • build controllers for every endpoint instead of using closures inside your routes file

Even if the service classes or form request classes get messy, it's contained and you can optimize later on. Even if a model becomes a god class, it's contained to that model.

Just make sure you don't cram everything from validation to business logic inside the controller(s), because that gets messy real quick.

If a service class get's messy, it's easy to optimize later on.

Oh, en build tests from the get go. Your future self will thank you for it.

Please or to participate in this conversation.