# Naming Things in Code

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

- **Канал:** CodeAesthetic
- **YouTube:** https://www.youtube.com/watch?v=-J3wNP6u5YU
- **Источник:** https://ekstraktznaniy.ru/video/26455

## Транскрипт

### Segment 1 (00:00 - 05:00) []

the classic quote is there are only two hard things in computer science cash invalidation and naming things I do agree that these are hard to get right but they're also easy to get wrong and we can get 80% of the way by avoiding bad patterns we're going to talk about what I consider to be bad naming practices that if you avoid you'll force yourself into letter naming the classic first example you'll see is you shouldn't name variables with a single letter I suspect this originally came from when math and computer science were more or less the same mathematicians Pride themselves on being turs they like to crystallize down to the smallest most concise way of expression the thing is you don't need to edit math but you do need to edit code so it's become obvious to programmers that we should avoid using single letter variable names because they don't tell you anything about the variable but I'd argue that you should take this one step further you should never abbreviate names period look at this code can you tell me its purpose what about now abbreviations rely on context that you may or may not have you spend more time reading code than writing code so forcing yourself to understand per system naming patterns makes it much harder to dig into unfamiliar code abbreviations used to help because of two reasons it saved you typing and screens were 80 characters wide but now when we write code we get this it takes less keyboard Strokes than ever to write variable names and we have massive 4K screens now so there's no real advantage to abbreviation don't put types in your name if you've edited older code on Windows you'll see something called Hungarian notation this is where you prefix the type to the variable name I think this goes back to before we had good standard types in C so everything would basically be an INT and their type of the variable wouldn't actually tell you what was inside of it but now with statically typed languages thep typ should tell you exactly what you're looking at so putting types in your variables is no longer necessary related it's considered good practice to put units in your variable names for example if you have a function that accepts a delay time if this value is in seconds you should name the variable delay seconds this way it's clear to the user of the function that they better be putting in seconds it's also more clear to someone editing the class itself what unit they're working with but even better than that is to have a type that removes the ambiguity completely for example in C the time span or Chrono duration in C++ the type abstracts the user from understanding the exact underlying unit you need to explicitly ask for a unit back like here getting seconds back for dynamically typed languages like python you sadly can't rely on type declarations to help so we'll need a bit of help from the variable name interestingly people also add types to their types in C there's this pattern of prefixing interfaces with I this is something I've never understood good code uses interfaces all the time and the consumer doesn't really care whether it's an interface class or abstract class they just want to know what they can call in this code we animate an object on the screen this is an interface if I swap this to an abstract class or concrete class it wouldn't change this code nor would it help the code do anything better C is still following this pattern even for new net Library code so for your C code it might make sense just to follow the pattern since bad Style Guidelines are better than no Style Guidelines for other languages I definitely avoid another example of typing their types is if you find yourself naming a class with bass or abstract this I've never found in a standard Library if I have a truck class and then realize it might make sense to create a parent class instead I've seen folks name the new parent Class B truck this isn't a great name because it doesn't help the users of the class it still represents a truck if you ever find yourself unable to come up with a good name for the parent class it probably

### Segment 2 (05:00 - 07:00) [5:00]

means that we should actually rename the child class instead of Bas truck let's just name it truck and for the child class let's overs specify the name we'll call it a trailer truck now if someone gets a truck they understand what they're getting it's a truck and they don't need to know about any of the details of subclasses and if they need to know a specific type of truck well then they get the specific name sometimes if you're struggling to name something it's actually indicative that the code structure is to blame a common anti pattern I see is if there's a collection of functions used widely in the code base but it's all bundled up into a single utils class or module if you're thinking of naming code utils or helper you should think if it's really the right spot for it here's some code from a node app that processes movies there's a bunch of util functions here firstly we should consider whether some of these methods actually make sense as a part of their respective types instead so this code here we can actually just move into the movie class itself for some of these we can instead create a class that represents a collection of movies and that has the desired methods and finally some of these can be separated into other classes with descriptive names the paging functionality can be moved into its own class and we can even make this generic if we want so that it can operate on more than just movies and the cookie function should really just be in a cookie class now we don't have anything in our utils class so we can just delete it you don't see a bundle of utils in standard libraries because they can all be sorted into modules that have good names so we should do the same in our code these few rules will help you write code that is easier to read and change what would you add
