code::dive 2016 conference – Kevlin Henney – Clean Coders Hate (…) When You Use These (…) Tricks

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

lol many images post

👍︎︎ 17 👤︎︎ u/hedgehog1024 📅︎︎ Oct 04 2018 🗫︎ replies

Man insists on spending time in non-Haskal codebases and still has the gall to complain

👍︎︎ 10 👤︎︎ u/affectation_man 📅︎︎ Oct 04 2018 🗫︎ replies

I propose to add Kevlin Henney to allowed jerks.

👍︎︎ 9 👤︎︎ u/crazyeyes420 📅︎︎ Oct 04 2018 🗫︎ replies

He’s British and doing a talk at a fancy convention so I accept everything he says without second thought

👍︎︎ 6 👤︎︎ u/flexmuzik 📅︎︎ Oct 04 2018 🗫︎ replies

/QUOTE unjerk

Are we supposed to jerk to unjerk now?

👍︎︎ 5 👤︎︎ u/StallmanTheThot 📅︎︎ Oct 04 2018 🗫︎ replies

oh wow, exact quote

👍︎︎ 2 👤︎︎ u/wafflePower1 📅︎︎ Oct 04 2018 🗫︎ replies

Can't jerk he makes a valid point.

👍︎︎ 2 👤︎︎ u/[deleted] 📅︎︎ Oct 04 2018 🗫︎ replies
Captions
[Music] I think the title of his dog didn't actually take part and the contest for the shortest title at the conference because it is clean coders hate what happens to your code when you use these Enterprise programming tricks let's welcome Kevin Hanny right good afternoon yeah this is a shamelessly clickbait tea title I got carried away one day and I have actually tried to give it a another conference but it wouldn't actually fit in the the information slightly they said it was too long a name and they said what's the short version of it there isn't one I've done a couple of things I very strong interest in how people do things patterns and how people do things well but also recent discovery I say recent about two or three years ago a discovery that problems in code arise not because people are being bad or stupid or evil and in fact it actually turns out when you ask people to write bad code it's quite difficult for them to sit down and write intentionally bad code but if you tell them to write in a particular style if you say I want you to assume a few things use this style suddenly they are able to generate the kind of code that we dislike well I hope we dislike and the editor of 97 things every programmer should know which may or may not appear at this point there we go and style therefore is one of the things that dominates this book but there's another thing I'm interested in I also run a page on facebook called word Friday it's about language and linguistics and words I'm very interested in words and language so occasionally when I want to understand a word I will try and find out a little bit about its history its range of meanings so I thought I'm using enterprise in the title so let's find out what it means what the problem is I'm of a particular generation that whenever I hear the word enterprise any one thing that comes to mind you know yeah so you know I still kind of think is like whatever people sitting there and doing enterprise architecture okay think of tomorrow you know yeah and you've got the sort of the gorgeous reboot model I was just like this is great stuff and actually Enterprise has been used as a hypothetical craft name for a number of things this is a this does it sadly does not yet exist but it's a it's an artist mock-up of what the first warp drive craft would actually look like and it's a NASA idea though financially warp coils and you think I'm kidding no this is real this is actually based on a theory by the Mexican physicist Miguel Alcubierre who realized in 1994 that there are certain interpretations of general relativity that actually allow you to create warp drive and that actually by using these coils you will actually distort reality in a way that allows you to as it were cheat our perception of speed of light you never actually break the speed of light okay you cannot travel faster than the speed of light in space-time but what you can do is make space-time move faster than the speed of light yeah it's clever it's a trick okay and it's just as reality distorting as Enterprise Architecture this is why I mention it okay there is a reality distortion field so let's actually dig through the dictionary see what's going on and so a project or undertaking that is especially bold complicated or arduous yes Enterprise Architecture is particularly this stuff readiness to engage in undertakings of difficulty risk danger or daring yeah this is getting it a design of which execution is attempted I love this because it doesn't say it says well we're going to try it we go attempt it who knows what will happen and then there's a bunch of others yeah these are boring we one of these we only want these ones okay so I'm going to write enterprise code what is code okay so code we think we know what code is okay code is a set of instructions for a computer we're kind of familiar with our okay that's good a computer program normally we don't deal with the lowest level we use term code to describe both the binary and the source and we are they're both referred to as code a system of words figures all symbols used to represent others especially for the purposes of secrecy hmm you know what you go around and you look at certain code bases and you think that's the meaning of code they're using I have no idea what this is doing a set of conventions or principles governing behavior or activity in a particular domain like a code of practice and so we have so this is the interesting thing that's because back to what I was saying there was this realization and I'll show you the code at the end or the related code at the end of a systematic code a systematic set of practices that allows you to generate enterprise architecture allows you to create these things and this is a non exhaustive list but I'm going to this is this is these are some of the common things okay singleton configuration yeah we're going to have one configuration and everything's going to go and see it and it's going to be a single term it's going to in fact you know what it's not just configuration its singleton anything in fact looking at this two working con Singleton's that's normally just a problem ironically it's very rare that you will ever find a code base that has only one singleton in it I find that ironic the number of Singleton's in a code base is zero or two or more verbose naming you see had I done this talk say 15 or 20 years ago I would not have put verbose naming it I were to put abbreviated naming I mean we still have a few abbreviated naming problems but these days it's it's like some kind of inflation has taken hold there's a hyperinflation and naming will come to that noisy logging let's just say I don't know what well let's just log everything okay this is one of the things I've discovered people often put arbitrary logging in because they don't understand what they need from logging they never ask the question they think they know what the requirements are for the functionality but nobody ever sits down and says what do we need from logging what are our requirements from logging you normally get statements like oh you know just log what you feel like or my favorite one yeah we know what our logging requirements are to log everything when somebody says our requirements are to log everything you know they have no idea what they're doing okay so I ask them little questions like so you're logging the logging no no no no of course we're not logging the logging and I said well in which case you're not logging everything you see I have a very simple mathematical understanding of what the word everything means and you're not doing everything you're doing a subset of it so then the question is what defines the subset and I will say things like well you didn't you didn't you didn't log the fact that you incremented a variable here or that you allocated a variable here or that you did this or that you took this branch of the I when we're not logging all the things we're not logging everything what you just said you were logging everything so there's this kind of idea that you end up with everybody's own little version of everything and you end up with a lot of noise and just where you need it is not where you get the logging repetition and duplication let me just say that again repetition and duplication because this is one of the most popular techniques and we either do it by explicit copy and paste or we end up seeing this by reinvention familiarity recurring structures unnecessary code just the dead stuff it's just either the dead code or the code that is just noisy in other words there really is a short way to do certain things and people sometimes find the longest way around mixed levels of abstraction there you are riding through a function riding through a function at a particular level of abstraction you're talking business that's great business business business boom socket connection what socket connection memset what on earth it's got boom business back down ma'am copy what what is going on here I mean yeah this is I mean great roller coasters are fun but really not in the codebase that's not where you want to see you you know you stay in a level you say okay well that soccer to your memory stuff that's down there don't you worry about it this level we're talking business stuff and it down so mixed levels of distraction you give you give the reader a rollercoaster ride they will be very confused legacy coding habits this is one of the things many of the languages that we work with many of the languages we work with are either old languages that have got a history and therefore it is possible to start programming in one decade at the top of a file and actually progress through time as you reach the end yeah you know one of most extreme examples I encountered a client who wanted me to give him a workshop on test-driven development and some agile development last year and I said yeah sure fine what language and I kind of expected because they do some numeric work I thought I sort of thought they might be doing some see some C++ Fortran no I did not expect Fortran that proved to be very challenging I thought I left Fortran in the last century but one of the one of the discussions that we had during the day they said yeah we actually have some Fortran that goes back to the late 1950s Fortran was invented in the late 1950s I mean so you know this is beautiful museum piece but apparently it's still running so that means they basically got code that smears through time through the 50s the 60s or 70s and 80s the 90s into the naughties and now all into the 20 teens and it's just wow that's impressive it just makes some of the C++ code bases I've seen look really quite young so the point here is it's not just the codes the habits and the although you may be given a new language and you can pile you still end up with the older habits sometimes you take on a new language but your habits from wherever you've come from they carry over so we find a lot of this and that's also builds up the unnecessary code and the noise and then there's programming by coincidence what does that mean it means that sometimes things work and really they shouldn't yeah the talk after mine is on undefined behavior okay and unfortunately one of the definitions of undefined and the clue is in the name it's not defined one of the one of the possible interpretations of undefined is it does exactly what you expect it and nobody questions it until maybe about four or five years down the line you you move platform you change the compiler or something happens a small disturbance in the force it's all it takes and suddenly you're on the dark side okay and there you are you you know but you don't feel like a really cool syslog you just feel like you're debugging till midnight so what is going on so there's just stuff that kind of works but actually you know and this is important because one of the standard phrases that software developers use when you sort of say when you point out something oh i question that it's a better way of doing it they will come out with the phrase it works as if that's all it needs well yeah it kind of worked that one time on this compiler in that phase of the moon but don't count on that in future and then programming my superstition sometimes people have these strange beliefs so why are you doing that well oh I'll optimize things really yeah yeah if you do this it kind of speaks speaks profoundly to the compiler and it just goes a bit faster so you do know that hasn't been the case since the 1980s hmm Oh some of the beliefs people have about exceptions and stuff like that it's great oh you can't do other great ones oh we're not going to use the standard template library why not well you know it's not as efficient as rolling your own code doing it by hand and stuff I as you know I actually did that with one client we was one of their concerns was that a lot of their developers were they they were C programmers C programs Java programs some C++ programs but whenever they program C++ they always ended up with this I mean what I would call c-plus um you know it's kind of oh yeah but not quite you didn't get the second plus I mean yeah you're using a C++ compiler in yeah you've tightened up the type system you've got a few things oh look you've used the word class once at the top of the 10,000 line file well done but sometimes you end up with stuff that you sitting there going oh well what are you doing all this memory management for assign while it's faster than using fact so so I did I did this example by showing how how different the code was if you wrote it using something in the standard library or if you wrote it out longhand C style and I had tests to prove that my code was correct and did the same thing the final code was the fully expanded version was something like five or six times longer and far more complex and when I say five or six times longer that is source lines I'm not including the fact that some of those were curly brackets because the actual version using the standard library was mostly space so we end up with these little beliefs and those beliefs guide us so what kind of enterprise the example and I'm going to try and explore well you know real real software fizzbuzz a legitimate Enterprise II okay so fizzbuzz quick show of hands who's come across fizzbuzz a number of you so a number of you haven't right um so the sink of all human knowledge wikipedia says fizzbuzz is a group word game for children to teach them about division I have to say when I look this up on Wikipedia I was very surprised because I know it is a drinking game I am NOT entire I am not entirely sure what kids are doing with this but you know so you know there's a basic idea players generally sit in a circle the player does it laughs let's just pick out the important bits okay what are you doing is you can account one to any number divisible by three fears for but any number divisible by five is buzz fears seven eight now if I say nine I get it wrong and normally there's a forfeit of some kind if you're a kid that means you no longer play which I don't think works it's not a good incentive model if you're a child that doesn't like maths then you get out of the game as quickly as possible yeah so I don't think that works anyway it divisible by both 14 biz bus okay so not exactly the hardest programming problem in the world however I found the most unusual thing about the Wikipedia definition was their use of the word dense fourth credit to whoever wrote that because thence forth is I mean if you can get that into a conversation well done because that is just not a word that we've used for like 200 years okay in English I mean dense fourth is really archaic and it's just like but it fits perfectly I mean you know well done and Wikipedians this is what happens when they get bored okay so what do I know adults may play fizzbuzz as a drinking game we're making a mistake leads to the player having to make a drinking brighter forfeit yes I this is more like it but the best bit about the Wikipedia page citation needed yeah every was too drunk to kind of you know yeah anyway so things we know first bus has been used as an interview screening device for computer programmers um but my best best comment I saw last year or a couple years ago fizzbuzz was invented to avoid the awkwardness have realized that nobody in the room combined research and array it's just like what are we gonna do is if you're I don't know through fizzbuzz you know what about binary search huh we always get off by one error so let's just do this buzz okay so let's have a look at how not to do this there we go that's outstanding this is a work of art I had to really sit down and create this with some effort but it's based on stuff I've seen so um let's walk through some of the craziness here first of all let's just go retro okay let's let's just channel that kind of like macro stuff forget the Const keyword okay and we're gonna and so we're going to put a constant in for Fears and for buzz because you never know they might change which will never confuse anybody if you've now got a constant name phase and it does something else and then because you know you're everybody seems it seems to be almost a right at patter you've got to put a null macro in that yeah okay yeah now it's a real file yeah and then PI because you just never know yeah so you know it's a constant isn't it you know there's not much of it but you never know we might need a couple of decimal places then we have uh you know let's go for meaningful names yeah we could call it fizzbuzz no we're going to call it fizzbuzz calculator I mean we could actually make the name longer you know you can use your creativity here but it's just really and then we're going to bunch all the declarations up at the top so they can huddle like a family which is great but that's not how declarations are supposed to be it's not cold in the code they don't have to they're not like penguins okay and then we get some great stuff here oh we've got our we've got our chart we've got a buffer how big should a buffer be well I don't know I'm gonna I'm going to sprint F into it so I know 5:12 well I don't know what about 10 20 figure out sounds safe so yeah that's kind of reasoning that goes through them just to make sure it's all empty we're going to zero it because you just can't trust the compiler to do it itself I did actually see a few years ago somebody wrote and I'm going to say his name but he wrote two articles that were about initializing a character buffer a fixed length two articles and lots of template code I remember my comment was you can do this in one line in C and it generates the same code that you've done it's just I really trust the compiler they they're pretty good this kind of thing memset if memset is the fastest option the compiler will use memset if it's not the fastest option then why are you slowing it down let it do its business okay and then because this programmer having worked happily in the 1970s and 80s with those macros heard about lambdas decided to go mad and just put it all on one line so we've got these crazy lambdas for doing the division then we got a range checking normally the problems defined from the range to 1 to 100 then we do our serious printf business because you know that whole C++ thing and then we log to our unique instance and if it's bad and then we throw so we log and throw very popular technique and you know that takes up a few lines and then we have comments I haven't mentioned the comments here this is great they just repeat what we're going to do we do are we use our lambdas we do some more sprint F and then we have this beautiful result statement that clarifies the name of the variable reptile just in case we hadn't figured it out its return value it's in parentheses and we say assign return value reptile and return what's the next line ret Val dot sign and the flying after is return ret Val so you know that comment just make sure it's all good ok you just you know just in case the compiler says I have no idea what you're doing with a code let me look at the comment oh that's brilliant thank you yeah okay so this is a great this is a great observation this is Paul Anderson is he was a science fiction author the coin is thing called Anson's law I have yet to see any problem however complicated which when you looked at in the right way did not become still more complicated this is the manifesto for software development okay and how can we do this what are some of our favorite crazy examples in something like C++ well that code was the code it was the code that launched it all it was the code that launched template metaprogramming it was the realization by Irvin Unruh in 1994 that he could create a program that did not compile and lots of people are going like yeah I can do that every day of the week but his compilation message was the first few prime numbers he discovered that templates were cheering complete and then from that point on the result has been anything but temporary it's been permanently in certain code bases and every now and then you're just looking at stuff and just there's a flock of angle brackets and you're looking this I no idea what this is doing and your colleague will say it's really clever so what's it doing I have no idea it's like it's out of my short-term memory just don't don't ask me to do anything with it so yeah why am i showing this because you can do some really crazy stuff with hey if it's buzz and this is by Adam Peterson who use the boost libraries to create this buzz and when it doesn't compile it gives you first bus see that's far more interesting the most of the error messages you get these days sadly C++ 11 onwards have come along and spoiled our fun it turns out we can do compile time compilation and stuff and it's our you know we can do it all cleanly we don't have to just sort of wallow in the mess that is template metaprogramming now I could I've got here n modulo 3 and n modulo 5 I could reduce that to n mod 15 because that's going to be the same but there's a there's a reason I want to try and keep it here because if you look carefully what you realize and this is one of the fun things about the fizzbuzz problem is that there's no way seemingly no way of writing a fizzbuzz implementation where you do not do some repeated state check so your eyes are going to check you're either going to say okay divisible by three let's add fizz to it divisible by five let's add buz to it so that means you feel really clever because you don't have to do the fifteen case and you've used concatenation the only problem is you still have another if to do if something happened otherwise I need the number so you're always going to end up with some state dependencies some temporal ordering alternatively you can have something that doesn't rely on temporal ordering but then leads to duplicate condition because effectively we are testing three and five twice so this observation was made this as wonderful paper and presentation by mojo pirogue the default action is executed only if some previous actions were not executed so you know the example that as I mentioned before there we go there's honor there's our accumulation accumulator if it's empty then do something but we're still depending on it alternatively I get try and get really clever I do this in a much more declarative style okay when I say look I've got an array it's got empty it's got fears it's got another arrays got empty and bars and then the result is well let's just do modular arithmetic on each and concatenate it but I still have to have the second check I still have to go back and say did something happen so there's no way of avoiding these repeated checks or is there well I was the whole thing this guy did this in Haskell so we 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 so you have a perfect flow with no rechecking discontinuities or anything sounds perfect yeah I had it I translated the Haskell it was quite fun you know if you really want to keep your job this is how you do it cuz nobody's going to get rid of you so we've got first buzz um and I'm going to throw in a lambda just just for the hell of it and that lambda is going to take a function object that takes a string and returns a string and the lambda well the first lambda is going to do a mod 3 check and if it is divisible by 3 then it's going to return a lambda so when people talk about higher-order this is what they're talking about slightly confusing in that sense so we're going to return fears plus the execution of that function on empty otherwise we're going to return the thing that was just passed in and we've got buzz here as well and then we just do the things at the end what we do is we have a function that just returns whatever it was given and then we compose them together and we chain them together so that what we've got is the identity function so when I pass in anything to the identity function it returns it then it's passed to the buzz which then returns a function that is dependent on whatever was passed in here through to ID through to the buzz and then gap gets passed into phase so we've got this chain of lambdas that normally at this point your you might you might find your brain is sort of leaking out of your ears or you're falling asleep I don't know which one and you're thinking either your work either you're doing a sort of standard thing in other words what the hell is this or you're thinking he's got duplicate code if you're thinking duplicate code you're right we can get rid of that there we go let's have a go at this one so here I'm going to factor it out even further so now instead of a function that just takes a lando it's going to take a divisor and a string and a lambda and so it's really generalized and it's obviously much easier to understand and then I'm going to lambda this I'm going to use bind because I feel like I want to use a little more of the C++ standard library so I'm gonna use bind and then I'm going to use so I'm going to bind that together for three so I've got a phizzer now I've got a buzzer and then everything is as before and you're done and it's amazing just a few lines of code we've reclaimed that lost territory of template metaprogramming is now dead basically for most uses so where are you going to write your obscure code like this this is how you do it and this is this is how you create you know this is how you create the kind of code that's so clever the you you feel like you're an artist Oscar Wilde observed a work of art is the unique result of a unique temperament please don't do this in your code I'm only joking let's be very clear about that I don't need to walk out it's going to out it's a really cool technique yes it is really cool do it at home but not at work okay one of the other aspects on the opposite side of the clever clever range is the Congo cult programming cargo cult program is a style of programming characterized by ritual inclusion of code or program structures that serve no real purpose so ah you know what we end up with is a problem of signal-to-noise ratio okay signal-to-noise ratio is a measure used in science and engineering that compares the level of a desired signal to the level of background noise we often use it as a metaphor to refer to the ratio of useful information to false or irrelevant data in a conversation or exchange or a code base so you know this is the classic this is a great Twitter feed to follow by the way the expert beginner yeah one of those people in operations want from me I'm already logging at the beginning and end of every method yes I'll do the middle too it's just like yeah now we've got this perfect example one of the other cases we've already hinted at this is the way that people will are often just comment stuff and there is that what are you commenting I'm just commenting what the code does I'm commenting so anybody can understand it I made this observation a couple of years ago it's probably my most retweeted tweet : fallacy is to assume authors of incomprehensible code will somehow be able to express themselves lucidly and clearly in comments because if you think about it what you're effectively asking somebody to do when you say we need to cut please comment that code it's not entirely clear what it does I want you to comment it so that people can read it and understand it now wait a minute who's going to write the comment the person who is unable to write the code clearly is now going to try and explain the code clearly so suddenly they're I mean it does happen I have met people who have an incredible ability to explain things and and to actually write decent comments and documentation and yet their code is as obscure as the lambda code I just but I'm going to say they're fairly rare for the rest of us it's a case of like well I had no idea what I was doing in C++ so now give me anglish yep still no idea and that's what you're left with you now got the same problem twice but now everybody feels happy because you've got comments okay what's the other aspect of cargo co-op program umm can also refer to the results of applying a design pattern or coding style blindly without understanding the reasons behind that design principle previously I was very rude about singleton so I'm going to continue to be rude about singleton singleton there is actually a good singleton it is a whiskey and I discovered I was it's clearing out its kind of tying up my office earlier this year and I found an empty bottle of singleton I also found my my next-door neighbor gave me a bottle II said oh did you know there's a bottle of cider with your surname on it and it's possessive huh nice that's great and then my wife gave all of us in the family those customized coke bottles so that's careful in any singleton if you want yours you get your own okay that's my one okay but this one is actually better for code than the one you put in code yeah it'll cause you to think reasonably so but it does lead us into this kind of cargo cult programming style this was a thing that appeared on bonkers world last year the world seen by an object-oriented program and you had this is the naming gone mad this is exactly what I was talking about look there's a living space separation decorator a visitor monitor interface what I favorites the multi but supporter and there's also the entertainment provide a single turn it's just that our dear God no no and this is the problem we've ended up with what might be called homeopathic naming you know as we keep diluting the names by making them longer and longer because I'm just making it more meaningful it's just like no you're it's like you know you're not going to make the whisky stronger by adding water okay so you know this is an easily solved problem one of the ideas behind object orientation is you say hey look that's a door you can't call it door everybody will understand what you're talking about look put some other things in front of it like controller or interface or some other wording so all the other things I'm interested in is actually writing writing fiction short fiction and there's a book that has a series of prompts and cards to try and get you to focus on things and I found these are quite relevant to code choose the right name get specific doubt the problem is that sometimes people try to over generalize and the thing is that nobody wants overly general names and they also don't want overly general code this is where a lot of complexity comes from people don't have general problems they have specific problems if you have customers that have general problems I want those customers because they're great because they'll you know what they're going to do is they're going to contact you hey we need some software oh that's cool what would you like it to do oh I don't know it's something really general I suspect that's brilliant just send up an operating system because that's so general that you can build anything out of it and with it you know here is a compiler from that you can build the universe okay that's so general that you're able to customize it well how do I customize it you configure it using a special a special configuration language what's the language it's called programming language and you know you can then make it do anything so get specific don't be afraid to be specific now when it comes to naming our current troubles are not actually that modern all my favorite books of advice actually comes from outside the C++ well um small talk best practice patterns from the mid-1990s and Kent back he he observed back then you say look people will be using the words you choose in their conversation at than the next 20 years we are now at that point 20 years on from when that book was published you want to be sure you do it right unfortunately many people get all kind of formal just calling it what it is isn't enough so what we end up with and they have to tack on flowery computer science the impressive sound ultimately meaningless word like object any class that ends in the word object is no really honestly it's blindingly obvious it's a blower object because it that's its class I mean I can't say there than that thing I've seen that I've also seen stuff in code component part manager entity item these are all really popular and they were popular back then but not as popular as they are now and I had this interesting this is actually relates to a client visit that I had I've this is my recollection of the code although I'm pretty sure they didn't get it Const correct so but I do remember the bits that are correct they had class at the root of a hierarchy it only it was only a pure interface of one function the dot dot there is to cover the virtual destructor pure virtual and the class was called condition checker which sounds meaningful I mean it's got lots of syllables it sounds like it's a proper word it's describing things and then you have a single function check condition it seems very self-explanatory but I remember asking them when we started looking through the code and the hierarchy it's basically checking rules you know there's something true in which case we fire an event otherwise here and you're able to compose these rules looking at it's just like we were what does this really do and I talked through I said you mean it's a condition ah yes and you're checking whether or it's true yes right that's what it is it's a bit like the bit in the matrix where Morpheus says to neo stop trying to hit me and hit me it's not a condition checker it is a condition you're not checking the condition it's true or it isn't it really is we keep adding these extra levels of indirection you know I'm expecting one day to go into a code base check to see if it's okay with you if that condition is true and the full name is there and then probably put equals equals true just to be sure yeah I believe the Italians call that levell overeater you know it's the true truth as opposed to the false truth yeah but you gotta be really really sure you got to check that condition because it might move around when you're not looking anyway the great thing is C++ 11 onwards it's just that you don't need any of this it's just like boom just disappears in a puff of lambda and it's actually quite simple not the lambdas I had earlier okay so we need to worry about tidying things up we need to worry about clearing things up so I want to walk through a kind of a it's kind of area there this question of refactoring and refactoring something that is often abused as a term I've had discussions with people where you know they've used the word refactoring as I as I observe I saw so when were you and you're just using the word refactoring to mean change the code and I said that's not what it means this oh well you know that you won't have any effect here Kevin because that's how we use the word so you know these guys would be sitting there and you'd say what are you doing refactoring because that's what they're doing they're touching the code they're doing something and you'd say well what kind of refit what I'm trying to make it faster well that's called optimization you performance tuning I'm trying to fix a buck that's not refactoring I'm afraid that's just called bug fixing we don't have a really cool name for it I'm sorry okay so the thing is it turns out we've already got names for a number of activities and refactoring as it as a term it goes back to Bill Updike about 25 years ago funny enough in C++ and people have kind of forgotten that there is a history there and it turns out C++ is one of the languages that has the most rubbish tools for refactoring but it does have a precise meaning it's a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior and come back to that cuz I'm gonna be more precise about what Martin means by observable behavior and therefore to refactor is to do this to restructure software by applying a series of refactorings now of course when you refer to things things you're not just improving the code or hopefully not just improving the code you could be making it worse sometimes as we've seen and but there is an idea of there is an idea of the behavior what is the behavior that is unchanged because clearly it might get faster it might get slow and that's not what Martin is referring to and now a number of years ago I felt really uncomfortable with I realized I was very uncomfortable with one of the most common phrases that we have in coding and software developed as a whole is the idea of non-functional requirements and non-functional behaviors of a system and I'm just just to let you know that is just terrible English oh I just think about it for a moment you got functional requirements okay this is good you got all these functional clothes and then you've got these other requirements what are we going to call these I don't know they're not those do we have a better nap no okay let's call them not the functional requirements that's a bit long it's quite non functional problem is the word non-functional in English already has a meaning it means broken it means not functioning as the name suggests yeah so when people say you know you know what are your non-functional requirements I want it to crash every Tuesday I want to not work okay I wanted to not work because that is a non-functional requirement I want it to be out of service for the whole of Tuesday that's not what they mean it turns out so it's it's a it's a complete failure of naming a complete lack of imagination okay but also it actually groups lots of different things together that are coincidental and they're not actually the same so for example and the this this is kind of one of the sort of kind of classic example I think I've taken it in James likes book on chaos just imagine going to the zoo I went to the zoo what did you see I saw elephants oh cool what else did you see I saw non elephants tell me about the know elephants well you know there was a very big no elephants um very small ones there ones that flew there are ones that swam there were there were non elephants that like the cold and non elephants that like the warm it's just like you know what we might be missing something about the domain here a certain richness and texture to it so yeah so one of the observations you know aren't with it is you can do this as roughly a right home rule okay what you can do is you can separate out a number of aspects functional behavior that's the semantics well this is all good this is the stuff that passes all fails the unit tests operational behavior is how it operates how it runs does it run fast has run slow what's its availability how does it scales all of those ill-at-ease but importantly those are the runtime elytis those are properties of the system at runtime and therefore they are in some ways like the functional ones in that they can be experienced by a user they can be experienced in the running product and then we have these down here developmental the developmental aspects of a system some of which we may have requirements about are the aspects of what is it like to be in that code what is it like to live in their code what is the quality of the code that's your that's your portability maintainability this is properties that developers experience not the users of the system developers and grouping those together doesn't help us because it's really those guys that need to be getting together so refactoring when we say it it doesn't change the behavior doesn't change the functional behavior what we're doing is we're trying to keep that vertical axis the same stable don't break the code then the developmental axis we're trying to improve the operational axis may get better or worse it may go faster may go slower may use more memory may use less and obviously once you've got this model you've got a really simple way of talking about things like performance improvements such as I'm going to improve the performance the operational axis but what happens to the developmental quality oh by the way do keep the functional axis the same because otherwise you end up with that kind of classic problem of like well you know it runs really fast yes but it runs incorrectly yeah but the bugs run really fast don't they this is that No so the idea is that both our functionality preserving transformations but flip through 90 degrees so let's go and have a look at some code the code that kind of inspired me or Ella I accidentally recreated my clients coding style by making just a few simple assumptions and so we've got a function here and so I want to zoom in let's have a look at what we've got so we've got I've got a function called create server connection returns pointer to a connection we've got a bunch of declarations at the top and then we've got some checking we're going to check the we're going to get the address from racial marachek it's okay do log and throw if it's not Oh same again more they dress and then Oh same again you know keep on going and oh yeah something actually happens now we're going to do a conversion put by to look interesting a to I and I'm using data of the config pool which is a string I'm not sure about that and Oh beginning to get worried I hadn't seen the log and throw for a while but it's okay there's one there so we're going to do that we can actually create the dam connection that we said we were going to do and we're going to do lots of checking on it we're going to check that the result of new is actually not null mmm yeah and then we're going to ask it you okay you were right are we good yeah no oh okay log and throw and then and just to make sure because it's such it's such a build up it's like practically a film finale you know the the you know the ships exploding at the end we've got a comment return the connection because it's not obvious what we do okay so let's let's just go through this the first thing I want to draw to your attention essentially the thing I didn't say the comments dear god the comments if you end up writing code that comes in sections and you put a comment at the top of every section in fact of it you are naming a block it turns out there is a really cunning little feature in C++ for naming blocks it's called a function I know I know do you know this is straight out of nineteen seventy okay so really what we want to do is not that you never want a comment it's just that if you are simply compensating for other negative aspects of the code then that's not a good comment that comment is they're trying to kind of hold the thing up but actually I'm not just going to delete the comments what I'm going to do is I'm bugged by the fact that they are there's bad grammar and bad spelling in there so I'm going to fix that first of all okay and so we're going to fix the spelling and we're going to fix all the mistakes and then we're going to delete it okay Heidi okay so you know so yeah it just makes you feel more comfortable check that version in and then check the next version buh it's gone right so um now the next thing we're going to do is those you know those those declarations they were huddled at the top of the function it's like they're scared of the code okay this let'slet's distribute them amongst the code let's just move them down and the code becomes well shorter and more logically structured because we've moved everything to its appropriate point we moved everything closer also I'm going to sort of take advantage of the fact that Otto does an awful lot of work for us sometimes I mean some people I had got to the point a few years ago when Otto was initially introduced to do ultimate automated type deduction people say oh you're messing with the type system now really or not the type the compiler knows the type you know the type you know it why are you typing it out again why are you having to do this ritual and with some of the type names we get these days they get really long and in fact some of them we don't even know so therefore I'm going to go with that if something is not obvious then we need to make it clearer but normally the idea is you want to make your code more obvious you want to improve the obvious nough so it's clear what the intent of the right-hand side of the declaration is you don't need to worry so much about the left-hand side just as we want to improve it so we don't need the amount of comments so let's zoom in a little bit so yeah we've got some legacy coding practice here this is kind of whole let's just check when let's just check that new it's just like really I think I'm going to get rid of that because Mew does not return null it's as simple as that when people say well you know it does deal on some compilers I get this every now and then and I asked people what compiler they're using and then there they said oh I'm using GCC you know I'm using Visual Studio and I said oh right so you're using Visual Studio for and so no I didn't even know the numbers went down that low you mean this was like you know four ad no no no no they will see where they were numbered they weren't just years originally and so there's this idea of like actually hasn't done that for so long that the world has changed in that time so really just just go with it because trust me if you haven't check for null there are far worse things you should be checking for okay now there's a little bit of a thing going on here because let's have a look at that we've got this raw pointer floating around there's an allocation there yeah so some people who are look we've got a thing that does that Auto pointer stand aside non C++ 11 person I've got something better and so now you're gonna go yeah I've got unique pointer or what word is this this pointer doesn't work and so yes make unique actually this is one of the subtle things this is the wrong time to do this refactoring because that refactoring ends up with a signature change so what I'm going to do is I want to change the internal stuff before I start changing bigger stuff so actually I'm going to be a little modest I can say right I'm going to keep the external API stable and I'm going to use unique pointer internally then I release at the end so now is be very careful there is a natural kind of sequencing to refactorings that means you don't break the universe at least not all the time so let's go back to the big picture where we got here well there's a few bits and pieces let's zoom in on a couple of things so configured res dot data I feel very uncomfortable that because data from a string is not guaranteed to be null terminated now it's very unlikely you're going to find an implementation that doesn't do that but this is programming by coincidence it's not intended to do that so there's this idea of that actually it should be C straight if you want it to be null terminated it should be there but if you look more closely it's like why are we doing this at all a - I that's a C function we already have s - I and C++ 11 onwards so therefore I don't even need to worry about data and dot's easter I keep finding people trying to wrench the port beating heart of C out of there C++ objects and it's just like let it be it's okay they're good so um the next thing is then starting to look at the sprint F's and it's really funny because people just get into a habit and these are kind of copy and paste lines new york's printf buffer the magic 1024 up there and then we got the message percent s and then we look and you think that's really funny because we're doing the same thing basically what we're doing a string concatenation except for the one at the bottom it's like well hang on isn't there an easy way of doing this is there a safer way of doing this well first of all yes we can make it safe in other words actually not run the risk of buffer overruns let's use n let's use s n printf to actually make sure that we don't run off the end of the buffer your colleague will go no but that's why I made it 1024 to make sure we'd never run off it yeah cuz of course you know 1 kilobyte is just like yeah nobody's ever accidentally gonna run off the end of 1 kilobyte he says looking is multi-gigabyte machine it's like no nobody ever does that kind of mistake so yeah so we do that but then if we zoom in a little bit it's like lying on we've got this we're just concatenated strings you know this is this is this is all doing and that string up there is actually constant this is the funny things when you start looking closely at things you realize you know what we could just get rid of some of this stuff we can first of all make it safe by using string stream but then we can start getting rid of stuff and realizing that one is actually a static string and this one can you use ordinary string concatenation why we're using a sembra def or string stream at all okay so if you zoom out again things are looking a little bit tidier but what I've done here there's a number of different paths we could take at this point there sometimes people will say well let's try and rationalize the logging let's attack that sometimes the best thing to do is say I really don't like that log and throw technique but what I'm going to do is I'm going to put it to one side I'm going to have a function fail to connect and that will do log and throw okay so in other words I don't like the log and throw but we're going to do it and I'm going to wrap that up and that now reduces everything to one-liners and because I'm rebellious like this I'm going to get rid of those damn curly brackets because oh the code gets readable there's this interesting idea that we should always put curly brackets absolutely everywhere and sometimes it becomes ritualistic and there is a good reason that people do that but I like the alternative way of looking at this which is perhaps I should try and make perhaps I should try and make the block one-line long in other words why is it longer you know I was challenged yourself the other way why I got along the block yeah can I make it shorter particularly for these guard style clauses so now we're getting something that's much much shorter so it's a little bit more timing we can do here so we kind of zoom in and there's a few other bits and pieces so one of the things I'm now going to start doing is I'm going to start at this point I feel comfortable enough to actually make a signature change so let's go and make the signature change let's go back to it let's actually transfer ownership explicitly in a way the API makes it visible the next thing I'm going to do is I'm going to get rid of this kind of like object-oriented assembler you can always spot object-oriented assembler it's kind of like get and set and create and stuff like that create server connection that's not what you want to do and you might say well yes it is I want to create server connection no no what you actually want to do what you really try to do what I'm trying to do is connect to the server that's what you're trying to do that involves the creation of something but don't tell everybody about the mechanics tell them what your intent is you can the intent is connect to server which also by the way happens to be shorter this boy McCoy okay right let's have a look at this singleton configuration first thing I'm going to do is configuration manager can't get value okay first of all it's a value off stop getting everything I'll talk a bit more about getting tomorrow stop getting everything just it's a value of something that's what it is not getting anything the next thing is is it a configuration manager or is it just I don't know you know the configuration oh oh I can tell what it's doing now is actually the configuration that's what we're talking to we're not we don't have a manager that's been controlled by an entity that the level of indirection that's no it's actually the configuration so we've tidied this up but if we look closely we will realize that why are we doing all this processing in here why don't we just pass them in as arguments two string arguments there we go there's the configuration addressing so in other words what I've done is I actually I didn't need to do all that stuff on the singleton configuration because it turns out that we shouldn't have been using singleton anyway and parameterised behavior using yeah wait for it parameters that's why they call parameters parameterize behaviors just like 'wow functions were built for this so pass that stuff in and then if you look carefully you'll see there's a lot of stuff to do a string conversion and if you pause a moment I think what hang on why are we passing in strings why doesn't the caller sort this out why are we having to do this and this is probably being repeated elsewhere in the code so we're going to come back to that and just realize you know what we've got here is this it's like Oh most of the code disappeared because what we've actually done is realize that we're working with the wrong types most systems we find these days are what we call string ly typed and so there's an awful lot of work going into conversion and validation and conversion and validation of course if you are getting paid for the line of code that you're right ignore everything I've said but the point is you want to get those types as early as possible binding them to the right thing bind them to the right abstraction in this particular case we already have a UNIX level abstraction and it's just like right well that's it so now what we're doing is we are talking to the object and they're going to ask are you okay and then I'm going to do a fail to connect but why are we why are we having this dialogue with it if it failed it should throw an exception it shouldn't be create the object and then to forget to call OK on it it should just be this so if it fails it fails a constructor is not simply an ordinary function a constructor is a special function it is a transaction and it like a transaction it either completes and creates a new valid state of the system or it rolls back to give you the previous state in other words it aborts with an exception so it's a special kind of function so if the trail if this succeeds we get a connection if it fails we get an exception and the world hasn't changed except the presence of the exception and at this point we realize there's no point in having this function at all thank you very much how would you recommend to replace the log and throw pattern um the the log and throw approach I think is it's one of those cases of the first thing I would do is find out what people want from their logging and that's actually a surprisingly hard question because often we will use the code as a way of recognizing there is a problem and then try and instead of trying to solve it as a coder try and solve was what we want from it because often it's a log and throw is probably not the right thing normally what you want to do is get out of there and somebody else up here is responsible for logging problem is one of the things we find is that logging gets coupled into lots of smut lower level code and the idea is you're supposed to this is why it you know unwinds the hierarchy you get to the point where somebody can make a policy decision at this point this logs because we have a business meaning for what just failed and the content of the exception tells us what happened the problem is we do it too close we think we must do it there so there's this kind of idea of we've ended up with a kind of a sense of like our something's gone wrong we must do something so therefore we do everything and so the first thing is actually not to tackle it use it as a as a recognition it's a smell it doesn't give you a solution but use it as a smells awards what do we actually want here and then find out what people want from their logging because you may find it's not the same as the things you're going to throw an exception about and so that's the problem is a lot of logging ins I've been very developer centric but to code centric it's not it's not system centric so find that out and then make sure that you have a more separated policy and one little code base I worked on I ended up using a plug-in approach because I really wasn't sure so it was also not so much a microkernel as a nano kernel as a piece of code you plug stuff in and you'd say look bad things happen it calls this maybe it throws an exception maybe it locks maybe use some other callback the idea is that the core code remains fairly straightforward and something bad happened I'm going to tell somebody and then maybe I'll continue with it so what you do is you invert it to compensate for the indecision so all the into season goes on the outside of the code instead of the inside you're invited to use the break to ask Kevin any additional questions big thank you to Carolyn honey thank you very much [Applause]
Info
Channel: code::dive conference
Views: 78,835
Rating: 4.8390093 out of 5
Keywords: code::dive, codedive, C++
Id: brfqm9k6qzc
Channel Id: undefined
Length: 59min 22sec (3562 seconds)
Published: Wed Dec 14 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.