473,799 Members | 3,147 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question about code style specifically variable declaration using _ prefix

Hi,

I was wondering, I've been reading some C++ code written by others,
usually libraries and stuff, and I've come across what strikes me as a
distinctive style that pervades most of the code I've been reading.
What I'm talking about is that some variables are declared with a "_"
prefix while others lack it. For example you may find a variable and
even functions like

int _temp

and one like

int temp
>From a Java perspective, this style is quite new to me, seeing as the
Java compiler does not accept variables of the aforementioned type. I
have, however, seen this used also in C, but not to the same degree,
and I have ignored it. But it has come to a point where I see it so
much that I think it carries some kind of significance such that if I
understood then I could grasp code quicker.

Explain away, or BS away, I'm willing to listen.

Thanks,

Matt

Jul 20 '07 #1
6 1879
ga*******@gmail .com wrote:
Hi,

I was wondering, I've been reading some C++ code written by others,
usually libraries and stuff, and I've come across what strikes me as a
distinctive style that pervades most of the code I've been reading.
What I'm talking about is that some variables are declared with a "_"
prefix while others lack it. For example you may find a variable and
even functions like

int _temp

and one like

int temp
Names with either a leading underscore followed by a capital letter or
two leading underscores are reserved for the implementation. That's why
you are seeing them in libraries.

It's common practice to avoid all names with a single leading underscore
in application code.

--
Ian Collins.
Jul 20 '07 #2
On 2007-07-20 10:41, Robert Bauck Hamar wrote:
Erik Wikström wrote:
>On 2007-07-20 09:59, ga*******@gmail .com wrote:
>>Hi,

I was wondering, I've been reading some C++ code written by others,
usually libraries and stuff, and I've come across what strikes me as a
distinctive style that pervades most of the code I've been reading.
What I'm talking about is that some variables are declared with a "_"
prefix while others lack it. For example you may find a variable and
even functions like

int _temp

and one like

int temp

From a Java perspective, this style is quite new to me, seeing as the
Java compiler does not accept variables of the aforementioned type. I
have, however, seen this used also in C, but not to the same degree,
and I have ignored it. But it has come to a point where I see it so
much that I think it carries some kind of significance such that if I
understood then I could grasp code quicker.

Explain away, or BS away, I'm willing to listen.

If you have read code mostly from libraries, and especially system
libraries, this is understandable and could be considered good (or not)
but if they are not system libraries it's probably not a good idea. The
C++ standard specifies that all names beginning with __ (two
underscores) are reserved for the implementation, this means that if you
write an applications you should *never ever* use names that starts with
two underscores.

Any name _containing_ two sequential underscores are reserved to the
implementation. It may be at the beginning, but also in the middle or at
the end. It doesn't really matter if it is in a nested namespace either.
Oh, you're right. Lucky that I've never used it (not that I can thing of
any situation where putting two underscore in a name might seem like a
good idea).

--
Erik Wikström
Jul 20 '07 #3
ga*******@gmail .com wrote:
Hi,

I was wondering, I've been reading some C++ code written by others,
usually libraries and stuff, and I've come across what strikes me as a
distinctive style that pervades most of the code I've been reading.
What I'm talking about is that some variables are declared with a "_"
prefix while others lack it. For example you may find a variable and
even functions like

int _temp

and one like

int temp
>>From a Java perspective, this style is quite new to me, seeing as the
Java compiler does not accept variables of the aforementioned type. I
have, however, seen this used also in C, but not to the same degree,
and I have ignored it. But it has come to a point where I see it so
much that I think it carries some kind of significance such that if I
understood then I could grasp code quicker.

Explain away, or BS away, I'm willing to listen.

Thanks,

Matt
I use that style to indicate member variables. I think it's commonly
accepted that it's good to distinguish member variables from others
(even in Java) but I'm often criticised here for my particular way of
doing it. I don't care, it's correct and I like it.

John
Jul 21 '07 #4
John Harrison wrote:
:: ga*******@gmail .com wrote:
::: Hi,
:::
::: I was wondering, I've been reading some C++ code written by
::: others, usually libraries and stuff, and I've come across what
::: strikes me as a distinctive style that pervades most of the code
::: I've been reading. What I'm talking about is that some variables
::: are declared with a "_" prefix while others lack it. For example
::: you may find a variable and even functions like
:::
::: int _temp
:::
::: and one like
:::
::: int temp
:::
:::: From a Java perspective, this style is quite new to me, seeing
:::: as the
::: Java compiler does not accept variables of the aforementioned
::: type. I have, however, seen this used also in C, but not to the
::: same degree, and I have ignored it. But it has come to a point
::: where I see it so much that I think it carries some kind of
::: significance such that if I understood then I could grasp code
::: quicker.
:::
::: Explain away, or BS away, I'm willing to listen.
:::
::: Thanks,
:::
::: Matt
:::
::
:: I use that style to indicate member variables. I think it's
:: commonly accepted that it's good to distinguish member variables
:: from others (even in Java) but I'm often criticised here for my
:: particular way of doing it. I don't care, it's correct and I like
:: it.

I guess the criticism is that, although correct, a leading underscore
indicates that a name is now *either* a member variable, or a global
implementation-specific name, or possibly both.

I understand that some of your colleagues can be a bit confused by
this. :-)
Bo Persson
Jul 21 '07 #5
On 2007-07-21 13:01, Bo Persson wrote:
John Harrison wrote:
:: ga*******@gmail .com wrote:
::: Hi,
:::
::: I was wondering, I've been reading some C++ code written by
::: others, usually libraries and stuff, and I've come across what
::: strikes me as a distinctive style that pervades most of the code
::: I've been reading. What I'm talking about is that some variables
::: are declared with a "_" prefix while others lack it. For example
::: you may find a variable and even functions like
:::
::: int _temp
:::
::: and one like
:::
::: int temp
:::
:::: From a Java perspective, this style is quite new to me, seeing
:::: as the
::: Java compiler does not accept variables of the aforementioned
::: type. I have, however, seen this used also in C, but not to the
::: same degree, and I have ignored it. But it has come to a point
::: where I see it so much that I think it carries some kind of
::: significance such that if I understood then I could grasp code
::: quicker.
:::
::: Explain away, or BS away, I'm willing to listen.
:::
::: Thanks,
:::
::: Matt
:::
::
:: I use that style to indicate member variables. I think it's
:: commonly accepted that it's good to distinguish member variables
:: from others (even in Java) but I'm often criticised here for my
:: particular way of doing it. I don't care, it's correct and I like
:: it.

I guess the criticism is that, although correct, a leading underscore
indicates that a name is now *either* a member variable, or a global
implementation-specific name, or possibly both.
And the risk that someday a less knowledgeable programmer will come and
make a change to your code and follow your style but uses a capital
letter as first letter. One of the main reasons it'd discouraged is
probably to prevent people who don't know better from screwing up in
hard-to-debug ways.

--
Erik Wikström
Jul 21 '07 #6
On Jul 21, 1>01 pm, "Bo Persson" <b...@gmb.dkwro te>
John Harrison wrote>
gara.m...@gmail .com wrote>
>I was wondering, I've been reading some C++ code written by
others, usually libraries and stuff, and I've come across what
strikes me as a distinctive style that pervades most of the code
I've been reading. What I'm talking about is that some variables
are declared with a "_" prefix while others lack it. For example
you may find a variable and even functions like
>int _temp
>and one like
>int temp
>From a Java perspective, this style is quite new to me,
seeing as the Java compiler does not accept variables of
the aforementioned type.
This is, of course, false. Java is even more liberal than C++,
as it not only allows names to begin with either a _ or $ (the
latter is illegal in C++), but doesn't reserve any of them for
the implementation.
>I have, however, seen this used also in C, but not to the
same degree, and I have ignored it. But it has come to a point
where I see it so much that I think it carries some kind of
significance such that if I understood then I could grasp code
quicker.
>Explain away, or BS away, I'm willing to listen.
I use that style to indicate member variables. I think it's
commonly accepted that it's good to distinguish member variables
from others (even in Java) but I'm often criticised here for my
particular way of doing it. I don't care, it's correct and I like
it.
I guess the criticism is that, although correct, a leading underscore
indicates that a name is now *either* a member variable, or a global
implementation-specific name, or possibly both.
Not really. The criticism is that, regardless of what the
standard says, system headers commonly DO define macros which
start with a _, and assume that this will not cause problems.
Using a _ prefix is just asking for trouble.

In general, as others have pointed out, library code intended to
be considered part of the implementation will make extensive use
of such symbols, precisely because the user is not allowed to
define them.

Outside of such library code, it seems pretty universal to
distinguish member variables from other names. In principle, if
you name well, it shouldn't be necessary, but such conventions
certainly don't do any harm. In cases where the variable
represents an externally visible attribute, they also allow the
setter/getter functions to have the name of the attribute. I've
seen at least four different conventions for this:
_member
member_
m_member
myMember
The first is, as I've just said, looking for trouble. The
second is, IMHO, just too ugly. The latter two also have the
advantage of extending easily to distinguish between static
members (s_member, ourMember) and non-static members. I use the
last in my own code; I like the fact that the results are
pronouceable. But I'll willingly either, according to the
requirements of the local standards.

--
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 22 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2936
by: srijit | last post by:
Hello, Here is an example code of xml writer in Python+PythonNet, Ironpython and Boo. The codes look very similar. Regards, Srijit Python + PythonNet: import CLR
12
3305
by: Steven T. Hatton | last post by:
This is something I've been looking at because it is central to a currently broken part of the KDevelop new application wizard. I'm not complaining about it being broken, It's a CVS images. Such things happen. The whole subsystem is going through radical changes. I don't really want to say what I think of the code just yet. That would influence the opinions of others, and I really want to know how other people view these things,...
136
5552
by: Merrill & Michele | last post by:
A derangement is a mapping of a set onto itself leaving no element fixed. I realized that that was what I was programming when I was asked to randomly determine who buys presents for whom this year in my wife's family. Obviously, one does not buy for himself. The code is followed by some questions. #include <stdio.h> #include <stdlib.h> #include <time.h>
6
1995
by: Osama Rajab | last post by:
Hi Guys, When I skim fast into my old code or others code, I forget what the type of a certain variable was or what its initialization value was. We use the Hungarian notation and it helps to remember what was the type (most of the times), but it dose not help to know what was the initialization value. Even with Hungarian notation some time the prefix gets the same for deferent types.
39
3513
by: Patrick | last post by:
The c# code style guide that I follow suggests that class variables (fields) be coded with camel casing, like this: int recordId; string name; It also suggests that variables within methods and method parameters use camel casing, like this: void SetName(int id, string newName)
8
1756
by: Steven Scaife | last post by:
Hello I am creating a reporting system using SQL Server 2000 and ASP, I have created 4 pages that display the results i want, however the reports take an average of 20 mins to run and i have been asked to automate them, i decided to create another ASP page that uses the filesystemobject to create a static htm file from the main page. I decided to put all the declarations and includes my main page creation page, and stripped these from...
10
407
by: Michael | last post by:
Hi, I'm trying to get my head around the relationship between pointers and arrays. If I have the following: int even = {2,4,6,8,10}; int *evenPtr = {even+4, even+3, even+2, even+1, even}; int **ip = evenPtr+3;
7
2415
by: Martin | last post by:
Can I have two setTimeouts running at the same time - with two different intervals? I want to start one timer and, before it times out, start another one I've tried this and they seems to interfer with one another.
20
4047
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This tells me if a variable has changed, give me the original and current value, and whether the current value and original value is/was null or not. This one works fine but is recreating the same methods over and over for each variable type. ...
0
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9546
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10491
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10247
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10031
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9079
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7571
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5593
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.