Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advice for dealing with code maintenance [closed]

I've been working at my university this summer in an image/video lab. Just recently, my professor gave me a program written by a grad student who just left the program to "fix up", because it was "giving some errors."

The project was written in C++ (seems to be a recurring bad sign in student code). I opened the project in VS08, and ran the project, and turns out, the "errors" was a bad_alloc. Sure enough, the memory management, or more precisely, the lack of it, was the problem.

The programmer seemed to like mingling mallocs, news and new[]s throughout the entire code, with absolutely no free, delete or delete[]. To make it worse, all the objects seem to do atleast 4-5 unrelated things. And to top it off, here's a comment left by the programmer:

//do not delete objects, it seems to cause bugs in the segmenter

From what I can see, there's a nice unhealthy mix of reference of pointers and references, and all values are changed by passing by reference to the monolithic class functions that may as well be static. At compile time, there were around 23 warnings---stuff like possible loss of data when converting from double to char, around 17 unused variables etc. It's times like this that I wish C++ never existed in universities, and that all lab work was done in like python or matlab...

So now, the professor wants me to "fiddle" with the program so it can run on datasets around 10 times larger than what it was used to. I admit, I'm a bit afraid of telling her the code is garbage.

StackOverflow, you guys have never failed before with giving good advice, so now I plead, any advice on dealing with situations like this would be MUCH appreciated.

EDIT The code is around 5000 LoC

EDIT2 The professor decided to go with the easiest approach. Which was getting more RAM. Yay for being about to throw money at the problem...

like image 217
Xzhsh Avatar asked Aug 05 '10 18:08

Xzhsh


People also ask

What is coding maintenance?

Maintaining a code base is about finding better ways of doing what you used to do. Software Business is about writing code to solve a client's problem, but it is not always about solving the problem. How you solve the problem also matters. So you would need to evaluate that piece of code often and often.

When should I refactor my code?

The best time to consider refactoring is before adding any updates or new features to existing code. Going back and cleaning up the current code before adding in new programming will not only improve the quality of the product itself, it will make it easier for future developers to build on the original code.


2 Answers

First, bad code is bad code. Python, Java, Matlab or anything else garbage collected doesn't == good code. You could just as easily be spending your time debugging bad Python code.

With that said, be absolutely upfront with the professor. Tell her the code is bad and show her some examples. The worst thing you can do is to try to hide the problem. If you do that, the problem is not only sure to land in your lap, but it's also sure to be blamed on you.

Figure out what the best solution is and propose it to her. It might be fixing that code, enlisting the help of the computer science department, or rewriting it, either in C++ or another language. Chances are good that she doesn't have any other options and will gladly accept whatever solution you propose.

like image 134
wadesworld Avatar answered Oct 12 '22 15:10

wadesworld


Refactor the code, one change at a time, until it makes sense to you. The most important thing is not the exact coding strategy, but that you understand what it's doing and why.

You're probably going to end up touching every line of code, so don't try to do things in any particular order - fix the first thing that jumps out at you as wrong first, then go on to the next one, and so on. Exception: if the previous person didn't use a consistent code formatting strategy, run the whole thing through an autoindenter as your first action.

Write a test suite as you go.

like image 24
zwol Avatar answered Oct 12 '22 17:10

zwol