Code Beauty

download Code Beauty

of 56

  • date post

    10-May-2015
  • Category

    Technology

  • view

    1.316
  • download

    0

Embed Size (px)

Transcript of Code Beauty

  • 1.Code BeautyProf. Fabio KonDepartment of Computer Science Institute of Mathematics and Statistics University of So Paulo SugarLoafPLoP'2010 - 24/9/2010Salvador, Brazil1

2. Agenda Definition of Beauty Is Code Beauty important for Software Development? What is Code Beauty 2 3. Beauty - Ancient View pre-socratic (e.g., Pythagoras): strong connection among Beauty and Mathematics; objects with proportions following the golden ratio are more attractive to the human brain two quantities are in the golden ratio if the ratio of the sum of the quantities to the larger quantity is equal to the ratio of the larger quantity to the smaller one. The golden ratio is an irrational mathematical constant, approximately 1.6180339887 greek architecture is based on symmetry and on the golden ratio 3 4. Beauty - Romantic View works do not need to be harmoniously proportioned and constitute a well-rounded whole to be beautiful; on the contrary, dissonance and fragmentation is beautiful more precisely: dissonance and fragmentation allude to a harmony and wholeness that is not in the work4 5. Beauty - Modernist View architect Mies van der Rohe: Less is More~ 1940 designer Buckminster Fuller (geodesic dome) adopted the engineer's goal of Doing more with less writer Antoine de Saint Exupry (1939) perfection is attained not when there is nothing more to add, but when there is nothing more to remove5 6. Beauty: Post-modernist view architect Robert Venturi: Less is a Bore, let's do whatever people like 6 7. the greek and modernist views of beauty provide us with a good basis for approaching Beauty in Science and Technology7 8. Science make everything as simple as possible, but not simpler8 9. Technology Conceptual integrity the architect or team of architects should develop an idea of what the system should do and make sure this vision is understood by the rest of the team to ensure a user-friendly system, a system may deliberately provide fewer features than it is capable of9 10. Beauty dictionary definition the quality that gives pleasure to the mind or senses and is associated with such properties as harmony of form or color, excellence of artistry, truthfulness, and originality. The American Heritage Dictionary of the English Language, 4th Edition, 2000.10 11. Does it matter? Why are we talking about Code Beauty on a serious Software Conference??? personally, I have two reasons... 11 12. there has been a strong movement towards managing software development projects as any other production process/system many cases of managers that have never written a single line of code is it possible to have a good Chef de Cuisine that has never cooked in his life? 12 13. scientists/engineers/practitioners sometimes tend to overestimate the power of science and mathematics can science and engineering provide all what's needed to explain life? and software development? 13 14. What is Software Development? Modeling (Jacobsen) Engineering(Meyer) Discipline (Humphreys) Poetry (Cockburn) Craft(Knuth) Art(Gabriel) (from from Alistair Cockburn) Common mistake: look at software as only one of the above items. 14 15. our goal here, is to bring attention to a few important aspects of software development that are neglected in some software engineering communities we'll look at the most important deliverable on a software project: The Code15 16. Beauty is fundamental vinicius de moraisBeautiful code brings pleasure to the reader makes the writer happy makes working in groups fun This leads to fewer bugs maintainability team productivity in other words, Quality 16 17. In Software Development,BeautyLeads to Quality17 18. What is Beauty in Software Development?sources: Robert C. Martin. Clean Code - A Handbook of Agile Software Craftsmanship. Prentice Hall. 2008. Andy Oram and Greg Wilson. Beautiful Code. O'Reilly. 2007. beaute(code) art exhibit by Bob Hanmer, Karen Hanmer, and Andrea Polli. exchange with experts 18 19. Rebecca Wirfs-Brock's code beauty it is a common recognition that these idioms and those structures and those ways of doing things are of valueit is important that a project or a team have the same sense of aesthetics or there will be clashes of willkeeping of every step in a method at the same level of abstraction/intention; it reads much more like prose that waycharacteristics that are "normally" present in beautiful code are balance, effectiveness, expressiveness, and profoundly doing well what it was designed to do. 19 20. Rebecca's example of abeautiful code the Smalltalk class collectionprovide all the built in behavior by requiring subclasses to implement three methods:add: anObject remove: anObject ifAbsent: exceptionBlockdo: aBlock methods in the abstract class collection to add, remove, check for emptiness, inclusion, occurrences, and to step through a collection in different ways accumulating values are all implemented use these abstract building blocks or invoking other defined behaviors that depend on these implementation; elegant, beautiful reuse! 20 21. Joe Yoder's code beauty I think it is important to most of us who live there; it is like cleaning up our living area, having clean code or beautiful code can make life much betterhowever let's face it, BBoM still thrive and exist and are successfulit matters, but is not necessary and sufficient for successful software projects. If so, then Clean Code would become the norm those of us who care strive for it because we believe it is the right thing to do and it will help us in the long run...but certain forces are in play that can make clean code or beautiful code a goal that is very hard to achieve21 22. Joe's example of a beautiful code also the Smalltalk class collection you see a certain beauty and elegance in thesystem as it was easy to understand and extend (it's also my favorite piece of code :-)design patterns can help with this as well; if a system is clean and well designed by implementing some of the best known principles and practices one can see certain beauty in the system22 23. W h a tis C l a n C ode ?e I l m y co de t be e l e g a n t a nd ike o e ffi c i e n t. Th e l gic sh o ul be o d s t ra i g h forw a rd t m a ke it h a rdo fo rbugstoh ide , teh dependenc i esmi ni malto ease mai ntenanc e, e rror h a n d l i n g c om p l e t e a cco rding t o ana rt a t d icul est t gy, ra e a nd p e rform a n c e c l os e to op t i m a l so a s not t t m p t p e op l t m a keo e e o t e co de m e ssy w it unp rincip l dh h e op t iza tim ions. Cl e a n c od e d oe s on e t h i n g w e l l .Bjarne StroustrupInventor of C++23 24. W h a tis C l a n C ode ?e C l a n co de is s i m p l e a nd d i re c t.eC l a n co de re a d s l i k e w e l l - ew ri t te n p ros e . C l a n code ne ve reobscure s t e de signe r's int nt buthera t e r is ful o f crisp [cl a rl de fine d]hl e ya bst ct ns a nd s tra i g h forw a rdra iol s o f co nt l inero .Grady Booch Author of Object Oriented Analysis andDesign with Applications 24 25. W h a tis C l a n C ode ?eCl e a n c od e c a n b e re ad , a nde nh a nce d by a de ve l p e r o t e r t a n o h hit origina l a ut or I t h a s u n i t a n ds h .ac c eptanc etests.It h a sm e a n i n g fu l n am e s . It p rovide son e w a y ra t e r t a n m a ny w a ys h hfo r do ing o ne t ing. It h a s m i n i m a l hdependenc i es,w h icha ree xp l l de fine d, a nd p ro vide s aicitycl a r a nd m i n i m a l AP I . C odeesh o ul be l i te ra t e since de p e ndingdon t e l ngua ge , not a l ne ce ssa ryh alinfo rm a t n ca n be e xp re sse d io Dave Thomascl a rl in code a l ne . e yo Founder of OTI, godfather ofthe Eclipse Strategy 25 26. W h a tis C l a n C ode ?e I co ul l a l o f t e qua l ie s t a t I d ist l hith no tice in cl a n code , but t e re is e h one ove ra rch ing qua l y t a t l a ds t it he o a l o f t e m . Cl e a n c od e a l w a ys l h l ook s i t w a s w ri t t e n b y s om e on e w h o c a re s . Th e re i s n ot h i n g obvi ou s t h a t you c a n d o t o m a k e i t b e tt e r. A l ofl t o se t ings w e re t o ugh t a bo ut by hhh t e co de 's a ut or a nd if you t t hh , ry o im a gine im p ro ve m e nt yo u're l d s,e ba ck t w h e re you a re , siting in ot a p p re cia t n o f t e code som e oneio h l ft fo r yo u c od e l e ft b yeMichael Feathers Author of Working Effectively s om e on e w h o c are s d e e p l yWith Legacy Code a b ou t th e c ra ft. 26 27. W h a tis C l a n C ode ?e In re ce ntye a rs I be gin, a nd ne a rl y e nd, w it B e ck's rul s of sim p l h e e co de . In p rio rit o rde r, sim p l code :y e R u n s al l t e s t s Con ta i n s n o d u p l i c a t i on E x p re s s e s al l t h e d e s i g n i d e a s t a ta re in t e syst mh he Mi n i m i ze s th e n u m b e r of e n t i ti e s such a s cl sse s, m e t o ds,a h funct ions, a nd t e l . h ikeRon JeffriesAuthor of Extreme Programming Installed 27 28. W h a tis C l a n C ode ? eY o u kno w you a re w orking on cl a n eco de w h e n e a c h rou ti n e youre a d tu rn s ou t t o be p re t t ym u c h w h at you e x p e c t e d . Y o uca n ca l it b e a u t i fu l code w h e n t e lhco de s a l m a k e s i t l ook l i k e sot h e l a n g u a g e w a s m a d e fort h e p rob l e m . Ward Cunningham Inventor of Wiki, Fit and much more Godfather of all those who care about code 28 29. W h a tis C l a n C ode ?e W it outo bvio ushS im p leim p rove m e ntsE fficie ntS t igh t rw a rdrafoE xp re ssiveC o nt ins noa Turns outt be w h a to dup l t ica ionsyou e xp e ct de R uns a l t st le s F ul o f m e a ningl L it ra leR e a ds w e llB e a u t i fu l : whenM inim a l th e l anguage was W rite n by t made forth e som e one w h o p rob l e mca re s 29 30. language may influence /* C Language */ #include #define NumLines 10 /* number of lines */ main(argc,argv) int argc; char **argv; {int tri[NumLines][NumLines], /* triangle */r,c,i; /* row, column, misc index *//* initi