background preloader

Best practices and common mistakes

Facebook Twitter

12 common programming mistakes to avoid. Programming is an art and science and like all art and science the only way to learn is from mistakes.

12 common programming mistakes to avoid

I have made many… and I would like to share with you the mistakes that I have made over my journey with development. Don't repeat yourself. Applying DRY[edit] DRY vs WET solutions[edit] Violations of DRY are typically referred to as WET solutions, which is commonly taken to stand for either "write everything twice" or "we enjoy typing".[2][3] See also[edit] References[edit] External links[edit]

Don't repeat yourself

How to think about OO. By Miško Hevery.

How to think about OO

Flaw: Brittle Global State & Singletons. Accessing global state statically doesn’t clarify those shared dependencies to readers of the constructors and methods that use the Global State.

Flaw: Brittle Global State & Singletons

Global State and Singletons make APIs lie about their true dependencies. To really understand the dependencies, developers must read every line of code. It causes Spooky Action at a Distance: when running test suites, global state mutated in one test can cause a subsequent or parallel test to fail unexpectedly. Break the static dependency using manual or Guice dependency injection. NOTE: When we say “Singleton” or “JVM Singleton” in this document, we mean the classic Gang of Four singleton.

Why this is a Flaw. Static Methods are Death to Testability. By Miško Hevery Recently many of you, after reading Guide to Testability, wrote to telling me there is nothing wrong with static methods.

Static Methods are Death to Testability

After all what can be easier to test than Math.abs()! And Math.abs() is static method! If abs() was on instance method, one would have to instantiate the object first, and that may prove to be a problem. Breaking the Law of Demeter is Like Looking for a Needle in the Haystack. July 18th, 2008 · 20 Comments · by Miško Hevery Every time I see Law of Demeter violation I imagine a haystack where the code is desperately trying to locate the needle. class Mechanic { Engine engine; Mechanic(Context context) { this.engine = context.getEngine(); } } The Mechanic does not care for the Context.

Breaking the Law of Demeter is Like Looking for a Needle in the Haystack

You can tell because Mechanic does not store the reference to Context. Most applications tend to have some sort of Context object which is the kitchen sink and which can get you just about any other object in the system aka the service locator.If you want to reuse this code at a different project, the compiler will not only need Mechanic and Engine but also the Context. But here is the real killer! Procedural Language Eliminated GOTOs; OO Eliminated IFs.

August 14th, 2008 · 7 Comments · by Miško Hevery Procedural languages allowed us to remove GOTOs in our code.

Procedural Language Eliminated GOTOs; OO Eliminated IFs

I would like to think that OO languages allow us to remove IFs (conditionals). I know you can’t remove of all the IFs, but it is interesting just how many IFs you can remove in an application. You can’t remove these IFs: Comparing relative sizes (>, < operators)Comparing primitives. How to think about OO. How To Think About OO. We Recommend These Resources Everyone seems to think that they are writing OO after all they are using OO languages such as Java, Python or Ruby.

How To Think About OO

But if you exam the code it is often procedural in nature. Falsehoods Programmers Believe About Names: MicroISV on a Shoestring. Thai personal names - James Clark's Random Thoughts. There's an election coming up in Thailand on December 23rd and the streets are lined with election posters.

Thai personal names - James Clark's Random Thoughts

As a bit of an i18n geek, I find it interesting that the posters almost all make the candidates' first names at least twice as big as their last names. If you're also an i18n geek, your reaction might well be: "it must be because Thais write their family name first, followed by their given name". But you would be wrong. Thais have a given name and a family name; the given name is written first, and the family name last. The correct explanation that given names play a role in Thai culture that is similar to the role that family names play in many Western cultures. ‘Real’ names online, part 2 : defining ‘real’ - A Modern Hypatia. Before we get into the more interesting meat of the topic (which will follow next week), I want to go into a digression about the problem of ‘real names’.

‘Real’ names online, part 2 : defining ‘real’ - A Modern Hypatia

First problem: What does a ‘real name’ look like? We like to think that we know them when we see them. But really, we don’t. Background (‘real’ names online : part 1) « A Modern Hypatia. I’ve been watching the conversations about the new RealID additions on the Blizzard forums with some interest, because they tie into a much larger conversation about online interactions and anonymity and pseudonymity. So, in the next week or so, a few posts about first, some background, and then some specific concerns and considerations around the use of real-world identifying names online. Why does this matter on a librarian’s blog? Well, first, because I use the ‘Net, and I’m fascinated by how other people use it, and about how to help give people tools to make informed choices for their use of it.

But also because I think this is one of the major freedom of information issues of at least the next decade: how do we balance a desire for sincere conversation, with meaning and history and in the context of a particular community, with the reality that some people will abuse, harass, intimidate, or otherwise seek to harm others. Personal names around the world. Background People who create web forms, databases, or ontologies are often unaware how different people’s names can be in other countries. They build their forms or databases in a way that assumes too much on the part of foreign users. This article will first introduce you to some of the different styles used for personal names, and then some of the possible implications for handling those on the Web.

This article doesn't provide all the answers – the best answer will vary according to the needs of the application, and in most cases, it may be difficult to find a 'perfect' solution. Comment les noms diffèrent à travers le monde ? Le W3C se penche sur les implications pour les formulaires et bases de données Web. Mise à jour de Gordon Fowler Le simple fait de demander un nom et un prénom dans un formulaire web peut paraître trivial. Mais à bien y réfléchir, cela ne l'est pas. C'est en tout cas le point de vue du W3C qui vient de publier une page de conseils. Le point central est de rappeler que la notion de « patronyme » varie avec les cultures et les pays.

Un développeurs ayant travaillé sur le système de réservation des JO de Pékin rappelait par exemple que demander son nom à un utilisateur en Chine était moins pertinent qu'en Europe. D'après Wikipedia, trois noms (Nguyen, Tran, Le) sont portés par 60% de la population Vietnamienne. A l'opposé, la France est un des pays au plus grand nombre de noms de famille. Autre point sensible, demander deux noms (de famille et prénom) peut paraître très étrange dans certaines régions du monde.

Bref, demander une identité exige de se poser certaines questions auxquelles beaucoup ne penseraient pas au premier abord. « Personal names around the world » Et vous ?