advanced practical programming - zib · (lisp, ml, haskell, erlang, ocaml) • object-oriented...
TRANSCRIPT
Plannedtopics
Overview: Imperative,OOP,Functional,sideeffects,threadsafe,DesignbycontractDesign: Informationhiding,Dependencies,Codingstyle,Inputchecking,ErrorHandlingDesign: Overallprogramdesign,Datastructures,MemoryallocationTools: git,gdb,undodb,doxygenLanguagesandcorrectness:Designerrors/problemsC/C++,C89/C99/C11,Compilerswitches,assert,flexelint,FP,HowtowritecorrectprogramsTesting:black-box,white-box,unittests,regressiontests,errortests,speedtestsTools:gcov,jenkins,cmake,doxygen,make,gprof,valgrind,cppcheck,clangSoftwaremetrics:Why,Examples,Isituseful?ControlflowcomplexityParallelprogramming:OpenMP,MPI,pthreads,OpenCLProgramoptimization:Codeoptimization,linkinglibrariesHowtodesignlargeprogramsCode-shootoutandcomparison:Documentation,Releasemanagement
Allinformationissubjecttochange.
AdvancedProgramming 4
Vorlesung12.05:DigitalFuture
Please register yourself viahttps://science-match.tagesspiegel.de/the-digital-future-may-2017Voucher/VIPCode:*future17-1*
AdvancedProgramming 5
Administrativeaffairs
AllemailsrelatedtothislectureshouldstartwithAPPFSinthesubject.
Everybodyparticipatinginthislecture,pleasesendanemailto<[email protected]>withyourNameandMatrikel-Nr.
Wewillsetupamailinglistforannouncementsanddiscussion.Everythingisherehttp://www.zib.de/koch/lectures/ss2017_appfs.php
Ifyouneedthecertificate,regularattendance,completionofhomeworkassignments,andinparticularparticipationinoursmallprogrammingprojectisexpected.GradeswillbebasedontheoutcomeandafewquestionsaboutitJ .
Nogroups.
AdvancedProgramming 6
Exercises(plan)
ExercisespartI:ReadingandwritingData1. 21.04.Readingcsv2. 28.04.Readingxml3. 05.05.Readingbinary4. 05.05.Checkinginputdata
ExercisepartII:Timeseriesprediction5. 26.05.Makingitwork1(make)6. 02.06.Makingitwork2(Documentation,fixinginputdata)7. 09.06.Testing8. 16.06.Measuringperformance,serializingdatastructures9. 23.06.Analyzingcodequality10. 30.06.Linkingthirdpartylibraries(staticvs.shared)11. 07.07.Makingitbetter
AdvancedProgramming 7
Howtosubmitexercises
• Youcansubmittheexercisesinanyprogramminglanguage.
• IthastorunonmyRasberry-pirunningLinux
• Examplesolutionswillbeprovidedatleastinoneofthefollowinglanguages:C99,C++14,python3,Ada2012.
• Ifyouuseanythingelse,youareonyourown.
• Iwilljudgeyourcodenevertheless.
AdvancedProgramming 8
Registeratgithub.comGotohttps://github.com/mattmilten/appfs
ForkChangePullrequest
Createanew folder with your name and then make apullrequest
AdvancedProgramming 9
AdvancedProgramming 12
Attitudes
Algorithmengineeringreferstotheprocessrequiredtotransformapencil-and-paperalgorithmintoarobust,efficient,welltested,andeasilyusableimplementation.
— Bader,Moret,Sanders
RealProgrammersdon'tcommenttheircode.Ifitwashardtowrite,itshouldbehardtounderstandandhardertomodify.
— Fortune(6)
Bewareofbugsintheaboveprogram.Ihaveonlyproveditcorrect,nottriedit.
—D.E.Knuth
Thesinglemostimportantruleoftestingistodo it.— Kernighan,Pike
Laws,Rules,andObjectives
• Laws describethefeasibleregionofasolution• Objectives (aredirections)describingwhichsolutiontoprefer• Rules trytoexclude(ruleout)badsolutions
“Look,that'swhythere'srules,understand?Sothatyouthinkbeforeyoubreak'em.”
―TerryPratchett,ThiefofTime
IngeneralIrecommendtofollowrules,untilyouaresureyouunderstandwhattheruleswantedtoachieveandyoubelieveyourobjectivesareservedbetterbynotobeyingtherule.
Butthinkfirst!
Iftheunionofallrulesdefinesanemptyspace,resistthetemptationtochangetheobjectivetolookforasolutionthatviolatestheleastnumberofrules.Slacksolutionsusuallyhaveahorribleobjectivevalue.
AdvancedProgramming 13
Examples
Thespeedoflightc invacuumisauniversalphysicalconstant.Itsexactvalueis299792458 m/s.Accordingtospecialrelativity,c isthemaximumspeedatwhichallconventionalmatterandhenceallknownformsofinformationintheuniversecantravel.(Wikipedia)
“Everythingshouldbemadeassimpleaspossible,butnosimpler.”― AlbertEinstein
Don’tusegoto!Seehttps://softwareengineering.stackexchange.com/questions/125715/do-we-still-have-a-case-against-the-goto-statement
Adesignerknowshehasachievedperfectionnotwhenthereisnothinglefttoadd,butwhenthereisnothinglefttotakeaway.
―AntoinedeSaint-Exupery
AdvancedProgramming 14
ImperativeProgramming
Incomputerscienceterminology,imperativeprogrammingisaprogrammingparadigmthatdescribescomputationintermsofstatementsthatchangeaprogramstate.Inmuchthesamewaythatimperativemoodinnaturallanguagesexpressescommandstotakeaction,imperativeprogramsdefinesequencesofcommandsforthecomputertoperform.
Imperativeprogramming,http://en.wikipedia.org/w/index.php?title=Imperative_programming&oldid=624302389 (lastvisitedSept.21,2014).
ImperativeProgrammierungisteinProgrammierparadigma.DanachwerdenProgrammesoentwickelt,dass„einProgrammauseinerFolgevonAnweisungenbesteht,dievorgeben,inwelcherReihenfolgewasvomComputergetanwerdensoll“.
DieimperativeProgrammierungistdasamlängstenbekannteProgrammierparadigma.DieseVorgehensweisewar,bedingtdurchdenSprachumfangfrühererProgrammiersprachen,ehemalsdieklassischeArtdesProgrammierens.SieliegtdemEntwurfvonvielenProgrammiersprachen,zumBeispielALGOL,Fortran,Pascal,Ada,PL/I,Cobol,CundallenAssemblersprachenzugrunde.
Seite„ImperativeProgrammierung“.In:Wikipedia,DiefreieEnzyklopädie.Bearbeitungsstand:20.September2014,14:27UTC.URL:http://de.wikipedia.org/w/index.php?title=Imperative_Programmierung&oldid=134202110 (Abgerufen:21.September2014,20:41UTC)
AdvancedProgramming 15
ImperativeProgramming
Iamincommand!
input->putput->outputDataseparatedfrominstructions(moreorlessJ asinstructionsaredata)Von-NeumannArchitecture/Stored-Program-ComputerAccessMemory,docomputationsincl.conditional,PCprogramcounter->allows:goto (jump),if(conditional),while(loop)
1. Doit!2. Anyway!
Structuredprogrammingvs.goto
Blocks,subroutines,scopes.
AdvancedProgramming 16
Object-Oriented Programming
Buildingyourownworldofobjects
Object-orientedprogrammingattemptstoprovideamodelforprogrammingbasedonobjects.OOprogrammingintegratescodeanddatausingtheconceptofan“object”.Anobjectisanabstractdatatypewiththeadditionofpolymorphism andinheritance.
Anobjecthasbothstate(data)andbehavior(code).
->Informationhiding->polymorphismcomesnaturally->singlevs.multipleinheritance.->templatesandgenerics
AdvancedProgramming 17
FunctionalProgramming
Thewaymathematiciansthink.Functionalprogrammingisastyleofbuildingthestructureandelementsofcomputerprograms,thattreatscomputationastheevaluationofmathematicalfunctionsandavoidschangingstateandmutabledata.Infunctionalcode,theoutputvalueofafunctiondependsonlyontheargumentsthatareinputtothefunction,socallingafunctionftwicewiththesamevalueforanargumentxwillproducethesameresultf(x)bothtimes.Eliminatingsideeffects,i.e.changesinstatethatdonotdependonthefunctioninputs,canmakeitmucheasiertounderstandandpredictthebehaviorofaprogram,whichisoneofthekeymotivationsforthedevelopmentoffunctionalprogramming.->sideeffects/mutablestate->rand(),getchar(),putchar()->callbyvalue,callbyreference->threadsafeness->errno
AdvancedProgramming 18
Programming Paradigms
• Imperativeprogramming– definescomputationasstatementsthatchangeaprogramstate(Assembler)
• Proceduralprogramming,structuredprogramming– specifiesthestepstheprogrammusttaketoreachthedesiredstate(C,Pascal,Fortran77)
• Functionalprogramming– treatscomputationastheevaluationofmathematicalfunctionsandavoidsstateandmutabledata(Lisp,ML,Haskell,Erlang,Ocaml)
• Object-orientedprogramming(OOP)– organizesprogramsasobjects:datastructuresconsistingofdatafields andmethodstogetherwiththeirinteractions(Smalltalk,C++,Java,Eiffel)
• Declarativeprogramming– definescomputationlogicwithoutdefiningitscontrolflow(Prolog)
• Event-drivenprogramming– theflowoftheprogramisdeterminedbyevents,suchassensoroutputsoruseractions(mouseclicks,keypresses)ormessagesfromotherprogramsorthreads(JavaScript)
AdvancedProgramming 19
Haveyouever...
• wastedalotoftimecodingthewrongalgorithm?• usedadatastructurethatwasmuchtoocomplicated?• testedaprogrambutmissedanobviousproblem?• spentadaylookingforabugyoushouldhavefoundinfiveminutes?• neededtomakeaprogramrunthreetimesfasteranduselessmemory?• struggledtomoveaprogramfromonearchitecturetoanother?• triedtomakeamodestchangeinsomeoneelse’sprogram?• rewrittenaprogrambecauseyoucouldn’tunderstandit?
Wasitfun?
From:Kernighan,Pike „Thepractise of programming“
AdvancedProgramming 21
Designgoals
Theseinclude
• simplicity,whichkeepsprogramsshortandmanageable;• clarity,whichmakessuretheyareeasytounderstand,forpeople
aswellasmachines;• generality,whichmeanstheyworkwellinabroadrangeof
situationsandadaptwellasnewsituationsarise;and• automation,whichletsthemachinedotheworkforus,freeingus
frommundanetasks.
From:Kernighan,Pike „Thepractise of programming“AdvancedProgramming 22
WinnerOSC1990Bestsmall programm
v,i,j,k,l,s,a[99];
main()
{
for(scanf("%d",&s);*a-s;v=a[j*=v]-a[i],k=i<s,j+=(v=j<s&&(!k&&!!printf(2+"\n\n%c"-(!l<<!j)," #Q"[l^v?(l^j)&1:2])&&++l||a[i]<s&&v&&v-i+j&&v+i-j))&&!(l%=s),v||(i==j?a[i+=k]=0:++a[i])>=s*k&&++a[--i]);
}
What might it possibly do?
AdvancedProgramming 23
Howtoachievethis
• Beabletofollowcontrolflow(-imperative,+structured,-OO,+functional)• Structuringprogramsintounits(-imperative,+structured,+OO)• Minimizedependencies(betweencomponents)
• Minimizescope• Minimizesideeffects(+functional)• Datahiding(+OO)• Howaboutthingshappeningautomatic?
(memberfunctionsinC++,Garbagecollection)• Beingclever?:while(*s++=*t++);• DbC
AdvancedProgramming 24
Somethingto think about
int data[10000]; // all 0..255
long long fun = 0;
unsigned i;
[…]
int t = (data[i] - 128) >> 31;
fun += ~t & data[i];
AdvancedProgramming 25
Thisis notthe way/* The Computer Language Benchmarks Game http://benchmarksgame.alioth.debian.org/
Contributed by Dmitry Vyukov
*/
#define _GNU_SOURCE
#include <stdlib.h>
[…]
#define CL_SIZE 64
void* cache_aligned_malloc(size_t sz)
{
char* mem;
char* res;
void** pos;
mem = (char*)malloc(sz + 2 * CL_SIZE);
if (mem == 0)
exit(1);
res = (char*)((uintptr_t)(mem + CL_SIZE) & ~(CL_SIZE - 1));
pos = (void**)(res - sizeof(void*));
pos[0] = mem;
return res;
}
AdvancedProgramming 26
DesignbyContract
(DbC),isanapproachfordesigningsoftware.Itprescribesthatsoftwaredesignersshoulddefineformal,preciseandverifiableinterfacespecificationsforsoftwarecomponents,whichextendtheordinarydefinitionofabstractdatatypeswithpreconditions,post-conditionsandinvariants.Thesespecificationsarereferredtoas“contracts”,inaccordancewithaconceptualmetaphorwiththeconditionsandobligationsofbusinesscontracts.
Pre-conditionsPost-conditionsInvariants
AdvancedProgramming 28
Exercise 1:Setup
Pleasecheckoutthedataforthisexerciselocatedhere:https://github.com/mattmilten/appfs
Youwillfindaprogramnamedex1_gen usedtogeneratetheinputdata.Runthisprogramasfollows:
./ex1_gen 100000000 >ex1.dat
Thefileex1.dat shouldthencontainroughly100millionlines.Youcancheckby
wc -l ex1.dat
Sizeshouldbearound2GB.Thefirstnumberis123456789.
AdvancedProgramming 29
Exercise1:FileFormat
Eachlineshouldconsistsof• asequence-number,• alocation(1or2),and• afloatingpointvalue>0.
Emptylinesareallowed.Commentlinesstartwitha”#”.Everythingaftera”’#”onalineshouldbeignored.
Readinthedataandcomputethegeometricmeanforeachlocation.Beawarethattheremightbesomeerrorsinthedata.
AdvancedProgramming 30
Exercise 1:Whattodo
Writeaprogramnamedex1 inCoryourfavoritelanguage,which
1. Readsinthedatafromex1.dat
2. ComputetheGeometricMean forbothlocationsOutputshouldlooklike:
File:ex1.datwith100001235linesValidvaluesLoc1:50004598withGeoMean:36.7817ValidvaluesLoc2:49994703withGeoMean:36.7825
AdvancedProgramming 31
Exercise 3:Howtoreport
1. Checkinthesourcecodeintogithub asexplained
2. usetime ex1 ex1.dattogettheruntimesofyourprogram
3. Copytheoutputofyourprogram
Sendtheoutputoftime,andyourex1 withsubjectofAPPFSex1vorname nachnameperemailto<[email protected]>
Deadline:27.04.16Uhr(earlier would be better)
AdvancedProgramming 32