20

Is there a way to make the constants below to be more readable without breaking golang naming convention?

const ( // stream types
    MPEGDASHStream  = iota
    HLSStream       = iota
    MPEGTSUDPStream = iota
    MPEGTSRTPStream = iota
)
ababo
  • 1,490
  • 1
  • 10
  • 24
  • Why wouldn't you use underscores? `MPEG_DASH_STREAM` – Jerry Saravia Oct 03 '16 at 11:10
  • 1
    That's logical, but I doubt it's recommended. – ababo Oct 03 '16 at 11:12
  • 1
    I don't think there is a way to make them more readable without breaking the code style. I'd suggest sticking to want you have, and maybe take icza's advice and put `Stream` in front of constants. – Ainar-G Oct 03 '16 at 11:33
  • My advice: readability is more important than dogmatic adherence to a particular code style. In this case something like `MpegDASHStream` or `MPEGDashStream` seems like an acceptable compromise. – Joel Cornett Dec 12 '18 at 03:26

3 Answers3

18

Go's naming convention prefers MixedCaps rather than underscores, so don't use them. Source: Effective Go: MixedCaps

Usually when you have constants for different values of an entity, a more easily readable way is to start constant names with the entity, which is then followed by the name of the concrete value. Great examples are the net/http package:

const (
    MethodGet  = "GET"
    MethodHead = "HEAD"
    MethodPost = "POST"
    // ...
)

const (
    StatusContinue           = 100 // RFC 7231, 6.2.1
    StatusSwitchingProtocols = 101 // RFC 7231, 6.2.2
    StatusProcessing         = 102 // RFC 2518, 10.1

    StatusOK                 = 200 // RFC 7231, 6.3.1
    StatusCreated            = 201 // RFC 7231, 6.3.2
    // ...
)

Also you don't need to repeat the expression with iota identifier. Spec: Constant declarations:

Within a parenthesized const declaration list the expression list may be omitted from any but the first declaration. Such an empty list is equivalent to the textual substitution of the first preceding non-empty expression list and its type if any. Omitting the list of expressions is therefore equivalent to repeating the previous list.

So in your case it could simply look like this, which is quite clear and readable:

// stream types
const (
    StreamMPEGDASH = iota
    StreamHLS
    StreamMPEGTSUDP
    StreamMPEGTSRTP
)

Also see Go Code Review Comments for more details. Acronyms can be found in the Initialisms section:

Words in names that are initialisms or acronyms (e.g. "URL" or "NATO") have a consistent case. For example, "URL" should appear as "URL" or "url" (as in "urlPony", or "URLPony"), never as "Url". Here's an example: ServeHTTP not ServeHttp.

This rule also applies to "ID" when it is short for "identifier," so write "appID" instead of "appId".

Community
  • 1
  • 1
icza
  • 389,944
  • 63
  • 907
  • 827
13

What you do is the canonical Golang way.

According to Code Review Comments, you should string the acronyms in ALLCAPS:

Words in names that are initialisms or acronyms (e.g. "URL" or "NATO") have a consistent case. For example, "URL" should appear as "URL" or "url" (as in "urlPony", or "URLPony"), never as "Url". As an example: ServeHTTP not ServeHttp. For identifiers with multiple initialized "words", use for example "xmlHTTPRequest" or "XMLHTTPRequest".

Do I like it? Not so much.

You could move the words around a bit to make it more readable, e.g. MPEGStreamDASH

If you really hate it - then you need to realize it's your code and you can do whatever you like - be it MpegDashStream, MPEG_DASH_Stream or even MpEg_DaSh_StreaM - but that would be evil ;-)

Jean Spector
  • 916
  • 9
  • 9
0

My understanding from https://google.github.io/styleguide/go/decisions.html (specifically each initialism in the name does not need to have the same case) is that MPEGdashStream would be correct.

Noel Yap
  • 18,822
  • 21
  • 92
  • 144