Inhaltsverzeichnis
13. Namen
- Themen
- Warum sind Namen wichtig?
- Allgemeine Regeln für Namen
- Spezifische Regeln für Variablen, Funktionen, Klassen, Objekte
- Ausblick: Namen lassen sich ändern
- Folien
- Glossar
- Video
- MP4
Webcast
Hinweis: Der Webcast wurde mit Tiny Webcasts for Lecture(r)s erstellt.
Zentrale Aspekte
- Gut gewählte Namen sind eine essentielle Voraussetzung
für lesbaren Code – und alles, was davon abhängt. - Ein guter Name beantwortet die großen Fragen:
Warum existiert etwas? Was tut es? Wie nutzt man es? - Programmierer sind Autoren:
Der Code sollte so offensichtlich wie möglich sein. - Ein Name, der in einem Kommentar erklärt werden muss,
ist (meist) ein schlecht gewählter Name. - Gute Namen erfordern gute beschreibende Fähigkeiten
und einen gemeinsamen kulturellen Hintergrund.
Fragen zur Vertiefung und Wiederholung
Diese Fragen dienen der persönlichen Beschäftigung mit der Thematik, werden aber nicht separat in der Vorlesung besprochen.
- Was sind notwendige Voraussetzungen dafür, gute Namen zu finden?
- Warum sind gute Namen in der Programmierung so wichtig?
- Welche (drei) großen Fragen sollte ein Name beantworten?
- Nennen Sie Kriterien für gute Namen und Dinge, die man bei der Benennung unbedingt vermeiden sollte.
- Was ist der Zusammenhang zwischen Namen, Editoren/IDEs und Refactoring? Warum ist deren Zusammenspiel für die Codequalität so wichtig?
Zur Bedeutung von Namen
Benennung ist ein Akt des Sich-Zueigen-Machens einer Sache: Wenn ich einen Namen für etwas habe, kann ich darauf verweisen, ohne es jedesmal beschreiben zu müssen. Eine humoristische Referenz an dieses Bestreben des Menschen, seine Umwelt durch Begriffe zu erfassen, findet sich auch in Douglas Adams' erstem Band des „Hitch Hiker's Guide“, die hier in voller Länge zitiert werden soll:
[…] against all probability a sperm whale had suddenly been called into existence several miles above the surface of an alien planet.
And since this is not a naturally tenable position for a whale, this poor innocent creature had very little time to come to terms with its identity as a whale before it then had to come to terms with not being a whale any more.
This is a complete record of its thought from the moment it began its life till the moment it ended it.
Ah …! What's happening? it thought.
Er, excuse me, who am I?
Hello?
Why am I here? What's my purpose in life?
What do I mean by who am I?
Calm down, get a grip now … oh! this is an interesting sensation, what is it? It's a sort of … yawning, tingling sensation in my … my … well, I suppose I'd better start finding names for things if I want to make any headway in what for the sake of what I shall call an argument I shall call the world, so let's call it my stomach.
Good. Ooooh, it's getting quite strong. And hey, what about this whistling roaring sound going past what I'm suddenly going to call my head? Perhaps I can call that … wind! Is that a good name? It'll do … perhaps I can find a better name for it later when I've found out what it's for. It must be something very important because there certainly seems to be a hell of a lot of it. Hey! What's this thing? This … let's call it a tail – yeah, tail. Hey! I can really thrash it about pretty good can't I? Wow! Wow! That feels great! Doesn't seem to achieve very much but I'll probably find out what it's for later on. Now – have I built up any coherent picture of things yet?
No.
Never mind, hey, this is really exciting, so much to find out about, so much to look forward to, I'm quite dizzy with anticipation …
Or is it the wind?
There really is a lot of that now isn't there?
And wow! Hey! What's this thing suddenly coming towards me very fast? Very very fast. So big and flat and round, it needs a big wide sounding name like … ow … ound … round … ground!
That's it! That's a good name – ground!
I wonder if it will be friends with me?
And the rest, after a sudden wet thud, was silence. [Adams, 1995Adams, Douglas (1995): The Hitch Hiker's Guide to the Galaxy. A Trilogy in Five Parts, Willian Heinemann, London], S. 99f.
Die Einzelheiten dieser Passage zu interpretieren und zu überlegen, wie die angesprochenen Aspekte sich auf das Thema Namen im Kontext der Programmierung (und darüber hinaus) anwenden lassen, sei dem geneigten Leser überlassen – ganz zu schweigen von den zahlreichen Anspielungen auf die abendländische Philosophie und einige ihrer größten Fragestellungen.
Weiterführende Literatur
Eine kommentierte und handverlesene Liste mit weiterführender Literatur zum Thema. Die Auswahl ist zwangsläufig subjektiv.
Eine Einführung in die Thematik, die viele Kriterien für gute Namen aufstellt, egal ob Klassen, Funktionen oder Variablen, ist Kapitel 2 in [Martin, 2008Martin, Robert C. (2008): Clean Code. A Handbook of Agile Software Craftmanship, Prentice Hall, Upper Saddle River, New Jersey]. Detaillierter und mit vielen Beispielen, dafür aber auf Variablen fokussiert, ist Kapitel 11 in [McConnell, 2004McConnell, Steve (2004): Code Complete. A practical handbook of software construction, Microsoft Press, Redmond, Washington]. Sehr kurz und prägnant sind die Ausführungen in Kapitel 1 in [Kernighan, 1999Kernighan, Brian W.; Pike, Rob (1999): The Practice of Programming, Addison Wesley, Boston].
Literarisch wurde die Bedeutung von Namen immer wieder aufgegriffen. Unsterblich ist das Gedicht The Naming of Cats von T. S. Eliot [Eliot, 1987Eliot, T. S. (1987): Old Possum's Book of Practical Cats, Faber & Faber, London] (online z.B. hier). Die Benennung von Dingen um des Verständnisses willen wird von Douglas Adams im ersten Band seines The Hitch Hiker's Guide to the Galaxy persifliert (s.o.).
Formale Kriterien für Namen und die Benennung unterschiedlicher Entitäten (Variable, Funktion, Klasse, Modul, …) sind immer von der gewählten Programmiersprache abhängig. Die Python-Gemeinde hat sich selbst relativ strikte Regeln gegeben, die im Python Enhancement Proposal (PEP) mit der Nummer 8 (kurz: PEP8) ausgeführt sind:
- Adams, Douglas (1995): The Hitch Hiker's Guide to the Galaxy. A Trilogy in Five Parts, Willian Heinemann, London
- Eliot, T. S. (1987): Old Possum's Book of Practical Cats, Faber & Faber, London
- Kernighan, Brian W.; Pike, Rob (1999): The Practice of Programming, Addison Wesley, Boston
- Martin, Robert C. (2008): Clean Code. A Handbook of Agile Software Craftmanship, Prentice Hall, Upper Saddle River, New Jersey
- McConnell, Steve (2004): Code Complete. A practical handbook of software construction, Microsoft Press, Redmond, Washington