Board index » cppbuilder » Re: programming style

Re: programming style


2005-03-05 03:32:38 AM
cppbuilder5
Quote
Well,

is wrong indeed, since such Delphi-derived objects can only be
pointer
types.
I haven't tried coding like that since. I'll probably get back into it
again sometime.
But, thank you for the explanation.
Jonathan
 
 

Re:Re: programming style

Quote
Learning how to code the most popular way would help
enormously when a team starts work on a project. However,
I prefer using the Tab key instead of hitting space 3 times.
Me too, but there was some reason not to use '\t' which I cannot
remember - could be something with draft printing.
--
Best regards,
Vladimir Stefanovic
 

Re:Re: programming style

Quote
Yes, it is. The * ties to the identifier, not to the type.
Coding like this makes the code accurately represents c++. I guess I
need to code correctly, rather than how I like it.
 

{smallsort}

Re:Re: programming style

Quote
Me too, but there was some reason not to use '\t' which I cannot
remember - could be something with draft printing.
Do you mean that your code was printed ? Might take a awful lot of
paper.
 

Re:Re: programming style

Vladimir Stefanovic wrote:
Quote
I remember some rules which were we strongly recomended
in a company I used to work. It was just a question of the
team agreement:
This seems to be the case for C++, indeed. For Java or C#, there seem
to be style guidelines from the main vendors. C++ does not have any, so
you'll see a range of styles.
What I find strange though, is that Borland has a style guide for
Delphi, but not for its own C++ code.
--
Rudy Velthuis [TeamB] rvelthuis.bei.t-online.de
"God is love, but get it in writing." -- Gypsy Rose Lee.
 

Re:Re: programming style

Quote

>TMyClass *mc;


That's the proper way to declare a pointer variable. Just don't tell Mr.
Stroustrup that :)
I find
TMyClass* mc;
more intuitve for several reasons.
A) The type is pointer-to-TMyClass, thus the type is more clearly defined.
B) Is is consistent with the common syntax for references:
TMyClass& mc;
C) It is consistent with common type-cast syntax:
(TMyClass*) or
static_cast<TMyClass*>()
The downside is in declaring multiple pointers of the same type, but this
honestly is rare, and any complex system is going to have exceptions. It
doesn't cost much to declare each on a sperate line, like
TMyClass* mc1;
TMyClass* mc12;
alternativly:
TMyClass* mc1,
* mc2,
* mc3;
The best solution would be if C++ devieated from C and defined TMyClass* as
the type, thus
TMyClass* m1, mc2, mc3
would declare three pointers. But to keep compatibility with C, the * is
officially a modifier to the variable.
I think we can agree that declaring both pointers and objects in a single
line is not wise. Hence,
(ptr) (ptr) (obj) (ptr)
TMyClass *mc1, *mc2, mc3, *mc4;
should never be used and there is no real need for this *feature* within the
context of C++. IMO, TMyClass* should be the type. Thus the TMyClass* syntax
gets as close to that definition while still supporting C compatibilty.
I'm sure not everyone agrees, but one thing I'm sure we can all agree on is
this:
TMyClass * mc1; //is very bad.
 

Re:Re: programming style

Quote
>Me too, but there was some reason not to use '\t'
>which I cannot remember - could be something with
>draft printing.

Do you mean that your code was printed ? Might take
a awful lot of paper.
Not all the code, of course. I myself haven't printed a single
page of code for years.
--
Best regards,
Vladimir Stefanovic
 

Re:Re: programming style

Most IDEs including BCB have a setting to convert tabs to spaces.
"Jonathan Benedicto" < XXXX@XXXXX.COM >wrote in message
Quote
>I remember some rules which were we strongly recomended
>in a company I used to work. It was just a question of the
>team agreement:

Learning how to code the most popular way would help enormously when a
team starts work on a project. However, I prefer using the Tab key instead
of hitting space 3 times.

 

Re:Re: programming style

Quote
should never be used and there is no real need for this *feature*
within the context of C++. IMO, TMyClass* should be the type. Thus
the TMyClass* syntax gets as close to that definition while still
supporting C compatibilty.
IMO 2 <g>
Quote
I'm sure not everyone agrees, but one thing I'm sure we can all
agree on is this:
I don't like this too.
 

Re:Re: programming style

Quote
What I find strange though, is that Borland has a
style guide for Delphi, but not for its own C++ code.
I didn't know that (for Delphi)!
--
Best regards,
Vladimir Stefanovic
 

Re:Re: programming style

Vladimir Stefanovic wrote:
Quote
>What I find strange though, is that Borland has a
>style guide for Delphi, but not for its own C++ code.

I didn't know that (for Delphi)!
I'm not sure it is online anymore.
Oh, I see it is:
community.borland.com/soapbox/techvoyage/article/1,1795,10280,00.
html
--
Rudy Velthuis [TeamB] rvelthuis.bei.t-online.de
"Sometimes a scream is better than a thesis."
- Ralph Waldo Emerson (1803-1882)
 

Re:Re: programming style

Jonathan Benedicto wrote:
Quote
TStringList* Str1, Str2;

and it didn't compile, so I have used a line for every pointer ever
since.

And that's exactly the reason why '*' and '&' should not be next to the
type - because when it is, visually it's conveying the wrong meaning.
.a
 

Re:Re: programming style

"Jonathan Benedicto" < XXXX@XXXXX.COM >writes:
Quote
>Not my preference. I prefer my { to line up with my } and with the
>keyword above, and both to be on a line of their own, except for
>simple
>enums.

How about how you declare pointers ?

TMyClass* mc;
or
TMyClass *mc;
My preference is "None of the above."
TMyClass * mc;
--
Chris (TeamB);
 

Re:Re: programming style

"Alex Bakaev [TeamB]" < XXXX@XXXXX.COM >writes:
Quote
Jonathan Benedicto wrote:
>TStringList* Str1, Str2;
>and it didn't compile, so I have used a line for every pointer ever
>since.
And that's exactly the reason why '*' and '&' should not be next to
the type - because when it is, visually it's conveying the wrong
meaning.
And * and & should not be next to the name, because when you use a
small font it's hard to sometimes tell what they are. :) At least,
that was one of the motivating factors for why we chose to have space
on both sides of the * and & chars in type declarations.
--
Chris (TeamB);
 

Re:Re: programming style

Quote
struct MyStruct{
... if(x == 0){
... try{
}catch(Exception& E){
Please don't put a brace at the end of a statement without an
intervening space.
You will learn why the next time you are tired and frustrated in a
debugging session because things do not work as they should only to
finally notice the brace. This is the same issue as is illustrated by
the semicolon in this while line (which is not one of yours) as you
sit there in your debugging session wondering why executable-statement
only executes once:
while (condition);
executable-statement;
Quote
for(int t=i;t<x.Length();t++){
Eliminating white space only eliminates the ease with which it can be
quickly read. There is no advantage, only disadvantage.
Post incrementing when it is not required is at best no less efficient
than pre-incrementing but syntactically suggests that a post increment
is needed, which it is not. For C++ classes and structures, including
and especially STL-related items, post-increment can be expensive in
both code size and execution time. Never use t++ when ++t will do.
Quote
}else{
I view that a step on the downward path to this:
if(condition){statement;statement;}else{statement;statement;}
Quote
enum TMyEnum { meOne, meTwo, meEtc };
Excellent! But only as long as the statement has only a few values
and does not line wrap. I like the blank line following it. It makes
it stand out.
If it has enough enum values to line wrap I prefer it to be formatted
much as a structure declaration is done. Using your brace style that
might be something like this:
enum TMyEnum {
meOne,
meTwo,
meThree,
meFour,
meFive,
meEtc
};
. Ed
Quote
Jonathan Benedicto wrote in message
news:42289f56$ XXXX@XXXXX.COM ...

I've been changing my BCB programming style slowly, and I think that
I have found the style I like, however it is always good to make the
code very readable.

So, please comment on this style :

struct MyStruct{
int x;
int y;
int z;
};

enum TMyEnum { meOne, meTwo, meEtc };

void __fastcall TMyClass::MyMethod(AnsiString x, int i)
{
if(x == 0){
}else{
}

try{
}catch(Exception& E){
}

for(int t=i;t<x.Length();t++){
// Hope this is readable.
}
}