background preloader

Programming in Good Style

Facebook Twitter

A Simple Example of Object-Oriented Design: An Address Book. Rob Pike: Notes on Programming in C. February 21, 1989 Introduction Kernighan and Plauger's The Elements of Programming Style was an important and rightly influential book. But sometimes I feel its concise rules were taken as a cookbook approach to good style instead of the succinct expression of a philosophy they were meant to be.

If the book claims that variable names should be chosen meaningfully, doesn't it then follow that variables whose names are small essays on their use are even better? Isn't MaximumValueUntilOverflow a better name than maxval? I don't think so. What follows is a set of short essays that collectively encourage a philosophy of clarity in programming rather than giving hard rules. Your comments are welcome.

Issues of typography A program is a sort of publication. Typographic conventions consistently held are important to clear presentation, of course - indentation is probably the best known and most useful example - but when the ink obscures the intent, typography has taken over. Variable names vs. vs. or. Laser Game - Squeak Development Example for Squeak 3.9 (2007) Introducing the Coding Dojo by Emily Bache. About - Hacker School. Hacker School is a free, full-time, immersive school in New York for becoming a better programmer. We're like a writers' retreat for programmers. People come from around the world to spend 12 weeks writing code and growing as programmers.

We run three sessions a year, called batches, and each batch has about 60 people. We make money by helping companies hire our alumni. Our pedagogy Hacker School is a "school" in the sense that it's a place people come to learn, but we intentionally forgo many of the trappings of traditional schools. Instead, Hacker School is largely unstructured, self-directed, and project-based. This does not mean that Hacker School is a vacuum. Our environment We've designed Hacker School with one thing above all else in mind: How to make the best place for people to grow as programmers.

The atmosphere here is friendly and intellectual, and we try to remove as many obstacles in the way of people's growth as possible. Facilitators Residents Who comes to Hacker School? Press. Defensive Programming vs. Batshit Crazy Paranoid Programming. We Recommend These Resources Hey, let’s be careful out there. --Sergeant Esterhaus, daily briefing to the force of Hill Street Blues When developers run into an unexpected bug and can’t fix it, they’ll “add some defensive code” to make the code safer and to make it easier to find the problem. Sometimes just doing this will make the problem go away. They’ll tighten up data validation – making sure to check input and output fields and return values.

Review and improve error handling – maybe add some checking around “impossible” conditions. Add some helpful logging and diagnostics. Expect the Unexpected The whole point of defensive programming is guarding against errors you don’t expect. The few basic rules of defensive programming are explained in a short chapter in Steve McConnell’s classic book on programming, Code Complete:Protect your code from invalid data coming from “outside”, wherever you decide “outside” is. I would add a couple of other rules. Different Kinds of Paranoia. SOLID software design... My hopeless backlog One of the bad habits I have is accumulating lists of things to read. Google Reader used to be a handy place to categorize and keep my "subscriptions" to blogs and so forth to read.

Unfortunately, Google pulled the plug on that service in July. Fortunately, another web service, theoldreader.com provided a very similar free service. In fact, their service was designed to look like an earlier version of Google Reader. Some time ago, Google removed some "social" aspects of Google Reader and the change irked some of their users. theoldreader.com sprung up to undo the perceived damage of Google's changes. When Google announced the planned demise of the Google Reader service, Google at least was kind enough to provide a mechanism for retrieving my list of subscriptions, so I opened a free account with theoldreader.com and submitted my subscription list.

If you've got your own solution to tracking new blog articles, I hope my blog here is on your subscription list. NbK - 17 - the cookbook and pensive way of doing things. It's been a while since I was wearing gloves and mixing solutions, but I find myself a lot of times still in a wet-lab mindset when it comes to how I do things. A lot of the daily routines in the lab is center on doing things. Like putting on gloves and mixing solutions. So it makes sense that a lot of information transmission in a lab using a way that is best at his. Watching somebody do something, and then trying to replicate. Basically mimicry. Over time as we get better we start to be able to improve the way things are done by changing one step at a time.

But the first questions somebody will ask when something goes wrong is "Did you follow the recipe? " Why am I rambling on? List_of_numbers = range(0,10) for each number in list_of_numbers: if remainder of number / 2 is 0: keep else don't keep Which motivates you to think along steps that are outline what is happening. [x for x in range(0,10) if x % 2 == 0] Which says the same as the lines above, in a more formulaic way. 1. 2. 3. 4.

Norvig's LISP style guide. Robert C. Martin - Clean Architecture. Where can I find problems - CS101. Short coding exercises in Python and Java. Interactive lessons in Python, JavaScript, HTML, etc. Math problems that usually require programming. Difficult problems from coding competitions. Always copy code examples, and get them running. When alternatives occur to you, try them out. See if you can do the same things with less code, or expand on the examples to make them do more. When you have a question about what the language does or doesn't do, see if you can answer it by writing short programs: hypothesize, experiment, observe... repeat as necessary. If you just started programming, it's probably too early to start looking for projects to join. But once you're ready, the possibilities are practically endless.

How to Write Without Writing. Fix error handling first. Created 6 February 2003, last updated 24 January 2007 Often when unexpected errors first occur, I've found problems in the way the error is handled. This isn't surprising: error handling code is often the least-exercised part of your system, so it's natural that there are bugs in it. It is important to fix the error handling problems first, then the original error.

Sometimes there is a whole chaing of problems started by the original error condition, then compounded as layers of error handling code mis-handle the condition. Fix all the problems, backwards. In any program, there will be code specifically designed to handle unusual situations, whether they be errors, exceptions, failed assertions, edge conditions, odd input, whatever. The important thing about this "anomaly code" is that it is not often executed, making it a good hiding place for bugs. When a problem occurs in your application, always check first that the error was handled appropriately. If it happens, it must be possible. Stringification. Created 9 December 2002, last updated 6 February 2003 One of the greatest features of modern programming environments is also the most humble: ubiquitous stringification.

When I first worked in Java, I didn't think much about the java.lang.Object.toString method. It seemed like a good idea, and made sense, but was sort of the low-tech sibling in the method list. It was hardly "computer science" to be able to turn an object into a string. Gradually, I understood. This may be one of those features you don't miss until it's gone.

Being able to always stringify objects without a lot of rigamarole makes it much easier to use those object in "natural" ways. As described above, Java has the java.lang.Object.toString method, inspired by Smalltalk's asString method. Python provides two built-in methods for dealing with stringification: __str__() provides the "informal" (or human-readable) string for an object, and is called by the str() built-in function and the print statement.

Books, course supplements and other learning resources - CS101. Teach Yourself Programming in Ten Years. How to be a Programmer: A Short, Comprehensive, and Personal Summary. Debugging is the cornerstone of being a programmer. The first meaning of the verb to debug is to remove errors, but the meaning that really matters is to see into the execution of a program by examining it. A programmer that cannot debug effectively is blind. Idealists that think design, or analysis, or complexity theory, or whatnot, are more fundamental are not working programmers. The working programmer does not live in an ideal world. Even if you are perfect, your are surrounded by and must interact with code written by major software companies, organizations like GNU, and your colleagues. Most of this code is imperfect and imperfectly documented. Without the ability to gain visibility into the execution of this code the slightest bump will throw you permanently.

Debugging is about the running of programs, not programs themselves. To get visibility into the execution of a program you must be able to execute the code and observe something about it. How to Fix Performance Problems. MattDeBoard.net. Written on Wednesday, November 23, 2011 I posted a very brief response to a post on HackerNews yesterday challenging the notion that 8 weeks of guided tutelage on Ruby on Rails is not going to produce someone who you might consider a "junior RoR developer. " It did not garner many upvotes so I figured that like most conversation on the Internet it faded into the general ambient chatter. Imagine my surprise when I woke up to couple handfuls' worth of emails from around the world asking me what I did, how I did it, and how I got a job. I'm assuming, judging by the relatively small amount of mail I got from a random aside on HN*, that there must be a lot of people who are trying to figure out how to pursue a career in programming.

First, A Disclaimer or Two Please note that this blog post is entitled, "How I Became a Programmer", not, "How You Can Become a Programmer. " I'm not a self-help guru or wise or even a particularly good programmer. My Story: tl;dr Voila. What I Didn't Do A lot. Become a Good Programmer in Six Really Hard Steps.

One of the more popular topics here on the GDNet forums goes something like this: "Hi, I just [bought a computer | wrote a simple game | discovered a game engine] and I want to know where to go from here. I'd like to [accomplish some particular goal] eventually. What do I need to learn to get there? " First of all, understand that Peter Norvig nailed this on the head a long time ago: it takes ten years to learn to be a programmer. There's a glut of "learn X in some small number of days" type books out there; there are hordes of blog posts about "how to improve your programming-fu in a few easy ways"; and in general a lot of people come around looking for advice on how to become a whiz with minimal effort.

I'm going to change up the pitch a bit. Step One: Suck It Up. For the rest of us, though, there's something alluring about getting Really Good at programming. So the first step to being a really good programmer is to bite the bullet. While you're doing this, pay attention. How I Learned to Program. Programming is, without a doubt, the most mentally rewarding thing I've ever done. Programming taught me that life should be fun, filled with creativity, and lived to the fullest.

Programming taught me that anything is possible; I can do anything I want using only my mind. Programming also taught me that learning is fun. It showed me that the more you know, the more power you have. Programming showed me that a life filled with learning is a life worth living. Programming revealed to me who I am inside, and has continuously helped me work towards my goals. I feel extremely lucky to have had the means and opportunity to learn programming early in my life.

I have no regrets. So I figured I'd share my methods with you, in hopes that a beginner will read this and get some value out of it. If you don't want to read all this, the important takeaway here is to, above all else, have fun. Install Linux on Your Box Despite what you may think, programmers don't just "program". Have an Intense Desire. Learning to Code: The Roadmap I Wish I Had Been Given - Jimmy Li.

Log message style guide. Created 9 December 2002, last updated 6 February 2003 One of the fit-and-finish issues all systems developers need to address is the text of log messages. This is a log message style guide. Once your product ships, it will be in customers' hands. They will run it on their machines. You won't be around, and neither will your debugger. These log messages will be some poor customer's first view of you after a problem occurs. Simply being consistent in your log messages will go a long way toward making them seem professional and authoritative. When logging a problem performing an operation (for example, reading a record), say what couldn't be done: GOOD: Couldn't read record.BAD: Something happened?

When announcing an operation (either an informational or trace message), say what is going to happen. GOOD: Reading transaction log.BAD: I think I'll read the transaction log.BAD: Step 17a Use normal sentence capitalization and punctuation. Don't be flip. “Note to Self” by John Barnette. Be Confident Tests build confidence. Write ‘em. They’ll save your ass, and they’ll let you take a chainsaw to your code without being afraid of unintended consequences.

Be Lazy Write tools. Be Asynchronous If it can be done outside the request/response cycle, consider queuing it. Be Stateful If there’s a lifecycle, model it as a real state machine. Be Clear You’ll write it once, but you’ll read it a lot. Be Consistent Inconsistent file names, task names, and coding styles hurt productivity. Be Timely (but not too timely) Keep frameworks, plugins, libraries, and tools up-to-date, but think twice before using a production app to play with the bleeding edge. Be Certain Don’t speculate, get data. Be Persistent Find the root cause. Be Wrong If it’s not working, change it, no matter how long it took to write. PEP 20 -- The Zen of Python. Abstract Long time Pythoneer Tim Peters succinctly channels the BDFL's guiding principles for Python's design into 20 aphorisms, only 19 of which have been written down. The Zen of Python Beautiful is better than ugly. Explicit is better than implicit.

Simple is better than complex. Complex is better than complicated. Flat is better than nested. Easter Egg >>> import this Copyright This document has been placed in the public domain. PEP 8 -- Style Guide for Python Code. The Principles of Good Programming. Python. Bret Victor - Inventing on Principle. Welcome to the SICP Web Site.