Clean Code - Uncle Bob / Lesson 2
Summary
TLDRLa vidéo explore l'origine de la Lune en commençant par débattre de diverses théories telles que la capture d'un astéroïde ou la Lune provenant de la Terre elle-même. Les caractéristiques particulières de la Lune, telles que sa composition presque identique à celle de la Terre mais sans fer, et son orbite unique qui ne s'aligne pas avec l'équateur terrestre, compliquent l'identification de son origine. La théorie la plus acceptée aujourd'hui est celle de l'impact géant. Selon cette théorie, un corps céleste de la taille de Mars aurait percuté la Terre il y a environ 4,5 milliards d'années. L'impact aurait causé une fusion de matériaux, projetant des silicates légers pour former un anneau qui s'est ensuite coalescé pour devenir la Lune, tandis que les noyaux de fer des deux corps auraient fusionné dans le noyau de la Terre. Ce processus arrivé il y a plusieurs milliards d'années a aussi engendré une orbite circulaire et stabilisée de la Lune, laquelle est verrouillée sur la Terre montrant toujours la même face. La vidéo mentionne également que le processus d'éloignement de la Lune continuera des milliards d'années.
Takeaways
- 🌍 La Lune provient probablement de la Terre.
- 🔄 Sa composition est similaire, mais sans fer.
- 🪐 Elle a un orbite circulaire atypique.
- 🔒 La Lune est verrouillée sur la Terre.
- 💥 Théorie actuelle : impact géant avec un corps de taille martienne.
- 🛠 Fusion de matériaux suite à une collision.
- 🌌 Formation dûe à un anneau de débris.
- ↔️ Éloignement progressif de la Lune.
- ⏱️ Ancien jour terrestre de 6 heures.
- ⚙️ Ajustement continu dans le système Terre-Lune.
Timeline
- 00:00:00 - 00:05:00
La question de l'origine de la lune est plus complexe qu'il n'y paraît. Elle pourrait provenir de la Terre, mais cela implique plusieurs théories sur sa formation : capturée comme un astéroïde, émergeant de la Terre, ou formée par la collision avec un autre corps. Les particularités de son orbite et de sa composition chimique compliquent davantage le mystère, jusqu'à la théorie moderne d'une collision géante ayant créé un anneau qui est devenu la lune.
- 00:05:00 - 00:10:00
Les commentaires dans le code servent à expliquer des parties de code qui ne peuvent pas s'expliquer d'elles-mêmes. L'auteur critique l'usage excessif de commentaires en se référant à des anecdotes sur les limitations de langages anciens comme le Fortran. Aujourd'hui, les structures des langages modernes permettent de s'exprimer par du code explicite, rendant souvent les commentaires redondants ou nuisibles.
- 00:10:00 - 00:15:00
Chaque commentaire représente un échec à s'exprimer clairement en code. Les commentaires se détériorent avec le temps, étant rarement mis à jour ou même lus dans le contexte du code. L'auteur défend l'idée que le code doit, autant que possible, expliquer son propre but et que les commentaires doivent être un dernier recours.
- 00:15:00 - 00:20:00
Les exemples de bons commentaires incluent des avertissements sur des conceptions spécifiques, par exemple les méthodes singletons ou des expressions régulières complexes. Cependant, même certains commentaires bien intentionnés peuvent devenir trompeurs s'ils ne sont pas maintenus à jour avec le code.
- 00:20:00 - 00:25:00
Les mauvais commentaires incluent ceux qui ne font que répéter le code, ceux qui deviennent obsolètes, ou ceux qui sont inutiles et encombrants. L'auteur préconise d'éviter les commentaires superflus, soulignant que la responsabilité professionnelle implique de ne pas laisser des excuses dans le code.
- 00:25:00 - 00:30:00
Les lignes directrices pour les commentaires indiquent que celles qui sont justifiées apportent une valeur ajoutée, surtout lorsqu'elles clarifient l'intention derrière des astuces de codage ou résument des concepts complexes qui ne peuvent pas être exprimés facilement en code.
- 00:30:00 - 00:35:00
Les commentaires de mauvaise qualité proviennent souvent d'une obligation de commenter, menant à des contributions vides de sens ou préjudiciables. Par exemple, l'abus de JavaDocs et les mises à jour manquantes transforment les commentaires en liabilité plutôt qu'en atout.
- 00:35:00 - 00:40:00
L'analyse des projets Java montre une variété dans la taille des fichiers source, peu corrélée à la taille du projet. Des projets plus petits peuvent avoir des organigrammes similaires aux plus grands, suggérant que la taille des fichiers est une question de style de codage et non de nécessité structurelle.
- 00:40:00 - 00:45:00
Concernant la taille des lignes, une tendance commune émerge des projets étudiés : les lignes de code ont tendance à avoisiner les 30-40 caractères, et peu dépassent 80 caractères. L'ergonomie des lignes courtes facilite la lecture du code et rend les longs défilements horizontaux désagréables.
- 00:45:00 - 00:50:00
Les noms dans le code apportent une mine d'informations sur la fonction et le contexte du système. Ils doivent être intelligibles et proportionnels à la portée : plus le contexte est large, plus le nom doit être court, et vice versa. Les noms trop cryptiques ou non expressifs mènent à des ambiguïtés dans la compréhension du code.
- 00:50:00 - 00:55:00
Les anciens préfixes pour identifier les variables et arguments sont remplacés par des outils modernes, rendant ces conventions obsolètes. Cependant, il est crucial d'éviter les mots factices ou redondants dans les noms, qui ajoutent du bruit sans clarification ni différenciation significative entre différents types ou variables du système.
- 00:55:00 - 01:00:00
La distinction entre les noms doit être claire pour éviter des erreurs visuelles dans le code. À une époque, les préfixes étaient utilisés pour cette raison, mais les IDE modernes rendent cette pratique désuète. Une gestion soignée des noms empêche la confusion entre des variables aux noms semblables.
- 01:00:00 - 01:06:00
En développant sur le sujet des noms, on met en avant l'importance de choisir des noms qui communiquent clairement l'intention, qui sont prononçables et éviter le charabia technique. La pratique de la programmation en binôme est vantée comme un moyen plus efficace de vérifier et partager le code que les revues de code traditionnelles.
Mind Map
Frequently Asked Question
Pourquoi la Lune présente toujours la même face à la Terre ?
La Lune est tidally locked à la Terre, conservant toujours le même visage vers nous.
Quels sont les arguments contre l'idée que la Lune a été capturée par la Terre ?
La Lune a une orbite circulaire non typique pour un corps capturé, et sa composition est similaire à celle de la Terre.
Pourquoi la composition de la Lune suggère-t-elle qu'elle provient de la Terre ?
La Lune manque de fer et présente des isotopes similaires à la Terre, suggérant une origine commune.
Quelle est la théorie actuelle sur la formation de la Lune ?
Une collision avec un objet de la taille de Mars aurait pu heurter la Terre, formant un anneau de débris devenu la Lune.
Pourquoi l'orbite de la Lune change-t-elle au fil du temps ?
La Lune s'éloigne lentement de la Terre et cela pourrait prendre onze milliards d'années pour que la Terre soit verrouillée par marée à la Lune.
View more video summaries
- 00:00:15yeah okay so where did the moon come
- 00:00:17from came from the earth probably this
- 00:00:23is true although it's a little more
- 00:00:24complicated than that
- 00:00:26I remember my my grade school science
- 00:00:29book gave us some options for where the
- 00:00:32moon came from one of the options was
- 00:00:34that the moon was captured it was an
- 00:00:36asteroid that the earth simply captured
- 00:00:39with its gravity and another option was
- 00:00:42that the moon bubbled out of the earth
- 00:00:45somehow and to demonstrate that point my
- 00:00:48science book showed a picture of the
- 00:00:51Pacific Ocean with the moon superimposed
- 00:00:55on it because the moon would fit right
- 00:00:57into the Pacific Ocean so clearly that's
- 00:00:59where the moon came from the problem of
- 00:01:04figuring out where the moon came from is
- 00:01:06actually fairly complicated because the
- 00:01:08moon is a strange object first of all it
- 00:01:12does not orbit in the plane of the
- 00:01:15Earth's equator it orbits in the plane
- 00:01:18of the solar system so the angular
- 00:01:20momentum of the moon is more strongly
- 00:01:22related to the Sun than it is to the
- 00:01:24earth that argues that the moon did not
- 00:01:27come from the earth on the other hand
- 00:01:30the orbit of the moon is circular it's
- 00:01:33very difficult for a captured body to
- 00:01:36have a circular orbit any kind of
- 00:01:38captured body would have a wildly
- 00:01:39elliptical orbit so how did how did we
- 00:01:43get this circular orbit an object if it
- 00:01:47didn't come from the earth and then you
- 00:01:49look at the composition of the moon and
- 00:01:51the composition of the moon is virtually
- 00:01:53identical to the earth right down to the
- 00:01:56isotope ratios with one glaring
- 00:01:59exception which is that the moon has no
- 00:02:01iron and the earth is mostly iron
- 00:02:04so although the minerals in the moon are
- 00:02:07very similar to the earths it's missing
- 00:02:10the iron if the moon came
- 00:02:12from the earth some process must have
- 00:02:14filtered all of that iron out
- 00:02:16and finally the moon is tidally locked
- 00:02:20to the earth it keeps the same face to
- 00:02:23the earth at all times and that can only
- 00:02:25happen if the orbit started much closer
- 00:02:28to the earth and was pushed outward by a
- 00:02:31transfer of angular momentum
- 00:02:33so what could satisfy all these
- 00:02:36constraints and scientists have worked
- 00:02:39on this for a very long time and
- 00:02:40eventually about 30 years ago they came
- 00:02:42to this very interesting conclusion it
- 00:02:45turns out that in every orbit 60 degrees
- 00:02:48away there are stable points in that
- 00:02:51orbit so if you take a look at the Earth
- 00:02:53Moon system and you go 60 degrees away
- 00:02:55from the moon there's a stable point in
- 00:02:58the Earth's orbit there it's possible
- 00:03:00that another body formed a body maybe
- 00:03:05the size of Mars and the these stable
- 00:03:09points are metastable they're not
- 00:03:11perfectly stable so if you disrupt an
- 00:03:13object there it will start to slide
- 00:03:15across the orbit until it impacts you so
- 00:03:18it's possible that some object roughly
- 00:03:20the size of Mars maybe four and a half
- 00:03:23billion years ago formed at one of these
- 00:03:26stable points in the earth-sun orbit got
- 00:03:30disrupted moved in and smashed into the
- 00:03:32earth melting both bodies the two iron
- 00:03:35cores went to the center of one of the
- 00:03:37bodies and the there was a splash that
- 00:03:40splash formed a ring the splash was only
- 00:03:43the lightweight silicates none of the
- 00:03:45iron the ring coalesced into the moon
- 00:03:48and then the angular momentum coupling
- 00:03:50drove the moon away from the earth and
- 00:03:52tidally locked it circularized the orbit
- 00:03:54so that's the current current theory for
- 00:03:57what we think about the origin of the
- 00:04:00Moon wouldn't it have been fun to be
- 00:04:03there to watch an object the size of
- 00:04:07Mars smash into the earth and form a
- 00:04:12ring that turned into the moon in those
- 00:04:14days the the length of an earth day was
- 00:04:19on the order of six hours and the moon
- 00:04:22was so close it would have filled the
- 00:04:24sky and then the
- 00:04:26angular momentum coupling drove the moon
- 00:04:28farther and farther away and tidally
- 00:04:30locked the moon to the earth the earth
- 00:04:32slowed its rotation rate down to once
- 00:04:35every 24 hours that process is
- 00:04:37continuing the moon is getting farther
- 00:04:38and farther away the earth continues to
- 00:04:41slow down and it'll take about eleven
- 00:04:43billion years before the earth finally
- 00:04:45tidally locks to the moon we don't have
- 00:04:49eleven billion years left so we don't
- 00:04:51have to worry about that in any way
- 00:04:52we're not supposed to be talking about
- 00:04:53this I do want to talk about comments so
- 00:04:57a complete change of topic let's talk
- 00:05:00about comments what is the purpose of a
- 00:05:03comment to explain the code that that
- 00:05:12you can't use the code to explain yeah
- 00:05:16okay fine that's fine um the purpose of
- 00:05:19a comment is to explain the purpose of
- 00:05:24code if the code can't explain its own
- 00:05:27purpose can code explain itself well
- 00:05:32generally it can now by the way it did
- 00:05:34not used to be the case Fortran could
- 00:05:38not explain itself there was no hope one
- 00:05:41of the problems with Fortran is that
- 00:05:44does anybody remember the length of a
- 00:05:46name the maximum length of a name in
- 00:05:48Fortran six characters right you can
- 00:05:53have a name six characters long that was
- 00:05:54it and in fact most languages in the
- 00:05:57early days had a limit very much like
- 00:05:59that I once worked on an assembler that
- 00:06:01had a limit of four you can't get a lot
- 00:06:03done with four basic the original basic
- 00:06:07one letter one number that was it a 1 B
- 00:06:112 k5 those were your choices so it's
- 00:06:14very difficult to get any kind of
- 00:06:17explanation done when you've got limits
- 00:06:19like that
- 00:06:20today's languages are remarkably rich we
- 00:06:25can have names that are very long we've
- 00:06:27got structure in these languages classes
- 00:06:30and variables and methods and namespaces
- 00:06:34so we've got all these tools that we can
- 00:06:37use to write code that explains
- 00:06:40itself
- 00:06:50I found this code on the internet I
- 00:06:54don't know if it's real I have seen
- 00:06:58enough like it to know that it could be
- 00:07:02real this is bad really bad it's really
- 00:07:08really bad hack if you're an employee of
- 00:07:11inter Troad communication but I'm really
- 00:07:14really sorry that you have to maintain
- 00:07:15this I was honestly planning on removing
- 00:07:18this tomorrow but I've been known to
- 00:07:19forget things like this it happens no it
- 00:07:24doesn't not if you are a professional
- 00:07:27software developer it doesn't happen you
- 00:07:29don't put an excuse like this in the
- 00:07:31code now he might have done something
- 00:07:34stupid here I'm not I'm not worried
- 00:07:36about the stupid thing he did he was in
- 00:07:37the middle of a production crisis and he
- 00:07:39had a whole bunch of pressures on him so
- 00:07:41he did something dumb that was okay the
- 00:07:44problem I have is that he wrote this
- 00:07:46comment and made an excuse for what he
- 00:07:48did and checked it in and that's you
- 00:07:52just don't do that do not check stuff
- 00:07:55like that in so here's the thing I can't
- 00:07:58seem to figure out why the account ID
- 00:08:00variable isn't set and I've looked and
- 00:08:01look but I gotta leave now well okay he
- 00:08:04had to leave okay anyway I found that I
- 00:08:07can just grab the account ID from the
- 00:08:09debugging logs I suppose that to fix it
- 00:08:16you'd have to locate where it's clearing
- 00:08:17out the idea again I'm sorry okay so the
- 00:08:20problem I have with this comment is that
- 00:08:22it was irresponsibly checked into the
- 00:08:25production source code control system I
- 00:08:28have no real objection to what he did to
- 00:08:30fix the problem in a minimun immediate
- 00:08:32emergency that's fine but he should
- 00:08:36never have checked that and he should
- 00:08:37never have made this excuse whoever that
- 00:08:39person was and then excuses the fact
- 00:08:43that he's not going to fix it later
- 00:08:44that's a problem so I want to talk about
- 00:08:48comments and I use that as a preface but
- 00:08:51let's talk about the right and the wrong
- 00:08:54reasons to do comments nothing can be
- 00:08:57quite so helpful as a good comment I say
- 00:09:00that up front because most of what I'm
- 00:09:02going to be doing is
- 00:09:03ranting about comments so I want to make
- 00:09:06it very clear upfront that not all
- 00:09:08comments are bad some comments are great
- 00:09:13on the other hand nothing can be quite
- 00:09:16so obscuring as a bad comment comments
- 00:09:20are not pure good many of us were taught
- 00:09:26that comments were pure good we were
- 00:09:31taught in school or we were taught by
- 00:09:32early early books that everything should
- 00:09:35be commented a programmer should comment
- 00:09:38everything comment comment comment we
- 00:09:40got to the point that we measured the
- 00:09:42quality of the code by Counting the
- 00:09:45comments which by the way it's really
- 00:09:47easy to make high quality code if all
- 00:09:49you're going to do is count the comments
- 00:09:51this led to absurd kinds of comments
- 00:09:56like AI + + / / increment I the proper
- 00:10:06use of a comment is to compensate for
- 00:10:09our failure to express ourselves in code
- 00:10:13to the extent that we can express
- 00:10:16ourselves in code we do not need
- 00:10:18comments but we cannot perfectly express
- 00:10:21ourself in code and so in those cases we
- 00:10:25need comments every use of a comment
- 00:10:29represents a failure a failure to
- 00:10:33express yourself well in code this is
- 00:10:35how I want you to look at comments
- 00:10:37comments are not the kind of thing that
- 00:10:39you pat yourself on the back for entail
- 00:10:41good I was a good programmer I wrote
- 00:10:43comments every comment you write is a
- 00:10:46failure
- 00:10:47now you will fail you will fail to
- 00:10:49express yourself you will have to write
- 00:10:51comments but you should look at every
- 00:10:53comment as an unfortunate necessity not
- 00:10:59a great achievement
- 00:11:07why well one of the reasons is the
- 00:11:11comments lie
- 00:11:12now nobody intentionally write them up I
- 00:11:15guess some people do but most people
- 00:11:17don't intentionally write like comments
- 00:11:19that lie but comments degrade over time
- 00:11:23let me ask you this what color does your
- 00:11:26IDE paint your comments in green like
- 00:11:32the grass Oh gray Eve Gray's even better
- 00:11:37soft gray ignore me gray I can't hurt
- 00:11:41you great I don't bother you gray I'm
- 00:11:44just part of the background gray we
- 00:11:46paint our comments and colors that are
- 00:11:49easy to ignore because we don't want to
- 00:11:52see them we want to look at the code we
- 00:11:54don't want to read the comments comments
- 00:11:56gets in the way so we we make them fade
- 00:11:59into the background now I changed my IDE
- 00:12:01and I have my IDE paint comments in
- 00:12:05bright red big staring glaring red
- 00:12:09because I figure that if someone wrote
- 00:12:11the comment I probably ought to read it
- 00:12:13and then if I read the comment and I
- 00:12:17think it's useless I will delete it and
- 00:12:19that helps me get rid of a lot of
- 00:12:21comments comments silently rot because
- 00:12:25no one maintains them we paint them in a
- 00:12:28color we can barely see so of course we
- 00:12:30don't maintain them and no one reads the
- 00:12:33comments when they're into the code
- 00:12:34they're fiddling around in the code and
- 00:12:35they're changing stuff around in the
- 00:12:37code they don't read the comment that's
- 00:12:38up here and so eventually the comment
- 00:12:41starts to say things that have nothing
- 00:12:42to do with the actual code
- 00:12:44has anybody seen code anybody seen
- 00:12:47comments that tell you to do exactly the
- 00:12:49wrong thing you know they're 5 years old
- 00:12:52and they tell you okay this is what
- 00:12:54you've got to do when you read the codes
- 00:12:55well if I did that that would crash
- 00:12:57right but do we delete those comments do
- 00:13:01we fix those comments or do we leave
- 00:13:03them there because they're there for
- 00:13:05posterity has anybody seen a comment
- 00:13:12that is broken loose from the main body
- 00:13:15of comments and migrated down into the
- 00:13:18guts of the code
- 00:13:19probably through copy/paste operations
- 00:13:22where it hangs out there like a piece of
- 00:13:24junk DNA just a fragment of a comment
- 00:13:27hanging out there serving no purpose
- 00:13:29whatever but no one will delete it
- 00:13:33comments don't make up for bad code now
- 00:13:36here's what happens you write a bunch of
- 00:13:38code and you look at the code after
- 00:13:41you're done with anything oh this is
- 00:13:42awful and then you start to write a
- 00:13:45comment I've better comment it because
- 00:13:46this is really bad code I better comment
- 00:13:48it no you should clean it don't comment
- 00:13:51the code clean the code make the code
- 00:13:53express your intent put the effort into
- 00:13:56the code don't put the effort into the
- 00:13:58comment if you finally fail at cleaning
- 00:14:01the code then write the comment most of
- 00:14:04the time however you will find a way to
- 00:14:05make the code express itself explain
- 00:14:14yourself in code not with comments so
- 00:14:17here's an example the top says check to
- 00:14:21see if the employee is eligible for full
- 00:14:23benefits and then below that there's
- 00:14:25this horrible boolean expression what
- 00:14:27wouldn't it be nice if there were a
- 00:14:28function called is eligible for full
- 00:14:30benefits and then you could say if
- 00:14:32employee that is eligible for full
- 00:14:34benefits that reads a heck of a lot
- 00:14:35better than that comment up there so
- 00:14:39much better to use the names of
- 00:14:42functions and variables to explain the
- 00:14:44code than it is to use a comment to
- 00:14:47explain the code now I'm going to walk
- 00:14:50through a set of comments that I think
- 00:14:54are I use the word good here on the
- 00:14:57screen but that's not quite right these
- 00:14:59comments are acceptable maybe I would
- 00:15:04leave them I would not delete them and
- 00:15:06I'm gonna walk through those and then
- 00:15:08I'll walk through a batch of bad
- 00:15:09comments copyrights
- 00:15:14well okay I don't know what you're doing
- 00:15:17in the Netherlands but in the United
- 00:15:19States you've got to do this if you want
- 00:15:20to protect your code and you have to
- 00:15:23have this kind of weird punctuation so
- 00:15:25fine we will leave the boilerplate code
- 00:15:28up there
- 00:15:30here's a comment
- 00:15:32it's an informative comment and it's
- 00:15:35really an interesting one it says it
- 00:15:38returns an instance of the responder
- 00:15:41being tested and then the function here
- 00:15:44is responder instance now you could ask
- 00:15:48this question why didn't he call this
- 00:15:50responder being tested
- 00:15:52why didn't he name it better why did he
- 00:15:54have to have this comment there and the
- 00:15:56answer is that he's using a design
- 00:15:58pattern what design pattern is he using
- 00:16:03singleton yeah now you might not like
- 00:16:06the singleton pattern by the way who's
- 00:16:07got the design patterns book and read it
- 00:16:11yeah good yeah okay and the rest of you
- 00:16:15who do not have this design patterns
- 00:16:16book you must go out and buy the design
- 00:16:18patterns book and read it and understand
- 00:16:19it and then come back five years later
- 00:16:21and read it and understand it again
- 00:16:22it is probably the most important book
- 00:16:24written within the last thirty years not
- 00:16:27because it says anything new because
- 00:16:29there's nothing new in that book it's
- 00:16:32all about old stuff and what the design
- 00:16:35patterns book does is it takes a bunch
- 00:16:38of old ideas that have been around
- 00:16:40forever and are still around today and
- 00:16:41it gives them names and canonical forms
- 00:16:45so that you and I can discuss singleton
- 00:16:49or decorator or visitor or composite we
- 00:16:55can talk about those things and if you
- 00:16:56know these patterns if you know if
- 00:16:59they're in your head then I can say to
- 00:17:01you you know we could use a visitor for
- 00:17:03that and the whole design pops into your
- 00:17:05head
- 00:17:06oh yeah we could use a visitor for that
- 00:17:08and we could talk about the design at a
- 00:17:11higher level of abstraction we could
- 00:17:13discuss the patterns that we're going to
- 00:17:16use to solve a problem if you are
- 00:17:18reading code and you see the word report
- 00:17:22visitor and you know that pattern the
- 00:17:24design of that code pops into your head
- 00:17:26if the author has been faithful to the
- 00:17:28pattern the design of that code pops
- 00:17:30into your head and all of a sudden you
- 00:17:32know what to expect
- 00:17:33inside that code it's very powerful very
- 00:17:36important please know your design
- 00:17:38patterns there are folks out there
- 00:17:41nowadays who are saying design patterns
- 00:17:43were so 1995 and now things are so
- 00:17:46more modern and our languages nowadays
- 00:17:48don't need pet it's complete nonsense
- 00:17:51don't fall for that silly trick right
- 00:17:54the design patterns are good things to
- 00:17:56know and good things to have in your
- 00:17:57head what happened here is that this guy
- 00:18:01is using the singleton pattern the
- 00:18:03singleton pattern canonical form has the
- 00:18:07function using this naming pattern
- 00:18:09responder instance so he could not name
- 00:18:12it a better name and therefore he used
- 00:18:14to comment to back himself up and that
- 00:18:16was fun that's a comment that I would
- 00:18:18leave there the pattern forced him to
- 00:18:20use this name so he could not use a
- 00:18:24better name fine he can use the comment
- 00:18:27below that we see another kind of
- 00:18:30informative comment which is to tell us
- 00:18:35what this regular expression matches now
- 00:18:37regular expressions are hideous horrible
- 00:18:40syntaxes that no one understands it
- 00:18:42looks like gobbledygook alright you look
- 00:18:46at emergent expression and your eyes
- 00:18:48kind of go oh so it's a really good idea
- 00:18:52to have a comment telling you what the
- 00:18:55regular expression is matching and
- 00:18:57clearly you can see that his attempt
- 00:18:59here is to match a timestamp he's gonna
- 00:19:02match hours minutes seconds and then
- 00:19:04whatever the heck of that is e
- 00:19:06month/day/year
- 00:19:07that's probably the time zone right so
- 00:19:10okay that's fine I'm glad he told me
- 00:19:13what he expected to match but do you
- 00:19:16notice that the regular expression
- 00:19:18actually matches much more than just
- 00:19:21that his comment is lying to me the
- 00:19:24regular expression does not just match
- 00:19:26that timestamp it matches lots more than
- 00:19:29that because of these stars so two-edged
- 00:19:34sword he was informative but he also was
- 00:19:37telling me a lie
- 00:19:43here's a comment that I would leave and
- 00:19:46I love the phrasing right when
- 00:19:49programmers write comments they use such
- 00:19:51interesting grammar right we are greater
- 00:19:56because we are the right type I love
- 00:19:59this right this is the Imperial weed we
- 00:20:02are not amused so okay what he's trying
- 00:20:07to tell us here is that this is the
- 00:20:09compare to function in Java and this is
- 00:20:13the canonical form of the compare to
- 00:20:15function in Java and the first thing you
- 00:20:16check of course is that the incoming
- 00:20:18object is the right type okay well what
- 00:20:21do you return if it's the wrong type and
- 00:20:23so what he said here is look if it's the
- 00:20:26wrong type than I'm going to say that
- 00:20:28the this object is greater than the
- 00:20:31incoming object we are greater because
- 00:20:34we are the right type okay fine I could
- 00:20:36have probably phrased it better but I at
- 00:20:38least understand the intent I would
- 00:20:40leave that comment there I might improve
- 00:20:44the wording this is our best attempt to
- 00:20:52get a race condition by creating a large
- 00:20:54number of threads now you can see what
- 00:20:56he's doing here he's going to create a
- 00:20:58thousand threads and then he's going to
- 00:21:03hope to get a race condition apparently
- 00:21:06he's got some some multi-threaded thing
- 00:21:08and he's expecting some kind of race to
- 00:21:10happen this is a terrible way to make a
- 00:21:12race condition by the way if you ever
- 00:21:14want to make a race condition you don't
- 00:21:16do it this way because this will just
- 00:21:18set up a nice little resonance amongst
- 00:21:20all the threads and they won't race
- 00:21:21properly so we really want to test a
- 00:21:23race condition you have to line up the
- 00:21:25threads right at the race point with
- 00:21:27some semaphores and then release the
- 00:21:28semaphores and let the race continue so
- 00:21:30he's using the wrong strategy but at
- 00:21:34least he's telling me what he's going to
- 00:21:36do here now he could have put this into
- 00:21:38a function named attempt race condition
- 00:21:42that would have been better
- 00:21:43so I'm questionable about this comment I
- 00:21:46think he probably should have put it
- 00:21:47into a better named function and then of
- 00:21:49course he should have learned how to
- 00:21:50actually create a race condition
- 00:21:54I wrote this probably about nine or ten
- 00:22:00years ago now and the comments over here
- 00:22:04were an attempt to deal with this
- 00:22:06horrible optical illusion code is rife
- 00:22:11with optical illusions we line things up
- 00:22:14in code there are repeated patterns in
- 00:22:17code and they make your eyes twist and
- 00:22:19turn in strange ways so it's very easy
- 00:22:22to create these bits of code that are
- 00:22:24virtually impossible to see just because
- 00:22:27of the optical illusion so you look in
- 00:22:29here and holy cow you know they're all
- 00:22:32assert truths at least there's no assert
- 00:22:34false is in there and Hasen a B's and B
- 00:22:37A's and B's and what the devil is going
- 00:22:39on so I tried to explain over here what
- 00:22:43the intent of these comparisons was and
- 00:22:46it's successful on the other hand it's a
- 00:22:50double-edged sword because a reader is
- 00:22:52going to come along a person is going to
- 00:22:54come along and try and read this code
- 00:22:56and they will be drawn to the comments
- 00:22:58and they will ignore the actual code if
- 00:23:01those comments are wrong they'll never
- 00:23:04see the real code so that's a bit of a
- 00:23:07problem I have struggled with this batch
- 00:23:10of code for many years trying to figure
- 00:23:12out a way to get it to be expressive
- 00:23:14this is one of those examples where it's
- 00:23:15very hard to make this code express
- 00:23:18itself well now here the programmer is
- 00:23:25warning you
- 00:23:29don't run that test until you've got
- 00:23:32some time to kill and you can see why
- 00:23:33you know what is that a 10 million he's
- 00:23:36gonna write 10 million lines in a file
- 00:23:38well that's going to take some time and
- 00:23:41some of you may remember old j-unit you
- 00:23:45know before we had all those @ signs and
- 00:23:48Janet we the way you turned off a test
- 00:23:51in Jane it was to put an underscore in
- 00:23:53front of the name of the test and that
- 00:23:56would turn the test off so what he's
- 00:23:58telling us here is that this is a
- 00:23:59disabled test and if you turn it on you
- 00:24:02can expect some pretty hefty delays I
- 00:24:04would leave the comment in seems
- 00:24:07reasonable to me this is a comment that
- 00:24:14I encourage you sometimes wonder you
- 00:24:18know what the age was of the people who
- 00:24:23wrote the Java library and you know what
- 00:24:25were they kindergarten programmers or
- 00:24:27something because sometimes the Java
- 00:24:29library is full of code that is you know
- 00:24:31very questionable this is one of those
- 00:24:33cases simple date format is not
- 00:24:36thread-safe they've got static variables
- 00:24:38in simple date format and if you don't
- 00:24:40know this and you don't anticipate it
- 00:24:42then you're going to get caught in some
- 00:24:43kind of a concurrent update problem so I
- 00:24:46always encourage people to put that
- 00:24:48comment in and say remember that simple
- 00:24:50date format is not thread safe just as a
- 00:24:55defensive measure when I wrote this
- 00:25:02slide probably six years ago I thought
- 00:25:07to do comments for a really good idea
- 00:25:09that two new comments were brand new in
- 00:25:12IntelliJ about six or seven years ago
- 00:25:13and I thought it's so cool that you can
- 00:25:16put this tutu in there and then you can
- 00:25:18push a little button on the IDE and get
- 00:25:20a whole list of all your to-do comments
- 00:25:22that's just so cool and now I realize
- 00:25:25that the word to do means don't do I
- 00:25:29finally understand that and so now I see
- 00:25:32code set code that is just loaded with
- 00:25:34to do comments everywhere nobody ever
- 00:25:36does them and so nowadays I have a
- 00:25:39different rule for to do comments I will
- 00:25:42use two
- 00:25:43comments I will put them in but I will
- 00:25:44not check them him to do comments must
- 00:25:47either be done or deleted before I will
- 00:25:49check the code in because once you check
- 00:25:52it in it turns into it don't do so
- 00:25:55that's my new rule for to do comments I
- 00:26:02wrote this comment oh probably nine
- 00:26:08years ago I was working on a system and
- 00:26:11I I needed to put this trim in now trim
- 00:26:17is one of those functions that it occurs
- 00:26:20so often people use it so often and they
- 00:26:24use it for reasons that are ignoring
- 00:26:28because just you just call trim on all
- 00:26:31kinds of if it comes in from the outside
- 00:26:32world called trim on it so I wanted to
- 00:26:35point out that this particular trim was
- 00:26:37actually really important it was
- 00:26:39actually doing something semantic inside
- 00:26:42the algorithm so I amplified that with a
- 00:26:44comment I didn't want people to just
- 00:26:46ignore the trim
- 00:26:51do you write Java Docs now I think Java
- 00:26:57Docs are fine you know especially if you
- 00:26:59are producing an API for the outside
- 00:27:02world if you're going to be writing a
- 00:27:04whole sub nice library for the outside
- 00:27:07world of concern to consume then Java
- 00:27:09Docs are fine but inside the team if
- 00:27:13you're writing code that only the team
- 00:27:15is going to see you don't need Java Docs
- 00:27:17for that because the team ought to know
- 00:27:20the structure of the code anyway and
- 00:27:22code ought to have names that
- 00:27:24communicate pretty well so I don't like
- 00:27:27Java Docs unless they are for external
- 00:27:31api's and even then I want them to be
- 00:27:33pretty minimal okay bad comments
- 00:27:43and for this I'm going to sit the
- 00:27:47programmer here was talking to himself
- 00:27:52notice where this comment is sitting
- 00:27:54it's in the catch block that does
- 00:27:57nothing now that's weird all by itself
- 00:28:02notice what the the code is doing right
- 00:28:05he's got some file that he opens up it's
- 00:28:08a properties file oh thank you for that
- 00:28:10oh that was for me so that screen is
- 00:28:13okay yep got it
- 00:28:14he's opening up a properties file and
- 00:28:17then he's going to load the properties
- 00:28:19from some from that file and then
- 00:28:21there's this i/o exception that he
- 00:28:23catches and he says no properties files
- 00:28:26means all defaults are loaded where are
- 00:28:32all the defaults loaded how do I know
- 00:28:35that's true he says it here he asserts
- 00:28:37it that all the defaults are gonna be
- 00:28:39loaded here
- 00:28:39I don't know that true I don't see the
- 00:28:40code that loads all the defaults he's
- 00:28:42talking about some code somewhere else
- 00:28:44and here it is he's talking about some
- 00:28:47code that's in some different place I
- 00:28:49don't know if that code is still there
- 00:28:50I don't know if this is true or not this
- 00:28:53helps me not at all I understand why he
- 00:28:55did what he did he's assuming that all
- 00:28:57the defaults are gonna be loaded but I
- 00:29:00don't know that that's actually taking
- 00:29:01place so I looked at this and think well
- 00:29:04this guy was just talking to himself it
- 00:29:06was justifying why he was catching the
- 00:29:09exception and then simply ignoring it
- 00:29:11what he probably should have done here
- 00:29:13is load the default he'd loaded the
- 00:29:15defaults here then I wouldn't have to we
- 00:29:17wouldn't have to have the comment it
- 00:29:18would be very clear what was going on
- 00:29:20but he did this instead now here's a
- 00:29:27comment that is harder to read than the
- 00:29:30code utility method that returns when
- 00:29:34this stock closed is true throws an
- 00:29:35exception if the timeout is reached okay
- 00:29:38now first of all it's wrong it just it
- 00:29:41describes the way this function works
- 00:29:43but it describes it incorrectly because
- 00:29:44it there are ways to return from this
- 00:29:47this function that it doesn't talk about
- 00:29:49and second of all it's easier to read
- 00:29:51the code you know while we're not closed
- 00:29:52on the timeouts greater than zero then
- 00:29:54wait 100
- 00:29:55decrement the time out that's pretty
- 00:29:57simple to read so I think the comment is
- 00:29:59not only useless it's worse than useless
- 00:30:04this kind of stuff makes me nuts what
- 00:30:09what motivated this guy to put a comment
- 00:30:13in front of every single variable not
- 00:30:16only that they're javadocs
- 00:30:17these are Java Docs and look at them the
- 00:30:21child containers belonging to this
- 00:30:23container keyed by name now first of all
- 00:30:25the word container is a strange word and
- 00:30:28he can't seem to be can't seem to make
- 00:30:31up his mind whether he wants to use
- 00:30:32container a component so sometimes he
- 00:30:34uses component in sometimes container
- 00:30:36he always capitalizes container I don't
- 00:30:40know why it's just it's a very strange
- 00:30:43thing the child containers belonging to
- 00:30:46this container keyed by name well it's a
- 00:30:48hashmap
- 00:30:49yeah it's keyed by name and what's the
- 00:30:51name of it children okay
- 00:30:52bunch of children keyed by name I don't
- 00:30:54need the comment to tell me that the
- 00:30:56processor delay for this component
- 00:30:57background processor delay the variable
- 00:31:00name says more than the comment does
- 00:31:01life cycle support event for this
- 00:31:04component life cycle support container
- 00:31:06event listeners listeners loader
- 00:31:08implementation loader logger
- 00:31:10implementation logger all these comments
- 00:31:13are completely useless I don't know why
- 00:31:15he put them in there I believe he was
- 00:31:17motivated by some strange urge to
- 00:31:21comment everything because comments are
- 00:31:22good I would much rather those comments
- 00:31:26be ripped out of there I already talked
- 00:31:32about that one has anybody seen a coding
- 00:31:36standard that mandates comments thou
- 00:31:39shalt put comments on every function
- 00:31:42thou shalt put comments on every class
- 00:31:43this is stupid and you must find the per
- 00:31:47people who wrote those that coding
- 00:31:50standard and inform them that this is
- 00:31:52stupid
- 00:31:53because you don't want to ever mandate
- 00:31:55comments when you mandate comments you
- 00:31:58get stupid comments that's when people
- 00:32:01will write the dumbest doggone thing so
- 00:32:03here
- 00:32:08this is stupid now what really gets me
- 00:32:20about this one is that this is a Java
- 00:32:24doc right and the purpose of Java doc is
- 00:32:26to run the Java doc tool to generate the
- 00:32:28HTML that perforates the nice little
- 00:32:31webpage for you right then and if you
- 00:32:34take that comment out and run the Java
- 00:32:38doc tool it will generate virtually the
- 00:32:41identical HTML because the Java doc tool
- 00:32:46is smart enough to look at the class and
- 00:32:47and say okay title author tracks
- 00:32:50duration in minutes I would miss a
- 00:32:52couple of things I guess but almost no
- 00:32:56difference in the HTML and somebody's
- 00:32:57told him he had to put that kind of crap
- 00:32:59in there
- 00:33:12long long ago in the deep dark past when
- 00:33:18we did not have source code control
- 00:33:20systems we would put the journal
- 00:33:24comments in the first part of the source
- 00:33:26file this is something we all did but
- 00:33:29nowadays we have source code control
- 00:33:31systems what what source code control
- 00:33:33system are you using git see there get
- 00:33:37good good that's the right one ok so
- 00:33:43nowadays we can put all of our journal
- 00:33:47comments and to get we don't have to put
- 00:33:50them in to the beginning of the source
- 00:33:53file so I hope nobody is doing this and
- 00:33:56if you find any of that stuff in the
- 00:33:58source code you could probably get rid
- 00:33:59of it now
- 00:34:00what would happen if you deleted it you
- 00:34:03would still be in git
- 00:34:05you're not going to delete it right you
- 00:34:07delete stuff out of a source file it
- 00:34:09stays in git you don't have to worry
- 00:34:12about the fact I hold I'm gonna delete
- 00:34:13this stuff don't do this please don't do
- 00:34:17this
- 00:34:18it is the dumbest thing to do it's also
- 00:34:22insulting you know I know that's a
- 00:34:24default constructor I'm a Java
- 00:34:26programmer who the hell do you think
- 00:34:27you're talking to
- 00:34:34now this one this was a little bit scary
- 00:34:39and because you got that last one wrong
- 00:34:43all right little copy-paste going on
- 00:34:47here I found this one in Tomcat and I
- 00:34:55was fascinated by it because I still
- 00:34:58don't understand the comment it's been
- 00:35:00years now right
- 00:35:01does the module from the global list mod
- 00:35:05in angle brackets I don't know why the
- 00:35:07angle brackets out there depend on the
- 00:35:10subsystem we are part of I can't I can't
- 00:35:16understand that comment but I was able
- 00:35:20to use the IDE to extract out variables
- 00:35:24so at the bottom you see how I extracted
- 00:35:27out the variables I extracted out module
- 00:35:32dependencies from s module get depends
- 00:35:36subsystem so you can see that here as
- 00:35:38module that gets depends subsystem is
- 00:35:40there I extracted that out into a
- 00:35:42variable called module dependencies the
- 00:35:44IDE told me what the type was so that's
- 00:35:48cool ArrayList of module ok cool these
- 00:35:51are the modules I guess that are that we
- 00:35:54are part of maybe I don't know and then
- 00:35:56I extracted out the other part you know
- 00:35:59subsys mod get subsystem and I called
- 00:36:02that our subsystem and the IDE told me
- 00:36:05that was a module and now look at the if
- 00:36:07statement if module dependencies
- 00:36:10contains our subsystem I love the way
- 00:36:12this reads I don't know if it matches
- 00:36:15the intent of the comment because I
- 00:36:17can't still can't understand the comment
- 00:36:22don't do this it's stupid you do not
- 00:36:28have to yell and then tell me that these
- 00:36:31are instance variables right I know that
- 00:36:33their instance variables you don't have
- 00:36:35to do that sometimes it is important to
- 00:36:38yell in a comment right sometimes you
- 00:36:42want to do that sometimes you are saying
- 00:36:43something that you really want to
- 00:36:45attract attention to and then it is
- 00:36:46appropriate to put some kind
- 00:36:48you know big flag in there like this but
- 00:36:50if you use that for instance variables
- 00:36:53no one will ever pay any more attention
- 00:36:55to your big red flags so this is like
- 00:36:57the little boy who called wolf don't do
- 00:37:00that stuff it's pure clutter does
- 00:37:03anybody do that anymore
- 00:37:06the comments on the closing braces
- 00:37:09remember there was a time than our IV
- 00:37:11eat well over the time we didn't have IV
- 00:37:12ease you know in the 80s when we were
- 00:37:15when we were coding in VI with text
- 00:37:18files how did you keep track of your
- 00:37:20closing braces and this was one of the
- 00:37:22techniques but it's probably not
- 00:37:24necessary anymore
- 00:37:26this is graffiti you do not have to tell
- 00:37:33me that you were here the source code
- 00:37:36control system will remember so if we
- 00:37:39need to know who to blame we can find
- 00:37:41out of all the sins of comments
- 00:37:47commented out code is the worst of them
- 00:37:50all it is a is it an abomination before
- 00:37:53nature and nature's God when I see
- 00:37:55commented out code I don't read it I
- 00:37:58don't try to understand it I just delete
- 00:38:00it and get it out of the system
- 00:38:03commented out code is a horror now
- 00:38:06people get very upset about that you
- 00:38:08can't delete that code someone might
- 00:38:09need it one day well if they need it
- 00:38:11it's in the source code control system
- 00:38:12so they can go back and look for it
- 00:38:14there but I'm not going to tolerate it
- 00:38:16sitting in a module like this do I
- 00:38:19comment out code yes while I'm doing
- 00:38:22experiments but I will not check it in I
- 00:38:24will not check in commented out code
- 00:38:26look at this what does it mean header
- 00:38:30pause equals bite pause did it first of
- 00:38:32all is it code it's got a semicolon
- 00:38:34after it so that's a pretty good
- 00:38:35indication that it's code why is it
- 00:38:39there
- 00:38:39is there a variable called header pause
- 00:38:41I don't know Dana posi equals bypass
- 00:38:44thank you I don't know why that's there
- 00:38:46I'm going to delete it gonna get rid of
- 00:38:48it do not let put commented out code in
- 00:38:51don't check it in this is somebody who
- 00:38:57wanted to write a really pretty Java doc
- 00:39:01so we loaded it up with all kinds of
- 00:39:04HTML macros and crud like that
- 00:39:08forgetting that the place you want the
- 00:39:11comments to be most readable is in the
- 00:39:14code the fact that you've got a Java doc
- 00:39:17tool that scrapes out HTML and does a
- 00:39:19nice little print job for you is fine
- 00:39:21you can use that tool but the place you
- 00:39:26want the comments most readable is in
- 00:39:29the code so you don't want to obscure
- 00:39:31your comments with a whole bunch of HTML
- 00:39:33and make them unreadable and force
- 00:39:35people to run the Javadoc tool and then
- 00:39:37look at it on the on the web page if
- 00:39:39you're gonna write a comment make sure
- 00:39:42it's readable in the code for that
- 00:39:43reason I don't tolerate HTML in my java
- 00:39:46- no one can put HTML in a java doc I'm
- 00:39:50just to use some other mechanism to
- 00:39:52format it so I will tolerate a pre and
- 00:39:55end pre in the giant as the HTML and the
- 00:39:59Java doc which keeps all these fonts
- 00:40:01mono spaced port on which Fitness would
- 00:40:08run defaults to 8080 - it does where do
- 00:40:13I see that 8080 to where here is this
- 00:40:158080 - there's a fundamental rule about
- 00:40:17comments right you never talked about
- 00:40:19code that's somewhere else if you write
- 00:40:22a comment you only talk about the code
- 00:40:23that's right there because if you talk
- 00:40:26about code that's somewhere else that
- 00:40:27codes going to change and your comment
- 00:40:29will turn into a lie for the same reason
- 00:40:32you never put where used lists in
- 00:40:34comments because those where of used
- 00:40:36lists will change like crazy as well
- 00:40:40here's somebody who simply did not want
- 00:40:42to write the code they wanted to write
- 00:40:44about the code so they wrote a whole
- 00:40:47little essay
- 00:40:53I hope I drove the point home comments
- 00:41:00aren't bad but I don't want you write in
- 00:41:03comments by default I don't want you in
- 00:41:05the mindset of saying I've got to write
- 00:41:07a whole bunch of comments now if you're
- 00:41:10going to write a comment you need a good
- 00:41:11reason to write a comment because what
- 00:41:13you should be doing is making the codes
- 00:41:16speak for itself to the greatest extent
- 00:41:18possible and then if you fail at that
- 00:41:22well then you'll maybe you'll have to
- 00:41:24write a comment
- 00:41:44how long should a source file be how
- 00:41:51many lines should there be in a source
- 00:41:52file this is not an easy question to
- 00:41:57answer it's not like you know the the
- 00:41:59one thing the one rule principle it's
- 00:42:03not like that this is something else
- 00:42:05so so I did a little study
- 00:42:08I took seven projects that I found off
- 00:42:10the off the internet and I analyzed
- 00:42:14their file sizes and I found something
- 00:42:16really interesting so here's the the
- 00:42:19seven projects one of them is j-unit
- 00:42:21this was probably 2001 mm maybe 2005
- 00:42:26when I did this and notice that Janet is
- 00:42:30like 6,000 lines of code and the the
- 00:42:37plot here is called a box plot so this
- 00:42:40is the smallest file this is the biggest
- 00:42:43file this is one standard deviation and
- 00:42:47the mean is right in the middle right so
- 00:42:50j-unit which is about 6,000 lines of
- 00:42:53code has a whole bunch of modules inside
- 00:42:56it but those modules the smallest module
- 00:42:58is four lines of code the biggest line
- 00:43:02module is two hundred three hundred four
- 00:43:04five hundred lines of code
- 00:43:06the average module is around 20 30 40 50
- 00:43:11lines of code and most of them are
- 00:43:14between 130 that's that's pretty good
- 00:43:18that's pretty standard notice there's a
- 00:43:20log scale here all right so as you move
- 00:43:23up it changes a lot Fitness this is
- 00:43:27another project it's about 50 thousand
- 00:43:30lines of code so much larger than j-unit
- 00:43:32but almost an identical structure this
- 00:43:36is interesting because you would expect
- 00:43:37that large projects would have a
- 00:43:39different structure they'd have a
- 00:43:40different different statistical array of
- 00:43:43file sizes but that does not appear to
- 00:43:45be the case
- 00:43:45Fitness much larger than Jane it has the
- 00:43:48same structure test ng sng is a tool
- 00:43:53similar to j-unit it's because 72
- 00:43:56thousand lines of
- 00:43:57which makes you wonder what the hell it
- 00:43:59does and it has a wildly different
- 00:44:04structure interesting and so I'm not
- 00:44:07quite sure what that's about but the you
- 00:44:09know there's a big file in there looks
- 00:44:11like 1500 lines of code
- 00:44:13the average is like 40 lines of code but
- 00:44:17there's a huge standard deviation so
- 00:44:19he's got no regularity to the size of
- 00:44:21the files time and money
- 00:44:24that's about 6,000 lines of code that's
- 00:44:26Eric Evans example of domain driven
- 00:44:28design very similar structure to fitness
- 00:44:32and J in it
- 00:44:34J depends JD Ben is really old it was
- 00:44:37probably written in 98 about 7,000 lines
- 00:44:40of code and a slightly different
- 00:44:42structure looks like his average file
- 00:44:44size is about 120 he's got a couple of
- 00:44:48big files in there although they're not
- 00:44:49really big it's just that the average
- 00:44:51seems to be larger than usual and I
- 00:44:53think the reason for that is that he
- 00:44:55Javadoc everything and that just drove
- 00:44:58his file sizes up a little bit aunt
- 00:45:03200,000 lines of code and aunt
- 00:45:06the average file of sizes around 200
- 00:45:09lines that's pretty big he's got a
- 00:45:11couple of big files in there is one
- 00:45:13that's 2,000 lines long Tomcat 384
- 00:45:17thousand lines of code look at that man
- 00:45:182 average size is about 200 lines he's
- 00:45:22got a file in there with 5,000 lines in
- 00:45:26it now what I find interesting about
- 00:45:30this is that there's no correlation
- 00:45:32between file size and distribution right
- 00:45:38you've got a couple of big ones a small
- 00:45:40one here and a big one here and another
- 00:45:42small one there and a slightly larger
- 00:45:44one there I'll have roughly the same
- 00:45:46correlation we've got really big ones
- 00:45:47out here and a small one here that you
- 00:45:50know there's no correlation there so
- 00:45:53what that tells me is that file size is
- 00:45:58not a function of project size file size
- 00:46:01is a style that you can impose upon your
- 00:46:04system and since that's true what style
- 00:46:07would you like to impose on your system
- 00:46:10how big should
- 00:46:11your files be and it looks here like you
- 00:46:14can build a fairly significant system
- 00:46:16with files that are on the average 50
- 00:46:19lines long most of them are less than a
- 00:46:21hundred lines
- 00:46:22that's probably nice so that might be a
- 00:46:26a goal to strive for I'll come back to
- 00:46:39that stuff because I want to do another
- 00:46:42statistical analysis this one this is an
- 00:46:45interesting analysis of the length of
- 00:46:47lines same projects but the length of
- 00:46:51the lines in the projects and look at
- 00:46:54the interesting correlation here every
- 00:46:56color is a different project but look at
- 00:46:59how they follow the same interesting
- 00:47:00curve now this is a histogram
- 00:47:03it's a histogram of line length so this
- 00:47:06is the number of these this is a line
- 00:47:08length down here and the vertical scale
- 00:47:11is the percentage of lines that have
- 00:47:13that length and notice that the vertical
- 00:47:15scale is a log scale so there's a lot of
- 00:47:20lines that have no length these are
- 00:47:23probably blank lines and and then they
- 00:47:26fall off pretty rapidly to a minimum
- 00:47:28here of about 12 and then there's this
- 00:47:32gradual climb look at the look at how
- 00:47:34tight that's clustered these seven
- 00:47:37wildly different java applications but
- 00:47:40really tight clustering right in here
- 00:47:42with a peak and they all peak about the
- 00:47:44same place right around 3035 something
- 00:47:47like that lines of code where 1% of the
- 00:47:49lines of code now are at this level and
- 00:47:52then it starts to fall off and this
- 00:47:54looks like a slow fall off but this is a
- 00:47:55log scale so it's actually a very rapid
- 00:47:57fall off you get to about here and
- 00:48:00realized that nobody wants to see any
- 00:48:02lines that are longer than about 80 so
- 00:48:05this is the interesting part of that
- 00:48:07curve and what I find fascinating about
- 00:48:10it is that it's that all seven of these
- 00:48:13projects follow the same curve so it
- 00:48:16seems that we have a preference for
- 00:48:19lines that are on the order of 30 to 40
- 00:48:23line 40 characters long
- 00:48:25is that a change in well so that's
- 00:48:34interesting yes lines the screens are
- 00:48:36getting bigger is that having an effect
- 00:48:39on our line lengths and this argues that
- 00:48:42it's not because look at how look at how
- 00:48:44small that is that Peaks it's in there
- 00:48:46right around 40 35 40 right so the fact
- 00:48:51that our screens are getting bigger does
- 00:48:52not seem to be affecting the
- 00:48:55distribution of line lengths I find that
- 00:48:57to be very interesting now what does it
- 00:48:59tell us about what kind of guidance does
- 00:49:01it give us well I think the guidance is
- 00:49:03pretty obvious you want your lines to be
- 00:49:05on the order of 30 or 40 characters long
- 00:49:07you don't want very many that go beyond
- 00:49:09maybe 80 I actually have a a barrier put
- 00:49:13into my IDE at 150
- 00:49:16I will not go beyond 150 and I do that
- 00:49:19because I believe it is rude to make
- 00:49:22your readers scroll to the right so all
- 00:49:26the codes should fit on the screen and
- 00:49:30you should not make your your readers
- 00:49:32scroll to the right if you make them
- 00:49:34scroll to the right they won't and then
- 00:49:36you can hide all kinds of crap out there
- 00:49:38yeah
- 00:49:50do they have different functions yeah I
- 00:49:55don't know I didn't do that analysis
- 00:49:57it's an interesting interesting thing to
- 00:49:58do we have an awful lot of code now so
- 00:50:01there's very interesting analyses that
- 00:50:03we can do like this but I didn't do that
- 00:50:05one anybody else have a question on this
- 00:50:08good ok
- 00:50:17show them let's talk about names
- 00:50:27and see where's our names that goes to
- 00:50:32about here there we go
- 00:50:45early in programming we didn't have a
- 00:50:47lot of options with names like I told
- 00:50:49you before we were limited to like six
- 00:50:50characters nowadays that limitation is
- 00:50:53long gone so we can have names that are
- 00:50:55as long as we want and we name things we
- 00:50:58do a lot of naming in software we named
- 00:51:00files and directories and programs and
- 00:51:02classes and namespace and variables and
- 00:51:04arguments we name all kinds of things
- 00:51:06and because we do so much of it we
- 00:51:08probably ought to be good at it so let's
- 00:51:10talk about some rules for naming things
- 00:51:14with the the rules that I'm going to
- 00:51:17show you here are old they've been
- 00:51:20around for a very long time they're
- 00:51:22derived from Tim Ottinger 's list of
- 00:51:26naming rules that has been very popular
- 00:51:28circled around the internet for years
- 00:51:31very obviously you want to reveal your
- 00:51:34intent in a name so I'm going to give
- 00:51:36you a couple of examples here is int D a
- 00:51:40good name for a elapsed time in days now
- 00:51:45your original thought would be well no
- 00:51:46of course not because D is just a one
- 00:51:48letter name that's awful but wait a
- 00:51:51minute
- 00:51:52how long should a variable name be
- 00:51:57what's the rule for the length of a
- 00:52:00variable name now consider the for loop
- 00:52:03for I equals 0 I less than 10 I plus
- 00:52:06plus do you want that eye to be
- 00:52:07something other than I the answer that
- 00:52:11is probably not and so there's there
- 00:52:14does seem to be a place for single
- 00:52:16letter variable names like I so what's
- 00:52:19the rule what is the rule for the length
- 00:52:22of a variable name and here's the rule
- 00:52:24that I use a variable name should be
- 00:52:28proportional to the size of the scope
- 00:52:31that contains it if the scope is very
- 00:52:35small like one line a single letter is
- 00:52:38fine you don't want to have anything
- 00:52:39else if it's a one line scope you don't
- 00:52:42want to have anything else a single
- 00:52:43letter is great D would be a perfectly
- 00:52:46valid name for a date
- 00:52:47if D existed only in a single line
- 00:52:51because you wouldn't lose the context
- 00:52:53you wouldn't need the name to remind you
- 00:52:55of anything the function call that
- 00:52:57generated the name would
- 00:52:59enough long scopes need long names so
- 00:53:04let's walk through the hierarchy here
- 00:53:06inside of a an if statement you've got
- 00:53:10maybe a couple of lines in that if
- 00:53:12statement variables inside that if
- 00:53:14statement ought to be very short
- 00:53:16variables inside of a really tiny while
- 00:53:18loop should be very short if you have a
- 00:53:20function and that function is four lines
- 00:53:23long the variables inside that function
- 00:53:26should probably be pretty short because
- 00:53:29it's four lines long maybe they'd have
- 00:53:30to be a little bit longer arguments
- 00:53:32would probably be a little bit longer a
- 00:53:34word would probably be good for an
- 00:53:36argument instance variables live inside
- 00:53:39a class they have a slightly longer
- 00:53:41scope they have the scope of the class
- 00:53:43so probably an instance variable should
- 00:53:45be long ish two words may be functions
- 00:53:50the arguments to a member function
- 00:53:52probably a word global functions mobile
- 00:53:58functions have a huge scope they better
- 00:54:00be very long got global variables sorry
- 00:54:02global variables have a huge scope so
- 00:54:04they should probably be very long
- 00:54:10variables should have a length
- 00:54:13proportional to the scope that contains
- 00:54:15them what's the rule for functions
- 00:54:19exactly the opposite
- 00:54:22exactly the opposite the bigger the
- 00:54:24scope the smaller the name for a
- 00:54:26function and for very obvious reasons we
- 00:54:29would not want to call the open function
- 00:54:32if the name of the open function was
- 00:54:34open file and throw exception if not
- 00:54:36found as ace as the scope of the
- 00:54:41function gets larger we want the name to
- 00:54:44shrink we want the name to shrink
- 00:54:46because we're going to call it more a
- 00:54:48function that lives in a large scope
- 00:54:50will be called from all over the place
- 00:54:51so we want to shrink the name down
- 00:54:53moreover if the function is in a large
- 00:54:56scope it must be abstract it must be
- 00:54:59dealing with a high level abstraction so
- 00:55:01we want the name to be short as the
- 00:55:05scope containing a function decreases
- 00:55:08the name starts to get longer so the
- 00:55:11instance variance
- 00:55:12since methods of a class will probably
- 00:55:15have slightly longer names private
- 00:55:17functions called by public functions
- 00:55:20will have even longer names private
- 00:55:22functions called by private functions
- 00:55:24will have even longer names you can
- 00:55:27continue down that hierarchy for a very
- 00:55:29long time especially if you're
- 00:55:30extracting until you drop as you'll
- 00:55:32extract and extract and extract and all
- 00:55:34these extracted functions are going to
- 00:55:35be private and every time you go down
- 00:55:37another level the name gets longer and
- 00:55:39longer and longer and it gets longer
- 00:55:41because the function becomes more
- 00:55:43precise it does something really tiny
- 00:55:45really precise that you need words to
- 00:55:48specify so the name of a function is
- 00:55:53inversely proportional to the size of
- 00:55:56the scope that contains it
- 00:55:59what about classes same as functions
- 00:56:03size of a class name is inversely
- 00:56:06proportional to the size of the scope
- 00:56:08that contains it classes at the global
- 00:56:10scope have one word names derived
- 00:56:12classes have multiple word names inner
- 00:56:14classes have multiple word names as the
- 00:56:17scope shrinks the name grows so that's a
- 00:56:21reasonable rule for naming things or at
- 00:56:25least controlling name length therefore
- 00:56:28if d is not necessarily a bad name for
- 00:56:31this variable as long as the scope that
- 00:56:33it was contained in was small if the
- 00:56:35scope was long than elapsed time in days
- 00:56:37is perfectly reasonable what does that
- 00:56:42function do take a look at it
- 00:56:46spend a little time
- 00:56:54it gets them what does it get well it
- 00:57:01walks through a list and it interrogate
- 00:57:04the first element of each list and if
- 00:57:06that first element is a four then it
- 00:57:10adds it to the output list list one
- 00:57:12which it returns excellent good now we
- 00:57:15know what this does it scans a list for
- 00:57:20first elements four elements whose first
- 00:57:24element is a four and then it returns
- 00:57:27only those lists that have that okay
- 00:57:30good I think I got that right that's the
- 00:57:33same code but this changes everything
- 00:57:41the the names here tell you what's going
- 00:57:45on inside this function it's getting all
- 00:57:46the flagged cells oh there are cells and
- 00:57:49the cells are part of a game board and
- 00:57:52every cell has a status value and if
- 00:57:54that status value is flagged then we're
- 00:57:57going to return that cell so this
- 00:57:59function returns all the flagged cells
- 00:58:02now notice what this nice naming system
- 00:58:05does this naming system does more than
- 00:58:07just make it easier to understand the
- 00:58:09function it also tells you what program
- 00:58:11contains the function this program is in
- 00:58:14some kind of a game this function is in
- 00:58:16some kind of a game probably minesweeper
- 00:58:19if you know minesweeper of course nobody
- 00:58:21knows minesweeper anyway because nobody
- 00:58:23has that old desktop accessory from
- 00:58:25Windows anymore but okay a good system
- 00:58:31of names tells you not just about the
- 00:58:34function you're working on but it tells
- 00:58:36you about the entire context of the
- 00:58:38system so that's the power of a good
- 00:58:41system of names
- 00:58:47let's see you gotta watch out for this
- 00:58:50what's the first place where these two
- 00:58:53variables differ that some of you
- 00:59:03probably got fooled by that this is
- 00:59:06really hard to see code is full of
- 00:59:08optical illusions and you have to be
- 00:59:09very you have to be cognizant of the
- 00:59:11fact you have to be aware of the fact
- 00:59:13that code can contain optical illusions
- 00:59:16and you have to fight against that this
- 00:59:18is really hard to see you're gonna be
- 00:59:20looking at here especially because of
- 00:59:23this and you also have to worry about
- 00:59:26stuff like this
- 00:59:27our modern ideas have gotten pretty good
- 00:59:30at disambiguating 1 and L and 0 and O P
- 00:59:35and Q but sometimes these letters and
- 00:59:38symbols differ by one pixel so you have
- 00:59:42to be careful about that sometimes it's
- 00:59:43hard to just differentiate them do you
- 00:59:50use prefixes any more like za and uh I
- 00:59:56used to use this this of this convention
- 01:00:00all of my local variables began with ah
- 01:00:04the indefinite article here all of the
- 01:00:08arguments to a function began with the
- 01:00:11and all instance variables began with I
- 01:00:15TS it's so its name it's date I used to
- 01:00:21use that all the time I've stopped using
- 01:00:23that now I used to use it in the 80s and
- 01:00:25in the 90s but I've stopped using those
- 01:00:27prefixes now because the IDS are just so
- 01:00:30good at telling me that that's an
- 01:00:32instance variable or a function I can
- 01:00:34hover over it so I've kind of dropped
- 01:00:37the whole prefix I'd be in that days
- 01:00:41it's probably not a good idea to use
- 01:00:43numbers series like this sometimes you
- 01:00:46have to fall back on it but it's better
- 01:00:47to give them reasonable names number
- 01:00:49series aren't a great idea watch out for
- 01:00:53noise words data and info David first of
- 01:00:59all I want should realize date
- 01:01:01is completely redundant right of course
- 01:01:03it's data you don't need to say it's
- 01:01:05data okay product and now here what is
- 01:01:08the difference between product and
- 01:01:09product data is there some is there does
- 01:01:13this tell you anything about what what
- 01:01:15is different about it there's a type in
- 01:01:17the system called product there's
- 01:01:19another type in the system called
- 01:01:20product data there's yet another type in
- 01:01:22the system called product info what is
- 01:01:24the difference between them and these
- 01:01:27type names don't tell you the difference
- 01:01:28so that's a problem
- 01:01:29those are noise words I'm not going to
- 01:01:33talk about that because I'm going to
- 01:01:34come back to it later this I found in a
- 01:01:37real application and I find it scary as
- 01:01:41hell
- 01:01:41the the first line says there's a
- 01:01:44function that will return the active
- 01:01:47account get active account that's great
- 01:01:49and it returns an account good but now
- 01:01:51look at the next function get active
- 01:01:53accounts this is in the same system now
- 01:01:57that begs the question what the heck is
- 01:01:59that first one returning this there can
- 01:02:03be more than one active account so
- 01:02:06what's the first one returning the third
- 01:02:08function is even scarier get active
- 01:02:11account info but it returns the list of
- 01:02:13accounts so these are not well
- 01:02:19disambiguated names the names are not
- 01:02:21telling you what's going on here and you
- 01:02:24look at them and go what that's not good
- 01:02:28that's a WTF make sure your names are
- 01:02:31pronounceable cooker three 'can't busy
- 01:02:38name jigna minam's
- 01:02:43now you can see why the last one
- 01:02:45especially you can see why you used this
- 01:02:47name it's the generation timestamp your
- 01:02:49month day hour minute second makes
- 01:02:51perfect sense but you can't say it now
- 01:02:56you know you guys pair program but any
- 01:03:00pair of programmers out there anybody
- 01:03:02repair how often do you pair program not
- 01:03:04very many people doing it why not it's a
- 01:03:09good idea
- 01:03:09pairing allows you to share knowledge do
- 01:03:12you do code reviews who does code
- 01:03:13reviews
- 01:03:14ooh everybody has code reviews code news
- 01:03:18a very inefficient pairing is very
- 01:03:19efficient right how much how much time
- 01:03:23should you spend on a code review let's
- 01:03:28say that the original author required
- 01:03:31five hours to write the module how long
- 01:03:33should it take to review that module
- 01:03:36well some function of five hours maybe
- 01:03:39not exactly five hours but some
- 01:03:41significant function of five hours
- 01:03:43because if you're going to review the
- 01:03:45module you need to walk through the
- 01:03:47reasoning that the author originally
- 01:03:48went through now hopefully the author
- 01:03:50made that easy for you by refactoring it
- 01:03:53and cleaning up the code so fine but
- 01:03:56it's still going to take you some fairly
- 01:03:58significant amount of time to review
- 01:04:00that module if you if you review a
- 01:04:03module that took five hours to write and
- 01:04:06you review it in 15 minutes
- 01:04:08all you've done is look for semicolons
- 01:04:11so all you've done right and maybe it's
- 01:04:13some indenting and maybe a few naming
- 01:04:15conventions but you haven't done
- 01:04:16anything significant you haven't
- 01:04:19actually reviewed the code so you should
- 01:04:21look at code reviews as requiring
- 01:04:24roughly the amount of time it took to
- 01:04:27write the module well okay if you're
- 01:04:30going to spend that much time reviewing
- 01:04:32the code why wouldn't you pair pairing
- 01:04:34takes about the same amount of time but
- 01:04:36when you're pairing you are actively
- 01:04:39involved you are not passively reviewing
- 01:04:42you are authoring there's a much better
- 01:04:45way to share knowledge and contribute to
- 01:04:48a team yeah
- 01:05:02so the question was about noise words
- 01:05:06because he didn't want to talk about
- 01:05:07pairing the word exception appears in
- 01:05:16lots of different types in Java
- 01:05:18an illegal state exception illegal
- 01:05:20argument exception nullpointerexception
- 01:05:22so on does the word exception constitute
- 01:05:26a noise word in that case no it doesn't
- 01:05:28it's the it's the noun that is the base
- 01:05:31class they all derive from exception and
- 01:05:34then the derived classes have to add a
- 01:05:36word on to that to describe what kind of
- 01:05:38exception they are so that's not a noise
- 01:05:40word and and it is not noisy also
- 01:05:43because it tells you it's an exception
- 01:05:45all right now back to this pairing thing
- 01:05:47thank you for that we have to go out for
- 01:05:51lunch
- 01:05:51oh yeah it's 12:30 isn't it its feet is
- 01:05:54what I'm talking about pairing even
- 01:05:55though I don't yeah
- Lune
- origine
- théorie d'impact
- orbite circulaire
- composition
- fer
- stabilisation par marée