This post is inspired by How To Write Unmaintainable Code Ensure a job for life ;-) by Roedy Green.
But rater I'd call it: How To Write Unmaintainable Code ensure headaches for life.
Well, I started programming when I was 15 years old, and since then I've learned a lot, and almost all I learned since then (except the very basics, which I learned at school) I learned by myself, I read a lot and I really enjoy programming, which helps, because as I code for fun I learn and have fun at same time.
And I lost almost all of my oldest codes. I wrote I game like space invaders using clipper once, and for what I can remember it's the code I regret most to have lost.
But these kind of code you write for fun, you don't have to maintain, even if it is a piece of shit, who cares? it's just dead code.
But the real problem starts when other people starting using your code, because you can't just trash it, and that's a big problem.
And 4 or 5 years ago, I dropped clipper and start with php, even php being a modern language it still have many problems, and one of these problems is that it is easy too learn, and it attracts a lot of newbies, like me, who you should have guessed got attracted by it's simplicity (please forgive me for using this word to describe php) and easy learning.
And what happens when a newbie join forces with other newbies to learn a language? Nothing good, I'm sure.
As clipper was a procedural language and I didn't have a clue what OOP was at that time, I started writing spaghetti code at speed of light, and that was not a good idea.
1 or 2 years after that I found myself a full time job as a php developer, or maybe you should call it, php code typer.
And man, I'm fucking proud of it, because even with spaghetti code I was able to write a web based software by myself which works flawlesslly and also was a passport to my new job.
But when you write unmaintainable code, you can be sure that it will be back to hunt you, and it started hunting me as soon as I quit that job, and till now (8 months later) I am the only one maintaining it, not that it is impossible to maintain, but it is so freaking complex, that every new php programmer (aka newbie) that looks at it says, holy shit!
Why do I think this code is unmaintainable? Well, I spent more that one hour reading my own code trying to understand what that fuck some piece of code did, and after understanding it I asked myself, why the fuck I wrote that?
And as the spaghetti code quantity grows, it becomes I snow ball, if you try to refactor it, you will spend a lot of time with the risk of breaking what is working, if you just keeps writing code crazily you will end up in a dead end some day, and if you try to write the new code in a better way, well it will help a little, but your software will be like when you make a collage with letters cut off from a newspaper, b-e-a-u-t-i-f-u-l.
Anyway, don't blame for skipping the topic, this is a blog damnit! So I got carried away. Sorry :)
The purpose of this post was to give a simple advice, before writing code, please take your time, hold your breath, and read something about coding quality, design patterns, or general programming books, and be sure to at least read the Pragmatic Programmer.
I'm sure it will open your eyes and make you write better code, because I knew (and Know) a lot of potencial programmers that get stuck in a company which doesn't care about code quality, and thinks that code quantity is equals code quality, and it's a shame that these companies sometimes make these good programmers rot into oblivion.
I left my last company and moved to another one, starting with a lower salary, the new company is in another city so I had to move and I spend money with rent and households stuff, but man, it was the best thing I did. I've learned a lot in this past 8 months, I can use my full potential here to learn new stuff and write better software, I don't have to be stuck 8 hours a day writing spaghetti code, because I can spend half hour researching and write a 10 line code in some minutes that will do the same work, and better.
If advice was good, we would sell it, not give, but here is a little piece of advice.
If you spend more than some minutes to understand some code (specially if it's your code), it's got to have something wrong, so think carefully about it, can it be better? Will you understand it if you read it again after 2 months? Can you really copy and paste code without any punishment? Will you be able to reuse the code?
Well, if you ask this kind of questions to yourself, I can asure you that you will not regret it in the future, I wonder what my code would be like if I had asked these questions to my self in the first place... I'm sure that I wouldn't spend 1 hour to understand it and also I wouldn't be here writing this stupid post.
Anyway, if you read everything till here, thank you, but if you just read this last line, go to hell, because this is the first sin of "spaghetti code writers", they don't like reading.