473,403 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Comments solicited about stylistic preference

I have the habit of using the scope resolution operator whenever I use
global functions. My premise is that it improves readability by
helping someone who is looking at the code for the first time to grasp
at once that the function has global scope. I stretch this habit to
using the operator on C library functions as well. So I write ::memset
instead of memset and ::abs instead of abs. I vaguely remember hearing/
reading somewhere that it is encouraged practice to use fully
qualified names of STL elements, e.g. std::vector instead of vector.
Can someone verify this? All constructive comments are welcome.

Jul 25 '07 #1
6 1287
ph**********@yahoo.com wrote:
I have the habit of using the scope resolution operator whenever I use
global functions. My premise is that it improves readability by
helping someone who is looking at the code for the first time to grasp
at once that the function has global scope. I stretch this habit to
using the operator on C library functions as well. So I write ::memset
instead of memset and ::abs instead of abs.
According to the Standard, if you (and the implementors) do the right
thing (which is rare, BTW), you're not supposed to use the global names,
but instead use 'std::' prefix.
I vaguely remember
hearing/ reading somewhere that it is encouraged practice to use fully
qualified names of STL elements, e.g. std::vector instead of vector.
Can someone verify this?
It's really difficult for me to verify whether you remember hearing
or reading something somewhere, you understand this, don't you?
All constructive comments are welcome.
Well, not giving into the desire to reduce having to type too many
characters by inserting 'using namespace std' or a using declarations
with standard names, is a good habit. Of course, you're going to
eventually run into this situation:

std::deque<
SomeRatherLongTypeName,
OtherLongNameOfTheAllocatormyRatherLongTypeNameCol lection;

and then you want to iterate of it using 'const_iterator'... How
bad is that? Then you resort to

typedef std::deque<
SomeRatherLongTypeName,
OtherLongNameOfTheAllocatormyTypeCollection_t;

and iteration becomes realtively easy then

myTypeCollection_t::const_iterator ...

Doing all that does *hide* the fact that the collection is, in fact,
a standard container. Well... I say, so? Bid deal!

Now, with some old (and even sometimes with some new) codebases there
can be a name conflict with some popular names like 'min' or 'max'.
You have to watch out for those. The bad thing is when somebody
actually switches those without you actually knowing it. There is
definitely less probability of a mistake if you fully qualify those.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 25 '07 #2
Victor Bazarov <v.********@comacast.netwrote:
Well, not giving into the desire to reduce having to type too many
characters by inserting 'using namespace std' or a using declarations
with standard names, is a good habit. Of course, you're going to
eventually run into this situation:

std::deque<
SomeRatherLongTypeName,
OtherLongNameOfTheAllocatormyRatherLongTypeNameCol lection;

and then you want to iterate of it using 'const_iterator'... How
bad is that? Then you resort to

typedef std::deque<
SomeRatherLongTypeName,
OtherLongNameOfTheAllocatormyTypeCollection_t;

and iteration becomes realtively easy then

myTypeCollection_t::const_iterator ...

Doing all that does *hide* the fact that the collection is, in fact,
a standard container. Well... I say, so? Bid deal!
On the other hand, typedef'ing the standard containers can help
maintenance. If you later decide that a different container is better
for your purposes, then (assuming you use the iterators correctly) all
you need to change is the typedef.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jul 25 '07 #3
In article <f8**********@news-int.gatech.edu>, ri******@gehennom.invalid
says...

[ ... ]
On the other hand, typedef'ing the standard containers can help
maintenance. If you later decide that a different container is better
for your purposes, then (assuming you use the iterators correctly) all
you need to change is the typedef.
IME, this is rarely true. If you make _extremely_ limited use of the
container, it can be possible, but (at best) for most real code is
extremely limited at best.

Just for example, to make much use of a container, you need to add
elements to the container. If you were using a vector, you almost
certainly do that with push_back. If (for example) you switch to a set,
your linker will quickly inform you that set doesn't have a 'push_back'
member -- you need to use 'insert' intead.

It's entirely reasonable to write specific algorithms to work on a
variety of containers -- but for a complete program, such a switch is
rarely possible and even when it is it's almost certain to accomplish
nothing (e.g. code written for a vector can usually work with a deque,
but gains nothing by doing so).

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 25 '07 #4
Jerry Coffin wrote:
[..]
It's entirely reasonable to write specific algorithms to work on a
variety of containers -- but for a complete program, such a switch is
rarely possible and even when it is it's almost certain to accomplish
nothing (e.g. code written for a vector can usually work with a deque,
but gains nothing by doing so).
It is true that choice of the container is usually made upfront, but
I don't agree that switching from a vector to a deque gains nothing.
A deque is faster when it comes to allocation, so if you expect your
code to use indexing (random access iterators) and require a growing
container, then the performance or contiguousness will be the factors
that tip the decision scale toward vector or deque.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 25 '07 #5
On Jul 26, 12:11 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Jerry Coffin wrote:
[..]
It's entirely reasonable to write specific algorithms to work on a
variety of containers -- but for a complete program, such a switch is
rarely possible and even when it is it's almost certain to accomplish
nothing (e.g. code written for a vector can usually work with a deque,
but gains nothing by doing so).
It is true that choice of the container is usually made upfront, but
I don't agree that switching from a vector to a deque gains nothing.
A deque is faster when it comes to allocation, so if you expect your
code to use indexing (random access iterators) and require a growing
container, then the performance or contiguousness will be the factors
that tip the decision scale toward vector or deque.
Switching between sequences is sometimes possible; switching
between a sequence and an associative container, rarely, I
think. Even switching between sequences can be tricky, since
iterator validity is often a dominant factor in choosing which
container to use to begin with.

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

Jul 26 '07 #6
James Kanze wrote:
On Jul 26, 12:11 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>Jerry Coffin wrote:
>>[..]
It's entirely reasonable to write specific algorithms to work on a
variety of containers -- but for a complete program, such a switch
is rarely possible and even when it is it's almost certain to
accomplish nothing (e.g. code written for a vector can usually work
with a deque, but gains nothing by doing so).
>It is true that choice of the container is usually made upfront, but
I don't agree that switching from a vector to a deque gains nothing.
A deque is faster when it comes to allocation, so if you expect your
code to use indexing (random access iterators) and require a growing
container, then the performance or contiguousness will be the factors
that tip the decision scale toward vector or deque.

Switching between sequences is sometimes possible; switching
between a sequence and an associative container, rarely, I
think. Even switching between sequences can be tricky, since
iterator validity is often a dominant factor in choosing which
container to use to begin with.
If that's the reason, yes. I sometimes choose 'vector' because its
storage is guaranteed to be contiguous. In that case changing to
'deque' or 'list' is definitely out of the question.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 26 '07 #7

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

Similar topics

17
by: Andrew Koenig | last post by:
Suppose I want to define a class hierarchy that represents expressions, for use in a compiler or something similar. We might imagine various kinds of expressions, classified by their top-level...
13
by: Chris Smith | last post by:
'Morning, Within the next few months, I'm going to embark upon a comparatively rather large base of JavaScript code to be called from a web browser environment. Not too awfully huge, but...
13
by: Greg G | last post by:
Present site: http://www.risky-biz.com First "sketch" of new design. http://www.risky-biz.com/new I'm trying some new ideas, mostly just changing the stylesheet. It's pretty rough so...
13
by: Christopher Benson-Manica | last post by:
This is intended to be a simple version of the Unix "head" command, i.e. a utility that displays the first n lines of a file. Comments welcomed... #include <cstdlib> #include <iostream>...
4
by: Russell Silva | last post by:
I have a class A with a member variable type vector<foo> which is initialized upon construction (no standard () constructor): class A { protected: vector<foo> foo_vector; public: A(const...
4
by: Vinayak | last post by:
Dear Members I'm new here. Please permit me to ask some newbie question I'm from a non-profit organization, working for gender equality. We wish to get a small message across to sister blogs...
4
by: Richard Buckle | last post by:
Hi fellow Pythonistas, I've been using Python in anger for some time, and I must say, as I wrote in <http://macprang.sourceforge.net/>: "It's refreshing beyond words to use a language that so...
98
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
12
by: Phillip B Oldham | last post by:
I'm keen on learning python, with a heavy lean on doing things the "pythonic" way, so threw the following script together in a few hours as a first-attempt in programming python. I'd like the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
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,...
0
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...

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.