سؤال

I got the fruitless task to incorporate some programs my predecessor wrote. One horrible mess of unorganized code.

So far I know only about the automatic identation (C-i). So I wonder if you can suggest some tool.

some key problems I'm having:

  • Assignment to the same structure is sparkled through the whole file
  • This is one big file of code - I would like to split it
  • Quite a lot of code produces results that are never used

Update On the nice looking part I found that the Matlab mode of Emacs can break overlong lines quite reasonable.

هل كانت مفيدة؟

المحلول

Incremental refactoring is the way forward.

  • Pick an m-file that seems reasonably self contained and work out what it's supposed to do.
  • Create a test for the m-file. This can be as simple as loading in some data from a file, calling the function with that data as an argument, and checking that the result matches the original output (obviously, before you make any changes, the test should pass!)
  • Start making changes to the file. Every so often, run the test to make sure that it still passes. If it doesn't pass, then you've broken something - undo your last set of changes (you are using version control, right?) and try again.
  • Repeat until complete, always starting from the least dependent functions to the most dependent.

Unfortunately there is no magic bullet. You can rely on the Matlab linter, which will tell you when a variable is never assigned to or used, or when a function is never called, but beyond that you need to tidy the code up a piece at a time, testing it as you go.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top