Are You Proud of Your Code?
An anonymous reader writes "I am downright embarrassed by the quality of my code. It is buggy, slow, fragile, and a nightmare to maintain. Do you feel the same way? If so, then what is holding you back from realizing your full potential? More importantly, what if anything are you planning to do about it? I enjoy programming and have from a young age (cut my teeth on BASIC on an Apple IIe). I have worked for companies large and small in a variety of languages and platforms. Sadly the one constant in my career is that I am assigned to projects that drift, seemingly aimlessly, from inception to a point where the client runs out of funding. Have any developers here successfully lobbied their company to stop or cut back on 'cowboy coding' and adopt best practices? Has anyone convinced their superiors that the customer isn't always right and saying no once in awhile is the best course of action?"
One thing to keep in mind when determining the quality of your code is that other people will most likely criticize the quality of your code. Usually saying that it sucks, when usually its just the person having their own way of doing things. I don't know why this is, I think its just human nature.
I've seen time and time again programmers taking over for other programmers' code and saying that the previous person's code sucks. Its like a right of passage or something.
the problem is... the client doesn't always know what he wants, and the continuous changing of the specs (and hence of the code) make it a mess. It gets worse when near release some 'minor' changes have to be included and a lot of code has to be written in a very short time. There's a big difference between the theory of the 'waterfall-model'(and it's derivatives) and reality.
Yes, I'm left. You have a problem with that?
Getting good IT practices is about establishing a business professionalism in IT that is respected. This means that you have to explain to the business what "good" looks like, you have to understand the business drivers so you can put your challenges into that context and you have to talk to the business in terms it understands.
All too often IT folks bitch and moan about coding, testing, requirements, design time or whatever and how its all the fault of the business. This is victim mentality IT, the way to change it is to actually work out what "good" would be for the business and then work with them to deliver it.
This means the most important coding skill in successful IT departments is the ability to communicate.
An Eye for an Eye will make the whole world blind - Gandhi
I seem to find that trying to code more slowly than I could helps a lot. I'm not the most efficient coder there is, but I tend to produce less bugs and have more time to make better design decisions when I slow myself down.
I've had several jobs where I've found that although management never seems to approve of a slower process in itself, they do begin to see the values once they notice that my code tends to be less buggy than that of my peer programmers.
As for turning around bad practices... That's always hard. Culture is a tricky thing. But it helps to use analogies, lots of analogies! System grown too large with too many kludges? Compare to building a skyscraper on the foundations of a cottage. Management wants to speed up a project by senselessly adding more people? Compare to: "One woman can make one baby in nine months. Two women can make two babies in nine months, but two women can't make one baby in four and a half months..."
Be creative, be thorough, and be proud of your work. Always try to make the next iteration better, but also remember that sometime meeting the deadline is all that counts.
My two cents, I guess...
.: Max Romantschuk
In programming, there are a million and one ways to do the same thing. There is no right or wrong, only good & bad. I've seen some damn shocking code over the past few years, and I've written my fair share too. It's swings and roundabouts, it's up to you to learn from your mistakes and push yourself as a programmer to better your code quality. Keep in mind that what you right is what people use, and it's the difference between "computers suck" and "hey, that was cool!".
And as the first reply said, someone will always criticise your code. Decent programmers know this and still do their best.
ilovegeorgebush
I have a lot of applications that are elegant enough. It may not have perfect validation for every field and not all the GUI bells and whistles, but it does what it's supposed to. I know my share of developers that spend a ton of time making their code elegant and beautiful. In one case, the developer spent so much time making their N-tier application with huge numbers of tables that were normalized to the bajillionth degree that they were finally let go. The goal is to meet the need, not to fulfill some inner desire to create art with lines of code.
Buzzword alert: Agile!
But seriously: Things like Test-Driven Development (TDD) and even Behavior-Driven Development have proven to be great approaches in software development.
Also, software management methodologies have evolved in a tight interaction between business (customer) and the development team. Take 'Scrum' for example... think 'iterative'.
If your superiors aren't convinced that at least the test driven part is necessary then you should seriously consider getting out of there.
You can still write crap code, but at least that crap code is backed up by a unit test. Otoh, if your test is crap... you _are_ the weakest link: Goodbye!
After a long time in the software industry, I came to realize that Code Is Communication.
By far the largest part of the lifespan of any code is Maintenance. Code has to be intelligible. Not just through commenting, but in every construct and usage.
Think about effective communication. The effort to be clear will improve what you are doing. It will also make your mistakes evident so you can correct them.
Rich And Stupid is not so bad as Working For Rich And Stupid.
Gather around me kids, for I am sitting here in my 18th century rocker to tell you a story about a programmer.. A good programmer..
I used to work for a small-sized IT business; a popular community that housed some 130,000 members. It began with the loss of a fellow employee who had basically coded 99% of everything on the site. To that date, everything had worked fine. We had some issues every now and then, but a backup system helped us from getting hammered if anything bad happened.
We never worried too much about him leaving because, for starters, I had some experience with the code/system. In addition, the now departed programmer had left a comprehensive list of features and explanations of his system that would help any programmer (that would replace him) to get around any tricky problems that would/could occur.
Unfortunately, I won't go into what type of business this was, but let's just say that it's not typical programming skills. When I began looking for his replacement, I realized how hard it was to get someone with adequate skills and all the knowhow that was required besides the actual programming. As we were on a tight budget, it was important for us to find that one guy who didn't expect a zillion dollar salary. Typically, that would be someone who shares our interests, a recent graduate who knows his ways around programming.
Eventually I found one guy who claimed to be all that we wanted. After a month, it turned out that the guy was more and more frustrated over how things worked at the company. He disliked about everything about the code and spent most of the time cursing. At this point, I started to believe that our entire code sucked.
Roughly a month later, we decided to rebuild "everything" so that he could have his ways around the code. Since we only had one programmer, I had to comply because it was an important role in the company. My limited coding skills provided no extra help in evaluating our current code, so I trusted this guy since he seemed to be very thorough and experienced. Also, I was promised it would take no longer than one month to do all this.
What a fool I was. If it ain't broken, don't fix it. I should have known, but a company on a tight budget and no one else with good programming skills forced us into this move. Turns out, our super experienced programmer needed not one month, but two, three, four, five, six and seven months to complete his task. By then, he had reprogrammed almost everything and merged some of it with the old code. We waited for the relaunch of our software with great anticipation. Three! Two! One! Go! Oh crap, everything f*cked up.
Following the launch of our new software, we had months and months of trial and error problems. Members were complaining and nothing went in the right direction. Eventually, we were essentially bankrupt and had to let the superb programmer go. The guy who had left us with a huge mess.
When I read this Slashdot story, I had a smile on my face because I learned that a programmer can only know that his code is perfect by the response of many other programmers who can view his code (i.e. open source). Some programmers seem to think their code is perfect and that occurring bugs are caused by impossible-to-foresee problems. The point of my story is that if you truly want to know if you are a good programmer, you must let a lot of programmers decide that for you. Unless your name starts with J and ends with ohn Carmack, of course.
Full Tilt
Design time is wasted time. People don't know what they want, and they won't make meaningful decisions until they have something running in their hands.
My advice would be to read The Pragmatic Programmer book by Hunt and Thomas. It provides the best blue print I have seen to solving the problem for you.
You may not be able to implement all the changes at once but you will find that you've got more authority than you realize.
You may not even agree with all of it, implement what makes sense and your code will improve. I've found with my friends that the longer you've been coding the more sense that the book makes.
Man Holmes
Yes, I am proud of my code, or maybe not always the code but at least my coding. It's not always possible to create beautiful, well mantainable code. I try to, but sometimes there's just not the time.
... it's a pretty large department for such a small company, as we write all our inhouse software ourselves and have been for the last 5 years. When I came to work here, the codebase was about that old -- 5 years -- and maybe a two dozen different 'cowboys' had been writing the software resulting in a large heap of steaming shit. They were not centrally coordinated and everyone of them was doing things in his own style either out of laziness of ignorance.
... she had no idea that it was a mess, the company being a travel agency and she having very little knowledge of automation herself. I used a difficult coding project (connecting to a GDS, the guys that administer plane reservations, car rental, cruises etc) and a general optimation project ( the application was becoming very slow due to all the bad programming going on ) to build her confidence in me and asked her to put me in charge of code sanity. She did.
... and I hope I'm succeeding. Gave them code standards to work to, asked them to clean up the code base where they stumbled upon crappy pieces, moved from Visual Source Safe to Subverion (thank god!) and started regular meetings once a week.
Yes, I have convinced my employer to stop allowing cowboy coding practices -- she didn't even realize it was happening. I'm currently head of the programming division of a inhouse IT dept for a large travelagency specialized in cruises
Anyhow, I managed to convince the CEO that there was a problem
I am now trying to reform the bunch of code cowboys that currently works here to a well disciplined programming team
The codebase is still very messy at places, but many basics (use of one and only one database class e.g.) have improved very much and I think the people here are happier for it. It's much less frustrating to work on nicely formatted code that doesn't have braindead sections that aren't commented.
To make a long story short, if you're not proud of the code you write, make sure you improve it.
---
"The chances of a demonic possession spreading are remote -- relax."
I came to the conclusion that you are the only person who is ultimately responsible for the state of the code you write is YOU. Nothing else, no one else.
:)
Project deadlines, crazy customers, chief engineers, thunderstorms, even a Tsumani. It's just you.
Reason:
if you write buggy code, whatever the reason may be, it falls back to you. You will have to fix it, you will be MADE responsible for it. EVERY time. No one asks WHY you did it.
And you don't like it yourself, which is a bad thing. One should LOVE his work, not hate it.
If you force yourself to push everything else into a state that enables YOU to write good/nice/beautiful code, you will gain something. If not, you will suffer. That's about it. It has nothing to do with other people, with companies, with unemployment.
So, get up, and write that good code. Whatever it takes.
Good luck
The customer may not always be right but the customer is NEVER wrong - the privlege pf the paymaster
All this means is they have a fixed idea of how it should be done and cannot bear to see it done any other way. Frequently (as you found to your cost) the final product is the result of trial-and-error techniques. It's very likely the original programmer thought of and tried the way it should be done, then found the flaws in this approach and adopted methods that produced the results.
It's equally likely that some of the ugly code in any implementation is to get around bugs in the development system, programs it interfaces with or even the O.S. itself. The inexperienced programmer only sees the ugliness of the end result, they assume that the original programmer was dumb/lazy/old-fashioned (because that's what they see in themselves?) and in their arrogance assume that there's nothing worth keeping and only a complete re-write will meet their high standards. If only this was Usually none of the "experience" is documented - only a description of what a module does, not why that method was chosen.
Of course the BIG mistake is to only have one programmer. What happens when they take a break or leave? Everything stops.
politicians are like babies' nappies: they should both be changed regularly and for the same reasons
Theoretically speaking, yes. Practically speaking, no. In fact, no no no no no no no. I've found that more time in the design phase means less time in the actually-doing-stuff and fighting-about-scope-creep and why-have-we-only-1-day-for-testing-and-bug-fixes? and you-didn't-ask-for-that-yes-I-did-no-you-didn't-yes-I-did-then-show-me-where-it-is-in-the-requirements phases.
You're 100% right that better design usually allows for better code; however, when you're in the real world where your actual work is interrupted every 2 or 3 hours by "status" meetings or calls from a "project manager" or some kind of "business analyst" or whatever asking if something is done, and your clients only care about it working just then and there so they can meet THEIR client's deadline (so they can then meet THEIR clients' deadlines and so and so forth), well, then you just get the project done, knowing full well that your questionable code is screwing yourself or someone like you over in the future.
You really have no choice. Principles, aside from the deeply held moral ones, don't carry much weight, especially if you work at a larger company. Calling for standards is all good and well -- until my fat, white (sometimes pimply) butt is on the line. Then I just get it done. I'd rather I get another paycheck than piss clients off and have 10 meetings (cutting into even more of my time) talking about how to implement coding standards that will, for all intents and purposes, never actually be implemented, even after we've decided to implement them!
Please don't use "umm" or "err" or "erm".
Just for the record: AmigaBASIC was my first real programming language (LOGO on paper doesn't count).
That you don't know this quote (it's from 1975 BTW) and have been to the university just shows how much CS is a science without respect for its history.
That you don't have heard of Edsger W. Dijkstra is shame.
http://en.wikipedia.org/wiki/Edsger_W._Dijkstra
Tell me, do you consider the GOTO command harmful?
UnNetHack: NetHack Improved!
Many good comments already. I'd like to add some based on my experiences since 1972.
Background: I was fortunate to be introduced to structured programming early on. I've used, and helped develop/test tools to implement, various coding methodologies (CASE, anyone?) I've worked on operating systems and compilers. Yes, plural on each of those. I've worked at huge multi-nationals and a 3-man startup.
Observations:
Lastly, here is a quotation I found back in the 80's (IIRC from someone at SofTech) and it has guided my thinking ever since:
Strive to understand your problem.
Don't try to solve it.
A fully-stated problem
embodies its solution.
One of my coworkers told me the other day he loved my new authentication and credentials system I used for the Data Access Layer. So much so that he snagged it and used it in another system that had similar authentication requirements.
Now, I've written a lot of bad code in my life, and I'd like to think a lot of good code to. I've seen beautiful code before. New attack vectors and amazing ways to approach problems I never would have though of. And each time I see those nuggets of perfection, I snag them. They get added to my pile of code samples for later use. Either in a straight copy or as a foundation of an idea that gets recoded, depending on license requirements.
Bad code is easy enough to deal with, bad design however... that will kill a project. Bad code can be hot fixed, cleaned up, or straight up replaced. But bad design will require new work from the ground up, getting the users and management to come back to the white board, verifying the requirements... If the system is not designed to meet the needs of the users, a memory leak won't be an issue because no one will ever use the software.
-Rick
"Most people in the U.S. wouldn't know they live in a tyrannical state if it walked up and grabbed their junk." - MyFirs
I've worked both as a manager and as a programmer, and -- although the programmers do have a significant impact on the quality of the code -- I feel that management's impact is even greater. Technically, even if the programmers all suck, it was ultimately a management decision to hire them (or to not hire enough developers, or to not train them if needed, or to re-assign them if required, or fire them if warranted). Sure, other factors may constrain what management can or can not do (budget, etc.), but... the buck stops somewhere, and that's usually at some manager's desk.
Now, I have seen programmers that wrote lousy code, but I've also seen a lot of really good, bright, motivated developers that were forced to compromise the quality of their code because they had no other choice. I remember one newly hired, enthusiastic jr. developer complaining about another developer's code (cutting and pasting, hacking, etc.), and then 6 months later he was resigned to the fact the he himself had to hack most of his code in order to meet the latest deadline..... this being a deadline that was already 50% shorter than the original developer estimate, grew larger due to "extra" customer committments (while keeping the "schedule" basically the same), and having to have worked most evenings and weekends for quite some time to "stay on schedule". There really wasn't much else that programmer could do, and he couldn't work any longer hours then he was already doing. If the schedule wasn't being met, then the developers were expected to make that time up and they weren't getting much sleep as it was.
And yes, the developer (not the manager) was still held responsible for the quality of the code afterwards. During one team meeting, a manager even stated "do whatever it takes, just get it done". So the developer did do whatever it took -- including design and coding shortcuts (and this was the developer's last resort: he tried working longer hours, but at some point couldn't keep it up). The developers even told management that they would need to take shortcuts to meet the deadline. Of course, the management critisized their designs and coding skills months afterwards.
Some of those managers believed that this aggressive scheduling was a "good thing" (after all, "...work expands to fill the amount of time given...", along with the reasoning that if they only give the developers 3 months to a 6 month project, then they have 3 months of buffer). Now, some managers were keen on actually changing things (as per some developers suggestions), but this turned out to really mean: "We're interested in changing things if it means that the developers change the way THEY do things. We're still going to keep managing things the same way we always did." That latter part was probably partially intentional, and partially unintentional (some really did want to change the way things were managed, but they themselves were too familiar with the old way of doing things to really change anything).
Now, I don't want to put too much blame on management (and some of my slant might be a bit biased, since the incidents are still fresh in my memory), but the point is that management can, and does, have a significant impact on the fate of the project and on the quality of the code being produced. Just because the developers are the actual ones writing the code, doesn't mean that they're solely responsible for the quality of that code.
As for improving the situation, I HAVE seen companies make changes for the better when those changes were management-driven, and driven by a manager that knew what changes to make and believed in them. When the changes were initiated by the developers, they tended not to really get adopted -- mostly because management didn't really embrace the new way of doing things. Not that that's a reason to remain quiet if you're a developer: your best bet may be to talk with management about why things aren't working well under the current system, and why things need to change (along wi
I think we're saying essentially the same thing. I'm arguing for better commenting and learning what kinds of comments are unnecessary.
I'm griping about comments that triple the size of the source code and disrupt the code's readability, yet say nothing that can't be discerned immediately from the code.
Comments should describe "what" in only very high-level terms, which is why "what" comments are better suited to method and class level. Beyond that, they should primarily talk about "why", and to a lesser extent, "how" (if the how isn't plainly obvious).
I think the "if in doubt, comment" suggestion is actually bad advice. It leaves people thinking, "God, I really should write a comment here, but I just don't know what would make this line any clearer." Crap like "// create an integer and add two to it" comes about as a result.
>> It's said comments are like sex - even when they're bad, they're still pretty good.
I would suggest over-commenting is like having sex while watching a porno -- great at first but after a while it becomes a distraction. When comments are well written, it really becomes a case where less is more.
Life would be easier if I had the source code.
Not quite true. I can think of a few people whose code gave me no reason for complaints. To pick just two extremes out of the pool:
- one of them is pretty much the pragmatic programmer prototype. He'll (also) apply some pattern only if it's needed, not because it would be fun to have it on the resume. Sometimes a switch block with 10 cases is really all you need, you know? His programs tend to be compact, work and be actually fairly easy to get the hang of.
- one of them is, well, pretty much the opposite. His programs tend to be a lot larger than they need to be, and have layers upon layers of patterns even to print "Hello World". With some reflection thrown in for good measure. Strangely enough, though, they _are_ well organized, and you can fairly easily find the class that processes a given event or command... or the singleton factory that supplies it, or the manager class it's registered with, or...
Are they perfect according to my tastes? Nope. But I can nevertheless tell good code when I see it, even if it's not perfect. And say it.
On the other hand, at the other end of the spectrum, I've had to deal with pieces of code which were truly atrocious. E.g., one particular program not only raped all best practices known to mankind, but also was living illustration of half the techniques from How to write unmaintainable code. Literally. It even had stuff like the using people's first names for variables, in addition to the more mundane techniques like mere undescriptive names, obfuscated flow, heavy use of global variables, non-obvious side-effects to those variables, or methods which can also do some 2-3 other unrelated things if called with the right parameters. And I don't mean just side-effects or cramming more functionality in one call than expected. I mean stuff like the class which should have normally sent an email, could render and print a PDF _instead_ of sending one email, or update the contract in the database instead. And for bonus points, not even determined by parameters, but by the contents of those global variables. But, again, the nicest touch was finding uninformative variable names like Pete and Eve in the very first class I opened.
Basically IMHO lumping it all into one everything-is-the-same "all programmers hate each others' code" pool doesn't do it justice, and paints a misleading image. _Some_ may be just nitpicking about the style, but some code is genuinely evil stuff that should be buried at crossroads with a stake through its chest.
A polar bear is a cartesian bear after a coordinate transform.
You know...I 'used' to get all upset about office politics...how requirements were screwed, this...that...etc.
When I finally realized that is the job is ONLY a paycheck...it is not my life, it is not even remotely a personal thing, just business and a paycheck....my life got much easier. I'll do what they want, I'll change what they want to the best of my ability, in the end, all that matters is getting a paycheck, the bigger the better.
This also helps to realize, there is always another job willing to sign a paycheck for you...you are not stuck, if someone offers me more $$, I will immediately go that way, no malice, it is all just business.
Once you can get into that mindset, your life will be much better, less stressful, and you make more $$ as you go along.
Light travels faster than sound. This is why some people appear bright until you hear them speak.........
One of the marks of incompetence is thinking you are competent.
One of the marks of competence is understanding how incompetent you are.
* Low-level form and factor: The code is written by somebody of much lower ability, so the code is disorganized with lots of run-on functions. You can understand the code, but modifying or maintaining it is next to impossible just as speaking with invalid grammar is very difficult if you know better (ie "somebody sent up us the bomb", or repeating verbatim pretty much anything GWB says).
* Syntax and naming: The code is roughly at your same level of ability. You can understand it and modify it, but you don't want to because it smells bad.
* High-level structures: The code is written by a more skilled programmer. You probably do not understand why the code was written as it was even with documentation, and even if you do then changes you make will cause a lot of side-effects and unexpected errors. Sadly you should rewrite this code because you cannot make other than superficial changes since you fundamentally do not understand it.
Generally the code is at the lowest level you are complaining about.
Another common trait is to deny one's own faults. First of all, if somebody says something is "equally likely" as something else that's almost always some kind of rationalization. There are precious few things that are equally likely (or fair and balanced), and in this case any decent programmer should be able to determine immediately if there are some few ugly sections vs the whole program being a huge pile of crap. Of course the BIG mistake is to only have one programmer. What happens when they take a break or leave? Everything stops. Another mistake is to have programmers of very different talent (as opposed to experience) working together. This invariably causes the very talented ones to leave and the poor ones are left with code they don't understand.