DrupalCon Portland 2013

Cómo cuantificar de forma objetiva la calidad del código

Anthony Ferrara  · 

Presentación

PDF (pincha para descargar)

Vídeo

Transcripción

Extracto de la transcripción automática del vídeo realizada por YouTube.

alright I think we're going to get started here hello my name is mike Nielsen and i'm the coding a development track chair and i'm here to introduce the coding developments featured speaker anthony our anti fraud is a core PHP contributor is specialty

for working how long is encryption and security in his spare time he does Swift brought water rescues either either an avid whitewater guy he couple of other need to look back to it's about him he got his pilot's license before his driver's license

he's blown a military and growing flown a mock russian military grant he's also a volunteer firefighter emt has to cpr saves was to his credit so that's really awesome let's see in two thousand he traveled over 300,000 air miles and early total

a cart three years old which is something he and i have in common so everybody get quick hand for empty ferrara thank you very much and everyone hear me because I like to move around a lot so today is a whole lot of science everything will talk to you about

is based in science and really care about is that it's close enough okay so I'm going to talk about a lot of things but consider them teristics they're not really yes your code is bad yes your focus group with more of friends and being able to

identify trends so so we care about complexity well there's a quote that I love the central enemy of reliability is compressible and it's true the more complex piece of software bugs in it the harder to be made with features and less complex a higher

the quality or at least it's hard to have complex code that is also higher now I want to make a caveat the only definitive measure is what the expert in it but again we want to get an idea we want to get a concept ice play some kind of scientific method

so the first basic metric I'd like to talk about is called cyclomatic complexity and it's actually really really simple it's a number of decision points but we also need to add one of the function itself so this dysfunction as a sick o matic complexity

simple straight over so some basic numbers if you have between one and four we say it's low complex it's not complex as low complexity 5 to 7 11 blah blah blah the point here though is if you look at a function and you see more than 11 two statements

don't you probably doing too much right this metric battle is a way of putting a number to common sense now no come here I said for a single met because we can also aggregate this console complexity her method and the numbers in the reason they train because

if you look at a function in the past it's pretty easy to public or not if you look at an entire and every function has for our statements it's a lot harder to realize what's going on so I ran and WordPress has an average complexity of 6.2 which

if you remember here is very highly complex I actually did this line before I did the next slide so I'm not eyes dear drupal 8 is actually very good at 2.1 sent me to is 1.8 and laravel noted that this number is do that can indicate that your copies too

meaning it's too spread out and it's not doing deficit so generally I like to say between like 1.6 and 2.5 is a good target to the app so tuple 7 is a little complex for my tastes but group of eight is perfectly so doubtingly we can actually analyze

this is if we look at the average complexity per line of code this handles code bases that are functional or code bases that are mostly procedural a lot better than previous birth so in this number actual rules and this is really easy to look at it real life

so low complexity is between point 0 1 point 0 5 but what does that mean well it means that one percent the five percent of your lines of code or consider running the same analysis point to wordpress and drupal starting here now looks really good so it kind

of gives us a picture to say we start either look into say why is so we're not really sure yet whether we have a good idea that's we need some other matters so let's talk about impact complexity which on the surface is very very similar to stick

lomatic important it's the number of unique tabs in a routine function remember so what is that well we take that original code base let me add and we run through it we can execute it without going inside of any of you as one thing we can go into only

the first so second that we can go into the else or we can go into the first so we have four paths so straight over notice thank for that example they're the same but in general they both always be the same so let's take a slightly different example

basically what this function does is it remembers together condition in PHP you could just return a plus B plus C but for purposes of demonstration what no cyclomatic complexity and then like city are related but they're slightly different so this function

and I'm going too fast for anybody please raise your hand I'll me and ask your question so it's pretty easy to see cyclomatic complexity is we let's count the number of cats we have no missed the first one only the first one in the second one

the first second third the first third so on so on so on so it turns out we have eight so your upper limit on it half is 2 raised to the cyclomatic complexity linesman again don't be overly concerned just realize that they're related but they're

not the same measurement they actually measure different things so with enhanced complexity we have similar ranges these ranges are basically pulled from the other and you can find the stuff on the internet and I'll get some resources a little bit so what

does this mean what does impact really mean really translated it's the minimum number of tests required to completely test routine so if you only have one test for a method that has a impact that might be the eighth you're missing seven test cases

so now we can use this interesting metric to really look and dive deep in system code some interesting good so if we think about it for a second if one has fermented well let's apply it to Drupal 7 let's take a look at some triple seven devils the

first one the first function I want to analyze is entity eaters for loop and represent really simple has one if statement and it returns clear what it does very good can anyone tell me what the cyclomatic complexity of this is going to be two very good which

is low complexity and the in Haskell exiting again too awesome this is what you want now let's look at a different function also from Drupal 7 input so page one page to page three page for sick lomatic complexity of 41 there's 41 decision points in

this function now this is why I love and half the quantity because it takes well ok very highly complex what does that mean 25 billion tests to completely test that one function we pissed off now I want to put this in context so if we assume a test is one

line of code ok it would take two terabytes worth of taxes the test is one pointer pretty impressive huh that's 400 12 dvds for 670 thousands of oil installations that's a heck of a lot of hats we might want to do something about that like I bought

[ ... ]

Nota: se han omitido las otras 3.574 palabras de la transcripción completa para cumplir con las normas de «uso razonable» de YouTube.