By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,341 Members | 1,396 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,341 IT Pros & Developers. It's quick & easy.

How to enforce coding style conventions

P: n/a
Hi,

As my company is moving into C# they are enforcing the use of styling
convention in the source code, such as methods naming conventions, newlines,
etc.

Does someone know of products that are good at this job? (free and/or
commercial)
Nov 16 '05 #1
Share this Question
Share on Google+
29 Replies


P: n/a
Ron Burd wrote:
As my company is moving into C# they are enforcing the use of styling
convention in the source code, such as methods naming conventions,
newlines, etc.

Does someone know of products that are good at this job? (free and/or
commercial)


There is a free tool from MS: FxCop
http://www.gotdotnet.com/team/fxcop/

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp
Do you need daily reports from your server ?
http://sourceforge.net/projects/srvreport/
Nov 16 '05 #2

P: n/a
Ron Burd <rb***@hexacta.com> wrote:
As my company is moving into C# they are enforcing the use of styling
convention in the source code, such as methods naming conventions, newlines,
etc.

Does someone know of products that are good at this job? (free and/or
commercial)


Have you tried FxCop?

http://www.gotdotnet.com/team/fxcop/

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

P: n/a
I am aware of FxCop, but AFAIK it does its chking through introspection
(reflection) of the Exe or Dll.
How can I make FxCop check my source code?

Per example (note the braces):

If we want to ensure that the code is written in the following style:

if ( something == true) {
...
}

instead of

if (something ==true)
{
...
}

or that constants are named using All-Caps letters.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Ron Burd <rb***@hexacta.com> wrote:
As my company is moving into C# they are enforcing the use of styling
convention in the source code, such as methods naming conventions, newlines, etc.

Does someone know of products that are good at this job? (free and/or
commercial)


Have you tried FxCop?

http://www.gotdotnet.com/team/fxcop/

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #4

P: n/a
>As my company is moving into C# they are enforcing the use of styling
convention in the source code, such as methods naming conventions, newlines,
etc.

Does someone know of products that are good at this job? (free and/or
commercial)


Microsoft FxCop is quite good at pointing out cases where programmers
don't follow the conventions. And it can be extended to include your
own rules and checks. And it's FREE !

http://www.gotdotnet.com/team/fxcop/

Marc

================================================== ==============
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
Nov 16 '05 #5

P: n/a
http://www.flexisoftsolutions.com/Pr...M2004.aspx?m=3
"Ron Burd" <rb***@hexacta.com> wrote in message
news:uf*************@tk2msftngp13.phx.gbl...
Hi,

As my company is moving into C# they are enforcing the use of styling
convention in the source code, such as methods naming conventions, newlines, etc.

Does someone know of products that are good at this job? (free and/or
commercial)

Nov 16 '05 #6

P: n/a
I am aware of FxCop, but AFAIK it does its chking through introspection
(reflection) of the Exe or Dll.
How can I make FxCop check my source code?

Per example (note the braces):

If we want to ensure that the code is written in the following style:

if ( something == true) {
...
}

instead of

if (something ==true)
{
...
}

or that constants are named using All-Caps letters. THIS_IS_A_CONSTANT


"Ron Burd" <rb***@hexacta.com> wrote in message
news:uf*************@tk2msftngp13.phx.gbl...
Hi,

As my company is moving into C# they are enforcing the use of styling
convention in the source code, such as methods naming conventions, newlines, etc.

Does someone know of products that are good at this job? (free and/or
commercial)

Nov 16 '05 #7

P: n/a
Ron Burd <rb***@hexacta.com> wrote:
I am aware of FxCop, but AFAIK it does its chking through introspection
(reflection) of the Exe or Dll.
How can I make FxCop check my source code?

Per example (note the braces):

If we want to ensure that the code is written in the following style:

if ( something == true) {
...
}

instead of

if (something ==true)
{
...
}
I don't believe it can do that, no. I don't know of a .NET tool which
does allow that.
or that constants are named using All-Caps letters.


That one *can* be done in FxCop, as constants are available through
reflection.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8

P: n/a
Try out FxCop (free):
http://www.gotdotnet.com/team/fxcop/

--
Scott
http://www.OdeToCode.com

On Fri, 30 Apr 2004 10:23:35 -0300, "Ron Burd" <rb***@hexacta.com>
wrote:
Hi,

As my company is moving into C# they are enforcing the use of styling
convention in the source code, such as methods naming conventions, newlines,
etc.

Does someone know of products that are good at this job? (free and/or
commercial)


Nov 16 '05 #9

P: n/a
OK, I know this is Off-Topic, but why would ANYONE prefer the former to
the latter?

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
"Ron Burd" <rb***@hexacta.com> wrote in message
news:Oq**************@TK2MSFTNGP11.phx.gbl...
Per example (note the braces):

If we want to ensure that the code is written in the following style:
if ( something == true) {
...
}

instead of

if (something ==true)
{
...
}

Nov 16 '05 #10

P: n/a
James Curran wrote:
OK, I know this is Off-Topic, but why would ANYONE prefer the former to
the latter?


Why does anyone prefer Country music to the Blues?

--
mikeb
Nov 16 '05 #11

P: n/a
James,

in the mid '80s, there were two equally popular styles (at least in the
group of 100 or so programmers that I was a member of)
if ( something == true) {
...
}
and
if (something ==true)
{
...
}
The later wastes space - one line with ONE character on it!

You've been around a while - I find it hard to believe that you have not
seen it, or used it, or worked on a job and knew someone that used to know a
programmer that used to do it that way! I've known programmers that would
spend 30 minutes or so reformatting substantial portions of a project from
one style to the other before they would touch a line of code.

regards
roy fine

"James Curran" <Ja*********@mvps.org> wrote in message
news:eI**************@tk2msftngp13.phx.gbl... OK, I know this is Off-Topic, but why would ANYONE prefer the former to the latter?

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
"Ron Burd" <rb***@hexacta.com> wrote in message
news:Oq**************@TK2MSFTNGP11.phx.gbl...
Per example (note the braces):

If we want to ensure that the code is written in the following style:
if ( something == true) {
...
}

instead of

if (something ==true)
{
...
}


Nov 16 '05 #12

P: n/a
James Curran wrote:
OK, I know this is Off-Topic, but why would ANYONE prefer the former to
the latter?


I prefer it as it places less emphasis on individual code blocks within
a method.

Here's an example of the _K&R bracing style_ in use:

void Foo()
{
if (something) {
DoSomething();
} else {
DoSomethingElse();
}
}
Nov 16 '05 #13

P: n/a
[quote=The later wastes space - one line with ONE character on it!
[/quote]

ROFLOL - this is funny stuff! Are you sure you're not a ghost writer
for Dilbert? I can just hear the pointy-haired boss now: "We've
going to start allocating lines of code - use them wisely".

Nov 16 '05 #14

P: n/a
Jimi <ji************@yahoo-dot-ca.no-spam.invalid> wrote:
[quote=The later wastes space - one line with ONE character on it!
[/quote]

ROFLOL - this is funny stuff! Are you sure you're not a ghost writer
for Dilbert? I can just hear the pointy-haired boss now: "We've
going to start allocating lines of code - use them wisely".


Well, one of the reasons I believe K&R became popular was because it
was used in books - and in books, vertical space *is* a relatively
precious commodity. People copy what's written in books, and thus a
tradition is born...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #15

P: n/a
Jon Skeet [C# MVP] wrote:
Well, one of the reasons I believe K&R became popular was because it
was used in books - and in books, vertical space *is* a relatively
precious commodity. People copy what's written in books, and thus a
tradition is born...


Personally, I don't know much about K&R's coming into being, but it's
interesting to note that Linus Torvalds states the rationale of using
K&R bracing style in the Linux source code as: "K&R", but goes on to say:

"Also, note that this brace-placement also minimizes the number of empty
(or almost empty) lines, without any loss of readability. Thus, as the
supply of new-lines on your screen is not a renewable resource (think
25-line terminal screens here), you have more empty lines to put
comments on."

http://pantransit.reptiles.org/prog/CodingStyle.html

Sure, Linus most likely didn't invent this bracing style, but I'd bet
that he's influenced many a person with his work ;-)

Also, in my opinion, the K&R bracing style makes C-like code more
similar to code from other languages that don't have the flexible block
begin/end markers that C has. Consider the following BASIC code:

If Something Then
DoSomething
Else
DoSomethingElse
End If

This is more equivalent to:

if (something) {
DoSomething();
} else {
DoSomethingElse();
}

.... than:

if (something)
{
DoSomething();
}
else
{
DoSomethingElse();
}

Due to this, I'd imagine that K&R would feel somewhat normal to BASIC
programmers who are picking up a C-like language.
Nov 16 '05 #16

P: n/a
"Roy Fine" <rl****@twt.obfuscate.net> wrote in message
news:eC**************@TK2MSFTNGP10.phx.gbl...
James,

in the mid '80s, there were two equally popular styles (at least in the
group of 100 or so programmers that I was a member of)
if ( something == true) {
...
}


and
if (something ==true)
{
...
}


The later wastes space - one line with ONE character on it!


I, personally, prefer the latter. In this day and age, nobody
really worries much about disk space, etc., and if I worked
for a company that enforced the former, I would just write my
own tools to format the code either way. That way I could
write mine so that it is readable to me, then just convert it
to their (horrible) format before anyone else had a look at it.
I could also format anyone else's to the way I like to see it
until I was done examining it, then delete my copy of the code.

In fact, one of the first things I challenged myself to do in C#
was to do this very thing. I refuse to work with code that is
written like the former. Period!

Just my two cents...
Nov 16 '05 #17

P: n/a
Gary Morris wrote:
I, personally, prefer the latter. In this day and age, nobody
really worries much about disk space, etc., and if I worked
for a company that enforced the former, I would just write my
own tools to format the code either way. That way I could
write mine so that it is readable to me, then just convert it
to their (horrible) format before anyone else had a look at it.
I could also format anyone else's to the way I like to see it
until I was done examining it, then delete my copy of the code.

In fact, one of the first things I challenged myself to do in C#
was to do this very thing. I refuse to work with code that is
written like the former. Period!

Just my two cents...


It seems that people here have a strong dislike for K&R-style bracing.

Before I'd ever tried the style, I felt much the same about it. I used
to wonder why anyone would ever want to use such a style. It just
seemed to make code hard-to-read. The style I was accustomed to would
make code look so much nicer.

Anyway, one day I was interested in submitting code to a particular
open-source project. K&R bracing style was used throughout the project.
I decided that my dislike for the style wouldn't get in the way of my
desire to add to the project, so I grinned and beared it.

I then got used to K&R style and, having done so, felt that it actually
makes code more readable.

The point I'm trying to make here is that I hated it until I really had
to try it and get used to it. However, it's still probably not for
everyone.

This is just my counter two cents :-)
Nov 16 '05 #18

P: n/a

"Gary Morris" <gw*******@hotpop.com> wrote in message
news:O$**************@TK2MSFTNGP10.phx.gbl...
"Roy Fine" <rl****@twt.obfuscate.net> wrote in message
news:eC**************@TK2MSFTNGP10.phx.gbl...

The later wastes space - one line with ONE character on it!


I, personally, prefer the latter. In this day and age, nobody
really worries much about disk space, etc., and if I worked
for a company that enforced the former, I would just write my
own tools to format the code either way. That way I could
write mine so that it is readable to me, then just convert it
to their (horrible) format before anyone else had a look at it.
I could also format anyone else's to the way I like to see it
until I was done examining it, then delete my copy of the code.

In fact, one of the first things I challenged myself to do in C#
was to do this very thing. I refuse to work with code that is
written like the former. Period!

Just my two cents...


It is NEVER about disk space - it is about code maintenance and general
coding style. Depending on the font that you use, you get a fixed number of
lines of code on the screen. There are some recommendations about by
leaders in the field indicating that you should try to get individual code
units down to one screen of code - and a very strong suggestion of no more
than two.

Once you get over petty presonal differences, some white space is needed for
readability, all other is just wasted space. But as many who eschew any
Style at all would intimate, all style rules are but petty presonal
preferences.

roy fine

Nov 16 '05 #19

P: n/a

I used to think K&R was satanically insipired as well, but once you are forced to use it a while you find that it has some distinct advantages

Especially when you abstract them to other structures beign spread across lines

if(blah)
isn't really much different

but when you start doin

if
bla
|| bla
|| blah

and

public int Foo ( int blah
int blah
int blah

You'll find that K&R is easier to keep a single "style" that fits more than just {}

Of course in the end, it's a purely religious arguemnt for most people, I.E. mine is great and your sucks, but with no ratinnal thought involved

Caladin

Nov 16 '05 #20

P: n/a
Agreed, and duly noted :)
I just cannot stand to try and read code like that. Again,
period.

"Roy Fine" <rl****@twt.obfuscate.net> wrote in message
news:u6**************@TK2MSFTNGP11.phx.gbl...

"Gary Morris" <gw*******@hotpop.com> wrote in message
news:O$**************@TK2MSFTNGP10.phx.gbl...
"Roy Fine" <rl****@twt.obfuscate.net> wrote in message
news:eC**************@TK2MSFTNGP10.phx.gbl...

The later wastes space - one line with ONE character on it!
I, personally, prefer the latter. In this day and age, nobody
really worries much about disk space, etc., and if I worked
for a company that enforced the former, I would just write my
own tools to format the code either way. That way I could
write mine so that it is readable to me, then just convert it
to their (horrible) format before anyone else had a look at it.
I could also format anyone else's to the way I like to see it
until I was done examining it, then delete my copy of the code.

In fact, one of the first things I challenged myself to do in C#
was to do this very thing. I refuse to work with code that is
written like the former. Period!

Just my two cents...


It is NEVER about disk space - it is about code maintenance and general
coding style. Depending on the font that you use, you get a fixed number

of lines of code on the screen. There are some recommendations about by
leaders in the field indicating that you should try to get individual code
units down to one screen of code - and a very strong suggestion of no more
than two.

Once you get over petty presonal differences, some white space is needed for readability, all other is just wasted space. But as many who eschew any
Style at all would intimate, all style rules are but petty presonal
preferences.

roy fine



Nov 16 '05 #21

P: n/a
"C# Learner" <cs****@learner.here> wrote in message
news:OK**************@TK2MSFTNGP11.phx.gbl...
Anyway, one day I was interested in submitting code to a particular
open-source project. K&R bracing style was used throughout the project.
I decided that my dislike for the style wouldn't get in the way of my
desire to add to the project, so I grinned and beared it.


My experience is the exact opposite. Learning C in the early 80's, I
used the K&R style, because everyone did --- it was just the way things were
done.

A few years later, a changed companies, and the new one mandated the
other code style. I resisted for about 2 days. Then I realized that the
code was so much easier to follow then you didn't have to pause to mentally
unwrap it.

(I had a similar revelation a bit later, when I discovered it's far easier
to read an if() statement, if you don't include an assignment in it, that
is,
prefer:
x = GetValue();
if (x!=0)
{....
over:
if ((x=GetValue())!=0)
and by far over:
if (x=GetValue())
--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
Nov 16 '05 #22

P: n/a
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Well, one of the reasons I believe K&R became popular was because it
was used in books - and in books, vertical space *is* a relatively
precious commodity. People copy what's written in books, and thus a
tradition is born...


The problem with the "save a line" argument is that it just don't hold
up.

Given the code:
if (x==y) {
ThisFunc();
thatFunc();
OtherFunc();
}

it could still be written as:
if (x==y)
{ ThisFunc();
thatFunc();
OtherFunc();
}

In exactly the same number of lines, and you still get the advantage of the
braces line up.

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
Nov 16 '05 #23

P: n/a
James,

read your post again -- it does hold the save a line, and does it quite well

this is the "wasted-line" example
if (x==y)
{
ThisFunc();
thatFunc();
OtherFunc();
}
these two show the "save-a-line" :

1) if (x==y)
{ ThisFunc();
thatFunc();
OtherFunc();
}
2) if (x==y) {
ThisFunc();
thatFunc();
OtherFunc();
}
I am not at all a fan or advocate of style 1 - the world does not need
another style to further split the community.

For what it worth - Visual Studio 2003 editor preferences has an option in
Text Editor | C# | Formatting | Indentation that reads : "Leave open braces
on same line as construct", effectively helping enforce style 2 of above.
My guess is that appeared because of either:
a) someone on the MS VS/C# team wanted that option
b) significant customer requests for the feature
regards
roy fine
"James Curran" <Ja*********@mvps.org> wrote in message
news:eP*************@TK2MSFTNGP11.phx.gbl... "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Well, one of the reasons I believe K&R became popular was because it
was used in books - and in books, vertical space *is* a relatively
precious commodity. People copy what's written in books, and thus a
tradition is born...

The problem with the "save a line" argument is that it just don't hold
up.

Given the code:
if (x==y) {
ThisFunc();
thatFunc();
OtherFunc();
}

it could still be written as:
if (x==y)
{ ThisFunc();
thatFunc();
OtherFunc();
}

In exactly the same number of lines, and you still get the advantage of

the braces line up.

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)

Nov 16 '05 #24

P: n/a
Skc
James Curran wrote:
The problem with the "save a line" argument is that it just don't hold
up.

Given the code:
if (x==y) {
ThisFunc();
thatFunc();
OtherFunc();
}

it could still be written as:
if (x==y)
{ ThisFunc();
thatFunc();
OtherFunc();
}

In exactly the same number of lines, and you still get the advantage of the
braces line up.


I am a newbie and from my point of view, the latter (2nd) option looks
more pleasing...

Regards
Skc

Nov 16 '05 #25

P: n/a
[top-posting fixed]
"Roy Fine" <rl****@twt.obfuscate.net> wrote in message
news:u6**************@TK2MSFTNGP11.phx.gbl...

"Gary Morris" <gw*******@hotpop.com> wrote in message
news:O$**************@TK2MSFTNGP10.phx.gbl...
"Roy Fine" <rl****@twt.obfuscate.net> wrote in message
news:eC**************@TK2MSFTNGP10.phx.gbl...
>
> The later wastes space - one line with ONE character on it!

I, personally, prefer the latter. In this day and age, nobody
really worries much about disk space, etc., and if I worked
for a company that enforced the former, I would just write my
own tools to format the code either way. That way I could
write mine so that it is readable to me, then just convert it
to their (horrible) format before anyone else had a look at it.
I could also format anyone else's to the way I like to see it
until I was done examining it, then delete my copy of the code.

In fact, one of the first things I challenged myself to do in C#
was to do this very thing. I refuse to work with code that is
written like the former. Period!

Just my two cents...
It is NEVER about disk space - it is about code maintenance and general
coding style. Depending on the font that you use, you get a fixed number of
lines of code on the screen. There are some recommendations about by
leaders in the field indicating that you should try to get individual
code units down to one screen of code - and a very strong suggestion of no more than two.

Once you get over petty presonal differences, some white space is needed

for
readability, all other is just wasted space. But as many who eschew any
Style at all would intimate, all style rules are but petty presonal
preferences.

roy fine


"Gary Morris" <gw*******@hotpop.com> wrote in message
news:e%****************@TK2MSFTNGP12.phx.gbl... Agreed, and duly noted :)
I just cannot stand to try and read code like that. Again,
period.


Then you can imagine how the rest of us feel about your top-posting!

--
Mabden
Nov 16 '05 #26

P: n/a
FXCOP

On Wed, 05 May 2004 01:45:17 GMT, Mabden <ma****@sbcglobal.net> wrote:
[top-posting fixed]
"Roy Fine" <rl****@twt.obfuscate.net> wrote in message
news:u6**************@TK2MSFTNGP11.phx.gbl...
>
> "Gary Morris" <gw*******@hotpop.com> wrote in message
> news:O$**************@TK2MSFTNGP10.phx.gbl...
> > "Roy Fine" <rl****@twt.obfuscate.net> wrote in message
> > news:eC**************@TK2MSFTNGP10.phx.gbl...
> > >
> > > The later wastes space - one line with ONE character on it!
> >
> > I, personally, prefer the latter. In this day and age, nobody
> > really worries much about disk space, etc., and if I worked
> > for a company that enforced the former, I would just write my
> > own tools to format the code either way. That way I could
> > write mine so that it is readable to me, then just convert it
> > to their (horrible) format before anyone else had a look at it.
> > I could also format anyone else's to the way I like to see it
> > until I was done examining it, then delete my copy of the code.
> >
> > In fact, one of the first things I challenged myself to do in C#
> > was to do this very thing. I refuse to work with code that is
> > written like the former. Period!
> >
> > Just my two cents...
> >
> >
>
> It is NEVER about disk space - it is about code maintenance and

general
> coding style. Depending on the font that you use, you get a fixed number
of
> lines of code on the screen. There are some recommendations about by
> leaders in the field indicating that you should try to get individual

code > units down to one screen of code - and a very strong suggestion of no more > than two.
>
> Once you get over petty presonal differences, some white space is

needed
for
> readability, all other is just wasted space. But as many who eschew

any
> Style at all would intimate, all style rules are but petty presonal
> preferences.
>
> roy fine
>
>


"Gary Morris" <gw*******@hotpop.com> wrote in message
news:e%****************@TK2MSFTNGP12.phx.gbl...
Agreed, and duly noted :)
I just cannot stand to try and read code like that. Again,
period.


Then you can imagine how the rest of us feel about your top-posting!


--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 16 '05 #27

P: n/a
Wow!!! I can't believe how this thread has evolved!

Anyway, thank you guys for the help!
"Ron Burd" <rb***@hexacta.com> wrote in message
news:uf*************@tk2msftngp13.phx.gbl...
Hi,

As my company is moving into C# they are enforcing the use of styling
convention in the source code, such as methods naming conventions, newlines, etc.

Does someone know of products that are good at this job? (free and/or
commercial)

Nov 16 '05 #28

P: n/a
Roy Fine wrote:
if (something ==true)
{
...
}


The later wastes space - one line with ONE character on it!


Are you referring to the "{" or the "}"? If you want to at least be
consistent, you should propose:

if (something) {
DoStuff ();
Do MoreStuff(); }

Hilton
Nov 16 '05 #29

P: n/a
Hilton wrote:
Roy Fine wrote:
if (something ==true)
{
...
}


The later wastes space - one line with ONE character on it!


Are you referring to the "{" or the "}"? If you want to at least be
consistent, you should propose:

if (something) {
DoStuff ();
Do MoreStuff(); }


So I guess that VB programmers aren't being consistent unless they write:

If Something Then
DoSomething End If

Right? ;-)
Nov 16 '05 #30

This discussion thread is closed

Replies have been disabled for this discussion.