Upload
andi-albrecht
View
2.205
Download
10
Embed Size (px)
DESCRIPTION
Talk given at PyCon DE 2011. See http://de.pycon.org/2011/schedule/sessions/17/ for details (german).
Verbesserung der Code-”Qualitat” durchstatische Code-Analyse
Andi AlbrechtProUnix
PyCon DE 2011
05. Oktober 2011
QS in der Entwicklung Statische Code-Analyse Die Tools fur Python-Code Verbesserung der Code-Qualitat
Statische Code-Analyse / Andi Albrecht ProUnix / PyCon DE 2011
Andi Albrecht – @andialbrecht
Erster Python-Kontakt vor knapp 10 Jahren als StudentischeHilfskraft bei der DFG
Aktuell: Anwendungsentwickler fur Webapplikationen beiProUnix in Bonn
Entwicklung und Pflege mittlerer und großer Systeme
OpenSource: Rietveld Code Review Tool, python-sqlparse,CrunchyFrog, hgsvn, ...
QS in der Entwicklung Statische Code-Analyse Die Tools fur Python-Code Verbesserung der Code-Qualitat
Statische Code-Analyse / Andi Albrecht ProUnix / PyCon DE 2011
Ubersicht
QS in der Entwicklung
Statische Code-AnalyseBegriffProbleme, Fehler, Warnungen, Infos
Die Tools fur Python-CodeUbersichtAusfuhrung und InterpretationIntegration
Verbesserung der Code-Qualitat
QS in der Entwicklung Statische Code-Analyse Die Tools fur Python-Code Verbesserung der Code-Qualitat
Statische Code-Analyse / Andi Albrecht ProUnix / PyCon DE 2011
QS in der Entwicklung
Qualitatssicherung bei ProUnix
Prozessbegleitende QS vom Konzept bis zum fertigemProdukt
QS in der Entwicklung ab der ersten Zeile Code(oder besser noch: als erste Zeile)
Code Reviews, UnitTests, Continous Integration (BuildBot),Statische Code-Analyse
QS in der Entwicklung Statische Code-Analyse Die Tools fur Python-Code Verbesserung der Code-Qualitat
Statische Code-Analyse / Andi Albrecht ProUnix / PyCon DE 2011
Was ist Statische Code-Analyse?
Statisches Testverfahren (ohne Programmausfuhrung)
Inspektion des Source-Codes
Leicht automatisierbar, sollte auch automatisiert ausgefuhrtwerden
Regelmaßige Analysen, um Anderungen zwischen zweiDurchgangen zu erkennen
Umfangreiche Ausgaben (“Informationsflut”)
QS in der Entwicklung Statische Code-Analyse Die Tools fur Python-Code Verbesserung der Code-Qualitat
Statische Code-Analyse / Andi Albrecht ProUnix / PyCon DE 2011
Was kann gefunden werden?
Syntaktische Fehler
Stilfehler, Einhaltung von Coding-Standards
Software Metriken: LOC, Komplexitat, ...
“Bad Smells”
! Keine Aussagen uber Korrektheit oder Zuverlassigkeit
QS in der Entwicklung Statische Code-Analyse Die Tools fur Python-Code Verbesserung der Code-Qualitat
Statische Code-Analyse / Andi Albrecht ProUnix / PyCon DE 2011
Tools fur Python-Code (Auswahl)
PyLint Syntax-Checks, Bad Smells, Style-Checks, Metriken
PyChecker Syntax-Checks, Bad Smells, Modul-Importerforderlich
pyflakes Syntax-Checks, Bad Smells
pep8 Style-Checks
”McCabe” Metriken
flake8 Syntax-Checks, Bad Smells, Style-Checks, Metriken
QS in der Entwicklung Statische Code-Analyse Die Tools fur Python-Code Verbesserung der Code-Qualitat
Statische Code-Analyse / Andi Albrecht ProUnix / PyCon DE 2011
Beispiel: PyLint
1 import sys
2 def mergesort(l):
3 # sort l using mergesort algorithm
4 if len(l) <= 1: return l
5 left_part = mergesort(l[:len(l)/2])
6 right_part = mergesort(l[len(l)/2:])
7 return merge(left_part, right-part)
89 def merge(left, right):
10 """Wrong implementation of merge step."""
11 return left + right
C: 1,0: Missing docstring
C: 2,0:mergesort: Missing docstring
C: 2,14:mergesort: Invalid name "l" (should match [a-z_][a-z0-9_]{2,30}$)
C: 4,20:mergesort: More than one statement on a single line
E: 7,28:mergesort: Undefined variable ’right’
E: 7,34:mergesort: Undefined variable ’part’
W: 6,4:mergesort: Unused variable ’right_part’
W: 1,0: Unused import sys
QS in der Entwicklung Statische Code-Analyse Die Tools fur Python-Code Verbesserung der Code-Qualitat
Statische Code-Analyse / Andi Albrecht ProUnix / PyCon DE 2011
Beispiel: PyFlakes
1 import sys
2 def mergesort(l):
3 # sort l using mergesort algorithm
4 if len(l) <= 1: return l
5 left_part = mergesort(l[:len(l)/2])
6 right_part = mergesort(l[len(l)/2:])
7 return merge(left_part, right-part)
89 def merge(left, right):
10 """Wrong implementation of merge step."""
11 return left + right
demo1.py:1: ’sys’ imported but unused
demo1.py:7: local variable ’right_part’ is assigned to but never used
demo1.py:8: undefined name ’right’
demo1.py:8: undefined name ’part’
QS in der Entwicklung Statische Code-Analyse Die Tools fur Python-Code Verbesserung der Code-Qualitat
Statische Code-Analyse / Andi Albrecht ProUnix / PyCon DE 2011
Beispiel: pep8.py
1 import sys
2 def mergesort(l):
3 # sort l using mergesort algorithm
4 if len(l) <= 1: return l
5 left_part = mergesort(l[:len(l)/2])
6 right_part = mergesort(l[len(l)/2:])
7 return merge(left_part, right-part)
89 def merge(left, right):
10 """Wrong implementation of merge step."""
11 return left + right
demo1.py:2:1: E302 expected 2 blank lines, found 0
demo1.py:4:19: E701 multiple statements on one line (colon)
demo1.py:5:36: E225 missing whitespace around operator
demo1.py:8:1: W293 blank line contains whitespace
QS in der Entwicklung Statische Code-Analyse Die Tools fur Python-Code Verbesserung der Code-Qualitat
Statische Code-Analyse / Andi Albrecht ProUnix / PyCon DE 2011
Beispiel: McCabe
1 import sys
2 def mergesort(l):
3 # sort l using mergesort algorithm
4 if len(l) <= 1: return l
5 left_part = mergesort(l[:len(l)/2])
6 right_part = mergesort(l[len(l)/2:])
7 return merge(left_part, right-part)
89 def merge(left, right):
10 """Wrong implementation of merge step."""
11 return left + right
2:1: ’mergesort’ 2
*Richtwert fur zu hohe Komplexitat: 7
QS in der Entwicklung Statische Code-Analyse Die Tools fur Python-Code Verbesserung der Code-Qualitat
Statische Code-Analyse / Andi Albrecht ProUnix / PyCon DE 2011
Den richtigen Zeitpunkt finden
QS in der Entwicklung Statische Code-Analyse Die Tools fur Python-Code Verbesserung der Code-Qualitat
Statische Code-Analyse / Andi Albrecht ProUnix / PyCon DE 2011
Verbesserung der Code-Qualitat
Unmittelbare Beseitigung von Fehlern
Vermeiden von Bad Smells, unnotig hoher Komplexitat, ...
Sauberer Code fuhrt zu besserer Wartbarkeit
Aufzeigen moglicher Fehlerquellen
Schulung des Gespurs fur problematische Stellen im Code
Einhaltung von Standards/Konventionen ist Team-Aufgabe
QS in der Entwicklung Statische Code-Analyse Die Tools fur Python-Code Verbesserung der Code-Qualitat
Statische Code-Analyse / Andi Albrecht ProUnix / PyCon DE 2011
Danke!
E-Mail [email protected]
Twitter @andialbrecht
Homepage http://andialbrecht.de
http://www.prounix.de/unternehmen/jobs/python/
QS in der Entwicklung Statische Code-Analyse Die Tools fur Python-Code Verbesserung der Code-Qualitat
Statische Code-Analyse / Andi Albrecht ProUnix / PyCon DE 2011