Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I don't have much experience in working in software industry, being self-taught and having participated in open source before deciding to take a job. Now that I work for money, I also have to deal with some unpleasant stuff, which is normal of course.

Recently I was assigned to add logging to a large SharePoint project which is written by some programmer who obviously was learning to code on the job. After 2 years of collaboration, the client switched to our company, but the damage was done, and now somehow I need to maintain this code.

Not that the code was too hard to read. Despite problems—each project has one class with several copy-pasted methods, enormous if nestings, Systems Hungarian, undisposed connections—it's still readable.

However, I found myself absolutely unproductive despite working on something as simple as adding logging. Basically, I just need to go through the code step by step and add some trace calls. However, the idiocy of the code is so annoying that I get tired within 10 minutes of starting. In the beginning, I used to add using constructs, reduce nesting by reversing if's, rename the variables to readable names—but the project is large, and eventually I gave up. I know this is not the task I should be doing, but at least reducing the mess gave me some kind of psychological reward so I could keep going. Now the trick stopped working, and I still have 60% of my work to do.

I started having headaches after work, and I no longer get the feeling of satisfaction I used to get—which would usually allow me to code for 10 hours straight and still feel fresh.

This is not just one big rant, for I really do have an actual question:

Is there a way to stay productive and not to fight the windmills?

Is there some kind of psychological trick to stay focused on the task, instead of thinking “How stupid is that?” each time I see another clever trick by the previous programmer? The problem with adding logging is that I actually have to understand what the code does, and doing so hurts my brain in an unpleasant fashion.

share|improve this question
Hungarian notation is not bad, read the original paper to see what he was talking about :) –  Woot4Moo Feb 22 '11 at 18:36
I know Hungarian is not bad. This is precisely why I wrote Systems Hungarian, not Apps Hungarian (the original one). I see no sense in using Systems Hungarian in C# because it has great type system and IDE. Having 10 variables in the same scope that all start with obj is daunting because it's basically unreadable. –  Dan Feb 22 '11 at 18:45
I wish I could give this question more than one vote! –  o6tech Feb 23 '11 at 5:22
possible duplicate of I've inherited 200K lines of spaghetti code -- what now? –  GlenH7 Apr 27 '13 at 22:14
I blow off steam by asking grumpy questions on stack that get me downvoted. –  Erik Reppen Jul 10 '13 at 23:52

11 Answers 11

up vote 23 down vote accepted

I'm sorry to tell you, but not all jobs are full of sunshine and glamor. The majority of development tasks involve drudge work like this. Sad, but true.

You are tasked with an important job, even if it's boring to the point of watching paint dry. It's important for two reasons: 1. It add much needed logging to a large system so that when something goes wrong you'll have a tool to help you find it. and 2. It gets you familiar with the code base so that if and when something goes wrong you can jump in and fix it.

You are basically creating your own safety net here. Glamors, no, but important yes!

So, that being said how should you motivate yourself? When I have a mind numbing task at work, I set goals for myself. Finish doing task x by the end of the week. If I make my goal, I reward myself. New restaurant I want to try? Go Friday night if I finish. New movie just came out? See it on the weekend if I finish.

I find talking with my supervisor and letting him/her know where I'm at and how I'm progressing keeps me accountable. If I tell them I'll be done by Friday, I feel more inclined to get it done by Friday b/c I told them I would have it done.

Keep faith that once you complete this task and you've done it well, on time and on budget that people will notice and when that shinny new project comes along, your name might just be suggested as the one who gets it. :)

share|improve this answer
Good points, thanks! –  Dan Feb 22 '11 at 17:20
I especially like the point about Friday motivation. It's funny that current release is also scheduled to Friday. I think it's worth adding that gratitude motivation is a moving one. You need to ensure someone's going to be grateful for your work, or else change what you're working on. Sincere ‘thank you’ often rolls back restless hours. –  Dan Feb 22 '11 at 17:53
@gaearon - I'm glad that the suggestions are helpful to you. Getting through the drudge work with a good level of motivation will pay off in the end. Last year at my current job, I had to do something similar to what you're doing. This year I was given a brand new app to write from scratch. People will notice what you do and how well you work. –  Tyanna Feb 22 '11 at 18:54
-1 Why link your work life and personal life? This sounds similar to staying overtime consistently - I didn't finish my under-estimated task by Friday - so I need to stay at home and feel bad. –  Vorac Jan 3 at 16:56
@Vorac ~ I said that's what I do to motivate myself. Everyone is different. And I can assure you, I don't work OT consistently. Find something that motivates you and use it. I find a material reward works best when I have a task I don't want to do. –  Tyanna Jan 7 at 18:21

Keep a file of candidate code snippets for submission to thedailywtf.com. Even if you don't really intend to submit them, it gives you a bright side to finding some code that's even worse than average.

share|improve this answer
Good one, thanks! –  Dan Feb 22 '11 at 19:01
I wish I could upvote it one more time. It turned out to be a really great suggestion, now that I found that these guys store their changelogs in application configuration files, right before actual settings. –  Dan Feb 22 '11 at 21:33

I was in a similar situation, tasked with cleaning up a large body of poorly written, massively copied-and-pasted code.

To maintain my motivation and my sanity, I wrote a script called current_score that counted the LOC in the project (which steadily decreased, as I eliminated duplication and switched to better algorithms) and compared it against the LOC when I started. Whenever I got discouraged or frustrated with the mountain of code I was facing, running current_score would give me a sense of tangible progress and would remind me of how much I'd already accomplished. And it was fun to see just how high of a score I could rack up when tackling a particularly bad section of code.

I'd look for similar metrics that you could easily script to give yourself a sense of progress and to turn it into a game of sorts. Lines of code (just run wc -l), cyclomatic complexity (which should go down as you clean up those nasty nested "ifs"), lines of code which have been touched by you instead of your predecessor (I think that FishEye can tell you this for $10), etc. You could even write a Perl script without much trouble to count the number of code blocks that don't yet have logging statements.

share|improve this answer
Wow, that's really fun. Thanks for sharing! –  Dan Feb 22 '11 at 18:32

I've seen this book recommended: Working Effectively With Legacy Code, but luckily haven't had a need to read it.

Like you are doing, refactor what you need to so that you can understand the code and just remember that you are resuscitating a system, which will pay off when you're maintaining it.
That should hopefully put a spring in your step on the way home.

share|improve this answer
+1 for book suggestion, I'll definitely check it out. –  Dan Feb 22 '11 at 17:29
This book is about refactoring existing code to make it testable; I don't think it's going to help much in the way of motivation. –  Billy ONeal Feb 23 '11 at 17:14
Good point @Billy ONeal, but having testable code and the associated metrics can show progress which could be motivating. –  StuperUser Feb 23 '11 at 17:23
I've read this book. Definitely worth reading. I actually did find WEWLC motivating just because it was nice to know there was someone out there who understood the exact sorts of frustrations I was having and had devised effective ways to mitigate those frustrations. –  Jason Swett Oct 3 at 14:24

Take extensive notes to organize your questions, thoughts, and understanding of the system. This has worked wonders for me when dealing with large legacy systems. It helps crystalize your understanding, helps put the open questions into words, and because your thoughts are already put together it makes it easier to communicate spontaneously with others about problems/questions/ideas/etc.

As an example, as I'm going through a piece of the code, I'll be taking notes to myself constantly. This is my conversation with myself. The mere act of writing helps more thoughts come out and helps me understand things better. After a while I may have a Eureka and need to draw out a little diagram with the "bigger picture" on paper to illustrate what I just thought of or what pieces I just put together. I always do this on paper only, getting rid of all the distractions of the computer. This lets me be more methodical and thoughtful about what I'm doing.

This is basically a convenient way to have a perpetual conversation with a domain expert :)

share|improve this answer
Thanks, very good point. –  Dan Feb 22 '11 at 17:15

Try to break the project up into chunks. Each day learn how a specific chunk works. Trying to understand it all at once is probably what is stressing you out.

Take pride in making the project better. Are there other coders you can talk to? It helps to stand around the water cooler discussing/laughing at the latest logic you found. I try to do this to keep a jovial atmosphere at work.

share|improve this answer
Yes, I'm working by chunks, and I already have been working on it for some time so I have a rough idea about each component. Still, it doesn't help much because it's the tiny pieces of logic that usually take time to understand—and I get angry when I realize that 30 line method I spent 10 minutes on can actually be re-written in 2 lines. As for the company, unfortunately, I'm the only developer on this project, and I'm currently working in client's office so there's no one I could really talk to. –  Dan Feb 22 '11 at 17:04
@gaearon - What prevents you from implementing your 2 line solution? You need to figure out how to do what you were tasked to do, the problem with the code can be solved later, when you are not in the client's office. You should keep your notes on what you did, how something works, so you can go back later in the future and implement your changes so code review(s) and integration testing can be done. –  Ramhound Feb 22 '11 at 17:16
@gaearon ah-ha! You're the only coder. So the guy before you was the only coder. You can get away with a lot when you are the only coder (as you've noticed from your predecessor). Keep this in mind when you look for your next job. ;) –  davidhaskins Feb 22 '11 at 17:43
@Ramhound I'm betting there won't be any code reviews. I bet there won't be any formal integration testing. I've worked in these positions a few times. Generally people only want code that works good enough, and they want it as quickly as possible. Explaining "best practices" is like talking to a wall, IMHO. –  davidhaskins Feb 22 '11 at 17:44
@Ramhound, there are no tests for this project, and I don't want to be responsible for ruining the system for the sake of cleaner code. There are many cases when current code implies exceptions being swallowed, or else relies on other kinds of bad behavior which is not obvious. This is one of the reasons I'm adding logging, by the way. –  Dan Feb 22 '11 at 17:56

I know you may feel unproductive because you're looking at it from the perspective of 'I'm only adding logging' when in fact, you're adding logging and doing a lot of refactoring. Your supervisor is probably aware of the code situation. Everyone may not appreciate it now, but when you get a request to add a really interesting and challenging feature, you'll be glad you cleaned up the code.

share|improve this answer
I'm afraid I'm going to end up rewriting the project, we already talked about that. Although I like this option better, it doesn't add productivity in working on throwaway code. I know logging is required in next release, and then I can go with my stuff, but it's just having-to-run-this-code-through-my-head that makes me crazy. I feel like I'm getting dumber after understanding it :-) –  Dan Feb 22 '11 at 17:28
"it doesn't add productivity in working on throwaway code" Suuure it does. You get to go through large portions of the code working on your understanding of it, while performing a low risk task (logging). This knowledge you are gaining will help immensely if it comes to a rewrite. If there's not a rewrite try to look forward to the reward you will feel when you've cleaned up large amounts of the app, how much better the code base will be due to your consistent and persistent efforts. –  qes Feb 23 '11 at 7:36

In these Cases I tend to rewrite a section of code. To make one area suck less and then I just add logging some where else. Then clean up some more code. Bad code is only bad if you leave it there.

share|improve this answer
System heavily relies on bad practices so to rewrite a method properly I would have to rewrite the whole project (which I probably will do eventually but I have some deadlines for current release). –  Dan Feb 22 '11 at 17:19
Ya I understand believe me. I just pick off a section I can clean up with out making my life to painful and clean it up and then the next area. Fixing code is a process that you never get time for but should always make time for. –  Erin Feb 22 '11 at 18:15

The trick to not getting bored or angry so you stay productive is to accept that the code is poorly designed. Accepting your position to having to understand and update the code will allow you to not keep commenting on "how stupid that is" and instead quietly accept it and move on.

Another trick is having a good homelife to look forward to at the end of the day. Girlfriend, friends, games anything will work, to give you a goal to get through the day and make the trudging though bad code all worth while.

share|improve this answer

"Working Effectively with Legacy Code" by Michael Feathers may help.

If you're worried about breaking things when you change them, write some tests first, make sure they pass before and after you make your changes. Writing the test should help you to summarise and understand what a given piece of code does and will allow you to edit with confidence.

share|improve this answer
Unfortunately, it's SharePoint project, meaning it's almost untestable. I've written some cool sandboxing for SharePoint in the past using Microsoft Moles but it requires a lot of additional work. –  Dan Feb 23 '11 at 16:50

Gamify your work. For example, give yourself 5 points each time you ask a good question about the code, and 10 points each time you answer it. Give yourself a badge each time you refactor a method or add a new feature. Once you accrue enough points you get privileges like coffee breaks or biscuits. Once you complete the whole project you get the privilege to treat yourself to something you really want.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.