share
Stack OverflowWhat are some bad programming habits to look out for and avoid?
[+111] [102] fossuser
[2009-05-03 22:19:03]
[ language-agnostic ]
[ http://stackoverflow.com/questions/818159]

I'm a new programmer in college and was wondering if there are any bad habits to watch out for early on. Anything that you wish you knew to avoid when starting.

(8) All programmers should review this question... ;-) - Frank V
(33) Wasting too much time on stack overflow :p - Sam Saffron
(33) Not wasting enough time on stack overflow - Min
(2) lol @Sam and Min - LnDCobra
[+182] [2009-05-03 22:24:21] Ayman Hourieh [ACCEPTED]

Copy and paste programming [1] is probably one of the worst habits that one can develop while starting to learn programming. Learning how to program is best achieved by writing code and understanding other people's code. If you copy/paste code without understanding what's going on, you are doing yourself more harm than good.

[1] http://en.wikipedia.org/wiki/Copy%5Fand%5Fpaste%5Fprogramming

(17) Yeah, even when I'm just copying someones code example, I"ll still type it out by hand to learn it more thoroughly. - CookieOfFortune
(2) Agree - I once heard this called "cookie cutter code". It seems good until you need to change it. Then you've got multiple copies to change, and if you miss one you're created a bug. Moral - DRY (don't repeat yourself). - Mike Dunlavey
(1) Sounds right to me. Looking at other people's code is good, and can give you a head start. But if you want to tinker with a little demo application of someone's, you can copy and paste it, but then start changing things so you get a good understanding of how it works. Don't just add to the code, try changing what's already there. - MBCook
(55) There's two aspects to this -- copy/pasting other people's code, and copy/pasting your own code. Both are bad. If you find yourself copy/pasting your own code in the same program, you're doing something wrong. Refactor it. - Andrew Coleson
(1) +1 to Andrew's comment. Copy & paste of existing code are a sure sign that something should be refactored into a more usable objet/library. - jdt141
(12) @Andrew: 90% of the time, it's bad. But when doing things like initialization, or creating a new class, sometimes copy and paste is the way to go. - Andrei Krotkov
(3) Most of the time when I copy and paste my own code it isn't business logic, but just the cruft that is necessary when coding in that language, like Andrei says, the code for declaring a new class is a decent example. - thomasrutter
(1) I think cut and paste is good though ... it's a low budget refactor ;) - Chad Grant
(2) @Deviant: Copy and paste coding is the exact opposite of refactoring. Refactoring is Cut, Paste, Delete Programming - TokenMacGuy
(1) I can't +1 this, I've managed to learn a lot by copy/pasting code and modifying it slightly. True, you don't learn it right away, but I've found that by copypasting code that works, testing to see if it does what I want, and then going back and looking at how what I wanted to do was accomplished, I can learn how to use oject or learn new concepts. - Sukasa
(1) Hey, copying and pasting is fine, as long as you post a big GUID marker on everything you copy and paste! codinghorror.com/blog/archives/001257.html - mquander
(1) -1, I learned more using copy and paste than in HighSchooL - e-satis
(1) Copy-and-paste, trial-and-error learning is a great way to learn about a reality almost, but not quite like our own. Unless you enjoy fixing subtle errors caused by misunderstanding stemming from "empirical" learning, don't bother---go pick up a book. - Derrick Turk
1
[+117] [2009-11-20 15:37:53] AndreaG

I start far too many hobby projects, and seldom manage to complete any one -- or even reach an intermediate milestone.


Brilliant - ditto! I figured out the only safe way to achieve completion is to start and complete the whole project in one weekend, otherwise day job gets in the way. Last weekend was my first success with fightsite.org :) - joshcomley
(3) This is actually why I don't start many side projects - I refuse to start something unless I believe there is a reasonable chance of completing it. - Tom Ritter
(18) I think one of the fundamental problems with hobby projects is we never give ourselves a professional specification. Which means there is no end to the project. It goes on as far as our hearts allow, which is forever. "Ooh I could add this... it could do this...". Then, before it's even finished "[Technology X] is out of date? I'd better refactor it with newer, better [Technology Y]!". Aside from the obvious facts that we can't feel pressure from ourselves, and no money is involved to hurry us on to an actual completed product... - joshcomley
(1) This is only a vice if you start the hobby project with the intention of finishing it. I start my hobby projects with the intention of exposing myself to new techniques or technology. I don't generally need to finish the project to develop a feel for the thing I'm trying to learn. - Greg D
(2) @Greg - I have a bit of both every time. The thought process is a little bit like this: "Ooh there should be a website/bit of software that does X... I could probably make it... I could give my friends the link, I bet they'd love it! Plus, I've been meaning to learn Technology Y for ages, this gives me a great platform to learn it!" - joshcomley
(1) @josh - I completely agree with your mention of the lack of an end-point on personal projects. Just as I think I've completed something I'll come across some new functionality in another site which I think I should add, then a month later the same happens - it's never ending! I really need to stop looking at pretty shiny things! - Katy
(2) ME! Today, not tomorrow but today I change! - chiggsy
(1) Great answer. I do finish, but what ends up happening is that I have to cut "cool" features left and right and go with the most simple version of what I have in my mind. Once I get it out the door, then I start to add cool crap based on what other people have to say about the software. - rball
(2) +1 I have a similar habit! - James
(1) I completed one of mine! Woohoo! sh.am - joshcomley
From the Zen of Python: "Now is better than never" - santiagobasulto
2
[+98] [2009-05-03 23:41:26] TokenMacGuy

Not using source control.

There are a few instances where it is feasable to skip the source control. If you can count the number of characters in the source file on one hand, It's probably ok to skip the source control. Otherwise, you need it.


(1) Can always do manual source control. - Joshua
(1) @Joshua: Please elaborate. Can't hardly imagine doing anything myself when git is available. - TokenMacGuy
(4) +1 but I'd add "correctly" after "Not using source control" The book Pragmatic Version Control really opened my eyes to some things that I'd been missing out on. - SnOrfus
(1) Manual source control sounds a bit like making your own dogfood: It's easier and less messy to just buy it from the shop. - thomasrutter
(12) Actually, manual source control sounds like making your own dogfood from old car tires, and then wonder why the dog gets sick. - sleske
(1) More importantly, how would 'manual' source control even work? does that mean you just back-up to a zip-file? How do you go about tracking the changes? By the time you make a two line perl script to add timestamps to the tarballs, you've already worked harder than just using git! - TokenMacGuy
(4) I found that this was a particularly common trait among my classmates when I was in school. Adopting source control for class projects would have saved them lots of heartache. There were constantly people who were freaking out right before a project was due because they'd introduced some bug, and didn't know how to get back to a known good state. It's a pity that more CS programs don't teach version control in their intro classes as a basic programming tool. - Scotty Allen
(1) I'm sorry, I don't agree that you skip source control in your example. Now you have a crucial (albeit short) file which you have to remember to include, you must manually type it out, you can't automatically provide to your build script, other devs have to read the docs or work it out... its just a no no. you should be able to build from source control. what are you saving by not including it? - Colin Pickard
(1) @Colin Pickard: If you need to include a file in another file, I strongly doubt that the entire system is still fewer than five tokens. There's a build script? sounds large enough. To be clear, I do not mean that some files of a project of any size may be left out of source control, Only single, stand alone solutions to an entire problem domain that happen to be trivial solutions are even within consideration of skipping source control - TokenMacGuy
3
[+86] [2009-05-03 22:39:25] Doc

Using poorly named variables.


(2) Also, it's better to use a bad naming convention and stick with it than it is to not use a naming convention. If you don't like your naming convention, refactor - or just use a new one next time. - Dean
(9) Don't abbreviate. In the age of autocompletion there's no need to write unreadable code just to save a few characters. - JC.
(2) So true. Good naming is really, really important. - Joren
Disagree with @JC. Having long symbol names makes code longer, which is always a bad thing unless you've got a good reason to do it. - nfirvine
4
[+64] [2009-05-03 23:27:52] joeytwiddle

A mistake programmers often make at the beginning, is to try to write the Whole Program, then compile it and run it. They will be bombarded with 100 compile errors, and even when compiled their program probably won't do the right thing.

The way beginners should start a project, is to make a small part of the program, compile and test it, and once it is working, move on to write the next part. Continuously re-running and testing your application, while you add or change only small pieces of code, is a great way to isolate any new problems in the recently changed code.


(2) I always have that problem. It's really a problem of me being too lazy to write Makefiles, so I never compile the code. - Zifre
(2) I suffered this problem and learned to write little tests as I coded, but now I write unit tests. - thomasrutter
(2) I disagree with this, doesn't quite work for me. I like to code bigger parts at once, because that way I can keep all the program inside my head while coding. Then I switch to test/debug mode, which really is a bit different. - J S
(2) I agree that it should done in increments, so you do not compound your early error. I like to write chunks of code while the program is running, you get even more feedback than what is available through simple intellisense. I suspect I'll get strong responses why this is bad and promotes sloppy code that isn't well planned. I would disagree & say that if used well, it is another tool in your belt and should not be avoided for idealistic reasons. 20 yrs ago, my boss told me that I over relied on what was at the time an advanced debugger and that it encourages bad code. I think she was nuts. - Chad
(1) This is sometimes referred to as "Agile programming" and I also like it. I usually Work in hourly to daily increments and try to get one step forward towards the goal - add a module here, add a module there, for each one doing a short test to see its working OK. Trying to locate bugs in a large system is something u wanna avoid as much as possible... - Neko
I'm a BIG fan of this. Hammer out the subsystem, throw in some unit tests, move on to the next subsystem. If it needs to be refactored because the big picture changes then at least you have something that works that you can fall back on if you go off the road somewhere down the line. - Repo Man
5
[+63] [2009-11-20 15:32:35] Tom Ritter

I rewrite code I didn't write.


(6) Ah yes good one, I do that. If I didn't write it, for some reason it looks really bad. And when I did write it, it looks really... - joshcomley
but that's not a vice, that's a great habit! It means you are constantly improving things, and to do that you must understand parts of the code that other people wrote... I firmly believe that continuously improving code is the only way to keep code rot away - mookid8000
(2) @mookid - Or you misunderstood that the code was perfectly well written in the first place! - joshcomley
(6) Of course if the original code was horrible (illegible, uncommented, poor algorithm, too much duplication, etc.), that's a virtue. - Loadmaster
(1) @joshcomley - and of course the code you write is perfect like that? ;) in my experience, maybe one out of 1000 methods cannot be improved - the rest can and should... - mookid8000
oh, and improving other people's code has a side-effect that I should perhaps emphasize: it makes you read and understand other people's code, thus you take responsibility in maintaining it along with your team... this way, noone owns one particular piece of code, which is great for too many reasons to mention here - mookid8000
@mookid - I agree, anything can be improved! But we have to always ask: should it? If it ain't broke, don't fix it! It costs business time and money to improve code, and if it already performs its function perfectly well, it might be a waste of time to touch it. - joshcomley
@mookid - also, you're assuming any changes you make will be for the better! It is not unheard of for me that I have rewritten a whole bunch of code thinking "why on Earth did they do this!! It's rubbish!!" only to realise too late "oh s--t, that's why they did it... oops...". Now this is my mistake, of course, but I'm merely pointing out that not all "refactors" are for the better! - joshcomley
6
[+60] [2009-05-03 22:31:38] Rob Kam

Don't optimise prematurely.

Donald Knuth made the following statement on optimization [1]: "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil."

[1] http://en.wikipedia.org/wiki/Optimization%5F%28computer%5Fscience%29

(2) I like that 97% qualification, because most of the time people just say "don't optimise prematurely" and leave it at that. A seasoned programmer knows what is worth optimising and what isn't, and most code isn't. - thomasrutter
(31) I actually think this is some of the worst advice that gets passed around. Most developers these days seem to confuse programming efficiently with premature optimization. If you're Knuth or one of his colleagues, this is great advice. Otherwise, it's often used as an excuse for sloppy programming. - Travis
(2) Travis has an excellent point. There are order-of-magnitude differences between algorithms you'll see students write in an intro programming course - e.g., unneeded triple nested loops, recursion without caching, etc. A colleague looking for CS grad student help at a major university (with a good CS department) gave candidates some simple tests - he had people writing O(n^3) algorithms for problems that were O(n log(n)). You don't want this kind of poor design to be excused because you "shouldn't optimize prematurely." - Mike Kantor
(20) Yes, you do. O(n^3) doesn't matter if n=5. It really depends on the combination of algorithm, input and available resources. And that is hard to predict, which is just Knuth's point. See also stackoverflow.com/questions/474714/… . - sleske
(3) I think all of you in the comments are misinterpreting. Converting an n^3 algorithm to an nlogn algorithm is not "optimization", its basic. Premature Optimization is converting an algorithm of 8nlogn+2n+17 algorithm into an algorithm of 7nlogn+n+9, wasting time and usually introducing bugs in the process, while the program performs 100 times faster then it needs to be anyway. - Neko
(2) Neko makes a good point. There is a difference between designing for efficiency and optimizing for efficiency. Good design should always be done, but optimizations are only useful when your code has been shown to not be fast/small/whatever enough. - Joren
Premature optimization means optimizing something that doesn't even work yet. See also: stackoverflow.com/questions/58640/great-programming-quotes/… Once the thing you're creating works, it's a good time to optimize. - Kyralessa
7
[+57] [2009-05-03 22:49:50] Rob

Not handling errors. Failing to check return codes, assert conditions or swallowing exceptions. Even in cases where it's "impossible", or "no one will ever use it again".


(8) Especially in cases where it's "impossible"... - sleske
(6) The problem with beginner programmers is that they still think "impossible" has the real-world meaning of "won't happen", while in practice "impossible" in programming means "will happen only after releasing the product and/or demo-ing it" - Neko
"Even in cases where it's "impossible"" - Hence why it's known as an Exception, because it's Exceptional. - Jess Telford
8
[+56] [2009-11-20 15:42:41] GreenMatt

Spending too much time on StackOverflow instead of actually developing!


(3) I´m guilty too! - Daniel Rodríguez
(4) StackOverflow is a social website like any other. We share, we acknowledge, we up vote, we down vote. :) - Secko
9
[+55] [2009-05-03 22:21:57] CodeToGlory

Not writing unit tests.


Arg! When I saw this question I thought "not writing unit tests" and then I looked and it was the first answer. - Zifre
(17) I wish somebody was there to tell me in school several years ago..some how this aspect is totally lost in our schools and universities. - CodeToGlory
(12) Even though vital for high-quality software, this practice generally clashes with real-world pressures and deadlines. In open source, totally - but good luck convincing business people that you have been doing something other that "create". A lot of us "just do it", on our own time, but that can get old if you are not billing by the hour, eh? - Andrei Taranchenko
(1) In answer to the pressure not to do anything other than "create", in many cases writing tests helps you with that creation task. TDD is the obvious example, though you don't have to go full-blown TDD for testing to help you write your code. Before I started writing proper unit tests I still found myself writing code to test my classes, I just didn't keep that code. - thomasrutter
(1) @CodeToGlory: +1 Agree. Designing the software and emiting the code is so much easier when you have an example of how it would be used. - TokenMacGuy
(1) In practice, the higher cost of code failure the more one should adopt unit tests. Code for the space shuttle should be well tested. - Chad
(1) Best answer yet! - Toran Billups
10
[+54] [2009-05-03 22:58:09] Mark Simpson

Not adhering to YAGNI is a big mistake. (YAGNI = you ain't gonna need it). Build what you need, not what you think you might need.

It helps when retaining a focus on the important things and the bigger picture.

Even at an API level, the intent of some functionality can be obfuscated by having multiple redundant additional methods or method overloads. Noise in a code base is annoying to work with. E.g. if you only need to load something in memory, don't provide an overload to load it from a file path "just because" you might need it in future.

This is especially the case when it comes to bugs. Unit testing finds a lot of bugs, but some will lay hidden until the functionality is actually used by someone. If you have code that is not being run, the bugs won't get found so quickly. Furthermore, when the bugs are discovered, that code might be ancient and the person who wrote it may not remember it, let alone be at the company any more.


(7) Agree. Don't start by writing the "library" code. Start the project by writing the main() method, inventing nice names for the things you will need, as you write. Once you have written the main() method, just implement all the functions and objects it used and your application will be finished! - joeytwiddle
True, but misses an important aspect: generalising well can save you time/code and open up opportunities/create functionality. Usually it's worth the time spent. - sje397
It can be good, but trying to anticipate future use cases via speculating is very hit and miss. Unless there's a concrete case for it or it would be prohibitively expensive to crowbar in the generality, I err on the side of caution. - Mark Simpson
11
[+40] [2009-11-20 15:36:36] Carl Smotricz

I'm much too easily sidetracked. I want to write a program in Clojure, but first I'm going to create a Web site where the Cheat Sheet and API reference are cross-linked and extensible. But before I do that, I want to write a proxy so I can hear last.fm while I code yet another diversion...


(1) Ah, the dreaded yak shaving. sethgodin.typepad.com/seths_blog/2005/03/dont_shave_that.html - Tenner
(12) Is there a term for yak shaving that involves reading about yak shaving? Hmmm, I was going to do something else, but now I need to figure this out. But first I need to check Wikipedia to find out what a yak is. Is it like a cow? Speaking of cows, I can't remember the last time I had a nice steak. Steak...stakes? Stakes...vampires! Which reminds me, what is up with all this Twilight/New Moon hype? And why do werewolves always have to get involved? Were they always the mortal enemies of vampires or is that just Hollywood? But seriously guys: Does anyone have a razor I could borrow? - Mike Spross
(1) Also, I have the same problem mentioned in the answer... - Mike Spross
(2) OMG, my diversion made me read this. Got to get back to work... - Frunsi
@Tenner: Thanks, now I've spent the last hour collecting anti-yak-shaving bookmarks. - J Bryan Price
12
[+34] [2009-11-20 15:36:54] Tenner
try {
    myCodeHere();
    doSomethingElse();
} catch (Exception e) {
    // TODO: Handle this intelligently later.
}

If I'm having a good day I might add e.printStackTrace();.


(5) cntrl+1 in eclipse does it all for you. How could you resist? - Stefan Kendall
(2) I'm guilty of this. - R Siefkas
(1) @Stefan: Because I primarily use NetBeans. :-) - Tenner
(1) Yep, the default behavior should be throw ex. It's still incomplete TODO code, but at least it's correct code. - Loadmaster
(3) @Loadmaster : You mean just throw; otherwise you lose your original stack trace. - ildjarn
13
[+34] [2009-05-03 22:26:20] Chad Grant

learn these by heart so you know when you are repeating them http://en.wikipedia.org/wiki/Anti-pattern


(3) +1 I regularly look back over these just to refresh. - Lucas B
14
[+30] [2009-05-03 23:13:44] Fortyrunner

Writing monolithic routines is one of the worst things you can do.

Try to write small, dumb code. Its harder to write code that looks like something anyone could write - that takes real skill.


(5) If the routine doesn't fit on a printed page (or is browsable in your editor without using scrollbars or PgUp/PgDn, depending on your hardware setup) it should be refactored. - peSHIr
(1) @peSHlr: my monitor gets bigger every year or two. - André Caron
15
[+30] [2009-11-20 15:39:22] Dave Webb

It's always easy to be wary of anything that's Not Invented Here.

I think Rands summed this up best [1]:

NIH ("Not Invented Here")

Term to describe behavior where an engineering team will not consider working with anyone's code except their own. It's not that the external code is good or bad, it's just foreign which means it must be reviewed, reformatted... oh, what the hell. LET'S REWRITE THE WHOLE DAMNED THING. Billions of dollars have been lost to NIH. I mean it. Billions.

[1] http://www.randsinrepose.com/glossary%5Frecent.html#nih

(4) Very true. Often also referred to as "Reinventing The Wheel". We're all guilty. The risk is - if something goes wrong fixing it is likely to be more costly, so on our (reasonable) assumption that stuff always goes wrong that it must be more dangerous! - joshcomley
16
[+27] [2009-11-20 15:36:15] Mikael S

Definitely the lack of comments.

Also, I often find myself afraid of using "ugly" solutions, preventing me from getting work done. Of course, in itself, this isn't a bad thing, but somethings you just have to do something to keep on moving with the project.


(1) You hit the nail. I have seen code with over one thousand lines of code and about two comments. Plus, this happens on a ton of code-files in the app! The fun part is that now I have to maintain the code, the original writer has left the company or NIH and the pleasantness of my performance review depends me doing a good job - Phil
(2) I consider a comment equal or greater in value to a statement, so I am rarely guilty of this. Writing a comment first helps me to write the rest of the code in a block. - Loadmaster
(1) I don't consider a lack of comments to be a problem in many cases. Well-written code will often explain itself. Where it doesn't, a comment can be added later on, after you've looked at the problem with fresh eyes. - Eric Walker
x++; //add one to x - Russell
Worse than the lack of comments: the need of comments. - Philip
17
[+25] [2009-05-03 22:22:00] Decio Lira

Don't get hooked on just one programming language, learn what that language is good for and what its not. Learn to use the right tool for the job.


(1) I disagree. If you can become fluent in one language, especially a general-purpose one (Python, C, Java, etc.), you can do just about anything. - Tony
(1) One language is just a tool you need to know how and where to use. You cannot work in Python if you are in a team proyect that doesn't use it, or if you are fixing code already done in another language. Not always one language can solve all your problems. For example if PHP or ASP.NET or JSP is not installed in the server that your client is using and paying, you just have to use the language that is already there. Simple as that! - backslash17
(2) Agreed, you don´t wanna have just a hammer in your toolbox. - Decio Lira
18
[+24] [2009-11-20 15:34:35] MarkPowell

I have a tendency to forge ahead with a poorly thought out implementation when a deadline approaches telling myself I'll go back and refactor... which, of course never, happens.


19
[+23] [2009-05-04 14:55:20] Gabe Moothart
try {
    ...
}
catch( Exception ex ) { }

(3) This type of thing is infuriating. Just thinking about it now is making me unbalanced and insane! - Harry Lime
(2) catch(Exception ex) { //dont crash } - Luke Lowrey
(1) The worst is "catch( Exception e) {throw e}" which does nothing but make jr people think you wrote error handling... - sal
(3) try { /*...*/ } catch {} // The lazy dude doesn't need to type "( Exception ex )" - jrcs3
(2) @jrcs3 yeah, but as a bonus if you leave it in you'll get an "unused variable" warning. My current project is littered with these. - Gabe Moothart
20
[+20] [2009-11-20 15:46:26] ProKiner

Being anal and formatting other developer's code because it isn't "pretty".


(8) Prettiness can lead to legibility, so this isn't necessarily a vice. Depends whether it was perfectly pretty before, but just not to your taste :) - joshcomley
(5) Get your company to define an eclipse formatting document, and make everyone use it. Cntrl+shift+f. You should never be manually formatting code. Ever. - Stefan Kendall
(1) @StefanKendall Other people think I'm doing some sort of magic when I wave three fingers an make it readable again. Gandalf style. - Secko
(1) @StefanKendall: Agreed. Ctrl+E+F from Resharper keeps my manual reformatting to a minimum. But when I see a line of SQL code in a sproc that is 400 characters wide, all on one line, I can't help myself. - ProKiner
(2) @Stefan, the beauty of Eclipse formatting docs are great, and it's even better when the authors themselves don't use them. - Stevus
21
[+19] [2009-05-04 02:59:39] Mark Essel

Quick list (in no particular order, but make sure you read about Occam's Razor)

  1. Stressing out is the worst thing you can possibly do, read up on the neuroscience here: Stress makes programmers dumber [1] and the original series The Programmers' Stone [2]
  2. Just keep doing and learning iteratively. Don't get too discouraged early on. Make time to look for prepackaged solutions that you can easily understand (try not to reinvent whenever possible).
  3. Don't blindly copy (cut & paste) " Copy and paste programming [3] is probably one of the worst habits that one can develop while starting to learn programming. Learning how to program is best achieved by writing code and understanding other people's code. If you copy/paste code without understanding what's going on, you are doing yourself more harm than good." (from tvanfossen [4]
  4. Always set enough time aside for testing each piece of code [5] that your write or connect to ("unit testing") codetoglory [6]
  5. Look for common pitfalls in logic ( antipatterns [7] from deviant [8])
  6. Use appropriate named variables, whether long or short hypoxide [9]
  7. Great one, don't optimize code before you have to, unless you know for sure the inner most loop call and the code isn't IO/memory limited originally from rob-kam [10].
  8. Catch error states. Die gracefully when you need to, pass back an error state otherwise
  9. OCCAM's RAZOR [11]

(My personal far out web design top 10 [12] designed them thinking about web content, but all are applicable to coding)

[1] http://www.victusspiritus.com/2009/04/12/stress-makes-programmers-dumber/
[2] http://the-programmers-stone.com/about/
[3] http://en.wikipedia.org/wiki/Copy_and_paste_programming
[4] http://stackoverflow.com/users/12950/tvanfosson
[5] http://en.wikipedia.org/wiki/Unit_testing
[6] http://stackoverflow.com/users/65159/codetoglory
[7] http://en.wikipedia.org/wiki/Anti-pattern
[8] http://stackoverflow.com/users/94306/deviant
[9] http://stackoverflow.com/users/84873/hypoxide
[10] http://stackoverflow.com/users/25093/rob-kam
[11] http://en.wikipedia.org/wiki/Occam%27s_razor
[12] http://www.victusspiritus.com/2009/03/26/10-far-out-methods-to-creating-effective-web-content/

22
[+18] [2009-05-03 22:24:31] Joey Baker

Comments. Comment everything you do. You will thank yourself later, and so will your teammates. Besides, it makes documentation writing easier/unnecessary.


(30) Better to write code that doesn't need comments to be understood. - tvanfosson
just don't comment, you have to learn what to comment and how. if I see another // increases by 1 i++ I swear to God... - Decio Lira
(5) my opinion is that properly written properly unit tested code will need 1 comment for every 20 lines; you're just adding a second set of code to maintain -- that's not compiled and is only vaguely coupled to what the actual code is doing - jcollum
(36) Comments Lie. If your comments are explaining the how, then they are not good comments. Instead of explaining the code in comment form, refactor it so that it is more understandable. The comments should ONLY express the WHY, as in 'why would anyone want to do this?' or 'why did you use this algorithm instead of that' or 'why does this even work at all?' - TokenMacGuy
(1) I upvoted you. I think comments are imperative for team environments, especially in circumstances where others will likely touch your code during maintenance. At work, I'm currently in the processes of debugging horribly written code by an engineer who was fired. My efforts would be greatly reduced had he written comments that briefly explained what the code is supposed to do. - Doc
(6) 90% of the time you find yourself writing a comment to explain what some function does or what some variable is for, it means you picked a poor name for that function or variable. Name things so as to make their role obvious. - glenra
+1 This was a question about beginners who are going to write crap code no matter whether they comment or not. Better off at least knowing what they were trying to do than to trudge through it trying to figure it out. - SnOrfus
(8) I write a comment for every method/function describing in brief what it does and what it returns, good for documentation afterwards, otherwise it probably averages a single comment every 10 to 20 lines. - thomasrutter
(1) 90% of the time I spend debugging other peoples code is spent cursing the ones who thought their code clear enough to not write any comments. Far too often they were wrong. - David Sykes
(2) I think a corollary for this would be to keep comments either up to date or delete them. There's nothing worse than an out-of-date comment: It wastes your time and it makes you distrust the rest of the comments in the code. - cdmckay
23
[+18] [2009-05-03 22:40:29] chris

Refactor. Don't be afraid to re-do it. With proper design and good test coverage, this shouldn't be a problem. If it is, figure out why - you'll learn more from mistakes than successes.

[Edit] Er, don't be afraid to refactor I guess is how it should start :)


(1) A good way to approach this issue is the Brooks observation "Build one to throw away, you will anyway" even down to the function level. First attack the problem as quickly as you know what the problem is. You may go through a few iterations of getting a solution. Once you have a function that solves the problem correctly, immediately rewrite it so that it is elegant while the knowledge is still fresh in your mind. The alternative is to have to MAINTAIN it in its scrambled up state months or years later, which usually results in the whole thing being deleted and rewritten. - TokenMacGuy
24
[+17] [2009-05-03 22:26:01] jrcs3

Don't get too stuck into the computer mode of thought. It does you little good to brilliantly solve the wrong problem with elegant code.

Customers are smart people, but they don't speak our language. And they pay our salaries. We need to come to them and learn to translate their requirements.


25
[+16] [2009-05-03 22:23:37] Chad Grant

Think 100x longer about the code before you write it. Don't just start spewing spaghetti


(1) Are you serious? 2.5 weeks of thinking for every hour of programming? You'd only be able write code for about 1 week every other year. Now that's productivity. - tvanfosson
(16) I disagree with this. If your code starts becoming spaghetti, then you need to refactor and move on. Some of the coolest things I've worked on, started as a mess of methods just to prove it could be done but then was refactored into a robust extensible thing... - JoshBerke
(5) I am traditionally tasked a project long before I start it and I think about how I will implement it and the new features weeks before I actually do which results in cleaner code and increase in productivity. Sorry you don't agree. It's more of a statement about newbies anyways and so many NOT thinking before coding. But I am probably unusual and OCD about code. - Chad Grant
(4) +1 - So many problems seem obvious and avoidable when given some proper thought. You could save a day of refactoring (and a day of writing the original) if you just think for 20 mins before 'coding from the hip.' - SnOrfus
(2) +1. The 100x is an obvious hyperbole. It's much better to think about the problem and the solution before diving right in. - Min
(1) codes are read more frequently than they are written. So spend more time before you write a code as that one line might become a legacy code down the line in your project, when you are long gone. - zeroin23
26
[+15] [2009-05-03 23:41:20] Jason Baker

Putting off writing documentation until the last minute. This is one that bites me no matter how much I try to avoid it.


27
[+12] [2009-11-20 15:32:18] Steve

For small functions, I use

  int tempval = ...

and the like. I know I shouldn't, but sometimes I can't think of a descriptive name, and ... oh well.


28
[+11] [2009-05-03 22:26:05] bcml

This should set a bad example [1], while this is more positive [2] (and serious).

[1] http://freeworld.thc.org/root/phun/unmaintain.html
[2] http://samizdat.mines.edu/howto/HowToBeAProgrammer.html

+1 for the first link - what a hoot - Preet Sangha
29
[+10] [2009-05-03 22:22:25] Theo Lenndorff

Don't duplicate behavior by copy and paste code.


30
[+10] [2009-05-03 22:59:21] Adam McKee

Don't assume anything - ask when you don't know, and comment your code when you do.


(4) +1 - Too many people try to pretend that they know more than they really do and end up writing or speaking garbage. If I ask a question, simple or not, now I know that I'm smarter than when I woke up (and I find that people love sharing knowledge, and generally don't care that I 'didn't know') - SnOrfus
31
[+9] [2009-05-03 22:26:27] Guffa

Have a look at programming anti-patterns [1].

[1] http://en.wikipedia.org/wiki/Anti-pattern#Programming%5Fanti-patterns

32
[+9] [2009-11-20 15:49:33] Jack

Thinking I know what the users want, without checking it often enough.


33
[+9] [2009-11-20 16:18:46] Galwegian

I actually listen to what the user says he wants. I later realise he hadn't a clue.


+1 A mockup tool can be a great help - Andomar
34
[+7] [2009-05-03 22:31:16] Roman

If half-year later you'll think that you are "guru" in C++ (or java or any other language or technology) then it'll be you great mistake. Your should always know a little more than other but you should think that you still don't know anything. It's a key to success in programming. Learn, learn and learn again.


That's right. Consider Norvigs "Learn Programming in 10 years": norvig.com/21-days.html - bcml
35
[+7] [2009-05-03 22:42:08] Mike Dunlavey

If you must optimize, don't be penny-wise-pound-foolish.

I've seen people sweat over which optimization level to use on the compiler, or whether ++i is faster than i++, on code full of function calls (i.e. p.c. never there) to multiple abstraction layers, sapping magnitudes of performance, while saving neither cycles nor dev. hours.


(2) I remember an urban legend about a team that optimized the wait loop of an old school program. The program still spent the same amount of time in that loop. - jrcs3
chuckle That's terrible. - Erik Forbes
(1) @jrcs3: I love it. - Mike Dunlavey
There should be a programming Darwin award. - Erik Forbes
36
[+6] [2009-11-20 15:29:49] James

I over-engineer....hence this question Ways to prevent over-engineering [1]

[1] http://stackoverflow.com/questions/1702052/ways-to-prevent-over-engineering

Of course, the opposite vice is to under-engineer, so that your code is not easily extensible. - Loadmaster
I struggle with this on a regular basis. YAGNI helps, but it can be tricky remembering to tell myself that. This is, I think, an extremely common problem. I'm concerned that most people who succumb to it don't realize that it is a problem. - Greg D
@Greg, I am the same, there is always that little devil on your shoulder whispering...."but you might". It is definetly a problem and its something that isn't going to go away as we have so many options/ways to do things nowadays. - James
37
[+6] [2009-05-05 05:53:38] Frank

Writing code that's hard to read and needs lots of comments is a bad habit to avoid. Instead, you should write code that documents itself and doesn't need comments.

Here's an (artificial) example:

BAD:

if(!node.GetChildren().Size()){ // leaf node
  ProcessNode(node, false); // false means not internal node
}
else{
  ProcessNode(node, true); // internal node
}

BETTER:

bool is_leaf = node.GetChildren().Size() == 0;
if(is_leaf){
  ProcessLeaf(node);
}
else{
  ProcessInternalNode(node);
}

38
[+6] [2009-11-20 16:05:23] Steve Wortham

A general lack of consistency with my naming conventions.

I'm usually consistent enough when I'm focused on just one project. But my naming conventions tend to change slightly every year. So this can become annoying when I work on a project over a long period of time.

For example, did I name that database column CreatedDate or date_created? I just need to pick a convention and stick with it.


39
[+6] [2009-11-20 16:11:02] Kelly S. French

Not using a source control system like Subversion or Git even for toy or one-shot programs.

Telling yourself "I'll check it in later" is just like your pointy-haired boss telling you not to worry about all the technical debt because "We'll come back and fix it" which is right up there with "The check is in the mail" and "We're from the government and we're here to help".


40
[+6] [2009-11-20 16:13:50] fatboab

I have the attention span of a goldfish if I'm not motivated. I'd rather dream about brewing beer than doing programming I have no interest in. I plan to carry this on until I ditch programming and open a brewery, where upon the day dreaming will probably flip back the other way...


41
[+6] [2009-11-20 15:52:33] rlong

One of my vices is more the opposite of the "Re-write everything" vice. At some point, legacy code should be thrown overboard. Perhaps not too soon, but it is possible to spend too much time trying to make something that is fundamentally broken, work again.


42
[+5] [2009-11-20 15:55:31] Bobby

I always want to re-engineer everything I find "That can be done smarter" and "I can do better then that" till I end up with three different implementations of the same thing.


43
[+5] [2009-11-20 17:07:09] Thomi
  1. Posting subjective questions on SO.com in the hopes of getting more SO rep when I should be working.

  2. Answering othe people's subjective questions on SO.com in a slightly tongue-in-cheek, sarcastic manner, and hoping that other programmers upvote my somewhat dry ramblings, thereby generating more SO rep.


44
[+5] [2009-05-05 06:54:07] khebbie

Not programming deliberatly. Many programmers don't know what they are doing, they don't know why their code is working, and hence they don't understand when it suddenly doesn't work. Read "Pragmatic programmer" by Andy Hunt, it's full of good programming habits. This habit is from that book.


45
[+5] [2009-05-04 05:11:20] Barry Brown

Shotgun programming. That's where bugs are fixed by randomly changing things here and there hoping one of them will solve the problem. Shotgun programming takes many forms:

  • If the compiler says you're missing a parenthesis, throw in a parenthesis. Anywhere. If you're especially observant, you'll read the rest of the error message and throw it in on the line the compiler mentions. Then re-compile right away.
  • Same applies for any other syntax error: quotes, semicolons, braces, etc.
  • If the program compiles but gives incorrect results, change an expression or calculation.
  • If that doesn't work, add some more parentheses or braces. Or take some away.
  • Still not working? Add some more variables. You can never have too many.

But never, never actually analyze your code. That takes too much effort. Just make changes and re-compile!


46
[+5] [2009-05-03 22:51:28] Rob Kam

Don't create solutions that are too elaborately coded and overly feature-rich - Keep it Short and Simple [1]

[1] http://www.faqs.org/docs/artu/ch01s06.html#id2877917

I agree. Who wouldn't? But I've seen 2 approaches to the same problem, both considered "clean and simple" by their builders - one was 10 times bigger. - Mike Dunlavey
47
[+5] [2009-05-04 01:39:18] Kronikarz

While it's true that thinking about a problem before approaching it is very important, if you're not a seasoned programmer, sometimes you just need to start writing code. You can't foresee everything, and going over concepts in your head only gets you so far.

If you're anything like me, you'll find out that the code evolves and validates in your mind as you write it. Sometimes something that made sense on paper will turn out to be a terrible idea as soon as you write a function implementing it.

If you don't want to risk breaking existing code, start a Test project and try out the ideas and concepts for solutions there. It's sort of like TDD, but for people who like to experiment first, and then plan.


+1 for test projects. If in doubt, try it out! - Umber Ferrule
48
[+4] [2009-05-03 23:12:52] Sake
  1. Procrastination.

  2. Arrogant.


So true. And these two often work together. Arrogance can lead to procrastination because by procrastinating, you don't have to face difficulty. - Tony
49
[+4] [2009-05-03 23:36:13] Zifre

Using all kinds of weird control structures to avoid one little goto. Everyone knows that spaghetti code is a bad thing, but one goto does not make spaghetti code. Everyone seems to believe that goto causes cancer and should be removed from the language. Way too often I see code that would be much easier to read and understand if it used one little if(something) goto somewhere; instead of things like do {... if(something) break; ... } while(0);.


(2) Everyone thinks one goto won't be harmful, but.... : xkcd.com/292 - kenj0418
50
[+4] [2009-11-20 17:24:23] Andrew G. Johnson

I buy domains and then don't use them, and then renew them :(


51
[+4] [2009-11-20 15:57:33] High Performance Mark

It's no wonder that young folk are queueing up to avoid IT courses and the IT industry if we are really as boring as this. Apart from Dirk Eddelbuettel's comment, none of the answers come close to my idea of a programming vice:

  • programming while drunk at the wheel of my Porsche 911;

  • programming while engaged in sexual activities with colleagues;

  • using recreational drugs to achieve the right frame of mind before starting work at about 45 minutes before heading off to the bar for the evening.

Please, colleagues, try a little harder

Sigh

Mark


No disclosure here, but seems I won't be up for canonicalization either. - chiggsy
52
[+4] [2009-11-20 15:49:38] fleebness

I feel I do not test my code enough, or that during testing, I let certain problems through out of a desire to move on.

Some of these come back to bite me later, which can be rather embarrassing. Iterative development is nice and all, but I wonder if I've found that fine balance between doing things 'good enough' (to meet a deadline), and doing things properly (to ensure you don't get embarrassed later).


53
[+3] [2009-05-03 23:08:31] mP.

One question i have always asked myself when doing code reviews and the like is, * Is this the result of being lazy, are there areas that could be improved ?*

By that i mean could the person have done better but skipped that bit because they

  • Is documentations missing
  • Could naming of variables, methods, classes be improved to be clearer ?
  • Is it readable ?
  • Are there tests covering all the possible code paths.
  • Are input parameters tested for validness.
  • Are there too many really really long - many screen methods ?
  • Are there any helpers - often they cant be bothered to extract.

Many times there are many vital things missing, the completed polished product will not have these.


54
[+3] [2009-05-04 04:24:49] Booji Boy
  • Functions & methods should only do one thing!

  • Avoid writing functions that are bigger than needed by breaking them into smaller pieces - look for duplicate code and place this in a helper function.

  • Try to break up functions & methods with more than a few dozen lines into smaller functions - this will greatly increase readability and make maintenance and testing easier.


55
[+3] [2009-05-04 15:00:01] Feanor

The one thing that has always been painful is failing to spend time designing the program before writing any code. I've regretted it every single time.


56
[+2] [2009-05-04 04:48:41] David Plumpton

Be careful about charging ahead in your own direction when five minutes talking to somebody might save you a day of effort. It's usually worth just asking somebody.


57
[+2] [2009-05-04 04:52:25] nevets1219

Using the debugger (and ONLY the debugger) to fix bugs or code. It's really handy and easy to do this sometimes that you end up spending more time with the debugger than thinking about the design or logic.


That's unfortunately true! - Frank
58
[+2] [2009-05-04 13:12:12] Benedikt

If you start a new programming task don't jump directly into the code. Great programming starts on paper (or in the UML-editor if you wish).

Think before you code - it will save you a lot of time by not having to throw away solutions that didn't work out.


Also: don't be afraid to throw away a solution that didn't work out. - cdmckay
59
[+2] [2009-11-20 15:39:10] Kaleb Brasee

When I tell myself, "Oh, I'm in a hurry so I'll write unit tests when I have some spare time", I never end up actually writing them. Which is why I've been forcing myself to write them first.


60
[+2] [2009-05-06 07:37:38] Rodrigo Guerreiro

One terrible habit I still encounter in the companies I work for is the lack of will to extend their knowledge. Most programmers learn a specific technology and stay there. Don't evolve. Others think that the company should pay them to take a course to recycle their knowledge. I don't think that's the way to go. We, personally, must have a conscience that we need to continuous learn.

The bad habit is: don't ever stop learning!


61
[+2] [2009-05-05 11:20:48] the_drow
  • If you are doing something really special then it has 90% chance to be a hack. Don't over complicate simple things.
  • Use a pen and paper! Write down a short description of your original idea, then list the features one by one. Give priorities and schedules. After that you can use a UML program to design the components of your program. Only then code.
  • Read [1] about top-down and buttom up design. Choose what's best for the language you use.
  • Don't relay on the internet as your only source of learning. Get books and read, they often elaborate more on your langauge.
  • Get to know well with the standard library of the language you're using. Don't re-do what's already done for you. It is probably done better anyway.
  • Take breaks between coding sessions, I often find that when I'm stuck doing something else helps a lot.
  • Sleep! Some coders tend to be obsessed to solve a problem when their code isn't working. While you're sleeping your brain still process thoughts and problems, just think about your problem before you fall asleep. I had sereval occasions where I woke up in the morning with the solution for a bug or a new feature that I didn't know how to implement.
  • Read about coupling, design patterns, coding standards.
  • Read blogs of programmers, you learn great things from first hand.
[1] http://en.wikipedia.org/wiki/Top-down

62
[+2] [2009-11-20 15:28:48] joshcomley

I don't comment enough.

I know I do it, and I keep trying to fix it, but I keep coding too quickly and forgetting to comment!


(3) I don't necessarily consider this a vice if the code is otherwise good. Comments, to me, are a code smell that suggest the code is doing something in a dumb way (which may be unavoidable) or that it's not as clear as it should be. - David
63
[+2] [2009-05-04 01:09:04] Mike Dunlavey

Believing (without question) what you've been told, or "common wisdom".

What others tell you may be true, but often it is just an attractive idea that started somewhere and keeps traveling, like a virus.

Your brain is yours. Use it. Think for yourself.


64
[+2] [2009-05-04 01:18:25] Jason
  • Always document your code
  • Use correct tab spacing
  • Name you variables and classes something of meaning

65
[+2] [2009-05-03 22:56:56] Steven Devijver

In OO languages, not knowing about high cohesion, low coupling. Or not caring about it (which means not constantly measuring).


66
[+2] [2009-05-03 22:28:22] eKek0

Not analysing anything before programming


67
[+2] [2009-11-20 16:02:43] dsimcha

Being too insistent on having a simple interface to things, no matter how much it complicates the implementation.


68
[+2] [2009-11-20 16:14:17] Juliet

Coding while intoxicated. I always regret it the next morning.


(1) Cue obligitatory 'ballmer peak' xkcd reference: xkcd.com/323 - Kirschstein
69
[+2] [2009-11-20 17:23:34] Hillarie

I think I see myself in so many answers here already!

My vice of late: Adding this comment: //TODO: Refactor this later and then never finding the time to return.


70
[+2] [2009-11-20 17:53:25] community_owned

For small projects, jump right into coding without having a clear design in my head; which costs me time later or at some point when things starts getting messy.


71
[+2] [2010-10-21 09:27:44] Zehntor

Writing:

if(variable == true) variable = false;
else variable = true;

Instead of:

variable = !variable;

Or:

if(condition == true) ...

Instead of:

if(condition) ...

Or:

if(condition) {
    // Do nothing
} else {
    // Do something
}

72
[+1] [2011-09-02 12:32:00] Libertytoad

Multiple return statements from a method/procedure is a big no-no. Don't code in a 1960's COBOL style and do this--it was unacceptable in the 70's and still is today. If you have multiple returns like this, then you code is structured poorly and needs to be re-thought. Chances are, the method/procedure should be broken into smaller methods.

Limit methods/procedures to no more than 100 lines or so. Again, if you are exceeding this your code is probably poorly structured. I see new coders writing methods that are over 1000 lines long. Don't do it!


73
[+1] [2009-11-20 17:34:03] chiggsy

I spend a lot of time tweaking my editor, my shell, my fonts, my colours etc. I do this because when my vim-fu is rocking, then programming is a tedium free romp. I always find time to do this, and I never think it is time wasted.

So really I have 2 vices here:

  1. See above.
  2. I lack remorse about my vices.

74
[+1] [2009-05-03 23:51:12] jrcalzada

Jumping straight into writing code without thinking about what you're trying to achieve and how you're gonna do it usually leads to inconsistent code that needs to be re-written again and again.

Also, don't get too attached to a particular paradigm. There are many ways to approach a certain task and one of the most important things you can learn is to choose the right tool for the job. Don't be afraid to step outside your box.


75
[+1] [2009-05-04 04:37:05] Gabe Anzelini

If you run into something not working the way you expect, it is possible that it is the framework's fault, but not in your (or my) case, check, double check, and triple check your code, then sleep on it and then check your code again... see it was your code and not the framework :)


76
[+1] [2009-05-03 23:22:58] Aaron Daniels

Not paying attention to details.

Programming is all about details, yet it amazes me how many times I come across code where the programmer just did a sloppy job. Cut and paste jobs, duplicate code everywhere, and inconsistency across the code base are inexcusable.

It really comes down to refactoring. You wouldn't expect any other professional to just walk away when they finally got something "working". I'd hope the surgeon would pull his gloves out of me and stitch me back up when he/she's done operating.

One side note...If Visual Studio gives you a warning, treat it as an error and fix it! Don't leave that variable there if its not used. You know who you are.


77
[+1] [2009-05-06 05:00:58] Chad

Also, trying not to blatantly repeat the many good ideas above

Avoid global variables. Write every with reuse in mind. Identify common patterns and implement them as parameterized routines.


78
[+1] [2009-05-08 03:48:34] Serapth

Just because you have a shiny new hammer, doesn't make every problem a nail!

This is the one I perhaps the most guilty of. I add a new tool to by arsenal and then I start going looking for reasons to use it. Just because your language added a neat new feature like say, generics or anonymous types, doesn't mean you should use it everywhere! Tried and true methods are tried and true for a reason. Just because something is new, doesn't mean that way is better.

Now, if you will excuse me, I have to go recode all of my WebForm code into MVC!


79
[+1] [2009-05-04 23:35:03] Marc-Andre R.

Things to avoid to me :

  • Not putting comments in code, this behaviour can become a dead end faster then you can imagine
  • Avoid difficulties by writting an extended code that does the same : BAD! You better learn how to do it right the first time so the next time you'll do it well the first time and you won't have to refactor it.
  • Do bread programming, explication : No indentation in code, everything is at the same level. You MUST put indentation in your code to help the comprehension, and also for other peoples who might get their hands on your code

Things I encourage :

  • Don't be affraid of learning new technics, going further than what you learn at school will difinitely pay off on your grades
  • Have a problem? Google is your friend. You can, with the good keywords, nearly solve every problem you can encountered with google.

This is my first tought on it, I might add some stuff later ;)

Hope this help you ;)


Good code doesn't need many comments. I agree on writing Javadoc / Doxygen comments for every function. But the code inside a function usually shouldn't need comments. - Frank
This is what I was talking about ;) - Marc-Andre R.
80
[+1] [2009-05-05 09:56:42] bubaker

Coding first

This may sound obvious, but coding before thinking through a task/problem beforehand is still more common than you'd think. It may even be behind the lack of useful comments in code - they're harder to add later, except maybe throughversion control.

I've itched this scratch and suffered occasionally from it with logic errors that cost more time debugging since the code give results but solves the wrong problem or in the wrong way.

I'm not saying you should contemplate universal implications or have a formal definition of things, . But, a simple diagram or checklist helps provide a mental model to revisit and confirm. Mind mapping [1] tools can help for many projects, for design as well as collaboration and documentation.

In programming exercises for beginner students, I usually recommend that they transcribe or paraphrase the homework problem text to serve as comments for their code.

This reminds me of recommendations for checklists in operating rooms [2] because they significantly reduce surgical errors and patient mortality.

[1] http://freemind.sourceforge.net/
[2] http://www.healthfinder.gov/news/newsstory.aspx?docID=623093

81
[+1] [2009-05-04 14:42:06] Frank V

The number 1 thing I wish had been emphasized in school was design patterns. The std. Design patterns have given me a new perspective on OOP and I wish my college had presented it. I've also started to try to invent my own design patterns based on rules the book outlined (mentioned below).

That book is (I think it is great): http://oreilly.com/catalog/9780596007126/

Head First Design Patterns

Also, take a look at: http://en.wikipedia.org/wiki/Design_pattern_(computer_science) [1]

[1] http://en.wikipedia.org/wiki/Design%5Fpattern%5F%28computer%5Fscience%29

82
[+1] [2009-05-05 05:30:45] amit-agrawal

Make sure that you get your code reviewed from your peers. It always helps!


83
[0] [2009-05-05 05:20:26] Cuga

Realizing that as a developer, you're going to have expectations placed on you to solve problems that you have no clue of how to fix. You'll never stop learning new things.

Runner up: Model-View-Controller (MVC).

Also-- get used to having weeks and even months worth of dedicated hard work completely thrown away by your manager b/c she decided to change direction on a project just before its deadline.


84
[0] [2009-05-04 13:21:22] community_owned
  • copy and paste
  • ignoring error handling
  • using PHP
  • meaningless variable/function/method/class names
  • lack of comments/documentation makes it difficult to find out the intent of the program

Why downvoted, it's not a bad answer. Quite meaningless, but so are most answers to this kind of questions? +1 compensation - erikkallen
Using X programming language doesn't make one bad programmer, PHP is widely used programming in web. You have so many web frameworks, CMS etc. . . . - kracekumar
85
[0] [2009-05-04 13:02:09] Mr. Brownstone

Code first, think later.


86
[0] [2009-05-04 16:58:18] King_Jester

No matter what approach you take to writing a program, it will likely take longer than you originally anticipate, especially when you are new to it. Learn to budget your time.


87
[0] [2009-05-04 17:24:53] community_owned

I agree with many of the above, but I would like to add three more; the first from "Unix Principles" that were compiled back in the early days of computing which are amazingly pertinent to today. That is create each program to

Do one thing and do it well

Look at such things as grep or awk. Many programs today try to be all things to all people and they are difficult to figure out and probably horrible to debug. This does not mean that you have to apply it to the top level application, but apply it in functions, methods etc.

The second thing is

** Do some planning on paper before starting to write code, even if you have a hot idea in your head about how you are going to write it. **

The reason for this is that you will tend to think of modifications to the basic design as you are writing the code. This means that you have to step back and re-design some of the code that you have already written. A little design brainstorming will make the whole process go better.

Pre-factor before you re-factor

The third thing is: **

Don't use a database for just a few records of a simple data structure

**

If you are using a random access record file of say 10 or 100 or even 10,000 records of some simple structure that doesn't need to cross reference another database just "roll your own" random access file or use xml and then access the data through your own code. I have had to install database programs of different types just to handle some insignificant amount of data. Databases are great for businesses that have millions of customers and a lot of tables are involved that have to access one another. But they are bloat for a few thousand or less simple records of say "Name, Address, City, State, Zip code"


88
[0] [2009-05-04 23:16:59] Gerard

Not getting an agreed requirements/specification document (or similar), signed off before writing any code.

It will kill your enthusiasm for coding rapidly when you discover that you have not hit the objective and have to re-write everything.


89
[0] [2009-05-09 06:31:27] zeroin23

things that might help you become a better coder (and that they forgotten to teach you in school).

Dont repeat yourself [1]
Command-query separation (CQS) [2]
Defensive programming [3]
S.O.L.I.D. [4]
Code Smell [5]
Smells to Refactorings [6]
Patterns & Anti-Patterns
Software Craftsmanship [7]

[1] http://en.wikipedia.org/wiki/Don%27t%5Frepeat%5Fyourself
[2] http://en.wikipedia.org/wiki/Command-query%5Fseparation
[3] http://en.wikipedia.org/wiki/Defensive%5Fprogramming
[4] http://mmiika.wordpress.com/oo-design-principles/
[5] http://en.wikipedia.org/wiki/Code%5Fsmell
[6] http://www.industriallogic.com/papers/smellstorefactorings.pdf
[7] http://en.wikipedia.org/wiki/Software%5FCraftsmanship

90
[0] [2009-05-17 16:51:37] ozke

Not planning things enough.

Sometimes even a pencil and paper is one of the best programming tools. Of course, UML and other techniques can be really helpful in big projects. Writting a summary of what you're about to do also helps. Doesn't matter if it's pseudocode, or just a regular descriptions.

Conclusion, you'll develop in a quicker and more efficient way if you REALLY know and understand what you're about to do.


91
[0] [2009-05-17 16:57:36] Marco van de Voort

Programming behaviour not matching the project-class, and in general lack of project management:

  • Overengineering simple programs. People are often way to anxious to show-off what they have learned software engineering wise, and that they have read GoF. A 1000 line throwaway program is not the place to demonstrate these.
  • Throwing together large apps. The opposite of the previous. Typically very slowly growing due to steadily growing requirements, without ever rediscussing the base assumptions etc of the project. It is always just that one feature more.
  • Doing 2.0 rewrites that totally get out of hand. Always keep the scope of rewrites limited or their time to market will slip horribly. There is no Silver Bullet, also not in rewrites.
  • Mixing in fashionable technogies without direct demand from the customer(s).

92
[0] [2009-05-20 01:35:27] stogdilla

For Large Projects:

Avoid trying to 'push through' a project if you get stuck. Go do something completely unproductive. (joke?) Something else and get your mind off of the task at hand... sooner or later the answer will come to you. Breaks are sometimes the best programmers true friend.


93
[0] [2009-05-05 14:29:29] Rob Spieldenner

Not writing unit tests for your code is a horrible habit to get into. Learn a unit testing framework for your language of choice and try out Test Driven Development for a couple of weeks you'll never go back. The tests help you design better APIs and actually change and improve your code without fear of breaking things.


94
[0] [2009-05-03 23:03:31] MatrixFrog

Pick a style of indentation that makes sense (if your professors are giving you lots of code samples, the easiest may be to just use whatever they use) and use it. When I try to help people in my classes I'm amazed by how some of them are totally unable to indent their code in an organized way. (I would say it usually means their thought process is equally disorganized.)

Learn other languages. If your classes are all in C++ or Java, go online and mess around with Python or Lisp or Prolog. It's fun!


95
[0] [2009-05-03 22:55:08] Bob Ralian

If existing classes or functions don't have quite the behavior you're looking for, modify them to include it. Don't rewrite new classes/fucntions yourself. Duplication is very bad.

When your code turns to spaghetti, spend some time rewriting it to make more sense.

Comment liberally.

Use sensible names for classes, methods, functions, variables, tables, etc. Use a standard naming convention for each (camelCase, underscores, whatever... just think about it and stick to it).


96
[0] [2011-11-12 10:01:53] Ankur

Do not use IDE's or code completion to write simple programs. Instead use simple text editors like VIM, PICO or notepad.


97
[0] [2011-08-04 04:59:54] kracekumar
  • You should code so that your peer can understand .
    Bad style
    a=b

Good style
a = b


Don't try that in bash, though: it won't work with spaces around the '=' ;) - VonC
@VconC : yea, but exceptions are not rules :) - kracekumar
Consistency is the important part. As long as you don't bounce back and forth, it doesn't really matter how you do it. Also, some languages lend themselves to certain idioms more than others, which also has an impact on styles. - cdeszaq
98
[0] [2010-01-06 13:01:01] Marco van de Voort

Care too much about what other people think about the subject.


99
[0] [2009-11-20 17:28:16] Woot4Moo

When I prototype

if(condition == true)
    return;

instead of

if(condition == true)
{
      return;
}

And of course I forget that I was the one who wrote it and chastise random people during code reviews.


100
[0] [2009-11-20 16:14:53] community_owned

Using very short abbreviations for variable and method names, a practice I picked up from my early days as a Fortran programmer.


101
[0] [2009-11-20 16:26:12] dsoltesz

Designing as I code. Part of this practice involves moving chunks of code off to their own functions as it's all coming together... except, sometimes I never get around to that "housekeeping" step, so I end up with lengthy functions and repeated code. Eventually, in all likelihood, I end up refactoring (cough throwing it all away and rewriting it).


102