Code quality; patch quality

Post on 13-May-2015

918 views 0 download

Tags:

description

Code quality; patch quality, Malcolm Tredinnick. Python user for 13 years. Linux user for even longer. Malcolm has worked with a wide variety of systems from banking and stock exchange interfaces, to multi-thousand server database-backed websites. These days, Malcolm's primary open source contributions are as a core developer for Django and advocate for Python. All Open Source projects welcome patches from people willing to help fix bugs or implement feature requests. That's why we launch the source code into the wilds in the first place. If you are wanting to contribute, however, the process can seem a bit daunting, particularly when you are first starting out. Am I doing it properly? What will happen if I do it wrong? How can I do the best thing possible from the start? These are all typical worries. I've had them, others have had them and you're not alone if they cross your mind. In this talk, we will go over a few basic ideas for producing patch submissions that make things as easy as possible both for yourself and the code maintainers. How to help the maintainers help you. Malcolm has been a core maintainer for Django for over give years and has seen a few good and bad contributions in his time. These are the harmless and useful lessons that can be drawn from that experience.

transcript

Code qualityPatch quality

Malcolm Tredinnickmalcolm.tredinnick@gmail.com

Code Quality Matters

“Have you ever written a library before? It's like people seeing you in your underwear. You gotta make sure it's clean.”

Do The Basics Properly

Yr Doin' It Wrng!

● If the word “print” is still in your patch– Similarly for the phrase “import pdb”

Yr Doin' It Wrng!

● If you don't have a test or a really good reason

– Your reason is never going to be good enough.

– Fail before; pass afterwards

Yr Doin' It Wrng!

● If you think “PEP 8” is the name of a new

energy drink

How Hard Can It Be(tm)?

● They're called patches for a reason.

● Go to the top of the project tree.

● Then: svn diff (or git diff or ...)

– “svn add” works locally

● Even if you're a translator or a documenter.

Code style is somewhat opinionated

Your opinions don't matter(that much)

Read the contributing document

Code is...

●Written once

●Read many, many times

Comments should last

Comments should be correct!

Fix problems not symptoms!

Symptom fixing...

The template code below will cause an infinite memory-eating loop if context_processors.auth is enabled. My main issue with that is that it was incredibly hard to debug for me, when I passed my own 'perms' queryset to a template....

{% for perm in perms %}{% endfor %}

Ticket #8182: Infinite loop iterating over PermWrapperInfinite loop iterating over PermWrapper

django/contrib/auth/models.py | 22 ++++++++++++++++++++++django/core/context_processors.py | 5 +++++docs/authentication.txt | 11 ++++++++---

3 files changed, 35 insertions(+), 3 deletions(-)

Where else does it happen?

Warning: may be hard!

Author: mtredinnickDate: Thu Jun 26 11:01:21 2008 +1000

... This is the same problem as [7597] but for values() field specifications, so this covers the second case where Django adds extra stuff to the select-clause.

r7740

Author: mtredinnickDate: Mon Jul 28 04:16:17 2008 +1000

...

Fixed a missed case of promoting table joins when using disjunctive filters.

r8107

r8783

Author: mtredinnickDate: Mon Sep 1 18:43:55 2008 -0700

...

Fixed an oversight when I first fixed ordering on nullable foreign keys.

r8853

Author: mtredinnickDate: Tue Sep 2 06:52:07 2008 -0700

....

+ def promote_alias_chain(self, chain,...):+ """+ Walks along a chain of aliases, promoting the first+ nullable join and any joins following that. If+ 'must_promote' is True, all the aliases in+ the chain are promoted.+ """+ for alias in chain:+ if self.promote_alias(alias, must_promote):+ must_promote = True+

Know your limitations

The crowd is smarter than you!(the code mostly works)

“Here is a trivial patch”

Translation: “here are some bugs I thought you should have”

Research is not a four letter word

● Django runs out-of-the-box on databases.

Plural.

– SQLite, MySQL, PostgreSQL, Oracle

● External backends, too

● Separate solution approach into pieces

– It has a chance of working and here's a good

start.

– It has a perfect implementation.

● Django runs on multiple operating systems

– Windows, Linux, Mac, ...

● Some things are hard

– File handling, locking, ...

– Character encoding

● Best defense is “don't do that”.

● Next best is a good offense (Research!)

● There are RFCs.

– They apply in this universe.

● There are competing specs

– Real world software beats paper

● There are contradictory requirements

– Duh!

Python needs research, too

● Pickle has mulitple protocols:

– 0 and 2 are important

● __len__() is eeevviiilll

– With good reason

– Swallows exceptions

● TypeError

● Others (Python 2.3, Python 2.6.0 – 2.6.1)

● Unicode matters. So does UTF-8

Always contribute!

Best beats perfect