background preloader

The Principles of Good Programming

The Principles of Good Programming
Heron-Centric: Ruminations of a Language DesignerThe Principles of Good Programmingby Christopher DigginsJuly 24, 2011 Today's post is a lightly edited repost from my blog at The Area, a web-site dedicated to users of Autodesk media and entertainment products. I came up with this list of principles to help with a recent C# training I gave, and I thought that members of the community could appreciate these principles and have some interesting insights to share. The principles of good programming are closely related to principles of good design and engineering. DRY - Don’t repeat yourself - This is probably the single most fundamental tenet in programming is to avoid repetition. KISS (Keep it simple, stupid!) Avoid Creating a YAGNI (You aren’t going to need it) - You should try not to add functionality until you need it. Avoid Premature Optimization - Don’t even think about optimization unless your code is working, but slower than you want. Related:  Programming in Good Style

PEP 8 -- Style Guide for Python Code Code should be written in a way that does not disadvantage other implementations of Python (PyPy, Jython, IronPython, Cython, Psyco, and such).For example, do not rely on CPython's efficient implementation of in-place string concatenation for statements in the form a += b or a = a + b. This optimization is fragile even in CPython (it only works for some types) and isn't present at all in implementations that don't use refcounting. In performance sensitive parts of the library, the ''.join() form should be used instead. This will ensure that concatenation occurs in linear time across various implementations.Comparisons to singletons like None should always be done with is or is not, never the equality operators.Also, beware of writing if x when you really mean if x is not None -- e.g. when testing whether a variable or argument that defaults to None was set to some other value.

Contrasting Performance : Languages, styles and VMs – Java, Scala, Python, Erlang, Clojure, Ruby, Groovy, Javascript There’s a better place to specifically look at performance comparisons across languages than this post – The computer languages benchmarks game. But this post attempts look at performance comparisons a little differently. Based on coding idioms as well. And for a much narrower range of problems (namely one). There are languages which are tightly opinionated on a particular way of doing things. And there are languages which allow you to implement a given logic in multiple ways. This is also a sequel to a post I wrote 3 years ago Performance Comparison – C++ / Java / Python / Ruby/ Jython / JRuby / Groovy. Problem Quoting from The Josephus Problem, Flavius Josephus was a roman historian of Jewish origin. In the general version of the problem, there are n soldiers numbered from 1 to n and each k-th soldier will be eliminated. Idioms I have considered three idioms : Object Oriented : This code has classes reflecting a person (or a soldier) and the chain. Contributions Hardware / Software Updates

Dean Wampler Sphere Online Judge (SPOJ) 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. Easter Egg >>> import this Copyright This document has been placed in the public domain.

Harold’s Corollary to Knuth’s Law Lately I’ve found myself arguing about the proper design of unit tests. On my side I’m claiming: Unit tests should only touch the public API. The goal is to make sure that the tests are as close to actual usage as possible. By contrast some programmers advocate that tests should be method-limited. This approach may sometimes let the tests be written faster; but not always. Would you believe performance? For instance consider this proposal from Michael Feathers: A test is not a unit test if:It talks to the databaseIt communicates across the networkIt touches the file systemIt can’t run at the same time as any of your other unit testsYou have to do special things to your environment (such as editing config files) to run it.Tests that do these things aren’t bad. More than 30 years ago Donald Knuth first published what would come to be called Knuth’s law: “premature optimization is the root of all evil in programming.”

Bruce Eckel's MindView, Inc. ::: Exceptional Learning Experiences Video Lectures | Introduction to Computer Science and Programming 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. 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. GOOD: Couldn't open storage file.BAD: Couldn't open storage fileBAD: Couldn't Open Storage FileBAD: COULDN'T OPEN STORAGE FILE Don't be flip.

MemoryImage database · application architecture tags: When people start an enterprise application, one of the earliest questions is "how do we talk to the database". These days they may ask a slightly different question "what kind of database should we use - relational or one of these NOSQL databases?". But there's another question to consider: "should we use a database at all?" One of the defining characteristics of enterprise applications is the need to store long term data, which naturally leads people to reach for a database. The key element to a memory image is using event sourcing, which essentially means that every change to the application's state is captured in an event which is logged into a persistent store. A familiar example of a system that uses event sourcing is a version control system. Event sourcing has many consequences, including the ability to rebuild past states. Against that you do have to ensure you can reliably store the events and process them. The most recent is LMAX.

Code refactoring By continuously improving the design of code, we make it easier and easier to work with. This is in sharp contrast to what typically happens: little refactoring and a great deal of attention paid to expediently adding new features. If you get into the hygienic habit of refactoring continuously, you'll find that it is easier to extend and maintain code.—Joshua Kerievsky, Refactoring to Patterns[1] Overview[edit] Refactoring is usually motivated by noticing a code smell.[2] For example the method at hand may be very long, or it may be a near duplicate of another nearby method. There are two general categories of benefits to the activity of refactoring. Maintainability. Before applying a refactoring to a section of code, a solid set of automatic unit tests is needed. The process is then an iterative cycle of making a small program transformation, testing it to ensure correctness, and making another small transformation. List of refactoring techniques[edit] Hardware refactoring[edit]