Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify capitalization of abbr and acronym #5876

Merged
merged 4 commits into from
Jun 3, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 65 additions & 41 deletions src/content/effective-dart/style.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@ code.

Identifiers come in three flavors in Dart.

* `UpperCamelCase` names capitalize the first letter of each word, including
the first.
* `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` names use only lowercase letters,
even for acronyms,
and separate words with `_`.
* `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` names use only lowercase letters,
even for acronyms, and separate words with `_`.

### DO name types using `UpperCamelCase`

Expand Down Expand Up @@ -196,54 +194,80 @@ as in the following cases:
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.
* `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 defined on an enum type is const and lowercase.

:::

[protobufs.]: {{site.pub-pkg}}/protobuf


### 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.
Capitalized acronyms can be hard to read,
and multiple adjacent acronyms can lead to ambiguous names.
For example, given an identifier `HTTPSFTP`,
the reader can't tell if it refers to `HTTPS` `FTP` or `HTTP` `SFTP`.
To avoid this,
capitalize most acronyms and abbreviations like regular words.
This identifier would be `HttpsFtp` if referring to the former
or `HttpSftp` for the latter.

**Exception:** Two-letter *acronyms* like IO (input/output) are fully
capitalized: `IO`. On the other hand, two-letter *abbreviations* like
ID (identification) are still capitalized like regular words: `Id`.
Two-letter abbreviations and acronyms are the exception.
If both letters are capitalized in English,
then they should both stay capitalized when used in an identifer.
Otherwise, capitalize it like a word.

```dart tag=good
class HttpConnection {}
class DBIOPort {}
class TVVcr {}
class MrRogers {}

var httpRequest = ...
var uiHandler = ...
var userId = ...
Id id;
// Longer than two letters, so always like a word:
Http // "hypertext transfer protocol"
Nasa // "national aeronautics and space administration"
Uri // "uniform resource locator"
Esq // "esquire"
Ave // "avenue"

// Two letters, capitalized in English, so capitalized in an identifier:
ID // "identifier", capitalized as "ID"
TV // "television"
DB // "database", originally an acronym from "data base"
IO // "input/output"
UI // "user interface"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue:

Suggested change
ID // "identifier", capitalized as "ID"
TV // "television"
DB // "database", originally an acronym from "data base"
IO // "input/output"
UI // "user interface"
ID // "identifier"
TV // "television"
UI // "user interface"


// Two letters, not capitalized in English, so like a word in an identifier:
Mr // "mister"
St // "street"
Rd // "road"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both this (and the "bad" list lines 246-260) seem really long. I'd suggest 2-3 examples per category max

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can trim the middle list, but the first list has 4 and 3 letter examples that are helpful.

```

```dart tag=bad
class HTTPConnection {}
class DbIoPort {}
class TvVcr {}
class MRRogers {}

var hTTPRequest = ...
var uIHandler = ...
var userID = ...
ID iD;
HTTP // "hypertext transfer protocol"
NASA // "national aeronautics and space administration"
URI // "uniform resource locator"
esq // "esquire"
Ave // "avenue"

Id // "identifier", capitalized as "ID"
Tv // "television"
Db // "database", originally an acronym from "data base"
Io // "input/output"
Ui // "user interface"

MR // "mister"
ST // "street"
RD // "road"
```

When any form of abbreviation comes at the beginning
of a lowerCamelCase identifier, lowercase the identifer:

```dart
var httpConnection = connect();
var tvSet = Television();
var mrRogers = 'hello, neighbor';
```

### PREFER using `_`, `__`, etc. for unused callback parameters

Expand Down