What's the correct way to treat numbers in snake case?
Asked Answered
G

3

19

If I want to write a phrase like "Column 1" in snake case (the usual C way of formatting identifiers that looks like some_function), do I insert underscore between a word or a number, like column_1, or not, like column1?

That may be a painfully trivial question, but I haven't been able to find a snake case definition that would answer this.

Graveyard answered 19/9, 2019 at 11:6 Comment(0)
P
14

I have only ever encountered specific documentation on this topic in one place - the Rubocop Ruby Style Guide (https://github.com/rubocop-hq/ruby-style-guide#snake-case-symbols-methods-vars-with-numbers)

It's probably safe to say there is not a clear winner in one approach over the other. One could also argue that the premise of the format is that: for a given string, all letters are lowercase and all spaces become underscores. By that standard you wouldn't format something column1 unless it started out as Column1.

Personally I prefer column_1 approach.

I find it easier to read, and easier to execute batch find/replace regex queries or to make multi-line edits in my text editor.

Potherb answered 25/9, 2019 at 17:43 Comment(0)
C
8

I would like to cite Rust naming conventions:

In snake_case or SCREAMING_SNAKE_CASE, a "word" should never consist of a single letter unless it is the last "word". So, we have btree_map rather than b_tree_map, but PI_2 rather than PI2.

So write column_1.

Cherlynchernow answered 18/4, 2020 at 4:39 Comment(0)
K
1

Using a code-searching tool (e.g. The Silver Searcher), you can probe a codebase to compare the use of both conventions so your choice can match closely your favorite language/libraries.

Here is a comparison on CPython 3.12 codebase:

# Underscored numbered variable name suffixes frequency
$ ag --python --stats-only '\W[a-z]_1 ='
1 matches
1 files contained matches
1963 files searched

# Not underscored numbered variable name suffixes frequency
$ ag --python --stats-only '\W[a-z]1 ='
863 matches
199 files contained matches
1963 files searched

So in Python it seems that not using an underscore (column1) is the convention.

Thanks @eliotsykes for the original idea.

Knight answered 16/1 at 21:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.