47
BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIEL Jeremy Laforet Équipe NSE - D2.16 - 4372 [email protected]

BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

BONNES PRATIQUES DEDÉVELOPPEMENT LOGICIEL

Jeremy Laforet    Équipe NSE - D2.16 - 4372

[email protected]

Page 2: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

PLANBonnes PratiquesGestion de versionsTestsProfilingOptimisation de code

Page 3: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

BONNES PRATIQUESRepris de software-carpentry.org

Page 4: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

RULE 1WRITE PROGRAMS FOR PEOPLE,

NOT COMPUTERSHard to tell if code that's difficult to understand is doingwhat it's supposed toHard for other scientists to re-use it......including your future self

Page 5: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

RULE 1AA program should not require its readers to hold more than

a handful of facts in memory at once.

Short-term memory can hold 7±2 itemsSo break programs into short, readable functions, eachtaking only a few parameters

Page 6: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

RULE 1BMake names consistent, distinctive, and meaningful.

p doesn't help the reader's short term memory as muchas pressureDon't use temp for both "temporary" and "temperature"i, j are OK for indices in small scopes

Page 7: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

RULE 1CMake code style and formatting consistent.

Which rules don't matter — having rules doesBrain assumes all differences are significantEvery inconsistency slows comprehension

Page 8: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

RULE 2LET THE COMPUTER DO THE WORK

Computers exist to repeat things quickly99% accuracy ⇒ 63% of at least one error per hundredrepetitions

Page 9: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

RULE 2AMake the computer repeat tasks.

Write little programs for everythingEven if they're called scripts, macros, or aliasesEasier to do this with text-based programming systemsthan with GUIs

Page 10: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

RULE 2BSave recent commands in a file for re-use.

Most text-based interfaces do this automaticallyRepeat recent operations using history"Reproducibility in the small"

Saving history supports "reproducibility in the large"An accurate record of how a result was producedIf everything can be captured

Page 11: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

RULE 2CUse a build tool to automate workflows.

Originally developed for compiling programsCan be used whenever some files depend on othersMakes workflow explicit

Page 12: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

RULE 3MAKE INCREMENTAL CHANGES

Most scientists don't have "requirements"They are their own usersCode evolves in tandem with research

Closest fit from industry is agile development

Page 13: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

RULE 3AWork in small steps with frequent feedback

and course correction.

People can concentrate for 45-90 minutes without a breakSo size each burst of work to fit thatLonger cycle should be a week or two

Page 14: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

RULE 3BUse a version control system.

Tracks changesAllows them to be undoneSupports independent parallel developmentEssential for collaboration collaboration

Page 15: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

RULE 3CPut everything that has been created manually

in version control.

Not just software: papers, raw images, ...Not gigabytes......but metadata about those gigabytes

Leave out things generated by the computerUse build tools to reproduce those insteadUnless they take a very long time to create

Page 16: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

RULE 4DON'T REPEAT YOURSELF (OR OTHERS)

Anything repeated in two or more places will eventuallybe wrong in at least oneIf it's faster to re-create than to discover or understand, fixit

Page 17: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

RULE 4AEvery piece of data must have

a single authoritative representation in the system.

Define constants exactly onceDitto file formats, geographical locations, ...

Page 18: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

RULE 4BModularize code rather than copying and pasting.

Reducing code cloning reduces error ratesCuts the amount of testing neededAnd increases comprehension

Page 19: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

RULE 4CRe-use code instead of rewriting it.

It takes experts years to build high-quality numerical orstatistical softwareYour time is better spent doing science on top of that

Page 20: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

RULE 5PLAN FOR MISTAKES

No single practice catches everythingSo practice defense in depth

Note: improving quality increases productivity

Page 21: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

RULE 5AAdd assertions to programs to check their operation.

"This must be true here or there is an error"Like diagnostic circuits in hardwareNo point proceeding if the program is broken......and they serve as executable documentation

Page 22: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

RULE 5BUse an off-the-shelf unit testing library.

Manages setup, execution, and reportingRe-run unit tests after every change to the code to checkfor regression

Page 23: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

TESTING IS HARD"If I knew what the right answer was, I'd have publishedby now."Compare to experimental dataOr to analytic solutions of simple problemsOr to old (trusted) programsIf nothing else, forces scientists to document what"errors" are acceptable

Page 24: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

RULE 5CTurn bugs into test cases.

Write a test that fails when the bug is presentThen work on the code until that test passes......and no others are failing

Page 25: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

TEST-DRIVEN DEVELOPMENTWhy wait? Always write the tests, then the codeImproves focusEncourages writing testable codeAnd ensures tests actually get written..."Red, green, refactor"

Page 26: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

RULE 5DUse a symbolic debugger.

Explore the program as it runsBetter than print statements

You don't have to re-run......or guess in advance what you'll need to know

Use breakpoints to stop program at particular points orwhen particular things are true

Page 27: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

RULE 6OPTIMIZE SOFTWARE

ONLY AFTER IT WORKS CORRECTLYEven experts find it hard to predict performancebottlenecksSmall changes to code often have dramatic impact onperformanceSo get it right, then make it fast

Page 28: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

RULE 6AUse a profiler to identify bottlenecks.

Reports how much time is spent on each line of codeRe-check on new computers or when switching librariesSummarize across unit tests

Page 29: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

RULE 6BWrite code in the highest-level language possible.

People write the same number of lines of code per hourregardless of languageSo use the most expressive language available to get the"right" version......then rewrite core pieces (possibly in a lower-levellanguage) to get the "fast" version

Page 30: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

RULE 7DOCUMENT DESIGN AND PURPOSE,

NOT MECHANICSGoal is to make the next person's life easierFocus on things the code doesn't say

Or doesn't say clearlyE.g., file formats

An example is worth a thousand words...

Page 31: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

RULE 7ADocument interfaces and reasons,

not implementations.

Interfaces and reasons change more slowly thanimplementation details, so documenting them is bettereconomicsAnd most people care about using code more thanunderstanding it

Page 32: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

RULE 7BRefactor code in preference to

explaining how it works.

Good code can be understood when read aloudGood programmers build libraries so that solving theirproblem is straightforwardAgain, "red, green, refactor"

Page 33: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

RULE 7CEmbed the documentation for a piece of software

in that software.

Specially-formatted comments or stringsMore likely to be kept up to dateMore accessible to interactive helpMany modern tools embed code in documentation ratherthan vice versa

Page 34: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

RULE 8COLLABORATE

Computers were invented to calculateThe web was invented to collaborateScience is more fun when it's shared

Page 35: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

RULE 8AUse pre-merge code reviews.

Have someone else review changes before merging inversion controlSignificantly reduces errorsGood way to share knowledgeIt's what makes open source possible

Page 36: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

RULE 8BUse pair programming

when bringing someone new up to speed and when tackling particularly tricky problems.

Two people, one keyboard, one screenAn extreme form of code reviewCan get a bit tired if done all the time...

Page 37: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

RULE 8CUse an issue tracking tool.

A shared to-do listItems can be assigned to peopleSupports comments, links to code and papers, etc.

"Version control is where we've been, the issue tracker iswhere we're going"

Page 38: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

GESTION DE VERSIONSArchivage des modifications incrémentalesIdentification des auteursPossibilité d'avoir des branches parallèles

Il existe de nombreux systèmes, un des plus courantaujourd'hui: GIT

Page 39: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

GITDécentraliséDépot local (+ distant)Hébergement personnel ou forges (github, bitbucket…)

Page 40: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

TESTStest-driven development

Écrire les tests avant le codePuis écrire le code qui passe les testsFaire passer les tests à chaque itération du code

Les tests sont une formalisation du cahier des charges etpermettent d'éviter les régressions ("ça marchait avant.")

Page 41: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

EN PYTHONpytestunittestnose

DEMO: PYTEST

Page 42: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

PROFILINGAnalyser les performances du code en terme de mémoire ou

de temps de calcul.

Permet d'avoir une vision objective des goulets deperformancesSavoir où et quoi optimiser pour améliorer effectivementles performances

Page 43: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

TYPE DE PROFILERcProfile: analyse du temps de calcul, à l'échelle de lafonctionkernprof: analyse du temps de calcul, ligne par lignememory_profile: analyse de la consomation mémoire,ligne par ligneguppy: analyse des types de donées utilisés

Page 44: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

OPTIMISATION DE CODEOptimiser là où c'est nécessaireVérifier l'absence de régressions

Page 45: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

PISTES

Appliquer la technique la plus adapté au problème local

Limite d'un construction particulière du langage: ré-écritureNombreux traitement indépendants: parallélisation…

Page 46: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

EXEMPLE

En python les boucles sont assez lentes, donc autant quepossible les remplacer pour des fonctions dédiées

for x in array: sum+=x

numpy.sum(array)

Page 47: BONNES PRATIQUES DE DÉVELOPPEMENT LOGICIELjlaforet/Suppl/Bonnes_Pratiques.pdf · Brain assumes all differences are significant Every inconsistency slows comprehension. RULE 2 LET

CONCLUSION