Name your Class properly

Flutter tips Published on

Naming your classes well is super important for writing code that others (and you!) can easily understand later.

What's the Big Idea?

The main point is simple: Your class name should tell you what the object is, not the action it performs.

Avoid Naming by Action

It might seem natural to name a class after a job it does, like EmailValidator. But this focuses only on one task.

The "DON'T" Example

Class EmailValidator

class EmailValidator {
  EmailValidator();

  void validate(String email) {
    // code to check if email is valid
    // ... throws an error if not valid
  }
}

This name is okay if the class only ever validates emails. But what if you add features like formatting or parsing emails? The name EmailValidator doesn't cover those other jobs.

Name by What it Is

Instead, name the class after the thing it represents.

The "DO" Example

Class Email

class Email {
  final String _value;

  Email(String email) : _value = email.trim(); // Represents an email

  void validate() {
    // code to check if *this* email is valid
    // ... throws an error if not valid
  }
}

Here, the class is simply named Email. This clearly states what the object is. Any actions related to an email, like validate(), format(), or parse(), become methods of the Email class. This makes your code structure much cleaner and easier to follow.

Better Code, Better Projects

Following this simple naming rule helps keep your classes focused and your codebase organized, making development smoother.

Save 3 months of work

Create your app using our 6 years of making Flutter apps and more than 50+ apps

kickstarter for flutter apps

Frequently Asked Questions

What is the key principle for naming classes?

Name your class based on what the object *is* or represents, rather than just what it *does*.

Why is naming a class by what it is better?

It makes your code more understandable. It tells you the type of data or concept the class handles, and methods inside the class describe the actions related to that object.

What's wrong with naming a class by what it does?

If you name a class after a specific action (like `Validator`), that name might not make sense if you add other actions to the class later. It can make the code confusing.

Does this naming rule only apply to Flutter?

No, this is a good general practice in object-oriented programming across many languages, not just Flutter/Dart.

Read more
You may also be interested in
OverflowBar or Wrap?  blog card image
OverflowBar or Wrap?
Published on 2025-05-12T12:42:33.412Z
Extract borders from image  blog card image
Extract borders from image
Published on 2025-05-12T12:19:08.227Z
ApparenceKit is a flutter template generator tool by Apparence.io © 2025.
All rights reserved