473,383 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

This keyword

Hi,

I need a little help in understanding why FxCop complains about this.

If I have a basic class like:
class Class1
{
static void Main()
{
Class1 class1 = new Class1();
class1.Do("Something");
}

public void Do(string PassedInString)
{
Console.WriteLine("String passed in is " + PassedInString);
}
}
Problem is that although this compiles OK and works as expected (most of my
C# books are full of examples just like this) FxCop complains because of an
unused parameter in the Do method. It basically says to use the "this"
keyword or make the method static.

In a project I have FxCop reports some methods and not others so I am unsure
what it is actually reporting, whether it is a problem and what the best
solution should be.

Thanks,
Dave.

Nov 17 '05 #1
3 1740
As good as it is, FxCop is not perfect, an example of that would be the
following warning that I received when I ran it over an assembly built with
your code:

'Class1' is an internal class that is apparently never instantiated. If so,
remove the code from the assembly. If this class is intended to contain only
static methods, consider adding a private constructor to prevent the compiler
from generating a default constructor.

From the looks of it, because of the static constructor it thinks that more
of the class should be static which given the example you gave would not be a
bad idea, unless you really need to create an instance of the class within
itself.

Brendan
"Dave" wrote:
Hi,

I need a little help in understanding why FxCop complains about this.

If I have a basic class like:
class Class1
{
static void Main()
{
Class1 class1 = new Class1();
class1.Do("Something");
}

public void Do(string PassedInString)
{
Console.WriteLine("String passed in is " + PassedInString);
}
}
Problem is that although this compiles OK and works as expected (most of my
C# books are full of examples just like this) FxCop complains because of an
unused parameter in the Do method. It basically says to use the "this"
keyword or make the method static.

In a project I have FxCop reports some methods and not others so I am unsure
what it is actually reporting, whether it is a problem and what the best
solution should be.

Thanks,
Dave.

Nov 17 '05 #2
Yes and no.

Yes, FxCop is suggesting that you make the method static because it
doesn't use any instance fields.

No, it's not for efficiency reasons.

FxCop is occasionally concerned with efficiency (viz its constant
admonishments to use str.Length == 0 rather than str == ""). However,
more than that it's concerned with standards and, ultimately,
readability.

In this case, what FxCop is telling you is that static methods are
inherently easier to read and internalize than instance methods. The
complaint doesn't really hold for one-line methods, but for larger
methods, here is why.

A large instance method is free to use class members anywhere inside
it. If you want to find out whether an instance method uses any object
state, or modifies any object state, then you have to read all of the
code. There is no clue in the method declaration that it might use or
change object state (as held in the object's instance fields).

A static method, on the other hand, guarantees by its very nature that
the only things it can get its hands on are the arguments passed to it,
and any static fields, properties, or methods that happen to be out
there. It can't (directly) modify object state. So, as soon as you see
the word "static" against a method, you trust it not to monkey with
your object's state. Sometimes, that's all you wanted to know: which
method is messing up the this._frobnitz field? You can automatically
rule out all static methods as potential culprits.

That's why, if you have an instance method that doesn't reference any
object state, and isn't virtual (and so cannot be overridden to refer
to any object state), then FxCop recommends that you make it static. In
the end, it comes down to a style thing: is this really a little
"helper" method that does a small job and doesn't need object state? If
so, why not make it static? I even make some private methods static if
they read only one or two instance fields: I just change the methods to
take the values as arguments and then make the methods static.

What I ask myself is: is this method really an _operation on the
object_, or is it a helper? If it's a helper, then I think it's clearer
to make it static. If it really is, logically speaking, an operation on
the object itself, then I leave it as an instance method.

Nov 17 '05 #3

"Bruce Wood" <br*******@canada.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
Yes and no.

Yes, FxCop is suggesting that you make the method static because it
doesn't use any instance fields.

No, it's not for efficiency reasons.

FxCop is occasionally concerned with efficiency (viz its constant
admonishments to use str.Length == 0 rather than str == ""). However,
more than that it's concerned with standards and, ultimately,
readability.

In this case, what FxCop is telling you is that static methods are
inherently easier to read and internalize than instance methods. The
complaint doesn't really hold for one-line methods, but for larger
methods, here is why.

A large instance method is free to use class members anywhere inside
it. If you want to find out whether an instance method uses any object
state, or modifies any object state, then you have to read all of the
code. There is no clue in the method declaration that it might use or
change object state (as held in the object's instance fields).

A static method, on the other hand, guarantees by its very nature that
the only things it can get its hands on are the arguments passed to it,
and any static fields, properties, or methods that happen to be out
there. It can't (directly) modify object state. So, as soon as you see
the word "static" against a method, you trust it not to monkey with
your object's state. Sometimes, that's all you wanted to know: which
method is messing up the this._frobnitz field? You can automatically
rule out all static methods as potential culprits.

That's why, if you have an instance method that doesn't reference any
object state, and isn't virtual (and so cannot be overridden to refer
to any object state), then FxCop recommends that you make it static. In
the end, it comes down to a style thing: is this really a little
"helper" method that does a small job and doesn't need object state? If
so, why not make it static? I even make some private methods static if
they read only one or two instance fields: I just change the methods to
take the values as arguments and then make the methods static.

What I ask myself is: is this method really an _operation on the
object_, or is it a helper? If it's a helper, then I think it's clearer
to make it static. If it really is, logically speaking, an operation on
the object itself, then I leave it as an instance method.

Thanks for the info. I said efficiency really because FxCop flagged this
under the Performance category but I understand now why these particular
methods may be better as static.

Dave.
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Edward Diener | last post by:
In the tutorial on functions there are sections on default arguments and keyword arguments, yet I don't see the syntactic difference between them. For default arguments the tutorial shows: def...
12
by: Jack | last post by:
Hi, Is it recommended to use the var keyword when declaring a variable? IE lets me create a variable with var, then create the same one again with no problem. Also it lets me create a...
12
by: Daniel Kay | last post by:
Hi Folks! Everytime I work with virtual and pure virtual methods I ask myself if there is a difference between class B and class C (see below). Is it redundant to repeat the virtual keyword in a...
7
by: Willem van Rumpt | last post by:
Hi all, coming from an unmanaged programming background, I took my time to sort out the IDisposable and finalizer patterns. Just when I thought I had it all conceptually neatly arranged, the...
3
by: Arthur Dent | last post by:
Hi all, Im just curious, what is the purpose of the keyword "Overloads" in VB nowadays? I understand conceptually what overloads are and what they do, but im a little puzzled, because if you...
18
by: vermarajeev | last post by:
Hello everybody, This is my second query in this post. Firstly thankx to Banfa, for helping me solve my first query. Here is the code which I have written. #include<iostream>...
1
by: rajeshkapadi | last post by:
Please help me optimize this: I have a table with columns: headlineid, keyword. headlineid+keyword combination is unique. Relationship between headline and keyword is many-to-many. i.e.,...
6
by: tom | last post by:
Hi I try to check whether a given input is keyword or not. However this script won't identify keyword input as a keyword. How should I modify it to make it work? #!usr/bin/env python import...
4
by: Pranjal9880 | last post by:
Hi all I am trying to parse the xml file using perl in which I am succeeded , I am able to fetch the data from the xml file by using one keyword. Now I want to do it using more than one keyword. It...
1
by: prasath03 | last post by:
Hi Gurus, I am doing one website project that project contains one search module. In that search page i have entered the keyword to search. If i want to search the keyword with "any keyword" or...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.