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
2  
The same way you do, unfortunately. –  Bobson Nov 1 '12 at 20:32
add comment

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.

7 Answers

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
add comment

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
add comment

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
add comment

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
add comment

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
add comment

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
add comment

Try to use the following:

  • factorisation
  • simple naming
  • unit testing

Good luck!

share|improve this answer
    
Size of code totally matters! Less lines of code -> less bugs, less to maintain, less things that can go wrong. –  MrFox Nov 1 '12 at 20:50
1  
Less lines of code -> less bugs. It's not really that simple, don't confuse LOC and complexity. –  StuperUser Nov 1 '12 at 20:52
    
I'm speaking in very broad terms, and I'm referring to 'actual statements where stuff happens' instead of LOCs. The less of that given the same result, the better. Obiously I'm not talking about sticking 4 statements into one line (but neither was asghar asgari) and I agree that that's horrible. –  MrFox Nov 1 '12 at 20:58
3  
Sorry man, this is very wrong. Probably the most important thing in programming is to write code that other people can and will understand, so code size does matter. Comments and reports are the worst way to document code, they are just too simple to ignore on the next change and usually they are lying after the second change to the code. So, no comments, well structured and refactored code, with simple and understandable logic. And of course unit test. –  Patkos Csaba Nov 1 '12 at 21:40
1  
@asgharashgari What are you talking about? What sized projects have you worked on? I'm not arrogant enough to think I've thought of absolutely every edge case or ignorant enough to think I will be free of integration issues with others' code. Read: joelonsoftware.com/articles/Wrong.html and altdevblogaday.com/2011/12/24/static-code-analysis –  StuperUser Nov 2 '12 at 9:28
show 5 more comments

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