Board index » cppbuilder » Re: programming style

Re: programming style


2005-03-05 06:49:10 PM
cppbuilder0
"Vladimir Stefanovic" < XXXX@XXXXX.COM >schrieb im Newsbeitrag news: XXXX@XXXXX.COM ...
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.
We are using spaces instead of tabs too, because code with tabs looks quite
ugly if you view it with an editor, which doesnīt have the same tab settings.
With spaces instead of tabs the source code will look always the same and will
have the same formatting, regardless how the editor is configured.
( At least if you use fixed size fonts ;-) )
Quote
--
Best regards,
Vladimir Stefanovic
 
 

Re:Re: programming style

Andre Kaufmann wrote:
Quote
>Yes, it is. The * ties to the identifier, not to the type.

In plain C perhaps, but in C++ ?
Obviously it does, as
int *a, *b;
proves.
--
Rudy Velthuis [TeamB] rvelthuis.bei.t-online.de
"It is better to be quotable than to be honest." -- Tom Stoppard
 

Re:Re: programming style

Jonathan Benedicto wrote:
Quote
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.
}
}

I have always found the little curly brackets rather annoying, so here is
what I do:-
if(x == 0)
THEN
// Do Something
ELSE
// Do Something else
ENDIF
THEN, ELSE and ENDIF (and ELSEIF) are macros defined like this:-
#define THEN {
#define ELSEIF }else if
#define ELSE }else{
#define ENDIF }
I realise this may not be to everyone's taste, but it works very well for
me.
--
Chris Gordon-Smith
London

 

{smallsort}

Re:Re: programming style

"Rudy Velthuis [TeamB]" < XXXX@XXXXX.COM >schrieb im Newsbeitrag news:xn0dze392vmin900brudys-toshiba@www.teamb.com...
Quote
Andre Kaufmann wrote:

>>Yes, it is. The * ties to the identifier, not to the type.
>
>In plain C perhaps, but in C++ ?

Was more a rhetorical question.
Quote
Obviously it does, as

int *a, *b;
Sorry - i meant not that the syntax of C++ doesnīt tie the pointer char*
to the identifier as it does C. Thatīs because C++ is compatible to C - which
IMHO hasnīt been a very good idea (macros, header files, old style casts).
A compilation mode - somewhat like extern "C" { old c code } - would have
been better.
I rather meant that the natural coding style (at least of Stroustroup -
the inventor of C++) is to bind the * to the type
(coding style wise - not syntax wise).
Naturally when your speaking of a variable youīll say that the variable is an
integer pointer and not that the variable pointer is of type int - or do you ?
And IMHO the coding style should reflect this.
Another problem (for me) is that i use the int* style when iīm casting
or if iīm using templates.
vector<int*>p;
int* p = static_cast<int*>(z);
or when i write a function returning an int*.
int* foo() {}
Why should i use a different coding style for variable declarations, only because
the C compatibility doesnīt allow me to define multiple pointers
separated by the , operator.
Then iīd prefer a type definition:
typedef int* IntPtr;
IntPtr a,b,c,d,e,f;
I prefer the style of Stroustroup, but others may prefer the C style.
Thatīs just a matter of taste.
Personally i find the int* style more naturally and more consistent
throughout the language.
Quote

proves.
I didnīt try to prove something, just wanted to say that i like the other style
a bit more.
Andre
Quote
--
Rudy Velthuis [TeamB] rvelthuis.bei.t-online.de

"It is better to be quotable than to be honest." -- Tom Stoppard
 

Re:Re: programming style

Andre Kaufmann wrote:
Quote
>Obviously it does, as
>
>int *a, *b;

I rather meant that the natural coding style (at least of Stroustroup
- the inventor of C++) is to bind the * to the type
(coding style wise - not syntax wise).
Yes, but a coding style which simply disregards syntax is a bit weird.
I can understand that you would like to see it that way (after all, in
Delphi it works that way too), but I think the language dictates a
different one.
--
Rudy Velthuis [TeamB] rvelthuis.bei.t-online.de
"We have art to save ourselves from the truth."
- Friedrich Nietzsche (1844-1900)
 

Re:Re: programming style

Quote
I never use for loops.
Just a question, what do you use instead ?
 

Re:Re: programming style

"Rudy Velthuis [TeamB]" < XXXX@XXXXX.COM >schrieb im Newsbeitrag news:xn0dzea2w14sf9n00frudys-toshiba@www.teamb.com...
Quote
Andre Kaufmann wrote:

>>Obviously it does, as
>>
>>int *a, *b;
>
>I rather meant that the natural coding style (at least of Stroustroup
>- the inventor of C++) is to bind the * to the type
>(coding style wise - not syntax wise).

Yes, but a coding style which simply disregards syntax is a bit weird.
Weird ? I would call something like that weird:
www.de.ioccc.org/2001/anonymous.c ;-)
I only prefer a more (for me) readable style - if iīm allowed to.
C++ does already force me to use very ugly styles e.g. for
function pointers.
Quote
I can understand that you would like to see it that way (after all, in
Delphi it works that way too), but I think the language dictates a
different one.
Hm - dictate would mean that itīs forbidden and will throw a syntax error
when the other style is used, but int* a,b,c;
compiles fine without any error. Itīs a shortcut for a definition of an integer
pointer and 2 integers ;-) , though it would be plain silly to use that.
The most C++ coding styles deprecate multiple pointer definitions in a
single line instead something like that should be used:
int* a; int* b; int *c;
or
int* a;
int* b;
The other style (int *a) is meant to be a C coding style by these C++
coding styles.
Since i define and initialize my variables in the same line, i never had problems
with my int* coding style.
int* a = 0;
int* b = 0;
Andre
Quote


--
Rudy Velthuis [TeamB] rvelthuis.bei.t-online.de

"We have art to save ourselves from the truth."
- Friedrich Nietzsche (1844-1900)
 

Re:Re: programming style

Andre Kaufmann wrote:
Quote
Weird ? I would call something like that weird:
www.de.ioccc.org/2001/anonymous.c ;-)
LOL!
Quote
I only prefer a more (for me) readable style - if iīm allowed to.
Of course you are. Everyone is allowed to use their own style (except
if superiors or company guidelines forbid it, perhaps).
--
Rudy Velthuis [TeamB] rvelthuis.bei.t-online.de
"If people can judge me on the company I keep, they would judge me with
keeping really good company with Laura." -- George W. Bush
 

Re:Re: programming style

On Sat, 5 Mar 2005 20:19:20 +0100, Andre Kaufmann wrote:
Quote
www.de.ioccc.org/2001/anonymous.c ;-)
That's just horrible :)
--
Good luck,
liz
 

Re:Re: programming style

This is the most irrelevant to the quality of code. And as usual the most
irrelevant stuff is often also the most hotly debated.
Get a copy of Sutter&Alexandrescu book about coding standards. This topic is
in Chapter 0, which discusses things that are a mater of individual taste.
--
----------------------------------------------------------------
Have we the courage and the will to face up to the immorality and
discrimination of the progressive tax, and demand a return to traditional
proportionate taxation? - Ronald Reagan
"Jonathan Benedicto" < XXXX@XXXXX.COM >wrote in message
Quote
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.
}
}

--
Jonathan



 

Re:Re: programming style

Jonathan Benedicto wrote:
Quote
>I never use for loops.

Just a question, what do you use instead ?
while
Hans.
 

Re:Re: programming style

Quote
This is the most irrelevant to the quality of code. And as
usual the most irrelevant stuff is often also the most hotly
debated.
I agree, but when you have to maintain/improve the code
someone else have made - you'll see the difference. It's
not just a question of styles. It's a question of unique naming
properties/methods/events, ways of doing things (new/auto_ptr),
copying buffers (strcpy, strncpy), ways of documenting things,
etc...
I was in the project where (at the very beginning) everyone
coded according to his own apprehensions. It was the mixture
of styles for doing everything. After some period we had to
reverese-engineer our own code.
After few such iterations, the product team decided to make
internal rules for *everything* - not only the C++ styles.
One rule was (not directly connected with C++ styles) first to
make a Functional Specification (FS) for something we want
to code.
One FS was about C++ styles.
Furthr, before any single line of code - we had to made a
FS, which is fact a first 'Reference manual' for the new
functionality. It was a MS Word document with screen shots,
descriptions, etc. Then, the product team makes some changes
on it, and when everyone shows the 'green light'
Now, here's how the project looks today (OWL & Borland C++):
www.ristancase.com/dac/v40/images/DACV40big.gif
--
Best regards,
Vladimir Stefanovic
 

Re:Re: programming style

Hans Galema wrote:
Quote
Jonathan Benedicto wrote:
>>I never use for loops.
>
>Just a question, what do you use instead ?

while
Why? One of the great advantages of a (C/C++/Java/C#-style) for loop is
that you have all the looping conditions in one place, whereas with
while, you have them all over the place, which is IMO much more error
prone.
--
Rudy Velthuis [TeamB] rvelthuis.bei.t-online.de
"We must all hear the universal call to like your neighbor like you
like to be liked yourself." -- George W. Bush
 

Re:Re: programming style

J Alexander wrote:
Quote
This is the most irrelevant to the quality of code.
I don't think so. Having a style you are comfortable with (and others
as well) makes your code easier to read, and that makes it much more
maintainable.
--
Rudy Velthuis [TeamB] rvelthuis.bei.t-online.de
"Sometimes when I'm alone, I google myself." -- unknown
 

Re:Re: programming style

J Alexander wrote:
Quote
This is the most irrelevant to the quality of code. And as usual the
most irrelevant stuff is often also the most hotly debated.
Get a copy of Sutter&Alexandrescu book about coding standards. This
topic is in Chapter 0, which discusses things that are a mater of
individual taste.
"J", you quoted* 34 lines, and only added the text above. Please note
that this is a violation of the guidelines in
info.borland.com/newsgroups/netiquette.html rule #1.
*Quoting is copying (automatically or manually) text from the previous
message into your own. Quoted lines are usually marked by one or more>
at the beginning of the line. Please don't quote entire messages, but
only just enough to give the reader a reference. More on proper quoting
can be found here:
www.uwasa.fi/~ts/http/quote.html
alt-usage-english.org/posting_quotes.html
Thanks for your future compliance with Borland's guidelines.
--
Rudy Velthuis [TeamB] rvelthuis.bei.t-online.de
"Room service? Send up a larger room."
-- Groucho Marx