×

Smok Code's video: Is your function too big

@Is your function too big?
What is a good metric we can follow to determine if function should be broken in parts - learn this with . Clean Code (R. Martin) - US: https://amzn.to/3jVUEDU | UK: https://amzn.to/2Iij4Jz Best Practices for Programmers - US: https://amzn.to/2SPRSEg | UK: https://amzn.to/2Fr2h6e When writing code we often hear that it is a good practice to keep your functions short, small or just concise, but what is the correct length of a function? This is often a very serious problem for the new developers, but more experienced ask this question too. Let’s start by explaining why it matters (and I hate to break it to you: it’s not because we used to have screens that couldn’t fit more than 24 lines at once). Methods should be small to be easy to reason about. As programmers we’re very good at managing multiple levels of abstraction in our heads, but we could never fit entire programs with all their low-level details. So if the function is too long it will be hard to reason about. Programming is difficult as it is - we don’t need any further complications. So paraphrasing famous Uncle Bob: your function is already too big. When you refactor it and make it smaller - you should actually make it even smaller than that. If you need a concrete number: functions shouldn’t ever be over 100 lines long, actually they shouldn’t be longer than 20 lines. And a line should be 150 characters long tops. So how to tell that your function is too long and should be broken into more functions? The most obvious question is: does it do one thing well and only that thing? If it does more than that - it’s too long. You can see already that we aren’t really focusing on the actual number of lines, but rather the logical scope of the function, and how it’s fulfilling the promise given in its name. Other red flags? Nested control structures: for loop in another loop or switch, multiple levels of if-statements? Too long. Having the same code in multiple functions? Too long. Got comments explaining what actually happens in the function? Too long. You don’t know immediately what exactly each line does? This function is too long. If your function touches different levels of abstraction at the same time - it is too long. If it can’t fit on the screen… Yep. Too long again. Another measure of function correctness when it comes to their size is the number of arguments used to call the function. Clean code practices state that an ideal function should have no arguments, if that’s not possible we should use one, and only if that’s not possible - two. Using three arguments needs special justification, and usually this justification is already wrong. These rules may sound harsh, but they’re exaggerated for a reason: to be fearful of long, complex functions that use dozens of arguments, because they're the best place for bugs to form their nest, lay eggs and spread to other parts of the code. Remember - it’s much easier to keep your code clean from the start, than refactor everything. Subscribe, and I’ll see you in the next one, cheers!

69

12
Smok Code
Subscribers
15K
Total Post
87
Total Views
318.6K
Avg. Views
5.7K
View Profile
This video was published on 2020-10-16 19:30:12 GMT by @Smok on Youtube. Smok Code has total 15K subscribers on Youtube and has a total of 87 video.This video has received 69 Likes which are lower than the average likes that Smok Code gets . @Smok receives an average views of 5.7K per video on Youtube.This video has received 12 comments which are lower than the average comments that Smok Code gets . Overall the views for this video was lower than the average for the profile.Smok Code #QuickAnswer. #programming #tech #softwaredevelopment has been used frequently in this Post.

Other post by @Smok