 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
tony_in_da_uk@yahoo.co.uk Guest
|
Posted: Wed Aug 10, 2005 11:34 am Post subject: Pet peeves (lighthearted) |
|
|
Hi all,
Having overhauled thousands of lines of code in the last couple days,
I'm wondering what other C++ programmer's pet peeves are. I think
there's some small value in this, as hopefully some of us will see
things we do and stop, or have someone explain a pet peeve and it won't
bother us in future. Am I going mad? Anyway, here are a few of mine,
all trivial...
- consecutive streaming statements to the same stream, especially
when they flush the stream unnecessarily
(e.g. cout << "a " << a << endl;
cout << "b " << b << endl;)
- failure to take advantage of string literal concatenation
(e.g. cout << "ab" << "n"
<< "xyz";
vs
cout << "abn"
"xyz";)
- people using const references to pass ints and bools
- people using ( and ) unnecessarily after return and sizeof
Cheers, Tony
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Wed Aug 10, 2005 12:16 pm Post subject: Re: Pet peeves (lighthearted) |
|
|
[email]tony_in_da_uk (AT) yahoo (DOT) co.uk[/email] wrote:
| Quote: | Anyway, here are a few of mine, all trivial...
[...]
- failure to take advantage of string literal concatenation
(e.g. cout << "ab" << "n"
"xyz";
vs
cout << "abn"
"xyz"
|
.... or not knowing at all about string literal concatenation. Other than
that, using double quotes to insert a single character into a stream. ;)
Over all, I share your feelings, although I'd like to point out that there
are worse errors and many more that drive me nuts, like forcing things that
don't describe an object into a class, redundant comments, not obeying the
'Law of Three', to name the ones I see most often. All of them don't cause
bugs by themselves but make code harder to read, debug and maintain.
Uli
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Torsten Robitzki Guest
|
Posted: Wed Aug 10, 2005 6:33 pm Post subject: Re: Pet peeves (lighthearted) |
|
|
[email]tony_in_da_uk (AT) yahoo (DOT) co.uk[/email] wrote:
| Quote: | Hi all,
Having overhauled thousands of lines of code in the last couple days,
I'm wondering what other C++ programmer's pet peeves are. I think
there's some small value in this, as hopefully some of us will see
things we do and stop, or have someone explain a pet peeve and it won't
bother us in future. Am I going mad? Anyway, here are a few of mine,
all trivial...
|
I've saw checking if a container is empty before iterating over it
several times today:
if ( !v.empty() )
{
for ( v::iterator i ...)
...
;
}
Sometimes I see v.size() == 0 or even v == std::vector<int>() instead of
simply empty().
regards
Torsten
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Pavel Vozenilek Guest
|
Posted: Wed Aug 10, 2005 6:56 pm Post subject: Re: Pet peeves (lighthearted) |
|
|
"Ulrich Eckhardt" wrote:
| Quote: | Other than that, using double quotes
to insert a single character into a stream. ;)
Hey, if stdlib is buggy (cough, ... BCB) then one |
quickly learns to put double quotes everywhere ;-)
/Pavel
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
mzdude Guest
|
Posted: Wed Aug 10, 2005 6:59 pm Post subject: Re: Pet peeves (lighthearted) |
|
|
pet peeve is when programmers don't declare and init in the same
statement.
bool x; // unitialized variable, dangerous
x = MyFunction();
or better yet
bool x = false; // initialized, and then immediately overwritten
x = MyFuntion();
if( x == true ) {
// do something
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Palik Imre Guest
|
Posted: Wed Aug 10, 2005 6:59 pm Post subject: Re: Pet peeves (lighthearted) |
|
|
[email]tony_in_da_uk (AT) yahoo (DOT) co.uk[/email] writes:
| Quote: | - people using ( and ) unnecessarily after return and sizeof
|
Some old sun compilers saying something like `Error: Unexpected type
name "DrvByte" encountered'. if you don't put those () after sizeof.
But there are worse things around than these small blemishes. I mean
couple hundred characters long code lines, in couple hundred lines
long methods, in a monolithic class, that also has a few friends
around, just to make the maintainer's life more miserable.
ImRe
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
tony_in_da_uk@yahoo.co.uk Guest
|
Posted: Wed Aug 10, 2005 8:56 pm Post subject: Re: Pet peeves (lighthearted) |
|
|
Standard is for sizeof(type) and sizeof value. E.g. should be
sizeof(std::string), but sizeof my_string.
Any you're right about the long lines & functions - there are a
plethora of these style-abuse issues. What's I'm finding extra
interesting is to read about some these more quirky code-efficiency
things, and stuff that implies a warped conception of the language
behaviour. Liberal sprinkling of friendship is certainly in there, at
least when in the code... .
Cheers - Tony
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carlos Moreno Guest
|
Posted: Wed Aug 10, 2005 8:57 pm Post subject: Re: Pet peeves (lighthearted) |
|
|
[email]tony_in_da_uk (AT) yahoo (DOT) co.uk[/email] wrote:
| Quote: | Hi all,
Having overhauled thousands of lines of code in the last couple days,
I'm wondering what other C++ programmer's pet peeves are.
|
Only one I can think of is the lack of const (and I mean lack of
const *in each and every single isntance* where you're not *required*
not to use it).
Carlos
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jeff Schwab Guest
|
Posted: Thu Aug 11, 2005 7:19 am Post subject: Re: Pet peeves (lighthearted) |
|
|
[email]tony_in_da_uk (AT) yahoo (DOT) co.uk[/email] wrote:
| Quote: | Having overhauled thousands of lines of code in the last couple days,
I'm wondering what other C++ programmer's pet peeves are. I think
there's some small value in this, as hopefully some of us will see
things we do and stop, or have someone explain a pet peeve and it won't
bother us in future.
|
// Redundant tests.
if( some_bool == true ) // if( some_bool )
if( some_pointer != 0 ) // if( some_pointer )
// Unecessary use of preprocessor.
T* p = NULL; // T* p = 0;
#define SOME_CONSTANT 5 // int const some_constant = 5;
// Inappropriate choice of methods, e.g. list::size()
// is O(n) in some popular implementations.
some_list.size() == 0; // some_list.empty()
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
|
| Back to top |
|
 |
Conrad Z. Guest
|
Posted: Thu Aug 11, 2005 7:24 am Post subject: Re: Pet peeves (lighthearted) |
|
|
people who define a function like this:
void Func( std::string PossiblyVeryLongText );
people who don't declare a member function const
whenever possible and not realising the chain effect.
Antoon
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Nicola Musatti Guest
|
Posted: Thu Aug 11, 2005 10:23 am Post subject: Re: Pet peeves (lighthearted) |
|
|
Jeff Schwab wrote:
[...]
| Quote: | // Redundant tests.
if( some_bool == true ) // if( some_bool )
|
The variant of this that kills me is
bool b;
if ( c == d )
b = true;
else
b = false;
| Quote: | if( some_pointer != 0 ) // if( some_pointer )
|
But I have to confess that i do compare pointers with zero myself.
Cheers,
Nicola Musatti
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Nicola Musatti Guest
|
Posted: Thu Aug 11, 2005 10:25 am Post subject: Re: Pet peeves (lighthearted) |
|
|
[email]tony_in_da_uk (AT) yahoo (DOT) co.uk[/email] wrote:
| Quote: | Hi all,
Having overhauled thousands of lines of code in the last couple days,
I'm wondering what other C++ programmer's pet peeves are. I think
there's some small value in this, as hopefully some of us will see
things we do and stop, or have someone explain a pet peeve and it won't
bother us in future. Am I going mad? Anyway, here are a few of mine,
all trivial...
|
{
A * pa = new A(b,c);
// ...
delete pa; // If they remember it!
}
Instead of
{
A a(b,c);
// ...
}
Cheers,
Nicola Musatti
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maciej Sobczak Guest
|
Posted: Thu Aug 11, 2005 10:26 am Post subject: Re: Pet peeves (lighthearted) |
|
|
[email]tony_in_da_uk (AT) yahoo (DOT) co.uk[/email] wrote:
| Quote: | Having overhauled thousands of lines of code in the last couple days,
I'm wondering what other C++ programmer's pet peeves are.
|
Not to repeat what others already posted, I'd add this:
1.
{
T *p = new T();
// ...
delete p;
}
instead of just:
{
T t;
// ...
}
2.
try
{
// something that may throw
// ...
}
catch (T t)
{
// oops, there was an error, but I don't know what to do
throw t;
}
(in general, the belief that dealing with exceptions means try/catch
everywhere)
3.
throw new T();
4.
throw "Oops, there was an error";
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
B. Perlman Guest
|
Posted: Thu Aug 11, 2005 10:26 am Post subject: Re: Pet peeves (lighthearted) |
|
|
| Quote: | if( some_pointer != 0 ) // if( some_pointer )
What's wrong with this? It is clearer than your version, which looks |
like a boolean test. I mean, you are not *really* asking if
some_pointer is true...
| Quote: | T* p = NULL; // T* p = 0;
What's wrong with this? Shows intent more clearly. |
| Quote: | #define SOME_CONSTANT 5 // int const some_constant = 5;
I have often seen your version recommended, but I am not completely |
convinced. First of all, in an embedded environment, one may be
limited in space such that one is literally counting bytes (yes, I
know, in some cases the place for const variables is different and they
don't take up needed space).
Also, a more general issue -- if one is using this in more than one
source file, one will want to put it into a header file. It seems to
me that either one must declare it "static" and it will be instantiated
once for each source file in which the header is included, or one must
declare it "extern" and have the "real" declaration in some source
file.
Is there a simpler way, or is it really worth it to do this? In some
cases, the gain (type checking) I find not very helpful
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|