Naming Things in Code

CodeAesthetic
27 Nov 202207:24

Summary

TLDRThe video script discusses the challenges of naming in computer science, emphasizing the importance of clear and meaningful variable names. It advises against single-letter variables, abbreviations, and including types in variable names, as they can obscure code clarity. Instead, it suggests using descriptive names, units, and avoiding the bundling of functions into generic 'utils' classes. The speaker advocates for a structured approach to naming that enhances code readability and maintainability, ultimately leading to better software design.

Takeaways

  • 🚫 Avoid single-letter variable names as they lack descriptiveness.
  • 📝 Never abbreviate names; they rely on context and can confuse readers.
  • 🖥️ With modern coding environments, the need for abbreviations has diminished.
  • 📊 Don't include types in variable names; let the type system do the work.
  • 🔢 Use units in variable names for clarity, especially in statically typed languages.
  • 🔄 For dynamically typed languages, use descriptive variable names to convey units.
  • 🔧 Interfaces in C# should not be prefixed with 'I' as it doesn't add value to the user.
  • 📚 Avoid using 'Base' or 'Abstract' in class names; they don't convey useful information.
  • 📁 Consider refactoring if struggling to name a class; it may indicate a structural issue.
  • 📂 Grouping functions into a 'utils' class is an anti-pattern; prefer descriptive, modular organization.

Q & A

  • What are the two hard things in computer science mentioned in the transcript?

    -Cache invalidation and naming things.

  • Why should single-letter variable names be avoided?

    -They don't provide any meaningful information about the variable's purpose.

  • What is the argument against using abbreviations in variable names?

    -Abbreviations rely on context that may not be immediately clear, making it harder to understand unfamiliar code.

  • Why is it no longer necessary to include types in variable names?

    -In statically typed languages, the type declaration should clearly indicate the nature of the variable, making additional type information in the name redundant.

  • What is the recommended practice for indicating units in variable names?

    -Including the unit directly in the variable name, such as 'delaySeconds', makes it clear what unit the variable represents.

  • Why is prefixing interfaces with 'I' in C# not recommended?

    -It doesn't provide additional value to the user of the interface, as they are more concerned with what they can call rather than the type of the object.

  • What should you do if you find yourself naming a parent class with 'Base' or 'Abstract'?

    -Consider renaming the child class to a more specific name, as the parent class should represent the general concept, not the base or abstract form.

  • What is the common anti-pattern of bundling functions into a single utils class?

    -It can lead to a lack of clarity and maintainability, as functions should ideally be part of their respective types or organized into classes with descriptive names.

  • How can you improve the readability and maintainability of a codebase with many utility functions?

    -By moving functions to their appropriate classes or creating new classes with descriptive names, and avoiding the creation of a catch-all 'utils' class.

  • What is the importance of following good naming conventions in coding?

    -Good naming conventions make code easier to read, understand, and change, ultimately leading to better maintainability and collaboration.

Outlines

00:00

📝 Good Naming Practices in Programming

This paragraph discusses the importance of proper naming conventions in programming. It starts with the classic quote about the two hard things in computer science and emphasizes the ease of getting naming wrong. The speaker suggests avoiding single-letter variable names and abbreviations, as they lack context and clarity. The paragraph also criticizes the practice of including types in variable names, especially in statically typed languages, and advocates for using units in variable names for clarity. The speaker provides examples and explains how to improve code readability by avoiding anti-patterns such as bundling functions into a 'utils' class.

05:06

🚀 Structuring Code for Clarity

The second paragraph focuses on the importance of code structure and how it affects naming. It advises against using overly general names for classes and methods, such as 'utils' or 'helper', and instead encourages breaking down functionalities into more specific, descriptive classes. The speaker provides an example of refactoring a 'utils' class into more focused classes, each with a clear purpose. This approach leads to a more maintainable and understandable codebase, where each class and function has a clear and specific role.

Mindmap

Keywords

💡cache invalidation

Cache invalidation refers to the process of ensuring that the data stored in a cache is up-to-date and consistent with the original source. In the context of the video, it's one of the two hard things in computer science, highlighting the complexity involved in maintaining data integrity in systems with caching mechanisms.

💡naming things

Naming things in programming refers to the practice of assigning meaningful and descriptive names to variables, functions, and other elements within code. The video emphasizes the importance of proper naming as it aids in code readability and maintainability. Good naming practices are contrasted with bad ones, such as using single-letter variable names or abbreviations.

💡bad naming practices

Bad naming practices are those that lead to confusing, ambiguous, or non-descriptive names for code elements. The video criticizes practices like using single-letter names, abbreviations, and Hungarian notation, which can make code harder to understand and maintain. These practices are contrasted with the recommended approach of using clear, descriptive names.

💡single-letter variable names

Single-letter variable names are discouraged in the video because they provide no context or information about the variable's purpose. This practice is seen as a relic from the days when mathematicians and early programmers valued brevity, but modern programming emphasizes clarity and maintainability over conciseness.

💡abbreviations

Abbreviations in code are shortened forms of words that can lead to confusion, especially when the context is not immediately clear. The video argues that abbreviations are unnecessary in modern programming due to advancements in technology, such as larger screens and easier typing, and they should be avoided for better code readability.

💡Hungarian notation

Hungarian notation is an old naming convention where the type of a variable is prefixed to its name, such as 'strName' for a string. The video points out that this practice is outdated, especially in statically typed languages where the type system already provides this information, making Hungarian notation redundant and potentially confusing.

💡units in variable names

Including units in variable names is a recommended practice to clarify the context and meaning of the data they hold. For example, 'delaySeconds' clearly indicates that the variable represents a delay in seconds. This practice helps both the author and other developers understand the expected units and usage of the variable, as illustrated in the video.

💡type abstraction

Type abstraction is a concept where the underlying data type is hidden from the user, and they only interact with a higher-level representation. The video mentions types like C#'s TimeSpan or C++'s chrono::duration, which encapsulate the details of time measurement, allowing developers to work with time without worrying about the specific units.

💡anti-patterns

Anti-patterns are common practices or solutions that are generally considered to be ineffective or counterproductive. In the video, the speaker identifies anti-patterns such as bundling functions into a 'utils' class, which can lead to code that is difficult to understand and maintain. The video encourages refactoring these functions into more descriptive and modular classes.

💡code structure

Code structure refers to the organization and layout of code within a program. The video suggests that struggling with naming often indicates issues with the code structure itself. Good code structure should allow for clear, meaningful names without resorting to abbreviations or other anti-patterns.

💡utils class

A utils class is a collection of utility functions that are used throughout a codebase. The video criticizes this approach, arguing that it can lead to a lack of clarity and maintainability. Instead, it suggests breaking down these utilities into more specific, descriptively named classes or modules, which aligns with the practices seen in standard libraries.

Highlights

Avoiding bad naming practices can lead to better code structure and readability.

Single-letter variable names are not recommended as they lack descriptiveness.

Abbreviations in code should be avoided because they rely on context and can hinder understanding.

The use of abbreviations was once justified by typing efficiency and screen width, but modern development has eliminated these advantages.

Putting types in variable names is unnecessary in statically typed languages where the type system provides clarity.

Units should be included in variable names to clarify the context, especially in dynamically typed languages like Python.

Prefixing interfaces with 'I' in C# is a pattern that may not be necessary, as the consumer is more interested in functionality.

Naming parent classes with 'Base' or 'Abstract' is not helpful; instead, rename the child class for clarity.

Struggling to name something may indicate a problem with the code structure.

Avoid bundling functions into a single 'utils' class; instead, consider integrating them into their relevant classes or creating separate, descriptively named classes.

Standard libraries avoid 'utils' bundles, sorting functions into well-named modules.

Following these naming rules can result in code that is easier to read and modify.

The quote 'There are only two hard things in computer science: cache invalidation and naming things' highlights the difficulty of these tasks.

Avoiding bad patterns can help get 80% of the way to good naming practices.

The evolution of coding practices has made some old habits, like Hungarian notation, obsolete.

Using types to abstract units can improve code clarity, as seen with C#'s TimeSpan or C++'s chrono::duration.

When naming classes, it's important to consider the user's perspective and avoid unnecessary technical details.

Transcripts

00:00

The classic quote is “There are only two hard things in computer science:

00:05

cache invalidation and naming things”.

00:07

I do agree that these are hard to get right, but

00:10

they're also easy to get wrong.

00:12

And we can get 80% of the way by avoiding

00:15

bad patterns.

00:23

We're going to talk about

00:24

what I consider to be bad naming practices, practices

00:27

that if you avoid you’ll force yourself into better naming, the classic

00:31

first example you'll see is you shouldn't name variables with a single letter.

00:36

I suspect this originally came from when math

00:39

and computer science were more or less the same.

00:42

Mathematicians pride themselves on being terse.

00:45

They like to crystallize down to the smallest, most concise way of expression.

00:50

The thing is, you don't need to edit math, but you do need to edit code.

00:54

So it's become obvious to programmers that we should avoid using single letter

00:58

variable names because they don't tell you anything about the variable.

01:04

But I'd argue that you should take this one step further.

01:07

You should never abbreviate names, period.

01:13

Look at this code.

01:14

Can you tell me its purpose?

01:21

What about now?

01:25

Abbreviations rely on context that you may or may not have.

01:29

You spend more time reading code than writing code.

01:33

So forcing yourself to understand per system naming patterns

01:37

makes it much harder to dig into unfamiliar code.

01:41

Abbreviations used to help because of two reasons:

01:45

It saved you typing

01:47

and screens were 80 characters wide.

01:50

But now, when we write code, we get this.

01:55

It takes less keyboard strokes than ever to write variable names.

01:59

And we have massive 4K screens now.

02:02

So there's no real advantage to abbreviation.

02:06

Don't put types in your name.

02:08

If you've edited older code on Windows,

02:10

you'll see something called Hungarian notation.

02:13

This is where you'd prefix the type to the variable name.

02:17

I think this goes back to before we had good standard types in C,

02:20

so everything would basically be int

02:22

and the type of the variable wouldn't actually tell you what was inside of it.

02:26

But now with statically typed languages,

02:29

the type should tell you exactly what you're looking at.

02:32

So putting types in your variables is no longer necessary.

02:37

Related,

02:38

it's considered good practice to put units in your variable names.

02:42

For example, if you have a function that accepts a delay time.

02:45

If this value is in seconds, you should name the variable delaySeconds.

02:51

This way it's clear to the user of the function

02:54

that they better be putting in seconds.

02:56

It's also more clear to someone

02:57

editing the class itself what unit they're working with.

03:01

But even better than that is to have a type that removes the ambiguity

03:06

completely.

03:07

For example, in C#, the time span

03:10

or chrono::duration in C++.

03:14

The type abstracts the user from understanding the exact underlying unit.

03:19

You need to explicitly ask for a unit back.

03:22

Like here, getting seconds back.

03:26

For dynamically typed languages like Python,

03:28

you sadly can't rely on type declarations to help.

03:32

So we'll need a bit of help from the variable name.

03:36

Interestingly,

03:38

people also add types to their types.

03:41

In C# there's this pattern of prefixing interfaces with “I”.

03:45

This is something I have never understood.

03:48

Good code uses interfaces all the time and the consumer doesn't

03:52

really care whether it's an interface, class, or abstract class.

03:56

They just want to know what they can call.

04:00

In this code, we animate an object on the screen.

04:03

This is an interface.

04:06

If I swap this to an abstract class

04:08

or a concrete class, it wouldn't change this code.

04:11

Nor would it help the code do anything better.

04:15

C# is still following this pattern, even for new .NET library code.

04:19

So for your C# code, it might make sense just to follow the pattern.

04:23

Since bad style guidelines are better than no style guidelines.

04:26

For other languages, I’d definitely avoid.

04:30

Another example of typing your types

04:32

is if you find yourself naming a class with “Base” or “Abstract”.

04:37

This I've never found in a standard library.

04:40

If I have a “Truck” class and then realize that

04:43

it might make sense to create a parent class instead.

04:46

I've seen folks name the new parent class “BaseTruck”.

04:49

This isn't a great name because it doesn't help the users of the class.

04:54

It still represents a truck.

04:56

If you ever find yourself unable to come up with a good name for the parent

04:59

class, it probably means that we should actually rename the child class instead.

05:05

Instead of “BaseTruck”, let's just name it “Truck”.

05:09

And for the child class lets over specify the name.

05:13

We'll call it a “TrailerTruck”.

05:15

Now, if someone gets a truck, they understand what they're getting.

05:18

It's a truck.

05:19

And they don't need to know about any of the details of subclasses.

05:23

And if they need to know

05:24

a specific type of truck, well, then they get the specific name.

05:29

Sometimes if you're struggling to name

05:30

something, it's actually indicative that the code structure is to blame.

05:34

A common anti pattern I see is if there's a collection of functions

05:37

used widely in the code base,

05:40

but it's all bundled up into a single utils class or module.

05:43

If you're thinking of naming code “utils” or “helper”,

05:47

you should think if it's really the right spot for it.

05:50

Here's some code from a no doubt that processes movies.

05:53

There's a bunch of util functions here.

05:58

Firstly, we should consider whether some of these methods

06:01

actually make sense as a part of their respective types instead.

06:06

So this code here, we can actually just move into the movie

06:09

class itself.

06:19

For some of these, we can instead create a class that represents

06:22

a collection of movies and that has the desired methods.

06:30

And finally, some of these

06:32

can be separated into other classes with descriptive names.

06:35

The paging functionality can be moved into its own class,

06:45

and we can even make this generic if we want,

06:48

so that it can operate on more than just movies.

06:51

And the cookie function should really just be in a cookie class.

06:55

Now we don't have anything in our utils class, so we can just delete it.

06:59

You don't see a bundle of utils in standard libraries

07:02

because they can all be sorted into modules that have good names.

07:07

So we should do the same in our code.

07:09

These few rules will help you write code

07:11

that is easier to read and change.

07:17

What would you add?

Rate This

5.0 / 5 (0 votes)

相关标签
CodingBestPracticesNamingConventionsSoftwareDevelopmentCodeReadabilityMaintainabilityVariableNamingAbbreviationsTypeNamingClassStructureCodeOrganization
您需要『中文』的总结吗?