Writing Beautiful Code

Video in TIB AV-Portal: Writing Beautiful Code

Formal Metadata

Writing Beautiful Code
Title of Series
CC Attribution - NonCommercial - ShareAlike 3.0 Unported:
You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal and non-commercial purpose as long as the work is attributed to the author in the manner specified by the author or licensor and the work or content is shared also in adapted form only under the conditions of this license.
Release Date

Content Metadata

Subject Area
Writing Beautiful Code [EuroPython 2017 - Talk - 2017-07-11 - PyCharm Room] [Rimini, Italy] Writing code is hard. Writing beautiful code is even more so. How to write code that is pleasant to read, easy to understand and joy to maintain? Simple things like using right variable names, avoiding to much nesting, using white space and comments etc. makes a lot difference to readability of the code. Even though these things look very simple, even experienced programmers find it hard to put them in practice. In this talk, I’ll try summarise the age old wisdom of writing beautiful code and explain those ideas using practical examples written in Python
Software Website Machine code
Human migration NP-hard Mechanism design Word Term (mathematics) Building Computing platform Pattern language James Waddell Alexander II Computer programming Row (database) Computer architecture
Context awareness Group action Length State of matter Multiplication sign Range (statistics) Water vapor Open set Function (mathematics) Mereology Computer programming Variable (mathematics) Neuroinformatik Medical imaging Mathematics Type theory Different (Kate Ryan album) Core dump Social class Theory of relativity Interior (topology) Electronic mailing list Computer Physicalism Range (statistics) Lattice (order) Price index Flow separation Type theory Category of being Arithmetic mean Process (computing) Order (biology) Website Normal (geometry) Summierbarkeit Right angle Cycle (graph theory) Figurate number Data type Data structure Task (computing) Writing Point (geometry) Functional (mathematics) Computer file Line (geometry) Rule of inference Number Latent heat Cache (computing) String (computer science) Subject indexing Summierbarkeit Loop (music) Form (programming) Task (computing) Boolean algebra Computer program Line (geometry) Machine code Subject indexing Number Commitment scheme Function (mathematics) Statement (computer science) Social class Musical ensemble Coefficient Scheduling (computing) Family
Axiom of choice Email Pixel Length Multiplication sign Mereology Computer programming Independence (probability theory) Heegaard splitting Semiconductor memory Object (grammar) Personal digital assistant Core dump Query language Process (computing) Endliche Modelltheorie Logic gate Social class Exception handling Block (periodic table) Structural load Gradient Parameter (computer programming) Bit Arithmetic mean Website output Data conversion Right angle Modul <Datentyp> Energy level Figurate number Writing Row (database) Point (geometry) Functional (mathematics) Implementation Computer file Line (geometry) Number Wave packet Frequency Read-only memory Operator (mathematics) Energy level Implementation Condition number Self-organization Rule of inference Addition Axiom of choice Compass (drafting) Computer program Independence (probability theory) Line (geometry) Machine code Group action Word Error message Search engine (computing) Personal digital assistant Function (mathematics) Video game Key (cryptography) Mathematical optimization Exception handling
Functional (mathematics) Multiplication sign Data analysis Mereology Computer programming Variable (mathematics) Independence (probability theory) Connected space Programmschleife Core dump Nichtlineares Gleichungssystem Data structure Implementation Chemical equation Stress (mechanics) Variable (mathematics) Computer programming System call Function (mathematics) String (computer science) Right angle Modul <Datentyp> Programmschleife Data structure Writing
Point (geometry) Web page Trail Functional (mathematics) Group action Length Multiplication sign Mereology Computer programming Number Element (mathematics) Bit rate Computer configuration Metropolitan area network Condition number Constraint (mathematics) File format Block (periodic table) Chemical equation Weight Maxima and minima Machine code Line (geometry) Variable (mathematics) Word Coding theory Network topology Website Quicksort Object (grammar) Local ring
the afternoon this is somewhat of a what something that's dear to my heart a difficult of the so the 1st cabinet produced myself so my design and so on so I arrived despite by the ghost the kind
that and once for them which is that people Academy a month so all of the understand color-coded and we're burning incense pattern from that so I use Python Harriet at my work and also teach the land to the students so that we start at the
quote from the crystal rows and the who coined the term about the migration so it's really hard to say what is beautiful and your political and actually felt while still Carol front nasal hands yes so and so it it's really hard to say no but was what according to what makes a quot beautiful OK so it's kind of some people say it's a Alexander this words exegesis it's the quality of that name so as you look at it it and fill of but it's really hard c c . mechanics of architecture with a place to and when you're utterances well answer programming that's not so that the court does
not uh this from the various companies book sector in recent commitments suppose that was written for people to read and only instantly for questions 6 here it's a profound statement because we usually we think programs are into to just get some job that the so like for computer that's what something but if you look at you deeply this must be for it and the reason is that look cycle of the typical from the program right once for stands for the computer for the that for the manipulation of the lifetime of the program sometimes I mean someone is looking them and sometimes you have to understand the core 11 is what happens is would add a quarter after we could do that with just can make sense of the program usually call this mantle face OK so sodium whatever right programs to keep in mind of the uh the people ordered according to the point socialist site to improve the image of the Romans so
all of course that decided that would be simple low-hanging fruit that everyone knows that I had to do it but does not meet people do this the to manipulated is so important that uh you know people with love experience fade to the attention to this of a despite unconditionally I do Python once by the court as to what an coefficients so I really find frustrating when people would like to fully use of program experience and a kind you and pick right of length and it's under the can't includes is that we don't despair that tension the dance uh modest show you some examples how important it is to make available vitamin so that the quote of and that 2 hot discovered Center catching relation and its meaning naming things is not easy it's not what's the longest tenure spent by you had to figure out what name to give to a class of filing more readable and we're having spending the to for figure out what should be this thing what point of land at so many things takes time and she knew that I can so it is important to do that so fast that this I wouldn't it means the really don't make sense if you want to so so what we do is have temporary wearable stamp them to manage a data it's going to was indignant severely band understand what the mean and the use of a team more specific names for example of yeah so uh should was visitors from the context what that means now the thing is that the you to the end of indicator maximize them the beam is 1 which is so what's so what answers think so I would using relations of people say you see of properties from ITER but doesn't it's really bad Sanderson what user fees so pivoting at 2 dB a recently even go that's so well in this reference that's fine but don't using something like the bank account does make sense that it's really hard promised understand probably say form items are encompass something so part of that is uh at the Buddha tumbled don't use solutions and this that's really common thing that everyone knows this other colonies is I keep finding in the people as these are data type is a name of variable this it's a list say OK but the list of what stink putting 1 right so it is better to say that actually what it says it is sum of numbers of codewords takes a string but actually sentence subparagraph filing so it's better to say specifically what it actually means not just think by profit that makes it a lot more readable to better tumble water norms and that is the use not for but it was in process the the same concepts and you have some functions so the actions so these of that sentence what looked so size price task the it's about the mobile phone or that it wasn't classes and you actions get filing is of this thing of function as violent to get filing so that it can indicates the inaction Mike account or deposit so the other kind of examples where our using that make sense for of functions and 0 you got a very simple rules massively into the age or reason that's been talked about from so many people need you find the practice of programming are this other books people written but understand to take that someone the output in the context of items so we have to look at at a family and list of values it's good to use approval from that so assailants lines of flights are also the severity and you get 5 there but don't look at other examples suffice always surfaces that are certified list of files to better use to say that's actually a list of values and this is exact and let's see I have a few examples of some people even been using it so people to 4 lines in open fire liberty lent the meeting really lines useless of lines the 2 images in the line not lengths so and stating dumplings does make sense of our right so as it's better to have a useful for a list under when use the open seas this different look at Unix nexus only not for the like this but it for example for i in range 10 is fine I is an index you going order of values Europe that was going what a list of values using ideas does make sense because I usually use for an index and of using high order numbers if it's like a it's a single level India but include the number of reproducing or anything right so use something which is more than this what actually want random was found what makes that much you might be thinking like what about what's really think figure that things out order organized but it's really hard to get it in practice ok so let me show an example the small and functionality can use science and what this is doing it's on the state let it and then see what is not good so explaining what this function that's the so yes right column wow awesome yeah that's a disorganized but let's look at this so you look at the same function written like that at all and that is of change the names of the martyrs in the center of the program so the occurrences of program and it's really hard to understand what is being X and Y and the writing things was edited so but since the names he made so much of a difference OK now we're looking the medieval that's the dataset it's an index and you're taking the role and then from there I think it that and and I would think about what I do so at these example because of our add to show how much difference news makes the users convention but this of dying to show how much this makes sense OK so that if f island could can make so much of a difference in being right available and imagine what would happen if very what the final in program are then doesn't program right so I but it's really wanting to think that I'd start names so that the thing is that when using similar means for comparing videotex it's going really confusing so when you're writing code a chance to keep in mind what people think but unconsciously OK so when you see a names is something similar we unconsciously expect that that the horn solidified its ceiling any would do this must be the same kind of ideas but you wouldn't listen 1 of them in the year that's really confusing so we had 2 of make sense of that so probably say values are an interesting only to and that's that's 1 of the issues that we had be used for example as well as X and Y in physics and must be the same types enabled there really supported India's or something but they're not by no it's look at comments thus if you believe writing garment is good but is it really when there's really no
need to see the obvious intimate explained to the 2nd and by the promise we look at it by the warranty on the recordings but to but this is that's obvious thing is all to write a common but it actually explain why you're doing it compensate for border and what sites for your adding 1 pixel on both sides you adding to so that makes more sense of coming why you're doing that so don't obviously weighted that and a lot of times it makes a lot of sense when you commenting on added a comment explaining why you made a choice so the falling out in operations in front of a desk as so in saying this is the reason why the following code of what is being done this way and uh so it's a good do dopamine special cases for example of you figured out unique career happening in your life what's happening and this nicely because that issue OK so about were then found wanted this especially this and Celtics fixing it so in future people will be careful in that in that part of core and such leader of actually make comments and gate conveyed codes that you that you really write comments for example but look at the 1st of the finding of longest line so this convention and find mathematically the that to see any enough along these lines you only have to comment and if so already cleared the program itself so you can make some of the core so that the code is simple enough for people to understand why he does a points of you have to write love conditions so that's something of the unsolicited do that because of yeah the other thing is of no longer functions what we can do is you have Stage 1 Stage 2 added block on say that this is what it does so crosses documents and upload obsession and as of and of course but it makes of more sense to spilled that into smaller functions and then oxygen across documents and then the 2nd summit search engine so you really need comments 6 part of course good now that we have a bit of and the other simple things that are more exible things like gradable lanes and common sense so that the optimal modulation so do you expect your program uh so divide and conquer so speed your program of smaller independent models and functions of the traits bodies and for people to make sense of that the the the coat the miller slot what it says is number acceleration when can would in working memory some person wants to is a prophet has performed implications on the how we should take the court reason is some us some ice 2 ways the number of things that Norton had that means that probably means when writing function should have more lands and that all have a class that them up when the 2nd public EPA should not more than that so when someone looks at your class and then stacked autonomic start using it it it has its own cluster minus functions the kind of people evidence of what memory and then would work with the but there's a lot of them too difficult so that's that about figure to keep in mind when you're writing a classes of functions so I think that's a good number to keep the size of function as well and that of the common thing people on this in women grandparents and as is duplication he said this is bad for example of what it's a function that takes an input data and dice 2 of them are that and then some some appointed by back but it so in the since the data is coming from you this model some a lattice intimate before x summing up so there's an hour train except but we're doing it twice at is actually really rigid and uh so this do we wanting a copy paste in 1 4 things and at see can afford them quickly left x here because that's but that's kind of of that we get into the OK so there is some duplicated we should decide whether actually generalize that say what you get in to the input data and then X and Y and and we maximize if you look at that function here attorneys Venice and now than what we had before the now of course just want you doing this 11 swing how function and it may have that is the mandible like writing for like this but it's a bit about was so there are many cases we 1 of in Locke was 118 of tag so is a big function that kind of stuff going all the steps but you see that the wheel loads of doing this 11 years so that's kind of hard to understand so what can be done is you can take each part of that and then make that a separate function that really makes it's not available OK so look at this function this functions as really getting it to other functions so if you look at this function it's easier to understand for the for the the can look at each of these of the functions to that of other of useful thing is to make sure you kind letters separately for example this function assigned to I'll get it the user from doing that in this to it's values are not I designed users to make sure they comes not what and then you got it the reason the use of but if see the core of the function the main function of this I brought this function is hidden in 2 levels deep a OK so according you that the splitting is that this is the core of the function but that's a hidden a deep inside of my house so we've conditions can be stored in the but would do better that this will be the most prominent part of the function so what you can do is compute it's handling a separate for example in this so you have to generalization fast and then in the top level you have the code of the function the so you get a gradient and created a risk of users and if you really think a function we just give their understanding that exam 2 of the main part of functions which is not so straightforward if you uh doing it like this note that the other away important thing is that we should try to suppress what is Nederlands which is possible reason is when someone understand programs and that that what of what the program is doing and how the program in agreement occluded from thanks so when someone comes to program means is more is to understand what the program is doing right so what we should implement we should suffer as it receivers as much as possible so that you can understand why they did have the program pretty as quickly the and then if required can go and see what each of the functions of the for example this rollout takes 1 time and I went to the file name and then the other words in the file compass of frequent of the words and you can see this is what we so as for length and the solid doing so the intended what a program is doing is very clear by looking at this program OK but right along the function and then put in the how the what's the red and all what things completed radians in function becomes too he couldn't understand why it's always doing it now I know that the word frequency company what frequency and then good what we can to function figure out how it's actually done they do additions the implementation and the Indian can actually separated them how on what it's good to separate those 2 things so that was to so now that's what I think of this the things
we want much more the writing between core and this is a quick summary of what of College of Art so choose many with limb so I can't stress enough how important it is part of it this is a mistake on a a lot of time but it's still it's what's going the time because of the amount of time we spend later trying to figure out why the revealing that our thanks for your call holds sexy working of is lot more if you don't spend enough time really to protect and is common to an equation that would also put comments just because you have to a speedup morning even when using functions and a wide duplication now at all cost suffers new is new data analysis was readability that these topics a quote from the programming and
have program should be like this as it somebody's gonna collect a single so I don't know what the elegance of the program that the spreading of burden should be attained short and having the clarity of that each of the program the there should be made to be too little or too much needed needed loops are not users variables the lack of structural Poland rigidity now to have the right balance program to make them beautiful of encoding and it's more than 4
questions the few
constraints of the of wanted to read and find out what be when we was use so if user for the might it I am so when you talked about the number of objects that the human brain track have what's your opinion on these 4 the recommended maximum minimum length of code block thank you they that the sort of recommended length of a code block so there's there's a school of thought that says if a code block is larger than and lines it becomes hard to read words and is to be any number between 40 years so and I think the humble is it should be more than half a page of sheets setting is what people say but I would say when y you usually the condition is not more than a balance is what maximum tree for a function the so telling examples eventually what's number of lines the function the what are your thoughts on code formatters likewise here to group please repeat occurred and what are your thoughts on code formatting tools likewise he have so but I think it's of and lipid pizza topological format to like yeah I think it's a really good to learn and so I think it's called of the bike should this Christian sites should be used so once the subtle spacer for this space before or after the good stuff and get used to whatever it is and the thing will form I'd go I see them really good the what of year 2 options you have this follows 1 formatting thing but I don't think we have and that obtained by the node is that they did something people this over the phone or not addicted were only allowed to have something that 4 bytes the I the so it is just a quick question not some it so the most specific non-tangible paradigms and by whole have agreed also is that of the rate that is what about this habit of deleting all the valves in variable is like trying to they picking a name and then abbreviated by deleting all the but I hate this what do you think about this practice of the injected principle the Christian that is kind of habit of deleting all the bells and variable names and tells the and to try and make a charter like some this is a trade between variable length and intelligibility yes uh so I think it is important data point so would say the regular and the length of the variable should be proportional can be published as to the scope of this of this for example is a local variable valve function which is 5 6 lines that make its value OK to have a single readable but when the ability will of which the scope is much larger is better to be most and have the full name so so this is a small function certified loop but also for the words so that you can think it is a part of its in element of words so that's a single word so that the polyol could have similar to but I don't really like to decode always sort of make it down a shot just the of that is more abundant than the length of the available that's what I say I the the thank you and in your opinion what is a good time to clean up your code when programming but I would say that it it it's and it's of having that individual time but it so how about you want to stand keeping these things in mind writing court of look into 1 school going to feel that it's gonna weighting that principle of the solution stack did in that sense of writing thing about smells so that when you're ready according even actually died that kind of thing so that the soul of man probably even your beginning you infer big takes a while to get that have solidified it and then the number but once you get to that would have been in probably stacked and in that sense that it can be considered having the the new versions OK and it's all done thanks few