“This is the 11th day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”
preface
A good coding style is important, and consistent naming makes for a better code reading experience and a more efficient team collaboration. This article introduces the official recommended naming conventions.
Identifier definition
- UpperCamelCase, for example, capitalizes the first letter of each word.
- Lowercase hump, such as lowerCamelCase, the first word is all lowercase and the first letter of each subsequent word is capitalized.
- Combine all lowercase words, such as lowercase_with_underscores, with the underscore “_”.
Naming rule 1: Use uppercase hump for naming types
Classes, enums, aliases (typedefs), and so on use capital humps and do not use separators. Here’s an example:
class SliderMenu {... }class HttpRequest {... }typedef Predicate<T> = bool Function(T value);
Copy the code
Including parameter annotations should also follow this principle:
class Foo {
const Foo([Object? arg]);
}
@Foo(anArg)
class A {... }@Foo(a)class B {... }Copy the code
If the annotation does not carry parameters, use a lower case hump.
const foo = Foo();
@foo
class C {... }Copy the code
Naming rule 2: Class extensions use a capital hump
As with types, class extension definitions should use capital humps:
extension MyFancyList<T> on List<T> { ... }
extension SmartIterable<T> on 可迭代<T> { ... }
Copy the code
Naming rule 3: Libraries, package names, directories, and source files should use the underscore “_” to combine lowercase words
Because some file systems are case insensitive, others are case sensitive. So many projects require all lowercase filenames. The use of delimiters in this case is more readable. Here’s a sample comparison:
// Correct example
library peg_parser.source_scanner;
import 'file_system.dart';
import 'slider_menu.dart';
// Error example
library pegparser.SourceScanner;
import 'file-system.dart';
import 'SliderMenu.dart';
Copy the code
Naming rule 4: Import alias uses underscore “_” to concatenate lowercase words
When you rename an imported package or plug-in, you should use the underscore “_” to form a lowercase word.
// Correct example
import 'dart:math' as math;
import 'package:angular_components/angular_components'
as angular_components;
import 'package:js/js.dart' as js;
// Error example
import 'dart:math' as Math;
import 'package:angular_components/angular_components'
as angularComponents;
import 'package:js/js.dart' as JS;
Copy the code
Naming rule 5: Use a lower camel case for variable names, class member attributes, parameters, and constants
Member attributes, variables, function parameters, top-level definition parameters, and named parameters of a class should all be camel case.
// Correct example
var count = 3;
HttpRequest httpRequest;
void align(bool clearItems) {
// ...
}
const pi = 3.14;
const defaultTimeout = 1000;
final urlScheme = RegExp('^([a-z]+):');
class Dice {
static final numberGenerator = Random();
}
// Error example
const PI = 3.14;
const DefaultTimeout = 1000;
final URL_SCHEME = RegExp('^([a-z]+):');
class Dice {
static final NUMBER_GENERATOR = Random();
}
Copy the code
It’s important to note that many languages recommend underlining constants with all uppercase letters, but Dart recommends a lower case hump. This is because in most cases all caps are unreadable, especially for enumerated values like CSS colors; Constants may be changed to normal variables, which may add name changes.
Naming rule 6: abbreviated naming rule
For abbreviations, it would be strange to use all caps for multiple characters, such as HTTPSFTP, which is obviously not as readable as HTTPSFTP. However, for two-letter abbreviations, it is recommended to use all caps or capital humps based on readability. For example, IO can be written as IO, and ID can be written as ID. Here is an example of a comparison.
// Correct example
class HttpConnection {}
class DBIOPort {}
class TVVcr {}
class MrRogers {}
var httpRequest = ...
var uiHandler = ...
Id id;
// Error example
class HTTPConnection {}
class DbIoPort {}
class TvVcr {}
class MRRogers {}
var hTTPRequest = ...
var uIHandler = ...
ID iD;
Copy the code
Naming rule 7: Unused parameters in the parameter list are identified by _ and __
Some callback methods use only part of the argument. In this case, you can replace the unused argument with an underscore to make it clear that the argument will not be used. If more than one parameter is not used, use more than one underscore. Here’s an example:
futureOfVoid.then((_) {
print('Operation complete.');
});
Copy the code
This guideline applies only to anonymous or local methods. You shouldn’t do this with declarative functions, and even if you don’t use them, you need to name them properly to keep the parameters semantically clear.
Naming rule 8: Underline only private class member attributes.
Dart uses an underscore to identify class members as private properties and top-level variables as file local variables. Therefore, underline only this class. At the same time, there is no concept of private members for local variables, parameters, and local methods, so there is no need to use underscores to avoid misunderstanding.
Naming rule 9: Do not use prefix letters
Some programming languages recommend using special letters to identify variables, such as k for numeric classes. This convention is difficult to follow consistently and does not contribute much to readability. Here’s an example:
// Correct example
defaultTimeout
// Error example
kDefaultTimeout
Copy the code
conclusion
Having a good and consistent naming style makes your code look more comfortable and pleasant. In fact, it is also a characteristic of personal soft power. Remember, code is written for people!
I am dao Code Farmer with the same name as my wechat official account. This is a column about the introduction and practice of Flutter, providing systematic learning articles about Flutter. See the corresponding source code here: The source code of Flutter Introduction and Practical column. If you have any questions, please add me to the wechat account: island-coder. If you feel you have something to gain, please give three pairs of love as follows:
👍🏻 : a praise to encourage!
🌟 : Collect articles, easy to look back!
💬 : Comment exchange, mutual progress!