How to enforce coding style conventions

  • Thread starter Thread starter Ron Burd
  • Start date Start date
R

Ron Burd

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)
 
Ron said:
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/
 
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.
 
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
 
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 said:
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.
 
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 said:
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();
}
}
 
The later wastes space - one line with ONE character on it! [/quote said:
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".
 
Jimi said:
The later wastes space - one line with ONE character on it! [/quote said:
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 said:
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.
 
Roy Fine said:
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)


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...
 
Gary said:
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 :-)
 
Gary Morris said:
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
 
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
 

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

Back
Top