# 3 More Laws of Writing Readable Code

## Метаданные

- **Канал:** Kantan Coding
- **YouTube:** https://www.youtube.com/watch?v=8sWDThdFFyI

## Содержание

### [0:00](https://www.youtube.com/watch?v=8sWDThdFFyI) Segment 1 (00:00 - 04:00)

does your code tell the truth let me put it a different way let's imagine there's a function that takes in a radius and calculates the area of a circle using the formula p r s then returns that area let's also Imagine That for good measure and to help with readability the dev who wrote this function threw in some comments that explain what the function's doing now fast forward a couple months later a different Dev is tasked with refactoring the function to calculate the area of a circle differently this time with the diameter being passed in and as is common in these types of situations let's imagine the dev forgets to update the comments after making their changes now I'll ask you the question again does your code tell the truth if it doesn't then you're likely not making use of the first law write self-documenting code now going back to the original function first of all these comments that explain what this function is doing are redundant we can already see the calculations that are happening just by looking at the code so removing them eliminates the possibility for these comments to lie to us in the future the name of this function on the other hand is far too ambiguous to make the code more self-documenting we can give it a more explicit name calculate area of circle for example sticking with the theme of comments let's imagine we have a function that calculates the tax for a given price and let's also imagine that there's some business requirement where items over a certain price command a higher tax for whatever reason and this is explained in a comment above the function so in the function body we just check if the price is over that threshold and if so calculate the tax at the higher rate otherwise calculate the tax with the normal rate and for good measure let's say that the dev added comments explaining each tax rate now the issue with this code is that it's riddled with magic numbers and as a result the dev had to compensate by making excessive use of comments which brings us to the second law which is to avoid magic numbers a better approach would be to use descriptive named constants for those magic numbers now when you read the code it's clear that when the price exceeds a certain threshold then a higher value item tax is applied and this helps us to avoid the need to riddle this function with comments the next example is a simple function to create a new user but in this case we're overwhelmed with function parameters now this doesn't make for very readable code especially if you're reading a call to this function you're presented with a row of random values with no clear indication of what they represent not to mention when calling this function it's easy to accidentally incorrectly swap values of the same type when passing them in which brings us to the final law in this video which is to avoid too many function parameters one more readable way of handling this would be to create an object that comprises all of these fields which we'll call user and then a builder for that object we can then use that Builder to build out the user explicitly setting values to their corresponding fields and finally we could adjust the function to take just one parameter which is a user

---
*Источник: https://ekstraktznaniy.ru/video/31501*