Identifiers

Domains: Dart

Identifiers come in three flavors in Dart.

  • UpperCamelCase names capitalize the first letter of each word, including the first.
  • lowerCamelCase names capitalize the first letter of each word, except the first which is always lowercase, even if it’s an acronym.
  • lowercase_with_underscores use only lowercase letters, even for acronyms, and separate words with _.

DO

DO name types using UpperCamelCase

Classes, enums, typedefs, and type parameters should capitalize the first letter of each word (including the first word), and use no separators.

class SliderMenu { ... } 

class HttpRequest { ... } 

typedef Predicate<T> = bool Function(T value); 

This even includes classes intended to be used in metadata annotations.

class Foo { 
   const Foo([arg]); 
} 

@Foo(anArg) 
class A { ... } 

@Foo() 
class B { ... } 

If the annotation class’s constructor takes no parameters, you might want to create a separate lowerCamelCase constant for it.

const foo = Foo(); 

@foo 
class C { ... }

DO name extensions using UpperCamelCase

Like types, extensions should capitalize the first letter of each word (including the first word), and use no separators.

extension MyFancyList<T> on List<T> { ... } 

extension SmartIterable<T> on Iterable<T> { ... }

DO name libraries, packages, directories, and source files using lowercase_with_underscores

Some file systems are not case-sensitive, so many projects require filenames to be all lowercase. Using a separating character allows names to still be readable in that form. Using underscores as the separator ensures that the name is still a valid Dart identifier, which may be helpful if the language later supports symbolic imports.

library peg_parser.source_scanner; 

import 'file_system.dart'; 
import 'slider_menu.dart';

Note: This guideline specifies how to name a library if you choose to name it. It is fine to omit the library directive in a file if you want.

DO name import prefixes using lowercase_with_underscores

import 'dart:math' as math; 
import 'package:angular_components/angular_components' 
   as angular_components; 
import 'package:js/js.dart' as js;

DO name other identifiers using lowerCamelCase

Class members, top-level definitions, variables, parameters, and named parameters should capitalize the first letter of each word except the first word, and use no separators.

var item; 

HttpRequest httpRequest; 

void align(bool clearItems) { 
   // ... 
}

DO capitalize acronyms and abbreviations longer than two letters like words

Capitalized acronyms can be hard to read, and multiple adjacent acronyms can lead to ambiguous names. For example, given a name that starts with HTTPSFTP, there’s no way to tell if it’s referring to HTTPS FTP or HTTP SFTP.

To avoid this, acronyms and abbreviations are capitalized like regular words, except for two-letter acronyms. (Two-letter abbreviations like ID and Mr. are still capitalized like words.)

HttpConnectionInfo
uiHandler
IOStream
HttpRequest
Id
DB

PREFER

PREFER using lowerCamelCase for constant names

In new code, use lowerCamelCase for constant variables, including enum values.

const pi = 3.14; 
const defaultTimeout = 1000; 
final urlScheme = RegExp('^([a-z]+):'); 

class Dice { 
   static final numberGenerator = Random(); 
}

You may use SCREAMING_CAPS for consistency with existing code, as in the following cases:

  • When adding code to a file or library that already uses SCREAMING_CAPS.
  • When generating Dart code that’s parallel to Java code — for example, in enumerated types generated from protobufs.

Note: We initially used Java’s SCREAMING_CAPS style for constants. We changed for a few reasons:

  • SCREAMING_CAPS looks bad for many cases, particularly enum values for things like CSS colors.
  • Constants are often changed to final non-const variables, which would necessitate a name change.
  • The values property automatically defined on an enum type is const and lowercase.

DON’T

DON’T use a leading underscore for identifiers that aren’t private

Dart uses a leading underscore in an identifier to mark members and top-level declarations as private. This trains users to associate a leading underscore with one of those kinds of declarations. They see “_” and think “private”.

There is no concept of “private” for local variables, parameters, or library prefixes. When one of those has a name that starts with an underscore, it sends a confusing signal to the reader. To avoid that, don’t use leading underscores in those names.

Exception: An unused parameter can be named ______, etc. This happens in things like callbacks where you are passed a value but you don’t need to use it. Giving it a name that consists solely of underscores is the idiomatic way to indicate the value isn’t used.

DON’T use prefix letters

Hungarian notation and other schemes arose in the time of BCPL, when the compiler didn’t do much to help you understand your code. Because Dart can tell you the type, scope, mutability, and other properties of your declarations, there’s no reason to encode those properties in identifier names.

defaultTimeout

Similar pages

Page structure
Terms

Dart

Variables

Parameters

Types

bool

Classes

List

Functions

Named parameters

Constructor