Writing better code: The self taught coder

At the start of this year I decided being ok at vbscript, VBA and SQL wasn’t enough and I was going to learn proper code. For my sins I picked VB.net and I’ve been pottering along, going to google here, buying a book there and cobbling together workable code.

The past few months has bascially been an excercise in re-writing all the stuff that books and sites told me do before I learned the next concept up, OO techniques in practice, for example, were a bit of an eye opener.

I suppose what I’m looking for is less a tutorial or even a code specific reference than something covering the underlying principles of designing/writing decent code/applications. If it matters I’m coming from a direction of wanting specifically to design/code apps that talk to Databases.

Any suggestions?

For OO techniques, the book Design Patterns by the Group of 4 is still pretty much the definitive reference.

If you’re looking for more info on designing enterprise applications, I would suggest you google the following topics:

MVC (Model Controller View) architecture (and variants of)
IoC (Inversion Of Control) architecture
Service Oriented Application architecture

Those’ll keep you busy for a while :)

Thanks for the link Talisker. I’m not a software developer but I’m starting to work with one on some small projects. I’m sure the scope of the book includes larger stuff than what I want to do, but I’m sure it will be helpful for me to understand better how the exact process works.

Definitely read Code Complete first.

Very few “self taught” coders ever end up being very good in my experience. The reason is that Computer Science programs generally teach the analytical thinking process much more than they teach any specific language.

When you are “self taught” you often learn the syntax of the language and a few ways of getting results without actually understanding which ways of getting results are good ways and which are bad ways. Unfortunately you don’t often get to learn how to think about the problems and how to organize your code well and so you end up constantly reinforcing bad habits.

If you really want to be a good programmer I would recommend you take a lot of time to understand different basic problems in computer science and understand how to solve them…the stacks and lists and simple algorithms that you’ll hear people mention.

Then when you start programming, you immediately want to assume that your code sucks and do everything you can to make it better. Your code should read like a childrens’ book when you start out. The variables should be named super-obviously, there should be comments on everything, the output should be logged exhaustively, and everything should be modularized.

This isn’t the final form of your code when you’ve had a few years of experience. It’s just so that you can be extremely intentional about what you are doing so that it’s 100% obvious to YOU what you just did. Once you get good at this then you can throttle back the obviousness.

another vote for code complete

FWIW, I’m mainly self taught, but I learn from books really easily. I had an awesome book on object oriented coding years ago, that was all theoretical, with no actual code (most books get hung up on syntax and code snippets).

The best way to learn imho is to to write stuff, then have people criticise it and then rewrite it. Working alongside really good and experienced coders is a godsend.

Interestingly, that style is probably more similar to a traditional school-education, since lectures tend to be more conceptual and less code-driven than books are. Its possible that’s a better way to learn code design concepts, and most books fail by taking the easy code-based format just because they can. (its easier to write example code than to eloquently explain a design concept)

I agree Code Complete is a good book, but its rather large and takes a while to get through.

Ive got a tremendous amount of mileage out of trying to follow these guidelines (which are elaborated in depth in Code Complete and other books):

  • Dont repeat yourself. If you have repeated logic in your code, factor that out into a seperate bit of code so it can be shared.
  • Have each bit of code (class, method, etc.) have one clearly defined task
  • Try to minimize dependencies between different parts of your application. Read up on the principle of inversion of control (IoC). A dependency injection framework can help here, but isn’t required.
  • The first attempt at writing any given bit of code may work, but it will likely suck. Look at the code, and think hard about how it can be made better (improve formatting, better variable names, better comments, more consistent level of abstraction, factoring bits out into other methods, etc.)

I agree that training in computer science is beneficial, but I think one of the key qualities of a good programmer is an unquenchable desire to be a better programmer. So I definitely believe it is possible to become a great self taught programmer, just as long the your brain is comfortable thinking in an analytical fashion. It takes a long time though, give yourself about 10 years: http://www.norvig.com/21-days.html

As a self taught coder who often has to train the fresh-out-college comp-sci majors how to write real code, let me respectfully disagree and state that you are only half right.

Both self taught and college educated programmers are, at the outset of their careers, missing a lot of key skills that are needed to be top shelf professional programmers. The gaps are different, but both have gaps. A good programmer will close those gaps over time and will take initiative to self improve.

I don’t think either of those things is true.

CS curricula don’t teach analytical thinking, they teach CS. (If you go to a good school. Bad schools teach trade-school type bullshit.) And since CS is useful in a lot of circumstances, this is a very, very handy thing. But it’s not the same as critical thinking, which really is the sort of thing that either you have or you don’t.

And self-taught people don’t need to just be VB monkeys. I know self-taught people who spend their self-teaching time learning about very CSy things, and who probably know more theory than a lot of people who have fancier education lines on their resumes.

Practicality
Code complete should cover it for day to day work.

Theory
If you don’t backfill your CS education that will eventually hold you back; there’s a large class of algorithms that are painful to solve without that background.

I’d recommend working your way through MIT’s free online courseware for CS, and buying whatever textbooks aren’t online.

Architecture
Interestingly, this seems to be the sole domain of hardcore industry professoinals no one ever listens to. Herb Sutter’s powerful mustache will teach you much, for example.

If all you do is take these guidelines to heart you will be better than 90% of the working professional developers (many with advanced CS degrees) out there. Sad but true.

Beyond that you should look at Gang of Four, mentioned above-- and try to get a little background in the algorithms as mentioned above, so you can recognize it when some problem’s been done already. Then you can go look it up in a book/buy a library to do the task.

You also might want to look into developing plugins (even just toy ones) for some existing open-source applications. That’s a quick way to get exposure to a complex, real-world design that has been thoroughly vetted.

Lot’s of good advice in the thread, but pay particular attention to Jason’s post. I am in a similar situation to you ( self taught code ) and am backfilling on the Theory section. My degree was in Electrical Engineering, so I think the analytical part was down pat, but there are many algorithms and nuances of data structures that you will have no idea about. The data structures come up less often, but the algorithms come up ALL THE TIME. ESPECIALLY IN INTERVIEWS. It was really painful to learn this while unemployed, so pick it up now while you have the time. It is as important or more so than learning an actual language.

You can practice your algorithms you learn about at TopCoder.com. they have loads and loads of practice questions that utilize many algorithms ( in the algorithms section of course ).

Thanks for all the advice and links so far. Code Complete is on order and I’m working through the other handy links people have provided.

I’m a long way off calling myself a developer let alone considering applying for jobs as one but it’s obvious to me that there are some pretty big gaps in my knowledge at the moment which I want to address.

Self taught programmers are great for one primary reason - passion. They liked the craft enough to teach themselves how to do it. That says something. They can learn how to write better code afterwards but, IMO, a self taught guy is in a great place to start from.

One of the best ways to learn, by the way, is to look at code written by people who are more skilled than you. Best of all is fixing bugs in someone else’s code–bugfixing requires you to develop a deep understanding of the code you’re working with. For example, I learned everything I know about Unix by figuring out how to make various pieces of software work on Linux, back in the days when Linux was obscure and nothing worked on it without hacking.

The tricky part is finding something good to work on. There are lots of open source projects out there, but the hard part is finding a project that excites you and that has a code base that matches your skill level. I would not, for example, recommend that a novice try to work on Boehm GC. :>

One more bit of advice that I forgot to mention (though it may be more of an ‘intermediate’ level tip):

  • Strive for immutable data structures where possible, and learn consider side effects in your code as evil (of course many times a necessary evil, but still…).

Expanding on this point: Take the time to learn about functional programming. I was an OO-style programmer for a number of years before I took the time a while back to read up some on Haskell and functional languages. That shit blew my mind (in the best possible sense), and fundamentally changed the way I program. Not everything benefits from a functional approach, but quite a bit does. Functional techniques are now supported in mainstream languages (go LINQ!); learn to use them!

Anyone else have experiences (good or bad) to share about functional programming?

I definitely agree with you guys that nothing beats crazy persistence and the desire to be a great coder.

I also agree that even people with the right kind of education don’t know how to code when they get out of school. I sure didn’t :)

On the other hand, I think that based on the original question, some of the advice you’re all giving is a little too deep. The advice itself assumes a higher level of knowledge than the questioners obviously possess. spacerat if either Nellie or Lorini knew what Dependency Injection was before now I’ll buy you a beer ;)

Don’t have to buy me one! I know what a dependency is from my old dbase 2 days, but not the injection part :)

I also agree that even people with the right kind of education don’t know how to code when they get out of school. I sure didn’t :)

Haha, yeah, same here. Whenever we talk about school and such, I tell my wife that I didn’t learn to be a programmer until after I graduated and got a job - my education started the same day I started working.

I was self taught and then went to school to make it “official”. The schooling was alright but wasn’t really all that helpful in the real world. Flow charts? Systems analysis? Those things had no place where I started working and this wasn’t even in games. :)