Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are "n" or "ch" prefixes common prefixes when naming int or char variables in C++?

I'm currently going through learncpp.com's C++ tutorials and I'm seeing that their variable naming trend has them naming int variables with an "n" prefix (i.e. int nValue) and "ch" prefixes for char variables (i.e. char chOperation). Is this something that is commonplace in the industry that I should form as a habit now?

like image 351
Adam Schmidt Avatar asked Aug 17 '13 02:08

Adam Schmidt


People also ask

Is prefix should be used for variables and methods?

is prefix should be used for boolean variables and methods. This is the naming convention for boolean methods and variables used by Sun for the Java core packages. Using the is prefix solves a common problem of choosing bad boolean names like status or flag.

Can you use numbers in variable names C?

Variable names in C are made up of letters (upper and lower case) and digits. The underscore character ("_") is also permitted. Names must not begin with a digit.

Does C use camel case?

Classic C doesn't use camel-case; I've written code in camel-case in C, and it looks weird (so I don't do it like that any more). That said, it isn't wrong - and consistency is more important than which convention is used.


1 Answers

Is this something that is commonplace in the industry?

This practice was common in some parts of Microsoft twenty or thirty years ago, due to a misunderstanding of a somewhat more useful convention used by other parts of the company (that of tagging variables indicate their purpose which, in a weakly typed language, can help avoid various kinds of category error). Neither convention serves any useful purpose in a strongly typed language like C++: the type system can catch such errors automatically and more reliably.

It became widely used by others, long after Microsoft (presumably) realised that it was pointless and advised against its use, presumably in the belief that emulating Microsoft's habits might also emulate their success. It's still occasionally seen today, by people who develop habits and never question their usefulness, and by companies who prioritise style guides above software.

Personally, I find it useful as a warning that the code is likely to contain worse horrors.

I should form as a habit now?

It only serves to make the code harder to read, and misleading if you forget to update the tags when you change a variable's type. You should develop a habit of writing clear, readable code, not of sprinkling it with mysterious runes.

Disclaimer: the brief comments about Microsoft are intended to give historical context and are not intended to be an authorative account of Microsoft's policy decisions; specifically the phrase "[Microsoft] realised [it] was pointless" is intended to mean "[some people at Microsoft] realised [the topic under discussion, using redundant type tags in modern C++ in most contexts] was pointless" not (as a commentor appears to have read) "[the entirety of Microsoft] realised [all use of variable tagging] was pointless". All opinions are my own, and may be based on imperfect knowledge.

like image 121
Mike Seymour Avatar answered Oct 13 '22 03:10

Mike Seymour