matching { } syntax style

M

Marc Ferry

In order to avoid these kind of "religious" matters, I was looking for a
tool enabling each developper to have his own style.

Found "C++ SourceStyler" from OchreSoftware
(http://www.sourcestyler.com/) but it is expensive and not available on
Unix/Linux.

Does anybody know of a FREE "C++ styler" available on Unix/Linux ?
Please don't answer with "indent".

Marc
 
P

Phlip

Marc said:
In order to avoid these kind of "religious" matters, I was looking for a
tool enabling each developper to have his own style.

Do you mean each developer reformats everything at check-out time?

That will fill the source control system up with irrelevant changes. I have
heard this suggested before; has anyone ever actually done it?

Developers should share a team style, and all code should match it. One can
support the style electronically, but the common style should support team
bonding. Developers can find more productive ways to be individualistic.
 
P

Phlip

The shorter the function, the more the relative waste of space the braces
become; so where so you put your braces in your 'very short functions'? How
about:

struct
A
{
virtual void fn0() { ... }
virtual void fn1() { ... }
virtual void fn2() { ... }
};

There. A is in the first column. 'struct', being less important, is out of
the first column, and the braces parallel each other.

The functions are so short that the next stiff breeze will make them
collapse into something using even less lines of code.
 
M

Marc Ferry

Phlip said:
Marc Ferry wrote:




Do you mean each developer reformats everything at check-out time?

That will fill the source control system up with irrelevant changes. I have
heard this suggested before; has anyone ever actually done it?

Have a look at "SourceStyler C++" at http://www.sourcestyler.com/
Source file doesn't seem to be reformated.
Marc
 
L

lilburne

Marc said:
Have a look at "SourceStyler C++" at http://www.sourcestyler.com/
Source file doesn't seem to be reformated.
Marc

Actually it is reformatted:
http://www.ochresoftware.com/sourcestyler-gui.html

I emailed the link to colleagues and you could tell when
they had opened it by the laughter.

A coding style contains much more than where you place a
brace or how many spaces you indent. It will have rulings on
language constructs to be avoided, naming schemes for
variables, classes, and functions etc, lots of stuff that
reformatting just doesn't address.

If you are going to adopt a coding style you need to pick
one have a meeting to discuss any contentious issues, and
have someone who is in a position to make a final ruling.
Then adopt and enforce the scheme, no one should be exempt
from the adopted style.
 
P

Phlip

lilburne said:
Actually it is reformatted:
http://www.ochresoftware.com/sourcestyler-gui.html

I emailed the link to colleagues and you could tell when
they had opened it by the laughter.

A coding style contains much more than where you place a
brace or how many spaces you indent. It will have rulings on
language constructs to be avoided, naming schemes for
variables, classes, and functions etc, lots of stuff that
reformatting just doesn't address.

If you are going to adopt a coding style you need to pick
one have a meeting to discuss any contentious issues, and
have someone who is in a position to make a final ruling.
Then adopt and enforce the scheme, no one should be exempt
from the adopted style.

If you are going to adopt coding styles, start by defining your terms.
There are two kinds - esthetic and technical. The OP asked about an
esthetic guideline.

You list technical things. The two guidelines should be democratic and
meritocratic, respectively. If we declare a technical guideline, and
you find a way to exceed it, you may simply tell us what the actual
technology is. (Tip: Fighting on USENET helps teach us how to defend
our principles ;)

For example, suppose we agree on camelCase for identifiers, and on
"complete words without excess abbreviations". But if you want to
write a big identifier, like camelCaseWithFullPack, you are allowed to
write and defend "camelCase_withFullPack". You added a _ for major
breaks in identifiers, and this has merit.
 
L

lilburne

Phlip said:
If you are going to adopt coding styles, start by defining your terms.
There are two kinds - esthetic and technical. The OP asked about an
esthetic guideline.


The poster to this sub-thread was talking about a standard
layout that wasn't a standard layout, something about not
having a religious war over brace style and such. I've yet
to see an in-house style that didn't combine the aesthetic
and the technical.

But consider for the moment what was being proposed:

"In order to avoid these kind of "religious" matters,
I was looking for a tool enabling each developper to
have his own style."

the idea is that when the file is extracted from the
repository it is formatted into the devvies prefered style,
then when it is returned to the repository it is converted
into house style. This presupposes there is a "house style"
or common layout, but the motivating factor for doing this
is that there isn't "each developer to have his own style".
If this group of developers can agree a common layout format
why can't they adopt it as *the* style?

Now think about what happens when two devvies are working
together, joint debugging, or extreme programming session,
or whatever. Whose layout style are they going to use?

I hope the company that produces this software makes a lot
of money from it, I pray that all my industry competitors
adopt it too.

You list technical things. The two guidelines should be democratic and
meritocratic, respectively. If we declare a technical guideline, and
you find a way to exceed it, you may simply tell us what the actual
technology is. (Tip: Fighting on USENET helps teach us how to defend
our principles ;)

For example, suppose we agree on camelCase for identifiers, and on
"complete words without excess abbreviations". But if you want to
write a big identifier, like camelCaseWithFullPack, you are allowed to
write and defend "camelCase_withFullPack". You added a _ for major
breaks in identifiers, and this has merit.

Of course, the standard will be revised from time to time
and there ought to be a way of managing that. What you don't
want is ad hoc changes or worse yet someone taking the
attitude that they are not bound by it. Practically all of
the in-house style will have some technical basis even the
aesthetic ones.
 
R

Roxann Higuera

Phlip said:
Do you mean each developer reformats everything at check-out time?

That will fill the source control system up with irrelevant changes. I have
heard this suggested before; has anyone ever actually done it?

Developers should share a team style, and all code should match it. One can
support the style electronically, but the common style should support team
bonding. Developers can find more productive ways to be individualistic.

I've done that. But I didn't check in that reformatted version.
Instead, I would run vdiff (a function from an old version of PVCS) to
determine what really changed and then make those same changes in a
fresh copy of the code. I'd then vdiff the two modified versions
again to make sure I'd copied in all of my changes before checking the
code back in. That way I could format the code in whatever way best
suited my understanding, and the resulting code didn't overburden the
source control system.
 
E

E. Robert Tisdale

bart said:
Why is it that everybody likes the following curly braces syntax

void function(void) {
implementations;
}

Style is a very subjective and personal consideration.
C and C++ programmers develop or adopt a style
in order to make their code easier for themselves
and other programmers to read, understand and maintain.
If you are developing your own style, there are no rules
except that you should try to be consistent.
Otherwise, you should try to adopt a style
with which other C and C++ programmers are comfortable,
familiar or that they will at least recognize.
Personally, I try to use the same punctuation rules
that are used for ordinary (mathematical) typesetting.
Here are my recommendations:

Terminators always follow immediately after an expression

x@ for all @ in {?, :, ,, ;}

and are followed by at least one white space.
Write

x? y: z

instead of

x ? y : z

or

x?y:z

and write

void f(int, int, int); void g(double);

instead of

void f(int,int,int);void g(double);

for example.

There is no space
between some binary operators and their operands

x@y for all @ in {::, ., ->, .*, ->*, *, /, %, &, ^, |}

but there is always a space
between other binary operators and their operands

x @ y for all @ in {+, -, <<, >>;, <, <=, >, >=, ==, !=,
&&, ||, =, *=, /=, %=, +=, -=, <<=, >>=, &=, |=, ^=}

except when expressions appear as subscripts.
Write

x + y

instead of

x+y
and

x*y

instead of

x * y

for example.
But you may wish to write

A[i+1][j-1]

instead of

A[i + 1][j - 1]

for example to subscript array A.


Most unary prefix operators never have any whitespace
between themselves and their operands

@x for all @ in {::, ++, --, ~, !, -, +, &, *}

but others do

@ x for all @ in {sizeof, new, delete, delete [], throw}

No unary postfix operators

x@ for all @ in {[], (), ++, --}

ever have any whitespace between themselves and their operands.

Use the normal typesetting rules for parentheses (),
square brackets [], angle brackets <> and curly brackets {}.
No space after (, [, < or { and no space before ), ], > or }.
Write

(x)

instead of

( x )

or

(x )

or

( x)

and write

[x]

instead of

[ x ]

or

[x ]

or

[ x]

for example.
There are, of course, exceptions
where extra white space helps to make your code more readable:

double A[2][3] = {{ 1, -1, 0},
{-10, 11, -21}};


Don't give identifiers cryptic, mangled names.
Use ordinary, meaningful words, conventional symbols
or abbreviations with annotations.
Write

double distance, velocity, acceleration, mass, Force;

Force = mass*acceleration;

or

double x; // distance
double v; // velocity
double a; // acceleration
double m; // mass
double F; // force

F = m*a;

for example.

Don't rely on defaults. Make declarations explicit.
Write

int i = 1;

instead of

i = 1;

to declare and initialize integer i and write

class X {
private:
// Representation
int I;
public:
// Constructors
// ...
};

instead of

class X {
// Representation
int I;
public:
// Constructors
// ...
};

to define the private data members of class X for example.


Use indentation to emphasize scope.
Everybody is comfortable with standard indentation:

void f()
{
// indent
}

But I indent curly brackets to the scope of the function body:

void f()
{
// indent
}

And I include the open curly bracket with the function heading:

void f() {
// indent
}

to save a line of code.

I always indent just two spaces at a time and
I place just one statement on each line so that
there is usually room for a comment at the end of each line
beginning in column 33 or 41.

Write

if (condition) {
// statements
}

instead of

if(condition) {
// statements
}

and

while (condition) {
// statements
}

instead of

while(condition) {
// statements
}

to distinguish flow control structures from function calls.

I use

// comment

for comments in C++ and I reserve

/*
a = b;
// comment
b = c;
*/

to comment out code which may include comments.


If you find yourself in an environment
that requires you to conform to style rules with which you are not
comfortable,
consider investing a little time and effort in a program like astyle

Artistic Style
http://astyle.sourceforge.net/

which changes the appearance of C or C++ programs
by inserting or deleting whitespace.

Write

constant == variable

instead of

variable == constant

when comparing a variable to a constant for equality
so that if you write

constant = variable

by mistake, the compiler will detect the error.

I always write

x < y

or

x <= y

instead of

y > x

or

y >= x

when comparing two values so that the expression is true
when the left hand side is to the left of the right hand side
on the real number line.
 
R

Ron Natalie

Keith A. Lewis said:
Is there a hyphen in anal(-?)retentive?

It is when it is used as a adjective prior to a noun. For example:
Robert posts anal-retentive messages
but not when used by itself:
Robert is so anal retentive.
 
P

Phlip

Roxann said:
I've done that. But I didn't check in that reformatted version.
Instead, I would run vdiff (a function from an old version of PVCS) to
determine what really changed and then make those same changes in a
fresh copy of the code. I'd then vdiff the two modified versions
again to make sure I'd copied in all of my changes before checking the
code back in. That way I could format the code in whatever way best
suited my understanding, and the resulting code didn't overburden the
source control system.

There's also the minor issue that the more frequently one integrates,
the lower the cumulative cost & risk of integration. Integrating once
per function is always cheaper than integrating weekly or less often.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top