How would you call this variable "direct3Ddevice" or "direct3dDevice"? - naming-conventions

I am following the naming convention rules:
Mixed case starting with lower case (e.g. theDevice)
Not using multiple capital letters for abbreviations/acronyms (e.g. XmlDevice instead of XMLDevice) to improve readability.
The issue I am having is whether I should write my variable as direct3dDevice or direct3Ddevice or direct3DDevice.
Argument for direct3dDevice
The '3' could be seen as a capital letter (since it is of similar size) or just as part of an abbreviation, therefore the 'd' after it should be a lowercase letter. Especially since 'Direct3D' is a name.
Argument for direct3Ddevice
The '3' is a number and should not be treated as a capital letter, therefore the first letter afterwards, 'd' should be capitalized. The next 'd' from 'device' should be lowercase since no two capital letters should be after each other.
Argument for direct3DDevice
The '3' is a number and should not be treated as a capital letter, therefore the first letter afterwards, 'd' should be capitalized. The next 'd' from 'device' should be a capital letter to follow the mixed case convention of starting a new word with a capital letter.

I find
direct3dDevice
to be the most readable and easier to adopt. You wouldn't have to think twice about which letters must be capitalized and thereby is less prone to naming errors when calling that same variable later on.
The most important part to this type of naming convention is just being consistent.
Or you can adopt a well-known style guide like Google's.

Related

Can kebab case have upper case letters as first letter?

Is there a strict definition for kabab case naming convention? Are uppercase letters allowed in kebab case? Does the following names follow kebab case?
Article
Article-Body
Yes, both of your examples adhere to the kebab case convention which only concerns how words in an identifier are separated. According to Wikipedia this naming convention is also called lisp-case, COBOL-CASE or brochette-case.
https://en.wikipedia.org/wiki/Naming_convention_(programming)#Delimiter-separated_words
It's worth noting that basically every programming language support PascalCase and camelCase, most support snake_case but only a few support kebab-case (since it clashes with the syntax for subtraction). Another interesting observation about snake case and kebab case is that they trade expression readability for identifier readability. Here are three examples:
myFirstVariable - mySecondVariable
my_first_variable - my_second_variable
(- my-first-variable my-second-variable)
In the first example the operator "-" stands out more so we see directly that this is a subtraction. In there last two lines there is more "noise" since the underscore character and the dash look somewhat like an operator. In this case the identifiers are easier to read but the expression is harder to read.

Naming conventions. Method or variable names containing numbers as a words

I can't find even couple of words about containing numbers in names of variables or on methods. Does anyone have any authoritative information about such cases:
string2map
its4me
etc...
Exactly using number as a word but not number as a number.
Is it acceptable? Not acceptable, stupidly, professional or not. Please argue your opinion.
I haven't found any information either but below are my own thoughts.
Using a digit in an identifier which happens 2 be pronounced in the same way as a word is just silly word play. It also makes the meaning of the identifier ambiguous - does char2old mean that a character is too old, is it an old version of char2 or is it a conversion? It's fun however to come up with names like a10sorFlow, the2lbox, my4mula but they are best avoided.
When it comes to using numbers 1 to N at the end of equally named identifiers, it is probably better to use an array instead if N > 2. Also, when N = 2 there are often clearer names that can be used, like leftCircle and rightCircle instead of circle1 and circle2, or currentChar and nextChar instead of char1 and char2.
Here is a good general guide for naming variables:
Identifier kind
Word class
Example
Boolean variable or pure function
Last word is an adjective
doorClosed, TablePrepared
Non-boolean variable or pure function
Last word is a noun
closedDoor, PreparedTable
Non-pure function (has side-effects)
First word is a verb
CloseDoor, PrepareTable

Snowflake - Check if 1st 3 Characters of string are letters

Am trying to determine how one attempts to identify, in Snowflake SQL, if a product code begins with three letters.
Suggestions?
I did just try: LEFT(P0.PRODUCTCODE,3) NOT LIKE '[a-zA-Z]%' but it didn't work.
Thanks folks
You can use REGEXP_LIKE to return a boolean value indicating whether or not your string matched the pattern you're interested in.
In your case, something like REGEXP_LIKE(string_field_here, '[a-zA-Z]{3}.*')
Breaking down the regular expression pattern:
[a-zA-Z]: Only match letter characters, both upper and lowercase
{3}: Require three of those letters
.*: Allow any number of any characters after those three letters
Note: in many cases, you would need to specifically indicate the beginning/ending of the string in the pattern, but Snowflake's implementation handles that for you. From the docs:
The function implicitly anchors a pattern at both ends (i.e. ''
automatically becomes '^$', and 'ABC' automatically becomes '^ABC$').
To match any string starting with ABC, the pattern would be 'ABC.*'.
You can try running these examples:
SELECT REGEXP_LIKE('abc', '[a-zA-Z]{3}.*') AS _abc,
REGEXP_LIKE('123', '[a-zA-Z]{3}.*') AS _123,
REGEXP_LIKE('abc123', '[a-zA-Z]{3}.*') AS _abc123,
REGEXP_LIKE('123abc', '[a-zA-Z]{3}.*') AS _123abc

How does camel case work with lower case proper nouns?

As the title says how would camel case, which is: theQuickBrownFox, work when one of the words starts with a lower case letter followed by a capital such as is the case in iPhone.
getiPhoneNumber() for instance looks weird.
Would it be getIphoneNumber() or getIPhoneNumber() or what?
what if it was the first word? iPhoneNumber vs iphoneNumber? Since only each different word should be capitalized.
Any algorithm that separates a camel case identifier back into individual words would correctly produce: get IPhone number from getIPhoneNumber and would be fooled by getiPhoneNumber because it would separate this into geti phone number. Therefore, the correct naming is getIPhoneNumber.
Using the very same criterion I would use iphoneNumber rather than iPhoneNumber.
EDIT
Given that there is some consensus about this question being opinion-based I would like to say that any criterion on how to capitalize a sentence using the camel case convention shouldn't be opinion based but consistent with whatever separation algorithm one would happen to use.

Why can't variable names have spaces in them? [duplicate]

This question already has answers here:
Is there any language that allows spaces in its variable names [closed]
(2 answers)
Closed 9 years ago.
Related: Why can't variable names start with numbers?
Is there a technical reason why spaces aren't allowed in variable names or is it down to convention?
For example, what's stopping us from doing something like this?:
average score = sum of scores / number of scores
The only issue that comes to mind is keywords, but one could simply restrict the use of them in a variable name, and the lexer would be able to distinguish between part of a variable and a keyword.
There’s no fundamental reason, apart from the decisions of language designers and a history of single-token identifiers. Some languages in fact do allow multi-token identifiers: MultiMedia Fusion’s expression language, some Mac spreadsheet/notebook software whose name escapes me, and I’m sure of others. There are several considerations that make the problem nontrivial, though.
Presuming the language is free-form, you need a canonical representation, so that an identifier like account name is treated the same regardless of whitespace. A compiler would probably need to use some mangling convention to please a linker. Then you have to consider the effect of that on foreign exports—why C++ has the extern "C" linkage specifier to disable mangling.
Keywords are an issue, as you have seen. Most C-family languages have a lexical class of keywords distinct from identifiers, which are not context-sensitive. You cannot name a variable class in C++. This can be solved by disallowing keywords in multi-token identifiers:
if account age < 13 then child account = true;
Here, if and then cannot be part of an identifier, so there is no ambiguity with account age and child account. Alternatively, you can require punctuation everywhere:
if (account age < 13) {
child account = true;
}
The last option is to make keywords pervasively context-sensitive, leading to such monstrosities as:
IF IF = THEN THEN ELSE = THEN ELSE THEN = ELSE
The biggest issue is that juxtaposition is an extremely powerful syntactic construct, and you don’t want to occupy it lightly. Allowing multi-token identifiers prevents using juxtaposition for another purpose, such as function application or composition. Far better, I think, just to allow most nonwhitespace characters and thereby permit such identifiers as canonical-venomous-frobnicator. Still plenty readable but with fewer opportunities for ambiguity.
I think it is bacause the designers of the language have followed this convention.
I have searched on Google and found that while naming a variable this is a rule which is followed while naming a variable.
Some links are given below:-
SPSS notes
The following rules apply to variable names:
Variable names cannot contain spaces.
C Programming/Variables
Variable names by IBM
Java Variable Naming convention
Variable names are case-sensitive. A variable's name can be any legal
identifier — an unlimited-length sequence of Unicode letters and
digits, beginning with a letter, the dollar sign "$", or the
underscore character "". The convention, however, is to always begin
your variable names with a letter, not "$" or "". Additionally, the
dollar sign character, by convention, is never used at all. You may
find some situations where auto-generated names will contain the
dollar sign, but your variable names should always avoid using it. A
similar convention exists for the underscore character; while it's
technically legal to begin your variable's name with "_", this
practice is discouraged. White space is not permitted.
Wiki for Naming Convention
In all of the above links you will find that the designers have followed this naming convention for naming the variable.
Also check Is there any language that allows spaces in its variable names
This is forced from language designing.
Compiler needs to find out the meaning of words.
Compiler works on a "State Machine" method, and it needs to distinguish key words.
Maybe placing variable names in "[" and "]" give us some solution(like SQL).
But it will be harder to use it in coding...