Treating Code As an Essay Proseminar Beautiful Code Jan Lelis 15. Juni 2010 Yukihiro Matsumoto rbjl.net.

Slides:



Advertisements
Ähnliche Präsentationen
Aufgabenbesprechung Programming Contest. Order 7 Bo Pat Jean Kevin Claude William Marybeth 6 Jim Ben Zoe Joey Frederick Annabelle 0 SET 1 Bo Jean Claude.
Advertisements

Java in 9 Folien Besser: Online-Buch Go to Java 2.
Don`t make me think! A Common Sense Approach to Web Usability
AufwÄrmung A look at the future…..Match the German phrases on the left to the English ones on the right. Do your best; I am grading on participation! Wie.
Montag den 16.Dezember Lernziel: To begin stage 2 of preparation for speaking assessment.
Wortschatz der Schulhof the playground die Aula the hall
Abteilung für Telekooperation Übung Softwareentwicklung 1 für Wirtschaftsinformatik Dr. Wieland Schwinger
“wish” “as if” “if only it were so”
CALPER Publications From Handouts to Pedagogical Materials.
Ein Projekt des Technischen Jugendfreizeit- und Bildungsvereins (tjfbv) e.V. kommunizieren.de Blended Learning for people with disabilities.
Clean Code Software-Entwicklung als Handwerkskunst Thomas Nagel, November 2011.
Objectives Verstehen was unterDelegate verstanden wird
Starten der Entwicklungsumgebung (IDE)
Natürliches Programmieren
Coordinating Conjunctions Why we need them & how to use them deutschdrang.com.
Übung Informatik I exercise01. 2 Inhaltsübersicht Nachbesprechung Übung 1 Individuelle Fragen/Bemerkungen.
Mein Arbeitspraktikum. Today we are learning to talk about work experience we have done, giving facts, details and opinions The bigger picture: We are.
Die Fragen Wörter Wer? Was? Wann?.
Nominative & Accusative Basic Rules for Relative Pronouns in German:
1 Bauhaus-Universität Weimar ArchitekturProgrammierung Generative Entwurfsmethoden Processing Grundlagen Professur Informatik in der Architektur.
Was glaubst du… Was ist ein gutes Geschenk für deine Gastschwester/ Gastbruder? Ein Buch? Ein Radiowecker? Ein Hut? Ohrringe? Eine DVD?
I will be able to use the accusative pronouns in a sentence (Buch Seite 200)
Java-Kurs - 4. Übung Hausaufgabe Weitere Kontrollstrukturen
Literary Machines, zusammengestellt für ::COLLABOR:: von H. Mittendorfer Literary MACHINES 1980 bis 1987, by Theodor Holm NELSON ISBN
Deutsch 3 Frau Snell.
Schreiben Sie fünf Sätze aus diesen Elementen. [Beispiel
GERMAN WORD ORDER ORDER s. Sentences are made up by placing a variety of words in a specific order. If the order is wrong, the sentence is difficult to.
COMMANDS imperative There are three command forms: formal familiar singular familiar plural.
COMMANDS imperative 1. you (formal): Sie 2. you (familiar plural): ihr
Studentenstreik 2009, 2010 in Deutschland Part 1: Studiengebühren.
Studentenstreik 2009, 2010 in Deutschland Part 1: Studiengebühren.
Montag den 8. Juni Lernziel:- To launch a project and receive results.
Kapitel 4: Mein Tag Sprache.
Imperfekt (Simple Past) Irregular or strong verbs
Phonics Lessons Grade 1 Adapted from: Phonics Lessons by Gay Su Pinnell & Irene C. Fountas Grade 1, page 431 caatt If you knowIt helps you know.
Memorisation techniques
Kapitel 8 Grammar INDEX 1.Command Forms: The Du-Command Form & Ihr- Command 2.Sentences & Clauses.
WILLKOMMEN. By the end of today’s lesson You will know where to find some important information in the text book You will know what is expected of you.
Reflexiv-Verben Deutsch 2/AC.
Kapitel 5: Einkaufen Sprache. Alles klar Look over and know all of the Wortschatz on Seite 171 and 172. Look over the illustration found on Seite 145.
Kapitel 9 Grammar INDEX 1.Formal Sie- Command 2.There Is/There Are 3.Negation: Nicht/Klein.
Was machst du gern?.
Großvater Großmutter Großvater Großmutter Tante/Onkel Vater Mutter.
Prof. Dr. W.-P. Buchwald Fachhochschule Braunschweig/Wolfenbüttel University of Applied Sciences Images Original Quantized 2-dimensional sampled Sampled.
ENVIRONMENT PROBLEMS What can I do? Pineapples Traffic  Use public vehicles  Use more bike and go by walking  There should be a filter in every car.
Java Programme nur ein bisschen objektorientiert.
Die Programmiersprache Ruby Seminarvortrag von Lia Schulze Dephoff 1.
Essay structure Example: Die fetten Jahre sind vorbei: Was passiert auf der Almhütte? Welche Bedeutung hat sie für jede der vier Personen? Intro: One or.
LLP DE-COMENIUS-CMP Dieses Projekt wurde mit Unterstützung der Europäischen Kommission finanziert. Die Verantwortung für den Inhalt dieser.
Fitness. An english presentation.
Interrogatives and Verbs
FREE ICONS POWERPOINT TEMPLATE.
Hello World! Javakurs 2013 Arne Kappen
Du bist am dicksten und am dümmsten.
Formation of Questions in German
Aspect-Oriented Programming: Fad or the Future
Telling Time in German Deutsch 1 Part 1.
Students have revised SEIN and HABEN for homework
Users outside of UCA Dear Elke Kerner, if this is your real name, and others: you have downloaded my PowerPoints and posted them on public access websites.
Wenn sich eine Türe schließt,
To English Translations
Freizeit Aktivitäten.
Wie viel Uhr ist es? Telling Time.
Was ist die Verbindung hier?
Interactive--So sagt man das! Saying when you do various activities
Implementieren von Klassen
Quality assured by the ALL Connect project (2015)
School supplies.
Die tiere Share: Introduce the session and PDSA.
- moodle – a internet based learning platform
 Präsentation transkript:

Treating Code As an Essay Proseminar Beautiful Code Jan Lelis 15. Juni 2010 Yukihiro Matsumoto rbjl.net

2/2/ Inhalt ● Einführung (7 Folien) ● Ruby: Fakten und Entstehung (5 Folien) ● „Beautiful Code” – Ruby Syntax und Beispiele ● Sprachfeatures (9 Folien) ● Verwendung als „Domain Specific Language” (3 Folien) ● Beispiel „Quicksort” (2 Folien) ● Anhang (2 Folien) Zitate von Yukihiro Matsumoto aus „Beautiful Code” sind in diesem Schriftstil

3/3/ rbjl.net Treating Code As an Essay? Both essays and lines of code are meant - before all else - to be read and understood by human beings. Essay Code „What is it about?” „What does it do?”

4/4/ rbjl.net Hello World C #include int main(void) { printf("Hello, World!"); return 0; }

5/5/ rbjl.net Hello World Java class HelloWorld { public static void main(String args[]) { System.out.print("Hello World!"); }

6/6/ rbjl.net Hello World Perl / Python / Ruby print "Hello World!"

7/7/ rbjl.net Perl #!/usr/bin/perl ;;;;;; ;;;;;;;;;;; ;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;; +$I=sub{+s+^+ ;;;;;;; ;;;;;;;;; $"x$_[1]+gem;$/x$_# ;;;; ;;;;;;;; [0].$_.$/};$W=sub{$~=!q~ ;;;;;;; ~.pop();system($^O=~Win?ClS:# ;;;;;;; 'clear'),print,select$Z,$Z,$Z,!" ;;;;;; ;;;;;; x$_[0],pop=~m-.+-g),!_};$_=!Mima,s-- ;;;;; ;;;;; wNXIBP]}"-see;s-^#!..+?$/(?=$"+;)--is ;;;; y-;-'-;s-\w-~-gi;$S=$_;#--Beautiful] ;;; ;;; -d =0;join$/,map{$j=$%;join!_,grep# ;;; Rhea !($j++%$_[$%]),m-.-g}grep!($i# ;;; -Titan ;;; -Huygens ;;; -&&20,051, $_=_^q-q-),&$I(20,41-!q- ;;; -,$_=F|K),$ I->(15,31,$_=&$R(4-!q- ;;; -)),&$I(13-!" ;;",28,$_=&$R(3)),&${ ;;; _^_^I}(10,20-!" ;;;;;",$_=$R->(2)),q- ;;; ;;;;;; "&&$H}($_,&${ ;;; R.!-_}($_))x$_,!" ;;;;; "+2..2*~~2 ;;; ;;;;;; &${m-- ;;; ;;;;;;; b- ;;; ;;;;;;; ;;;; -],!1!~~1);&$W($S.!q- ;;;;;;;;; ;;;;; -,$L,0.16)for$%..5+!q- ;;;;;;;;;; ;;;;;;;;; Cassini-;&{$W||q- ;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;; ;;;;;;;;;;; ;;;;;; Most programs are not write-once [...] it is therefore more important by far for humans to be able to understand the program than it is for the computer.

8/8/ rbjl.net Yukihiro Matsumoto Beautiful code is really meant to help the programmer be happy and productive.

9/9/ rbjl.net Ruby ● Ruby ist ● 2 Teile Perl ● 1 Teil Python ● 1 Teil Smalltalk ● Verbindet gute Elemente aus verschiedene Sprachen mit schöner Syntax ● Große Standard-Library ● Viele Erweiterungen („gems”)

10 / rbjl.net Entstehung ● 1995: Erste Version 0.95 ● Lange Zeit nur mit japanischer Dokumentation ● 1999: ruby-talk ● Englischsprachige Maillingliste ● 2000: „Programming Ruby” (Pickaxe Book) ● Buch von den „Pragmatic Programmers” ● War die erste englische Dokumentation ● 2005:

11 / rbjl.net Ruby on Rails ● Rails: In Ruby geschriebenes Webframework ● Beinflusste viele Webframeworks anderer Sprachen ● Nutzt MVC Architektur (Model, View, Controller) ● Verbindet „Best Practises” mit einer Web-DSL In order to eliminate redundancy, we follow the DRY principle: Don't Repeat Yourself. If the same code exists in multiple places, whatever you're trying to say becomes obscured. Screenshot von rubyonrails.org

12 / rbjl.net Ruby Interpreter ● Derzeitige Version: / ● Implementationen ● MRI: Matz Ruby Interpreter, Referenz ● JRuby: auf der JVM ● IronRuby: auf.NET ● MacRuby: Cocoa, LLVM ● Rubinius: in Ruby mit kleinem C++ Kern ● (Cardinal: auf der Perl6-VM „Parrot”)

13 / rbjl.net Ein einfaches Programm Human beings are more conservative than you might think, [so Ruby] is an extremely conservative programming language. Ruby sticks to traditional control structures programmers are familiar with, such as if, while, etc. def fib(n) if n == 0 || n == 1 return 1 else return fib(n-1) + fib(n-2) end

14 / rbjl.net Konzentration auf das Wesentliche ● Dynamisch ● Keine Typdeklarationen ● Keine Klammern bei einfachen Methoden- aufrufen ● Letzte Statement einer Methode ist automatisch der Rückgabewert, kein return notwendig Brevity is one element that helps make code beautiful. [...] Because there is a definite cost involved in scanning code with the human eye, programs should ideally contain no unnecessary information. def fib(n) return 1 if n <= 1 fib(n-1) + fib(n-2) end

15 / rbjl.net Datenstrukturen (Auswahl) ● Alles sind Objekte, auch simple Datentypen

16 / rbjl.net Datenstrukturen: Strings & Symbole 'Beautiful Code' =~ /Spaghetti/ #=> nil 'Beautiful Code' =~ /Beaut.*/ #=> 0 a = 'Hello' #=> Hello a Hello University a.delete! 'o' #=> Hell University a.empty? #=> false a[5,3] #=> Uni 'hello'.size #=> 5 :hello.size # NoMethodError

17 / rbjl.net Datenstrukturen: Arrays & Hashs a = [] #=> [] b = [11,4,7,10] #=> [11, 4, 7, 10] a [2, 4, 6] a + b #=> [2, 4, 6, 11, 4, 7, 10] a & b #=> [4] b.sort #=> [4, 7, 10, 11] a = {:a_key => 'is here', :another => 3} a[:a_key] = 'is not here' a[6] = 15 puts a # outputs: # {6=>15, :a_key=>"is not here", # :another=>3}

18 / rbjl.net Anonyme Funktionen ● Methoden nehmen beim Aufruf „Blöcke” an (Anonyme Funktionen / Closures / Lambdas / Procs) ● Blöcke sind gut in Syntax integriert ● Werden mit do und end oder { und } gebildet ● Hoher Stellenwert im Programmierstil a = [1,2,3] i = 0 while i < a.size # do something with a[i] i += 1 end a = [1,2,3] a.each do |e| # do something with e end

19 / rbjl.net Funktionaler Stil a = [1,2,3,4].map{ |ele| ele*ele } #=> [1, 4, 9, 16] ● Aussagekräftige Syntax aufgrund der Closures ● Nützliche Methoden für Objektsammlungen („Enumerables”) wie Arrays 5.times{ puts "Hello World!" } r = r.member? 13 #=> false r.max #=> 42 r.select{ |ele| ele%3 == 0 } #=> [36, 39, 42]

20 / rbjl.net Variablennamen ● Konstanten- und Klassennamen werden groß geschrieben ● Methoden und lokale Variablen fangen mit Kleinbuchstaben an ● Instanzvariablen eines Objektes und Klassenvariablen beginnen ● Globale Variablen starten mit $

21 / rbjl.net class Example def initialize(input = '', params = input, params end def end def = value end Klassisches Objektsystem a = Example.new 'my_data', :user => 'Yukihiro' puts a.options[:user] # Yukihiro a.options[:user] = 'matz.'

22 / rbjl.net Metaprogrammierung ● Alle Klassen sind „offen” ● Bestehene Klassen erweiterbar ● eval ● Code zur Laufzeit generieren und ausführen ● Reflektion ● method_missing ● Wird ausgeführt, wenn keine Methodename unbekannt 5.is_a? Integer #=> true When information is duplicated, the cost of maintaining consistency can be quite high.

23 / rbjl.net Ruby für einfache interne DSLs ● Domain Specific Languages (DSL) ● Formale Sprache für bestimmtes Problemfeld ● Ziel: Einfache Bedienbarkeit ● Unterscheidung in ● Extern: Neue Sprache ● Intern: Nutzung einer bestehenden Sprache ● Mittel von Ruby erlauben gut verständlichen und einfach lesbaren Code ● Methodenaufrufe wirken wie Schlüsselwörter

24 / rbjl.net task :default => [:test] task :test do ruby "test/unittest.rb" end DSL: Rake („Ruby Make”) task({:default => [:test]}) task(:test, &lambda(){ ruby "test/unittest.rb" }) ● In Ruby geschrieben (anstatt in Extra-Sprache) ● Beide Varianten korrekt ● Erste wesentlich besser lesbar We often feel beauty in simple code. [...] when programs are obscure rather than comprehensible, the results are bugs, mistakes, and confusion.

25 / rbjl.net DSL: “Ruby on Rails” ● Model-Definitionen class Entry < ActiveRecord::Base has_many :comments has_and_belongs_to_many :tags end class Comment < ActiveRecord::Base belongs_to :entry validates_presence_of :title end a = Entry.find_by_id 42 b = a.comments.find_by_title 'hi' b.title = '' b.save # fails..

26 / rbjl.net Live Coding ● Quicksort [Another] important element in the concept of “beautiful code” is flexibility [which I define] as freedom from enforcement from tools.

27 / rbjl.net Live Coding ● Quicksort class Array def qsort return self if self.size <= 1 left, right = [], [] pivot = self.shift self.each do |ele| ele <= pivot ? left << ele : right << ele end left.qsort + [pivot] + right.qsort end

28 / rbjl.net So, what is „Beautiful Code”? And if you also make sure to have fun writing and reading code, you will experience happiness as a programmer. Happy Hacking! balance brevity flexibility simplicity conservatism

29 / rbjl.net Quellen, Ressourcen ● Oram, Wilson: „Beautiful Code” (O'Reilly, 2007) ● Websites ● ruby-lang.org (Offizielle Seite) ● rbjl.net (Mein Ruby Blog) ● Perl-Saturn von eyepopslikeamosquito ● ● Bilder ● Yukihiro Matsumoto: public domain, Wikimedia ● Ruby Logo: © Ruby Association LLC ● Impress Template: hagetaka0