code quality. patch quality

35
Code quality Patch quality Malcolm Tredinnick [email protected]

Upload: malcolmt

Post on 12-Jul-2015

486 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Code quality. Patch quality

Code qualityPatch quality

Malcolm [email protected]

Page 2: Code quality. Patch quality

Code Quality Matters

Page 3: Code quality. Patch quality

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

Page 4: Code quality. Patch quality

Do The Basics Properly

Page 5: Code quality. Patch quality

Yr Doin' It Wrng!

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

Page 6: Code quality. Patch quality

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

Page 7: Code quality. Patch quality

Yr Doin' It Wrng!

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

energy drink

Page 8: Code quality. Patch quality

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.

Page 9: Code quality. Patch quality

Code style is somewhat opinionated

Page 10: Code quality. Patch quality

Your opinions don't matter(that much)

Page 11: Code quality. Patch quality

Read the contributing document

Page 12: Code quality. Patch quality

Code is...

●Written once

●Read many, many times

Page 13: Code quality. Patch quality

Comments should last

Page 14: Code quality. Patch quality

Comments should be correct!

Page 15: Code quality. Patch quality

Fix problems not symptoms!

Page 16: Code quality. Patch quality

Symptom fixing...

Page 17: Code quality. Patch quality

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

Page 18: Code quality. Patch quality

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

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

Page 19: Code quality. Patch quality
Page 20: Code quality. Patch quality

Where else does it happen?

Page 21: Code quality. Patch quality

Warning: may be hard!

Page 22: Code quality. Patch quality

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

Page 23: Code quality. Patch quality

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

...

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

r8107

Page 24: Code quality. Patch quality

r8783

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

...

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

Page 25: Code quality. Patch quality

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+

Page 26: Code quality. Patch quality

Know your limitations

Page 27: Code quality. Patch quality

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

Page 28: Code quality. Patch quality

“Here is a trivial patch”

Page 29: Code quality. Patch quality

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

Page 30: Code quality. Patch quality

Research is not a four letter word

Page 31: Code quality. Patch quality

● 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.

Page 32: Code quality. Patch quality

● 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!)

Page 33: Code quality. Patch quality

● There are RFCs.

– They apply in this universe.

● There are competing specs

– Real world software beats paper

● There are contradictory requirements

– Duh!

Page 34: Code quality. Patch quality

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

Page 35: Code quality. Patch quality

Always contribute!

Best beats perfect