This happens to me most of the time. After I have finished writing some code, I get this feeling that it is not the optimum code or there is a better way of writing the same code. Perhaps someone else is more intelligent than me who can implement the same alogrithm in more smarter way.
This sometimes let me down and sometimes leads to depression. Is it just me or has anybody else felt this same way? What do you do in such case?
Update: I do not know why this question was closed? I still think it is relevant to 'programming'. Does anyone thing this question is not as good as 'What’s your favorite “programmer” cartoon?'. Sorry but a bit dissaponited now.
Whenever I look at my old code I always find things I would improve.
At the point of initial completion I in fact tend to be happy, because my Unit tests pass. So I have immediate feedback that something good is happening. Do you practice TDD? Perhaps that feedback may help.
I would say that it is quite remarkable how many really competent people in many fields (music, acting etc.) also feel full of self-doubt. Many of the techies I work with that I really rate are continually self-critical. Their defining characteristic though is that they don't let that stop them from getting on and delivering the next thing. Sometimes one just grits ones teeth and ploughs on ...
First thing: the perfect code does not exist.
Second thing: you will improve your coding skills by learning and coding. This is called "experience".
For example, take a piece of code you wrote some months ago, and see how you can improve it. If you can't, then it means that you are not learning enough from your work.
I'm not sure it would qualify as "depression" but it's certainly common among programmers to be perfectionists. Just one more refactor, one more rewrite, one more unit test and it'll be "perfect". It doesn't need to be perfect. In fact it'll never be perfect. It just needs to be "good enough".
A far more typical attitude is that no one else can do it as well as you can (it's why you get so many reinvented wheels).
Your issue isn't so much that as it is a basic lack of confidence so you're a perfectionist of a different sort. What you fear you create and if you become so fearful of what essentially amounts to criticism and judgement then you've got a problem that needs addressing. If you do get depressed then you also have a problem that needs addressing.
We do the best we can with the information, skills, experience and constraints (time, requirements, etc) that we have at the time. Do the best you can. Don't be afraid to make mistakes. Learn to take criticism (as in don't take it personally). Do things better the next time around.
No one can do more than that.
If you finish a project, look back at your code, and see no room for improvement, you're not a good programmer.
I look back at code I wrote a year ago, start laughing and think: how could I've been so silly. Then I'm glad and thankful that I've improved over the last year. That keeps me going. because next year I will feel embarrassed over the code that I wrote today (to my best knowledge).
I know this feeling too well. However this away after I realized:
I will assure you that when you read your code in 10 or so years... you will be terrified what you did there! But that's what is so important in life - getting better with time.
This is natural and means you're a perfectionist, which I think most of the time is a good trait in a programmer. Always make sure when you write code you minimise cross dependencies. This makes the refactoring process of swapping out large modules of poor code a breeze. The feeling of clean code working properly will feel great and no doubt cure your depression.
If your feeling depressed a lot while doing work, it might be an early warning for some larger issues you are having.
Go talk to someone about this.
There is always a better way of writing a program. It's the basis of the whole premise of compiler optimization, isn't it? Accept it and move on.
The real question is, does your code satisfy the problem requirements? If so, then it's good enough, so why depress yourself?
One solution is to change the role of programming in your life. For instance, become a programming teacher or maintainer. This way you don't need to obsess about the code you produce.