Clean Coders Hate What Happens to Your Code When You Use These Enterprise Programming Tricks

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

TLDW?

👍︎︎ 1 👤︎︎ u/MauranKilom 📅︎︎ Sep 01 2017 đź—«︎ replies
Captions
so often you get to say good morning good evening whilst doing talks this is the third talk my third talk today so obviously it's the one with the longest title actually if you look at the talks that I've done today they titles have got progressively longer so good job we don't have a late evening talk so this is actually is probably the longest talk title I've ever had and yes I did actually go to BuzzFeed to just check the fonts on the colors and go just to make sure I have that like air of authenticity what can I say about myself and two books with long titles another book with a long title and some other stuff these books have nothing to do with me it turns out that in addition to being interested in patents coding techniques software craftsmanship and taking photographs of books I also have an interest in words and language and I have a page on Facebook word Friday where I present various unusual bits and pieces and acquaintance of interest so if you language is a thing that you are interested in and then I recommend this so given that context I'm going to explore the word Enterprise from moment because I've used it in the title and in fact we use it everywhere these days we start talking about enterprise scale and is that ready for the enterprise and a system system can barely go by without somebody throwing the word enterprise in its direction now there is a small problem well I don't think it is a problem but if you are of a particular disposition and possibly even a particular generation in spite of the onslaught of the word enterprise in software development over the last two decades I still think of this you know somebody says enterprise enterprise software and I'm thinking ok yeah running a starship but warp speed that's going to take some serious horsepower and you're going to need some major CPU work here and I want to know what languages they use then what's the runtime there's lots of interesting things there and yeah it's an elegant and it's an elegant design the reboot especially so and indeed it does turn out that you'll look at if you look at this drawing there's a NASA logo lurking in the background somewhere this is an assets impression of what a warp drive craft would actually look like and it is actually based on some real science the those are warped rings warp coils and this is based on a theory this be explored by a NASA by a NASA team obviously it's in the highly speculative department Sonny white is exploring potential applications of what is known as Alcubierre Drive Miguel Alcubierre a Mexican physicist who in 1994 demonstrated that general relativity actually does accommodate the concept of what we would call warp drive and without violating that that awkward speed limit the speed of light you cannot travel faster than speed than the speed of light in a vacuum we can work around that with software developers we know about work around it turns out that this is not an obstacle all you need to do is move space-time move the vacuum faster than the speed of light and you're good yeah you will be traveling in the vacuum at a slower speed and that's great and basically you warp space-time that's what those coils are for they contain exotic matter this is what we would call a technical challenge we don't actually know that that exists estimates vary but it is entirely possible the amount of exotic matter you would need to warp space in this fashion is approximately the mass of the moon I think that is known as a implementation detail and we're going to map it out on our storyboard and just see how we go why am i introducing this stuff well one it's the end of the day - good nerdy fun and three what I want to talk about is the reality distortion field that is the enterprise we do warp space and time and the very substance of reality on a regular basis so I'm plundering the dictionaries we are told that enterprise is a project or undertaking that is especially bold complicated or arduous yeah complicated largest that can't let sir that's a enterprise polite way of saying all this is going to be tricky it's going to be a lot of work readiness to engage in undertakings of difficulty risk danger or daring you know it's just that yeah we've got this budget oh this is going to be interesting oh you need to deliver it by then yep okay I dare you a design of which I love the wording I cannot remember I'm going to suspect this is the Oxford English Dictionary given the phrasing a design of which the execution is attempted nothing about achievement nothing about completion we'll just try it and there's a bunch of other ones that are really boring so we're not interested in those so um there's also least the question of what we mean by code if I'm talking about Enterprise coding clearly I need to have a reasonable definition of code a set of instructions for a computer I think we're all reasonably comfortable with that they compute a program or a portion thereof yeah likewise now it gets interesting a system of words figures or symbols used to represent others especially for the purposes of secrecy yes this is enterprise code now we have a satisfactory definition also a set of conventions or principles governing behavior or activity in a particular domain there's a concept of a code of conduct concept of code as a set of conventions and practices or even a definition of a culture by which we participate in our daily activity so there's also a lot going on here so let's talk about some of these implied practices and here is an interview if you follow the practical dev on Twitter but these have been doing the rounds these kind of really these fake O'Reilly book covers they are they are very funny in that kind of like there's so much truth in that it hurts taking on needless dependencies fragile developing guard yeah code written by some stranger on the Internet is always perfect and we have a beautiful example of this about ten months ago in fact one developer just broke no Babel and thousands of projects in eleven lines javascript this was a little piece of code left pad it turns out that lots of libraries had a transitive dependency on this little piece of code and for various reasons the author pulled them for all npm and lots of websites stopped working now clearly this little piece of code is very very important i mean you know it must be really really important it must be must be something super clever because otherwise why would you be reusing it because that is one of the reasons people often give this is the reason we have code reuse we reuse code because somebody else has done the work and it is tricky or daring and you know what we don't want to reinvent the wheel and really that code has stood the test of time let's not look at the code this is JavaScript which I will happily say is not my favorite language this is the code and it seems a little bit clunky it seems a little bit of Rasik and what's it doing it's um left pad yes it's actually filling out a string it's padding the string for this thousands of websites went down it is padding a string because it turns out there it turns out sometimes when I have a casual discussions with people about the use of the word developer and versus the word programmer and I know some actually well I use the word developer I'm actually a strong advocate of the word programmer because I've had people sort of say to me wow you know programmer just makes it sound like you're a coder and I said well I don't think that's really true I mean developer makes it sound like you're involved in real estate you know so you know they both have potential negatives but the thing is that I have encountered the deeper truth in all of this developer there's a lot of developers you can't program it's a shameful confession it turns out that programming is a lot harder than developing and this is this is one of the demonstrations so what I found absolutely fascinating was that NPM went and put up a version that somebody else is now maintaining now I would have presumed that somebody who's maintaining this piece of code is going to do something visually clever and really tidy is up because I mean there's a unfortunately forced to follow the rather curious semantics that this thing has in terms of its defaults but you'd think they're going to do something really clever instead they did this there's a cache up there that is actually if you look at it is going to be slower than actually running the code there's lots of comments just to help you in case you can't understand all of this and it is a and we don't use plus equals because obviously equals something equals something plus something is what this is written by somebody who I think it has got their heart in the right place but their brain was out of the room at the time you know we have a lot of newbie programmers and it's a shame that this is now the standard version absol I'd have a go at it as I say not a Java program a JavaScript programmer so this is my version which is shorter it's direct it even accounts for the weirdness this also has a very interesting property it works I had I know this stuff because I wrote some test for it so I created this ad hoc testing framework and a bunch of assertions is assertions that's the testing framework and that's what happens when you run it against my code these are the expectations you would have a somebody described less pad to you if I run it against the versions they're actually maintained at NPM and three tests fail so yeah alternative you could use a real language like Python which actually has this kind of built in and that doesn't have the ambiguity you've got a choice there between zero filling and write justifying correctly building two languages Java Script will have such a padding function you know I don't think two decades is too long to wait for such a thing but there is this issue about how we relate to code and this kind of stuff it doesn't just pop up on the internet what we see is in large organizations relatively simple programming tasks are well people bring a mindset to them and it's not that people are stupid it turns out programmers are surprisingly smart it is just that sometimes when you approach the problem from the wrong angle you can really make something of it uses Buzz earlier so that's that's so let's go let's go back to first bus what looking at people solving this problem it's absolutely fascinating because we people often think I was fairly trivial I was when I started really looking at fizzbuzz I've been dismissive of it before that's right four years ago now I started looking at him gosh you know what there's some fun to be had here so I thought let us consult the great sink of all human knowledge Wikipedia and see what it has to say on the subject apparently I discovered at that point is a group word game for children to teach them about division that's certainly not how I learnt it when I was a teenager and there's a whole description of how to play it we highlight the things of interest in account numbers 1 2 3 except any number divisible by 3 is fear so 1 2 fears for buzz 5 fears and so on 14 is buzz 16 and then so and the idea there is that you are doing a substitution and conditional substitution it's not the hardest thing in the world but actually the thing that drew me and this is the great thing there's these little gems these little nuggets buried in Wikipedia just like they're buried in enterprise comments it is the use of word dense force I mean perhaps after whoever wrote that piece that is archaic it is only occasionally used in poetry it kind of left mainstream English around the 17th or 18th century it would be unusual to find it even in Victorian England so you know well done that is probably the most interesting thing about that article apart from the fact that it then goes on to say adults may play fizzbuzz as a drinking game this is where I learnt it we're making a mistake leads to the player having to make a drinking related forfeit and really I'm going to say you do need to have alcohol to make this interesting well we tried playing it as a family we're on holiday about a year ago and so that's me my wife and the two boys at the time my young my younger one was 10 but he really likes his numbers he's got a good relationship with mathematics so we decided to play this while waiting for our food and our wine which means the adults were shockingly sober at this point and we can all do our times tables and I don't know I think we got about 50 before we gave up it's just like we are sober and we can all do this this is dull you need alcohol to make it interesting the best bit about the Wikipedia article though citation needed yeah there's a lot of people who won't remember that they had a drinking forfeit every time they got it wrong but you know there it is now there's a deeper reality that what we find is that it's been used as an interview question for many programmers and and we kind of assume that everybody is able to do this stuff and we have it will generally tend toward something like this so here it is in in Python and there's a kind of a sort of a simple ways of doing it for example this is the accumulation model we recognize that there is a divisible by three a divisible by five we accumulate into a result but we check did we get anything if we didn't we're going to go with a number alternatively we have a basically a tail return approach where we check each case explicitly and there's no RIA cumulation of state but actually we are still testing something twice that and mod fifteen is effectively n mod three and in more five so we're always stopping we're either rechecking something or stopping to recheck something and it's there's something here that is quite interesting of course we can take the fully and so there's nothing exciting if you just want to solve it to get something going but if you're going to do this in a in a truly scalable solve no more than you need simplest solution works all this kind of stuff you may end up with this approach and you can end up with this if you do TDD horribly wrong we'll test the one case now we'll test the two case now with tests for three case you'll see the progression you're getting paid by the line of code well done good job so you know there are some fairly crazy ways of doing it crazier than this still might still my favorite and it gets embellished and added to over the years is the fizzbuzz Enterprise Edition which I did have to sell somebody know this really is a spoof fought a spoof site because somebody was people are not always sure you know it's just like is this a joke or is this not because we've had so much experience of enterprise software but sometimes you think this is funny but actually no really isn't it could actually be real enterprise software marks a special high grade class of software you can always feel the scare quotes coming in a software that makes careful use of relevant software architecture design principles to Bill's particularly customizable and extensible solutions to real problems which of course fizzbuzz it's just crying out for parameterization configurability you know sir this project is an example how the popular fizzbuzz game might well be built aware it's subject to high quality standards of enterprise software and here I'm just going to just I'll just go to this yeah just look at the the past name on that and get hub and it's just right here the some of the factories because yeah you know that's that's how you doing real software development you've got factories that's a you know and there's above strategy factory there's a buzz string printer factory there's a buzz string returner factory I I was slightly disappointed not see any factory factories but you know we've got integer integer string returner factories which I thought was quite good and don't have any don't have any loops because we've got loop component factories and it's just you know this is the kind of thing that you could hang up in in an art gallery you know it's truly a work of art you aren't hanging up in an art gallery or you get a large budget for it so it's at this point that we can kind of take a bit of wisdom from Paul Anderson I have yet to see any problem however complicated which when you looked at it in the right way did not become still more complicated okay this is almost a manifesto for software development sometimes you feel that there's a approach that sometimes people take they don't know they're doing it and this is the thing how do you create a large problem you give people the mindset that it's going to be big if you tell people this is going to be six months worth of work they will pack it full to the six months and beyond if you give them the same task in 18 months they will more than lies for that challenge and overrun the project okay if you take a ten people to solve a task I'm pretty sure that ten people be able to do it but they will also be able to do you also be able to do with a hundred people and what you get out of that hundred people will be not too similar to this okay sorry I thought it's sort of a tangential experience of one that was actually a ten versus 100 system it was done in two different parts of the country and being a true enterprise one passed one part of the organization didn't know the other part was producing identical software and they didn't know it was a big problem so they had ten developers work on it in under a year whereas the other guys clearly thought that it took a hundred people three years to do the same thing you know one of those guys doing ten people you know delivering early doing have they have no idea of the complexity they have not looked at it closely enough so it is this sort of Anderson approach that we were able to take we can mess about I mean there's a lot of funion you feel you want to throw a lamdaur at it and you know you can do that you can make it even shorter in Python this is one of the shortest that I know of I've not been able to get it I'm able to get it to know apart from knocking out whitespace that's about as short as I've been able to get it fits in a tweet which I write which I rather like but it also there is a case where we can also become too clever in a different way and there's a a rather elegant paper that somebody pointed me in the direction of last year match your pirogue fizzbuzz in haskell by embedding a domain-specific language you know you know when somebody's going to you know when somebody uses words like DSL and Haskell and then they throw it at fizzbuzz that you're in for some kind of rollercoaster ride and it's a very interesting paper it's also worth looking at on SlideShare it's more gentle in that and as he says yeah the default action is executed only if some previous actions were not executed so if I revisit this one and go back to this one in JavaScript there's our kind of accumulation model we have to revisit stuff I can be a little bit crazy here and do it as a kind of sort of table lookup I'm quite quite proud of the bonkers notion of that one that is a little bit crazy but again I have to recheck the or on the right-hand side basically is a recheck basically I have to check the results on the left and then do an action on that so I haven't really got rid of the recheck although I've certainly well there is a there is a saying that if you kind of dazzle people with brilliance baffle them with and this is a kind of a good example because I you busy looking at the table lookup going whatever but a little all on the right-hand side means I have not got rid of the second check so he comes up with a sort of says although we asked if we can accomplish this without having to check the conditions for the previous actions twice in other words if we can make the control flow follow the information flow without losing modularity and there's a whole load of detours and fascinating stuff in the haskell paper but I really wasn't sure I could understand it so I gave myself the challenge I thought I can only I will understand it if I can implement the same result or all parts of his working if I can do that in a number of different languages so I had to go I going ban Java and C++ Python and groovy and here's is a the JavaScript version and which I are kind of proud off in a kind of sad way but all nicely landed and it turns out that this is all based on function composition and you return a function you return a higher-order function that gives you the result you make the decision about the 3 and the 5 only really once and you return a chain of functions set of compositions that will give you the right answer based on what was given and this is one of those things that it certainly doesn't have the vast expanse of the enterprise fizzbuzz Edition but the density of ideas in there versus if else you know you're looking like maybe somebody's been a little bit too clever here yeah this is the other kind of enterprise hanging out in every enterprise it's a there are smart individuals who are frustrated by the rather sort of verbose nature of other code and they are seeking to distill everything down to I mean I feel a little bit indulgent that I've actually used I've actually I've retained the vowels and the word test fees and buzz but there we've certainly got rid of all the other letters those are not necessary clearly and we've reduced it to its essence and we can wander around feeding mathematical functional and remarkably cool and also very certain of our jobs so there's this other side to enterprise programming that we see in a number of cases at and again I want to refer to this point as being asked there's clearly a unique result of a unique temperament so where else can we take the stuff well one of the things that we saw one of the things that we've seen is is this is concept as cargo cult programming a style of computer programming characterized by the ritual inclusion of code or program structures that serve no real purpose and we see a lot of this in their enterprise programming and it's perhaps best to think about it from an engineering point of view signal-to-noise ratio your code your source code it has desired signal and it has noise and we use this informally and as a metaphor talk about useful information versus false information or irrelevant data most common source of noise well first of all there's the intelligent expansion of things managed to get Shakespeare into my other two Talk's today so it I felt it was necessary to get into the video final talk so a reasonably famous soliloquy to be or not to be that is the question the author Tom Burton had a go at turning this into a business speak in his book long words bother me same content and yet skillfully double the length with words which are somewhat longer and you know continuing existence or cessation of existence of those are the scenarios is it more empowering mentally to work towards an accommodation of the downsizings and negative outcomes of adversarial circumstance those of you would buzz with bingo cards at this point you're probably doing quite well okay but as I then pointed out once when I want to show this I've something that this is not how a developer would write it this is not how an enterprise developer writer they use some kind of like crazy alignment and you know if you find most of your code is actually on the second screen or something like that and some guy now I know it's fine I've got 2 HD monitors you know and I'm going to upgrade to WiMAX soon so I can just run out to column 500 not a problem and and so you end up with this kind of approach now the other one of the other sources of this not simply paucity in terms people's approach you know if I explain it if I use really long words and I really spell things out and everything then it'll be more easily comprehensible the other one is comments now Rob Pike wrote this around 1989 actually round the time that I was learning see I came across this year and a half later as he says in these notes on program you see a delicate matter requiring taste and judgment I tend to err on the side of eliminating comments for several reasons first if the code is clear and uses good type names and variable names it should explain itself but I'm going to make a sort of a cautionary observation here people often say well you know my code is you know it's self-explanatory you know it's a right if you say that that's that's wonderful but who gets to judge that and let me just offer you the suggestion that the self that makes that judgment is not the self that wrote the code okay you need somebody else to come along and go yes my my goodness that is so clear and crisp it is it does explain itself beautifully as opposed to have you been reading that Haskell fizzbuzz paper again which clearly is a different different path and second comics aren't checked by the compiler and I mean the obvious corollary to that is they also not check by the programmer so is that your two audiences and neither of them is actually interested and so there's no guarantee they're right especially after the code is modified a misleading comment can be very confusing third the issue of typography comments clutter code but my favorite thing about the Rob Pike quote is is this aspect he he threw in this little observation you know this is there's a famously bad comment style I equals I plus one and you know therefore add one two I there are worse ways to do it and then he shows a big banner comment which is magnificent add one to I and says don't laugh now wait until you see it in real life so obviously I laughed when I first read it five years later I actually did see that in production code it was at a bank in the city they no longer exist I mean I've just put those two Khan's I just put those two things there and your mind has already made a causal relationship between them and it wouldn't surprise me if there was some deeper causal relationship because some of the code I saw there was shocking and any was stuff like this we were never told about adding to though that was on a few lines later apparently that wasn't worthy of comment so it does lead to this interesting challenge a common fallacies to assume authors of incomprehensible code will somehow be able to express themselves loosely and clearly in comments it's not that it never happens but if the person that could not explain their code clearly using code and you say please explain what you did in English I'm pretty sure that well experience suggests that most of the time they will have the same problem it is the same thinking process that goes into it if they understand it at some level but that's not the same as being able to express it so this is why you know don't do this stuff alone you need that other head it turns out somebody else will give you the insight that you probably had but were unable to articulate and cargo can't programming the ritual inclusion of code that has no purpose and there's this really interesting stuff here so I found this so this is one of those frustrations I deal with a number different languages at different times I and there is a there is a habit in the Java world of importing everything explicitly and in detail if you want to use something from packaged you import just that thing you don't use wild cards or anything like that you know I currently have an average of 15 to 25 imports in each source file which is seriously making my code mixed up and confusing I'll just point out on Stack Overflow too many imports spamming my code is the URL and that is the that is the perfect description of it is spam it is just noise is too many imports in your code a bad thing is there any way around this Stack Overflow this is this is where this is actually my older son discovered Stack Overflow without my help and he's not really a programmer he was just doing some code and I'm sitting there and coming along and I said you need any help said I'll no dad I did have a question but I found this site stack over it's just like yep all roads lead to stackoverflow the problem is that not all the roads are that well cleaned okay and you know you get all kinds of people traipsing in and so it's normal in Java world to have a lot of imports we haven't said if it's good we haven't said if it's bad it turns out that a lot of people you know if lots of people are doing it you know that's okay not importing whole packages is good practice and assertion no there's nothing to back it up what else did I find it's a good practice to import class by class instead of importing whole packages again we are asserting this without any kind of qualification so I mean I'm you know I'm an inquisitive person I like to know the reason for things in fact that has got me into trouble I'm not a troublemaker normally but that has got me in trouble in in the past when I really because I've not understood what something is supposed to be for well how can I develop it I mean I really need to understand what is this practice what makes us tick what problem are we actually trying to solve it turns out those are surprisingly awkward questions and most people choose not to answer them so back to Stack Overflow here's people's explanations enough this it's not a problem any IDE will manage imports and show them to you only when needed hang on most IDE support code folding where all the imports are folded down to one line I rarely even see my imports these days as the ID manages them and hides them as well hang on so anyway the IDE puts from there and then it hides it away because it will get in the way otherwise any good IDE such as eclipse pause for a moment frankly will collapse the imports in one line and you can expand them when they needed so they won't clutter your view so basically we're saying you don't need them so the IDE give us and it taketh away so I mean I remember reading this and it was just you know facepalm moment so let's have a look closely what's really going on here ah it's not even March so what is the matrix control The Matrix is a computer-generated dream world built to keep us under control in order to change a human being into this that is an IDE you won't pass the matrix the whole thing is that ideas can really help us with a lot of stuff but it turns out that when I ask people about certain coding practices why you doing that all the IDE generated it for me the idea said this is a good thing the IDE said it was a bad thing but this is a this is a good style and I can show you why but that's not the way the IDE defaults you do know you can change the default we don't touch the sacred defaults the sacred defaults has been brought down to us from the mountain and it's a case of originally the purpose of the idea is to help us get around all these things it's justice to extend our reach but what happened is some crazy through the mirror thing where we reach in he reaches back to us so let's go back to the original question I currently have an answer is it says excusing ya is too many inputs in your code a bad thing yes it's noise it's classic noise if I open up a Java source file assuming it's the first thing I see is a huge great typically in bonuses for many languages the first thing I see is a huge great copyright statement which is obviously incredibly useful and you know exactly what you wanted to look for the terms and conditions and all the rest of it so a practice that I'm pretty sure that John Jagger told me a few years ago was you pull the noise at the back if you need the copyright state when you put it at the end and you have a little bit at the beginning that says you know look at the ends it you don't want to read all that stuff it's irrelevant it's not the first thing you want to see yeah is there's a there's a thing you want to drop people into the action here is the stuff of interest oh it's not actually the copyright comment it's the import again not interesting so is there a way around it yes it turns out back in the mid-90s when Java was created there is a very simple technique and Uncle Bob has the same view on this is that I do avoid long import lists by using what oh gods long lists of imports are daunting to the reader we don't want to clutter up the tops of our modules with 80 lines of imports but people do and what happens your your eyes slip off the screen which is why they add the feature that you can now fold them away they're so important that they default to don't worry about this you don't need to see this don't look at the don't look at the imports behind the curtain rather we want the import to be a concise statement about which packages we collaborate with the fine detail about you're actually using this collection class and this collection class and this collection class is not interesting you're using some utils great let's move on to the next thing of interest a broad brushstroke view of your dependencies is sufficient so I I went out and raided some open source software I tried to find stuff that was that was dependent only on the Java standard libraries and with fairly core code and just doing basic tasks and there was much longer ones but I thought I'd do stuff that was in a reasonable font size so we see this is not untypical and it reduces beautifully to one-line I mean are you actually that interested in all of that detail remember that you've actually got the you've got a source file where you can actually see the coding this has surprisingly little values similarly we can reduce all of this to our right we're using beans that's unusual reflection and some usuals great not as obvious this is the point this is noise I can't actually really see what's going on if I cared about the detail while I can see the detail it's called code we've got that this is should this should be a broad brushstroke here are the dependencies it should not be fragile that every time I make a slight shift in my choices collections if I want to use a tree map instead of a hash map why is that important here it's not it has no relevance but we artificially raising its importance we're creating noise so what is the original motivating example I was interested so again I thought I'd dig through and see what why why are people people are doing this partly because of IDE support but what was the original motivating example what is the motivating example that makes people say I need to physically import to avoid name collisions and in over fifty percent of the cases that I found and indeed the ones that were when when this practice kind of started the ones that are deal with name collision the observation in the AWT which nobody uses and inutile there is a thing called list it has the same name and therefore there is potential for a collision and this is the motivating example the number of times has actually happens in production code is vanishing it might not even fit in a double okay so it turns out that this is a solvable problem this is how you do it and been able to do it for over two decades in other words this is a this is a salt puff of smoke that has led people into this crazy style if I look at other languages I don't see people doing the same things people are not individually exporting absolutely everything a couple of people have pointed out that people do that in Python no they don't they don't do this to the import structure is different and there is a very very different set of cultures and support for that as well so nobody really does it the way the Java programs do they're kind of special in that sense now if we're talking about cargo cult programming the point is that where does truth come from this is a very profound question we're in January and we're in the post post truth year and I'm going surely sure that the posts cancel out but you know but there is this point how do we obtain truth or one of the most common ways that we as people detained truth is through repetition the more you say it the more true it is I want to get back to Morpheus here yeah and we all know what it says because we've seen it on a thousand means what if I told you Morpheus never says what if I told you in any of the Matrix films mind blown matrix moment red pill taken he never says that okay and and likewise um you know Luke I am your father turns out Duff never says that either but these are true because we've repeated them and this is how coding guidelines and enterprise cultures are created by the repetition of an idea or something somebody heard from someone else and it just goes around and suddenly that's how we do things here becomes a culture so we need to be on our guard for this so we have this idea this is creates our cargo style and programming and sometimes we do this without understanding this idea a wonderful one a good one to follow as well the expert beginner what are people in operations want for me I'm already logging at the beginning and end of every method guess I'll do the middle - yeah so there's another aspect cargo cult programming applying a design pattern or coding style blindly without understanding the reasons behind that principle and this has led to as it were the over adoption of Factory ok the factory concept is a really good concept the idea of the complexity of creation of an object cannot be handled through a simple new expression there are reasons perhaps that it cannot be handled typically related to lack of knowledge about the core point at the point of creation we do not know enough to create this object but somebody else does that knowledge exists somewhere in the code if only we were able to embody that and encapsulate it let us call that idea Factory it's a very powerful concept but and one of the things I quite like about the path and style that Alastair Coburn used to use is when describing a pattern he would not simply describe the mechanics and the motivation for it he would also include an overdose section what happens when I do this everywhere in other words this is a really good idea you know I enjoy a glass of wine what happens when I have 10 I have actually I can't actually answer that question because I can't remember so the point there is and over does it what happened this is a good idea it solves a particular part but what if we just decide you know this is the finest hammer I have ever seen it is gold and is wonderful and all of those things out there are really looking like nails let's just do that everybody's going to get a factory this is brilliant and you end up you know apartment what happens with hammers I think it was now at price that gave me the wonderful observation that you know when all you have is a hammer then all the world looks like a thumb and another guy who maintained some of our code tell us less turbo he has his observation was when it when all you have is a hammer then everything begins to look like a screw you so you start kind of like yeah that screws really not going in I just need to hit harder so we have this problem a kind of lack of understanding now normally when people talk about patterns one of the first up is going to be singleton so I'm going to get that one out of the way I'm not a big fan of singleton I actually know it's not bad as a whiskey that will actually improve your design okay I'm offering you the singleton that will work because you're going to sit there and you're gonna have a sweet little sip of it and kind of roll around I kind of like beautiful burning fire of a single mole that's just my air and you pause because you can't drink whisky without pausing and getting philosophical at some point and you will reason about the nature of the universe the concepts of code and ones and zeros will stream through in a matrix like fashion and you will go you know what I won't put that big global variable in my code today I will I will do good things from my code you just chill out and relax map sync from never appears in your code just in your stomach okay so that's the good stuff and I did find that I could actually spell out Kevin any single - it's surprising what I keep in my office it turns out so what are some of the other practices that we have well I referred earlier on to ever-expanding screen size so it turns out that you know there was a concern number of decades ago and it's resurfacing about what is the carrying capacity of this planet in population explosion and so on and you know we're hitting the seven and a half billion people mark this is this is a lot but the population is exploding but there's a big problem screen sizes and resolution is expanding far far faster and there is a problem here because people start thinking well there's more real estate on the screen so what I shall use is I should use longer names I should be expressive because you're supposed to be expressive and what and what I found is about twenty years ago I was I spent a lot of time telling developers you know the great vowel shortage is over the great vowel shortage of the 70s 80s and early 90s is over you can spell word by including all the vowels you don't have to abbreviate the hell out of them it's okay good times are here these days at a point I guess the point is the crossover must have happened around 10 to 15 years ago because now I go around telling me much too long you need less you need less you know we're not running a lira based economy here it's just like less you know those names are too long we've got stuff here so bonkers world two years ago beautiful cartoon the world seemed important importantly the scare quotes are there object-oriented program we have the entertainment provider singleton we have the indoor session initializer we have the privacy manager delicate my personal favorite the multi buck supporter you know it's all there this is this is how people name stuff now this naming is not merely labeling if you get the names right you are communicating design the programming is an act of communication it is an act of Applied thinking if this is how you think you need help and it's not the kind of help that we can offer you here in this conference and there are professionals who can probably sort you out okay this is this is a problem the whole concept of the object metaphor originally was that we are able to say you know you see that thing that looks like a door well it's a door but what if that collides with another door use namespaces or packages or whatever you know it's you know these things are more direct the only thing that even remotely looks like its previous forms of television remote control and but everything else has simpler name use it referring once again to Morpheus there is a point in the matrix where he says Taniya and by the way if you haven't watched the matrix you really really need help you're a software developer okay come on there's a kind of standard syllabus of things you need to know that is in there so I make these references without apology and there is a point in the film where you give me as cart come on stop trying to hit me and hit me and you look at a lot of names like this it stopped trying to communicate and just communicate you're trying way too hard you're spelling out everything about this yeah and its mother and it's its parentage and it's family och no you don't need that so one of the things that I do apart from taking photographs of books and things is I do actually write a little I don't just write technical stuff I write short fiction occasionally for rid of fun and and so I've got a few books on guidance for writing and this particular book comes with the set of cards namely Apple's observation deck that I thought it's quite good because it's going through this a sh t hoff these are actually applicable to code and I have picked three here eliminate words name choose the right name okay these are the name is not merely a label on your design your name is your design you don't have much bandwidth when you're talking or communicating to somebody through code if you think about it when you communicate through codes you only really got three tools you've got names you've got spacing you've got punctuation and there's only so much you can do with spacing but it helps and there's only so much you can do with punctuation because clearly the compiler has an opinion about where you can and can't put it it's not like arguments over should we or shouldn't we use Oxford commas the compiler has very definite opinions about know that semicolon doesn't belong there you wanted that semi core down here it has very strong opinions but that's it names spacing punctuation names of where you have the greatest freedom that is where the greatest communication comes from and so often we squander them because we think about them as merely labels you know I will just point out that my two boys are not called son one son - or even sing one thing - in reference to dr. Seuss we did actually spend a lot of time coming up with names so you know treat your code in this sense if you're trying to do something is going to going to have some endurance and communication and get specific okay there's a feel free to get specific so this are throwing a piece of code that's quite common one I didn't just see an a piece of code now we've got an object it's probably a single tooth configuration manager that word manager I'm not going to say it's always bad but I'm going to say that it's pretty good probability that it is bad you know if we were rolling a six-sided die and you know I said that yeah use of the word manager comes up on one that might be about the right ratio that might even be too generous we might even have to go to a d20 configuration manager what is this what does it represent and so I had this conversation with one group and so what is it oh it's the configuration you mean can you can do that yes it turns out this is the configuration that's what that's its job it represents if it doesn't manage it it don't you know stop trying to hit me and hit me it is not merely a kind of an indirection to a thing about a thought about an abstraction about an idea that maybe possibly we have something to do with configuration but we can't say so directly because that we call no that's too close say yeah you know it's configuration there it is this is in a piece of code for my clients condition checker for hierarchy of condition checkers check condition there's a bunch of noise words get set check validate there's a whole load of words they they hang around together you know like the hanging around together they're the kids behind the bike sheds just okay yeah yeah we're going to make plans we're going to take over the school we get validations here check get set we're going to take over we're going to get everywhere and they do and this is object-oriented assembler okay we're using these very very simple overly primitive kind of knuckle scraping of naming approaches so I have a conversation with them because sometimes you don't it's not obvious what the idea is it normally comes out and in the space between people you throw a conversation it so what do these do and they told me about the things in the hierarchy and they were firing events based on certain conditions becoming true oh you mean this yes they said they are conditions condition checker did we add anything by adding the word checker except a few extra letters check condition well what the conditions do I mean you know let's let's have a really deep meaningful consider discussion about the nature of conditions they can be true or false oh well there then we're done and then if you're using the right language you suddenly realize oh this disappears in a puff of land Ernest I don't actually need something there at all because it's just stuff that I can pass around I don't need a class hierarchy my conditions that have to be in that form so it just disappears and this progression this procession as it were of disappearance is quite an important idea refactoring we often talk about refactoring as shuffling around code and the one of the most common refactorings people deal with is rename okay that is leaving something in place but transforming its ability to communicate and there's there's a fluidity to that but that's what you want you you don't know what you're going to say until you say it in fact sometimes you don't know what you mean until sometime after the fact that's what I meant so names should be considered to be as fluid as possible okay be a little bit reluctant about putting things in published interfaces until you've let them settle in your mind now I truly understand the nature of this thing okay well I'm happy with that okay listen to your conversations when you're having them with people see what words they use in that you use it's just that you know that abstraction is not the that's not the right name for it now I understand that it started over here and I've retained the same name and people people give you feedback that was honest in your head you you have all kinds of substitutions that you quite happily apply without realizing most extreme example I know of was a an example a number of years ago where I struggled to understand the purpose of the class that was named database I've been looking at the documentation I didn't have the code I've been on the train been looking at the documentation and I've been called in to kind of a chat with somebody about the problems they were having with the code from the project manager called me in and I'm just looking at the documentation and this is a beautiful example of the documentation was written by the person that wrote the code and it turns out that they weren't very good at communicating and the code had the same problem and so I said you know I've been puzzling all the way from Bristol to London on the train why is database the most important class in the system it seems to be the anchor for everything and the guy says are no no that's actually not the database that's the application controller obviously with a name like database you would immediately think that is the application controller no all the database stuff is in the classical database to add a database also database the son of so names matter they really do you know you don't want to be sent off in the wrong direction it's never too late to stop going down the wrong road so there is a simple idea a refactoring is a change made to the internal structure of the software but it's not just about names it is about moving things around but one of those common approaches that I think is overlooked is the nature of reduction reducing things down refraction can lead to additions they can need to move they can really lay relabeling but elimination is one of the most important things this vital act so I'm going to close with a sort of a brief exploration of a number of here's that here's a few smells that I kind of see and they're kind of interesting because this is actually based on some code from a from a client and and I've been dealing with them for a number of years and so I could actually run off a number of things that they enjoyed doing that I've also seen other people they loved singleton configuration in fact quite frankly they have a a deep and quite frankly unhealthy relationship with single things overall they're good on the verbose naming they're also good going to the other extreme you know you'll find something that is verbosity named and then ten lines later it's they've ripped out the valve and it's just like how did we go from that to this this is the bit where I need more and this is the bit where I can't don't understand you you've got what I would refer to as homeopathic naming it has been diluted below any reasonable level of potency it has no force whatsoever but it has a certain belief system around it noisy logging what is it about logging there is a logging one of the most fascinating things you can ever do in a project is ask somebody what are you logging do you have requirements for your logging have you as a team discussed what is it that your logging people get a little bit uncomfortable with that question because it's like oh we discussed the customer requirements and the logging was just left to the developers so what is it that you're logging oh we we were told we should log everything oh you see I love it when people say that so you log the act of logging well no that would be silly well okay you know call me a little bit you know pedantic but I have a very clear idea in my head of that what the word everything means and clearly you are not logging everything are you or are you logging that assignment there no no we don't log that sir well clearly you are doing a subset of everything because everything means everything and then you're not logging everything so there is a subset what is it that defines that subset and then they were so a lot of stuff that will be useful and then to give me an example and then somebody else will say oh I don't log for that I log for this oh you have personal different definitions of everything great so you end up with this one codebase I saw as three years ago they had three different approaches to logging cohabiting in the same codebase if somebody asked you what does this code base do just a quick cursory look at the code i'll just code base logs it actually controls machinery but first blush it logs that's what it does most off if you looked at the proportion of the lines of code two things that actually did stuff logging is the most important single most important activity in that code base and why were there three because these three different factions could not agree with each other and nobody called in the United Nations to resolve it log and throw also popular when it comes to logging log it repetition and duplication because that's where truth comes from okay it must be true I repeated it but also there's a there's a comfort you don't even see that you're duplicating stuff and that is a problem because we often talk about dry principle but the things you don't notice it when you become comfortable with something it disappears when it no it's either automated by the IDE or it becomes automated through this and it bypasses this bit yeah and I want you to think about your breathing for a moment okay you don't normally think about it do you that's because it's automatic it turns out the code duplication is down there with breathing heartbeat and a few other fundamental things in a developer it is not coded into your basic reptile brain it's automatic your autonomic system handles it perfectly unnecessary code source of noise you know either it's for those or it's just like leftover code mixed levels of distraction business concept business concept bit bash bit bash socket connections per sub point business concepts you're taken on a rollercoaster ride which is absolutely brilliant if you're having a boring day then going to one of those methods is quite exciting yeah you'll be awake pretty quickly it's like whoa hang on we were doing high-level stuff related to governance and suddenly on bit shifting here why am i doing that yeah you can't fall asleep in that code because the code will get you legacy coding habits any language has been around for more than about two minutes has legacy code I mean in the Java exaggerated but obviously there are different standards you know today I've been talking a lot about C++ and there's a whole load of C++ 98 code out there that never really was good C++ 98 and now he's having to catch up with C++ 11 and so on visited a company two years ago they had Fortran that actually goes back these guys take the record you can think about how old is the oldest piece of code that is existed in a company that you have worked in these guys will beat it these guys actually had Fortran code going back to 1959 Fortran was invented in 1957 they get the record I have never met a company that has that kind of like and they are now programming in the 21st century version of Fortran that means they have a legacy spread over half a century you can start in one part of the codebase and time travel let us put it this way those coding idioms have not remained stable for half a century or so so you end up and you know in the JavaScript world you can get legacy coding habits within like about three or four days because you know a different framework it's not this week spring when I was last week's framework and that's so programming by coincidence it works and you look at it and go does that yes it does it's always worked what works on my machine and programming right superstition if somebody once told me the number we've always followed that ever since so I'm going to pick on this code it is what I was fascinated by as a sort of thinking exercise and I'm not asking you to read the detail of the code was that I was able to I was trying to give an example these guys about something I basically adopted the style I said I when you ask some of your write code badly they can't do it but if you give them a system and this is the most important thing I want to get across here is people are not soft developers in our it's stupid or malicious by and large though they they what they do is they follow codes they follow conventions they don't even know they pick them up and what happens is that what we often consider to be bad code is a product that is systematic it is not coincidental or random actually it turns out that if you say we're going to do log and throw write that function as if you were doing log and throw you will be able to write that function if I give you a drop down menu of here I like these style choices so I basically took a number of these style choices and took a simple problem and then coded it in their style and I was or rather I coded it using those and I'm surprised at how close it came to their style so it was a revelation to me it's like yeah what we often think of as poor code is systematically created it is not created arbitrarily or with malice so um what do we got here we've got a bunch of C++ code I don't need you to know the C++ code I'll talk you through the the bits that you don't have to worry about we've got a function called create server connection the name create server connection tells you kind of what's going on so create some connection we've got a bunch of declarations all the way up at the top of the function then we've got a whole load of stuff we're going to get an address and check its ok and get it some configuration manager single turn and then we're going to check whether the string is empty if it's empty then oh no we've got a configuration value missing log and throw we can do the same again for other aspects of the address and other bits of configuration and so we're going to convert things and then we're going to create a new object because we want to create connection well I mean that's the nature of life really ultimately isn't it we want would create connection but here this particular piece of code that's what we're going to do and then finally we're going to tell people that that's what we're going to return just to make sure and we're going to do that right so first of all let's go look at these these comments whenever you have comments there are a number of things that they suggest one of those common guidelines is that if you comment off blocks then perhaps those blocks are named blocks you know we have a name from named blocks functions okay so perhaps you should be organizing like that but I'm not going to get rid of them just yet and in fact I'm not going to do that refactoring the first thing I'm going to do is I'm going to correct all those damn spelling and punctuation errors right now I've corrected it now I delete them okay okay but there you got it you you gonna you know this is do the right thing before you do the right thing okay so that's gone right there history now the next thing we may notice is that all of those declarations are looking at the top and this is a habit that funnily enough I mentioned Fortran started out with fortunately people love to there's some kind of up with gravitational force the sovereign function or maybe maybe it's just smoke smoke rises so maybe maybe that's what happens but people feel compelled to put declarations at the top even when the languages don't require it so we will distribute this and so that we can say well look really you should only be initializing things you should only be declaring them when you can initialize there are going to take a bit of advantage there's a bit of a bit of extra type information there I can actually reduce the amount of type information and use deduced types if you're familiar it's also in in C++ of you familiar with c-sharp that's far and so on so I'm just going to let compiler do some of the work we both know what's really going on there's no secrets between us so I'm good with this now wait let's let's go and peel off a couple of bits here there's some fascinating stuff going on here not least of which is these log and throw things so there's sprinter sprinter F is like I'm working and see if I start working in C++ now I'm working in C sprint F is all really good at things like allowing buffer overrun attacks it's that's a great feature built into the standard library and we also have another element here which is a curious piece of legacy coding is that we create a new object what if new fails if new fails somebody's going to check that it's not a null pointer no no it doesn't return a null pointer it throws an exception so this first part is a fancy way of writing true so we'll get rid of that but the other thing that people like is they will notice if they are C++ programmers I got all your using raw pointers you should use some smart pointers so they'll go ahead and do a C++ 98 Auto pointer their colleague would come along correctly with C++ 11 say oh no you should be using unique pointer I'm going to come along and say I love your thinking but let's just pull back a moment because I don't want to change the interface yes I want to change the internal structure I'm gonna use I'm going to use a unique pointer internally but I don't know changing outward appearance of the function just yeah let's sort out the insides before I start propagating signature changes so I'm going to take a sort of a rain check on that one so the next thing we are going to do is going to have a look at these these are these various print X which are there for logging and the first thing I notice is that there is a string object and we are asking for its data that seems a little bit suspicious the data is the raw data that is the underlying data of the string I don't know if what we want what we want in this particular case is we're passing it to C style functions we want a C style string a C star string ends with a null null terminated you're not guaranteed that a success the string will have this by coincidence data will not that the buffer held will normally have a null termination it's just easier to implement it that way so this works by coincidence so let's do the right thing and change it to I want to see string please let's actually capture the intention but if we look more closely we realize it in a number of these cases this is actually not going to be particularly helpful but in that case there we can actually instead of using 8y we clearly love the C for the C library here we can actually do this in C++ and use f2i which does a proper string conversion the next thing is to go and look at the rest of these things all these others printf notice the buffer I have no idea how big the buffer should be 512 make it bigger that should avoid overruns now people take a love you know they pick at an arbitrary number and it's morally a power of two so it's not completely arbitrary but if your colleagues ever pick a number that's not a power of two you know they are freaks and should be exported from the building immediately so I'm you know but nonetheless there's a piece of hard coding there and let's get the nature of these things some of you may go along and say well look but really you should be using sprint f you should be using SM printf which does the buffer checking for you in conditions of a buffer overrun it's way beyond your control 10:24 is not going to help you okay so the next thing let's kind of move in on these guys and say well really you should probably be using string streams and so let's go and use string strings that's the kind of like the politically correct C++ 98 answer but if you start looking at the nature of the strings the first thing that springs up mind is that wait a minute that first one is actually two string literals people don't intentionally do this in other words it's actually a fixed string it turns out the other one you can just do by adding two strings together you don't need all this choreography the problem is when people start down repeating it I set up a string buffer I put this I put that they stop their habit prevents them from seeing that the actual content of the message can already be dealt with by the features of the string the next thing is to go and look at these other these other ones that don't submit to quite that simplicity but we've seen we can add everything together the next thing I'm going to deal with is this whole log and throw now sometimes people may say well hey why don't we need we need to really rethink to this and figure this one out I'm actually going to do a minor simplification here I'm going to take that one way to tidy things up is to pull all the stuff you don't like and put it into one bucket and say I'll come back to it so I'm going to factor out log and throw I'm going to make it a function failed to connect okay it's got a little more intention there and inside that it does log and throw in other words I haven't sold the log and throw problem but I've just parceled it away it brushed it under the carpet we're going to come back to that carpet in a bit and in fact now we look at it you know what I could get rid of those extra curly brackets there and I know that this are now I'm now actually touching a deep point of religion with many people and you know we all have different faiths and there you go I'm doing it it's the end of the day you can run screaming from the building afterwards if you like but I like the idea it was an idea that Uncle Bob put forward a while back you sort of said well actually why we why do we always put these double why we've put these mashed pans in why we always put the curly braces and even when it's only a single statement because we are afraid of what will happen in certain cases well it says that compiler is mostly on your side with this one and all the other tools are as well but I like the other way of looking at you said well I want to create it so that I don't need them in other words that everything should be as short as a single statement ideally and that gets you thinking about your code very differently so we'll take that now the next thing to look at is our friend the less resizes next thing to look at is our friend the configuration manager and indeed the name of the function and its signature I'm ready for the signature change so we're going to make that change to the smart pointer at this point so we're going to do that we're also going to change the name because create server connection well whilst that at one level isn't it's an accurate description is that really my intention is it my intention to create a serve connection or is it my intention to connect a server the point is that we know we're going to create that try and capture what somebody really wants okay the problem is there is a developer sometimes you don't it's very difficult to zoom out and go that is what we're trying to achieve are we are trying to achieve the creation of an object or we are we trying to have a connection and that is what we're after next thing that's going to look at this this singleton get value not a big fan of get I would prefer to say that what I'm after is the value of the address or simple renaming there we did the other one earlier on configuration manager becomes configuration it's at this point that you have a moment of insight and you think wait a minute why are we having this conversation with this big global object why don't we just pass in the configuration the address and the port as arguments well there you go that's a lot easier and then if we're passing them in as arguments why don't we pass them in as the right types because the only reason they were coming in as strings is because we got mad the config which defaults to representing everything as a string yeah it's a string lis type system so the idea is that by the time we get to this code I shouldn't have to do any kind of tight laundering or conversion the goal of this piece of code is to connect to server' is not to check whether or not the configuration is correct it is not to do transformations on you gave me a number but you gave it to me in the wrong type so now I've got to convert it so well that's a bit easier okay so now we've reduced this down we can actually start asking deep philosophical questions about the nature of creation and connection and I'm going to say that the purpose of a constructor which is being invoked inside the make unique helper the purpose of a constructor is not merely to initialize an object it is to create an object in a well-defined state therefore we should never be in a position ideally where we go back to an object as are you okay you good it was that good for you you know you know what worked for you what didn't um we shouldn't be in that position if that object could not be created we do not have a valid connection therefore there should be no object for a stole on to the constructor should throw an exception now is why constructors were originally invented so therefore there is no we're going to we're going to get rid of that and suddenly whoa we got rid of all that log and throw stuff it's no longer our problem whoever called us is going to get all of that somebody else is going to do work there's a configuration problem it should be found earlier this is this is the wrong kind of late binding if there is a failed connection we will give them enough information in the connection constructor for them to make a decision as to what they do next at this point however we realize that there is a surprisingly little value in this name and so this is the final refractory thank you very much [Applause]
Info
Channel: NDC Conferences
Views: 449,652
Rating: undefined out of 5
Keywords: kevlin henney, ndc, ndc london
Id: FyCYva9DhsI
Channel Id: undefined
Length: 71min 23sec (4283 seconds)
Published: Mon Feb 27 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.