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 always try to write easily readable code that is well structured.

I face a particular problem when I am messing around with something new. I keep changing the code, structure and so many other things. In the end, I look at the code and am annoyed at how complicated it became when I was trying to do something so simple.

Once I've completed something, I refactor it heavily so that it's cleaner. This occurs after completion most of the time and it is annoying because the bigger the code the more annoying it is the rewrite it.

I am curious to know how people deal with such agony, especially on big projects shared between many people ?

share|improve this question

closed as not a real question by Robert Harvey, TMN, Walter, Jim G., gnat Nov 2 '12 at 5:49

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

2  
The same way you do, unfortunately. –  Bobson Nov 1 '12 at 20:32

6 Answers 6

up vote 11 down vote accepted

So long as you are refactoring you are doing it right.

Consider this: at any point you can chart a solution to the problem. However, as time progresses you are finding out more and more about the problem which leads you to being able to come up with better solutions. The first iteration may be backwards, and the last one is hopefully elegent. If you keep refactoring as your knowledge grows then you will always end up with better results. There is no shortcut to jumping to the most elegant solution right away without going through the 'learning' steps unless you've already encountered a similar problem, are able to generalize from past problems, or get lucky.

share|improve this answer

I am curious to know how people deal with such agony, especially on big projects shared between many people ?

  • Acknowledge it exists.
  • Schedule time for prototyping.
  • Try to learn new things in hobby projects rather than production code.
share|improve this answer
1  
"hooby projects" sounds like code written by Bill Cosby. "With the IDEs and agile methodololidgies, the KIYDS don't know... what low level code... IS ALL ABOUT." ;) –  StuperUser Nov 1 '12 at 20:51
    
@StuperUser - thanks for the typo heads up :] –  Telastyn Nov 1 '12 at 20:54
3  
+1 for learning things in hobby projects rather than production code. Too bad my company won't pay me for doing my hobbies :( –  paul Nov 1 '12 at 20:56

Well I do TDD with Unit tests. The whole TDD mantra revolves around the following cycle: TDD cycle

Since I'm using TDD I am forced to write unit tests. If a test is hard to write then it's a sign that my design is too complicated and it needs to be simplified.

The cycle doesn't end unless you refactor to remove redundancy, so most of the time refactoring is done in small chunks and isn't time consuming. In addition, after each refactoring I run all test cases to make sure I haven't broken anything.

image source: http://www.javacodegeeks.com/2012/05/test-driven-development-win-win.html

share|improve this answer

Everyone learns how to write a program by writing it. Refactoring is part of the process, because you're better qualified to write it the second time through. The trick is not to try to avoid the refactoring, but to do it sooner and more frequently. Refactor as soon as you notice an issue, instead of waiting until the "end."

share|improve this answer

Once you've written and debugged the code then set that code aside and write it again in a different way. Vary the data structures. Or the control structures. Try wacky designs. Repeat. Often the cleanest code structure isn't immediately obvious and by trying different appraoches you may stumble upon a better solution.

share|improve this answer

I detected from my practice that I write more clear and simple code when I understand problem domain completely. Unfortunately, customers often cannot exactly clarify what they want and this is a normal working situation. If you misunderstood the domain, it will be reflected in your code. And if you are a project lead, one of your main tasks is to help customer to get new sight on his business solution he wants you to implement. So, try to give more time for a domain analysis and it helps you to write more simple and clear code.

share|improve this answer
    
Welcome to P.SE and thanks for your answer. This question has now been closed. –  Andrew Nov 2 '12 at 6:25

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