7 Replies - 1697 Views - Last Post: 23 May 2017 - 11:16 PM

#1 bobsmith76  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 25
  • Joined: 14-February 17

Avoiding spaghetti code: how often do you revise code?

Post icon  Posted 20 May 2017 - 09:46 PM

I'm writing some software that I hope to work on for the rest of my life. I have 9000 lines so far in Python. Till now, I have rarely had to worry about spaghetti code, that is to say, code which is so complex that even I have trouble understanding it and debugging it. I'm starting to think that each time I get the code to do exactly what I want, I should go back and try to simplify it, since now I know how to do what I am trying to do. I have always been reluctant to do this because I'm in such a hurry to move to the next step which is so much more exciting. But now I'm starting to think that maybe it's in my own interest to go back and revise code and make it simpler because it will make debugging easier in the future.

What is your experience? Do you often go back and revise code for big projects? Are you in a hurry to move to the next step and decide not to go back and revise? Any thoughts would be appreciated.

This post has been edited by macosxnerd101: 21 May 2017 - 12:23 PM
Reason for edit:: Featured thread


Is This A Good Question/Topic? 3
  • +

Replies To: Avoiding spaghetti code: how often do you revise code?

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2111
  • View blog
  • Posts: 4,168
  • Joined: 30-May 10

Re: Avoiding spaghetti code: how often do you revise code?

Posted 20 May 2017 - 11:22 PM

*
POPULAR

Do you have a test suite to automatically exercise the program whenever you make a change, to ensure that you haven't introduced any bugs?
- Manual ad-hoc testing is superficial at best, and so very easy to skimp on (or just plain forget) when you're in a hurry. Automated tests mean you can just hit a button and take a work break.

Do you use any kind of test coverage tool to check how effective your tests are?
- Sure, you've got 100's of tests, but if they only hit 50% of the code, does it really say 'quality'? You should be aiming at say 95%++ of all non-error paths in the code.

Do you have a source control system such as git?
- Code branches will save you more times than you can count. The ability to 'diff' between any pair of commits (to find out what broke when it used to work for example) is invaluable.

Yes you should refactor, but without all of the above, it will be a crap-shoot.

Recognise the possibility that at some point, maybe a complete re-write is in order.
Was This Post Helpful? 5
  • +
  • -

#3 jon.kiparsky  Icon User is online

  • Screw Trump (before he screws you)
  • member icon


Reputation: 10495
  • View blog
  • Posts: 17,829
  • Joined: 19-March 11

Re: Avoiding spaghetti code: how often do you revise code?

Posted 20 May 2017 - 11:35 PM

How often do I revise code? Well, I try to follow Fowler's approach of constant refactoring. I generally start out by looking over the worksite and doing any initial cleanup that's needed to let me work on acceptable code, and then I make the changes I need and check for any refactoring that I can do as a result. So I would say the code I work on is being revised more or less constantly. However, because I work on a team that has pretty strong code standards and does pretty good review, the revision is usually pretty minor. I generally try to avoid refactoring for its own sake, unless I'm looking at serious legacy code that needs for example to be brought under test so we can work on it, or that needs really fundamental improvements like ripping up a 5000-line file with thirty different classes in it into thirty files. So I don't generally have any tickets which are simply about revision, they're all about delivering value to our stakeholders.

Quote

Till now, I have rarely had to worry about spaghetti code, that is to say, code which is so complex that even I have trouble understanding it and debugging it. I'm starting to think that each time I get the code to do exactly what I want, I should go back and try to simplify it, since now I know how to do what I am trying to do.


Here's a few questions to ask yourself:
  • look at your functions. each function should do exactly one thing, and its name should tell you what that thing is. keep to this standard for better clarity
  • how long are your functions? If they're longer than 10 lines, consider making them smaller. (if you need more than ten lines, you're probably doing more than one thing)
  • do you have tests which exercise the functionality that's important to you? are those tests well named and expressive? do you run them?
  • if a reasonably competent Python programmer took a typical bug or feature off of your to-do list, how hard would it be for them to deliver the fix or feature? (obviously, we're assuming they've never looked at your code before)


Quote

maybe it's in my own interest to go back and revise code and make it simpler because it will make debugging easier in the future


I would be wary here. What changes should you make? Where do you stop? How do you know that what you're doing is an improvement? And most importantly, what is the impact of this work on your velocity?
As much as possible, this work should be done by setting solid standards for your code, writing them down, and holding yourself (and your team, if you have a team) to them. This will feel like it's slowing you down, because every feature ticket will now require you to do some head-scratching about how to make it comply with your shiny new standards, but the win is that you'll only be doing this on code you're actively developing on. The stuff that you're not touching, you'll notice, is not costing you any developer time because, well, you're not touching it. Also, this will ensure that going forward you don't find yourself competing over whether to allocate work to features or to codebase improvement - the cost of keeping the codebase up to snuff will be baked into the cost of features, as it should be.
Was This Post Helpful? 4
  • +
  • -

#4 bobsmith76  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 25
  • Joined: 14-February 17

Re: Avoiding spaghetti code: how often do you revise code?

Posted 21 May 2017 - 11:28 AM

View Postjon.kiparsky, on 20 May 2017 - 11:35 PM, said:

[*]look at your functions. each function should do exactly one thing, and its name should tell you what that thing is. keep to this standard for better clarity
[*]how long are your functions? If they're longer than 10 lines, consider making them smaller. (if you need more than ten lines, you're probably doing more than one thing)


Wow! What amazing advice!! I just started employing this advice and man! it is working wonders! Someone had told me this before but they just said 'make the functions short' and I thought to myself 'my functions are short', that is, 200 lines. Had they written exactly what you wrote I would have saved a lot of time.

Quote

[*]do you have tests which exercise the functionality that's important to you? are those tests well named and expressive? do you run them?

yes. I just hit one button and it takes 8 seconds to run a whole battery of tests, so I'm good there.
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Screw Trump (before he screws you)
  • member icon


Reputation: 10495
  • View blog
  • Posts: 17,829
  • Joined: 19-March 11

Re: Avoiding spaghetti code: how often do you revise code?

Posted 21 May 2017 - 08:04 PM

Glad I could help! Yeah, the ten lines thing is a little startling at first - what can I do in ten lines??? - but it really makes a huge difference when you start doing it.

There are a few books that might be useful to you, based on what I'm seeing here. Fowler's Refactoring, which I referred to above, is a classic and worth at least two reads IMO. Feathers' one on Working Effectively With Legacy Code is good if your code has got to the point where you think it really needs the sort of up-on-blocks treatment that real legacy code gets. Sounds like you might not be quite at that stage, so maybe you don't need that one. Also, Bob Martin's Clean Code is an absolute must-read. It's all about coding style that supports long-term maintainable code. These are books that people tend to keep and lend out, so used copies are hard to come by, but your local library may well have copies of Fowler and Martin, if not Feathers.
Was This Post Helpful? 1
  • +
  • -

#6 bobsmith76  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 25
  • Joined: 14-February 17

Re: Avoiding spaghetti code: how often do you revise code?

Posted 21 May 2017 - 10:06 PM

View Postjon.kiparsky, on 21 May 2017 - 08:04 PM, said:

Glad I could help! Yeah, the ten lines thing is a little startling at first - what can I do in ten lines??? - but it really makes a huge difference when you start doing it.

There are a few books that might be useful to you, based on what I'm seeing here. Fowler's Refactoring, which I referred to above, is a classic and worth at least two reads IMO. Feathers' one on Working Effectively With Legacy Code is good if your code has got to the point where you think it really needs the sort of up-on-blocks treatment that real legacy code gets. Sounds like you might not be quite at that stage, so maybe you don't need that one. Also, Bob Martin's Clean Code is an absolute must-read. It's all about coding style that supports long-term maintainable code. These are books that people tend to keep and lend out, so used copies are hard to come by, but your local library may well have copies of Fowler and Martin, if not Feathers.



Thanks for the book recommendation. I sincerely appreciate it.
Was This Post Helpful? 0
  • +
  • -

#7 jjl  Icon User is online

  • Engineer
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,960
  • Joined: 09-June 09

Re: Avoiding spaghetti code: how often do you revise code?

Posted 23 May 2017 - 12:49 PM

Design the software architecture before writing the code. No sense if doing the same work twice

This post has been edited by jjl: 23 May 2017 - 12:49 PM

Was This Post Helpful? 1
  • +
  • -

#8 bobsmith76  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 25
  • Joined: 14-February 17

Re: Avoiding spaghetti code: how often do you revise code?

Posted 23 May 2017 - 11:16 PM

Jon, I'm reading Clean Code now, and very much enjoying the book. Thanks once again for the tip.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1