a4b99rph re en probl m u a hry cist k d
play

A4B99RPH: Reen problm u a hry Cist kd. Petr Pok Katedra - PowerPoint PPT Presentation

CZECH TECHNICAL UNIVERSITY IN PRAGUE Faculty of Electrical Engineering Department of Cybernetics A4B99RPH: Reen problm u a hry Cist kd. Petr Pok Katedra kybernetiky CVUT FEL A4B99RPH: P. Pok c 2012


  1. CZECH TECHNICAL UNIVERSITY IN PRAGUE Faculty of Electrical Engineering Department of Cybernetics A4B99RPH: ˇ Rešení problém˚ u a hry ˇ Cistý kód. Petr Pošík Katedra kybernetiky ˇ CVUT FEL A4B99RPH: ˇ P. Pošík c � 2012 Rešení problém˚ u a hry – 1 / 14

  2. Clean Code Zpracováno podle Robert C. Martin: Clean Code: A Handbook of Agile Software Craftsmanship , Prentice Hall, 2008. A4B99RPH: ˇ P. Pošík c � 2012 Rešení problém˚ u a hry – 2 / 14

  3. Který kód je ˇ cistší? A proˇ c? Dvˇ e implementace téhož algoritmu: PRIME = True NONPRIME = False def generate_primes_up_to (max_value): """Find primes up to the max_value def generate_primes_up_to (max_value): using the Sieve of Eratosthenes. """Find primes up to the max_value using the Sieve of Eratosthenes. """ if max_value >= 2: # There are some primes """ # Initialize the list (incl. 0) if max_value < 2: f = [ True for i in range (max_value+1)] return [] # Get rid of the known non-primes else : f[0] = f[1] = False candidates = init_integers_up_to(max_value) # Run the sieve mark_non_primes(candidates) for i in range (2, len (f)): return collect_remaining(candidates) if f[i]: # i is still a candidate # mark its multiples as not prime def init_integers_up_to (max_value): for j in range (2*i, len (f), i): return [PRIME for i in range (max_value+1)] f[j] = False # Find the primes and put them in a list def mark_non_primes (candidates): primes = [i for i in range ( len (f)) if f[i]] # Mark 0 and 1, they are not primes. return primes candidates[0] = candidates[1] = NONPRIME # max_value < 2 else : for number in range (2, len (candidates)): return list () # no primes, return empty list if candidates[number] == PRIME: mark_as_not_prime_multiples_of(number, candidates) def mark_as_not_prime_multiples_of (number, candidates): for multiple in range (2*number, len (candidates), number): candidates[multiple] = NONPRIME def collect_remaining (candidates): primes = [i for i in range ( len (candidates)) if candidates[i]==PRIME] return primes A4B99RPH: ˇ P. Pošík c � 2012 Rešení problém˚ u a hry – 3 / 14

  4. Co je “clean code”? Bjarne Stroustrup, autor jazyka C++ a knihy “The C++ Programming Language”: I like my code to be elegant and efficient . The logic should be Clean Code straightforward to make it hard for bugs to hide, the dependencies • Který kód je ˇ cistší? minimal to ease maintenance, error handling complete according to an A proˇ c? • Co je “clean code”? articulated strategy, and performance close to optimal so as not to tempt • ˇ Cistý kód v praxi people to make the code messy with unprincipled optimizations. Clean • Smysluplná jména code does one thing well . • Eratostenovo síto: smysluplná jména • Komentᡠre • Eratostenovo síto: komentᡠre • Funkce a metody • Eratostenovo síto: funkce • Eratostenovo síto: pˇ revod na tˇ rídu • Eratostenovo síto: funkce a tˇ rída? • Závˇ er A4B99RPH: ˇ P. Pošík c � 2012 Rešení problém˚ u a hry – 4 / 14

  5. Co je “clean code”? Bjarne Stroustrup, autor jazyka C++ a knihy “The C++ Programming Language”: I like my code to be elegant and efficient . The logic should be Clean Code straightforward to make it hard for bugs to hide, the dependencies • Který kód je ˇ cistší? minimal to ease maintenance, error handling complete according to an A proˇ c? • Co je “clean code”? articulated strategy, and performance close to optimal so as not to tempt • ˇ Cistý kód v praxi people to make the code messy with unprincipled optimizations. Clean • Smysluplná jména code does one thing well . • Eratostenovo síto: smysluplná jména • Komentᡠre • Eratostenovo síto: Grady Booch, autor knihy “Object Oriented Analysis and Design with Applications”: komentᡠre • Funkce a metody Clean code is simple and direct . Clean code reads like well-written prose . • Eratostenovo síto: funkce Clean code never obscures the designer’s intent but rather is full of crisp • Eratostenovo síto: abstractions and straightforward lines of control . pˇ revod na tˇ rídu • Eratostenovo síto: funkce a tˇ rída? • Závˇ er A4B99RPH: ˇ P. Pošík c � 2012 Rešení problém˚ u a hry – 4 / 14

  6. Co je “clean code”? Bjarne Stroustrup, autor jazyka C++ a knihy “The C++ Programming Language”: I like my code to be elegant and efficient . The logic should be Clean Code straightforward to make it hard for bugs to hide, the dependencies • Který kód je ˇ cistší? minimal to ease maintenance, error handling complete according to an A proˇ c? • Co je “clean code”? articulated strategy, and performance close to optimal so as not to tempt • ˇ Cistý kód v praxi people to make the code messy with unprincipled optimizations. Clean • Smysluplná jména code does one thing well . • Eratostenovo síto: smysluplná jména • Komentᡠre • Eratostenovo síto: Grady Booch, autor knihy “Object Oriented Analysis and Design with Applications”: komentᡠre • Funkce a metody Clean code is simple and direct . Clean code reads like well-written prose . • Eratostenovo síto: funkce Clean code never obscures the designer’s intent but rather is full of crisp • Eratostenovo síto: abstractions and straightforward lines of control . pˇ revod na tˇ rídu • Eratostenovo síto: funkce a tˇ rída? • Závˇ er Dave Thomas, zakladatel firmy OTI (pˇ revzata firmou IBM v roce 1996), kmotr Eclipse: Clean code can be read, and enhanced by a developer other than its original author . It has unit and acceptance tests . It has meaningful names . It provides one way rather than many ways for doing one thing. It has minimal dependencies , which are explicitly defined, and provides a clear and minimal API . A4B99RPH: ˇ P. Pošík c � 2012 Rešení problém˚ u a hry – 4 / 14

  7. ˇ Cistý kód v praxi Jediné správné mˇ eˇ rítko kvality kódu: Co-to-k-ˇ certy za minutu Clean Code • Který kód je ˇ cistší? A proˇ c? • Co je “clean code”? • ˇ Cistý kód v praxi • Smysluplná jména • Eratostenovo síto: smysluplná jména • Komentᡠre • Eratostenovo síto: komentᡠre • Funkce a metody • Eratostenovo síto: funkce • Eratostenovo síto: pˇ revod na tˇ rídu • Eratostenovo síto: funkce a tˇ rída? • Závˇ er A4B99RPH: ˇ P. Pošík c � 2012 Rešení problém˚ u a hry – 5 / 14

  8. Smysluplná jména ■ Vymyslet dobrá jména je velmi tˇ ežké ! Vˇ enujte tomu dostateˇ cnou pozornost! ■ Nebojte se jméno zmˇ enit, pˇ rijdete-li na lepší! ■ Dobré jméno odhaluje autor˚ er (intention-revealing). Clean Code uv zámˇ • Který kód je ˇ cistší? Pokud jméno vyžaduje komentᡠr, neodhaluje zámˇ er. Porovnejte: A proˇ c? • Co je “clean code”? ■ self .d = 0 # Elapsed time in days • ˇ Cistý kód v praxi self .elapsed_time_in_days = 0 ■ • Smysluplná jména • Eratostenovo síto: ■ Názvy tˇ ríd: podstatná jména (s pˇ rívlastky): smysluplná jména • Komentᡠre Customer , WikiPage , AddressParser , Filter , StupidFilter , Corpus , TrainingCorpus ■ • Eratostenovo síto: komentᡠre ■ Názvy funkcí/metod: slovesa (s pˇ redmˇ etem): • Funkce a metody • Eratostenovo síto: post_payment , delete_page , save , train , test , get_email ■ funkce • Eratostenovo síto: ■ Jeden termín pro jeden koncept! Nepoužívejte stejné slovo k více úˇ cel˚ um! pˇ revod na tˇ rídu ■ Nebojte se dlouhých jmen! • Eratostenovo síto: funkce a tˇ rída? ■ Dlouhé popisné jméno je lepší než dlouhý popisný komentᡠr. • Závˇ er ˇ Cím delší oblast platnosti promˇ enné, tím popisnˇ ejší jméno by mˇ ela mít. ■ ■ Používejte pojmenované konstanty místo magických ˇ císel v kódu! A4B99RPH: ˇ P. Pošík c � 2012 Rešení problém˚ u a hry – 6 / 14

  9. Eratostenovo síto: smysluplná jména def generate_primes_up_to (max_value): """Find primes up to the max_value using the Sieve of Eratosthenes. """ if max_value >= 2: # There are some primes # Initialize the list (incl. 0) f = [ True for i in range (max_value+1)] # Get rid of the known non-primes f[0] = f[1] = False # Run the sieve for i in range (2, len (f)): if f[i]: # i is still a candidate # mark its multiples as not prime for j in range (2*i, len (f), i): f[j] = False # Find the primes and put them in a list primes = [i for i in range ( len (f)) if f[i]] return primes # max_value < 2 else : return list () # no primes, return empty list A4B99RPH: ˇ P. Pošík c � 2012 Rešení problém˚ u a hry – 7 / 14

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend