0

I use resharper and resharper adviced me to declare one method as static and the another not. But i can't understand why the other method can't be static ?

method recommended to be static

 private static string Prehod(string retazec)
    {
        var pole = retazec.ToCharArray();
        var output = "";
        char? temp = null;

        for (var i = 0; i < pole.Length; i++)
        {
            if (temp == null)
            {
                temp = pole[i];
                continue;
            }

            output += pole[i].ToString() + temp.ToString();
            temp = null;
        }
        return output;
    }

and method not recommended to be static

 public string HashToString(string hash,int dlzka)
    {
        var hashChar = hash.Substring(0, dlzka*2);
        var retazec = "";

        for (var i = 0; i < dlzka*2; i++)
        {
            if(i%2 != 0)
            {
                retazec += hashChar.Substring(i, 1);
            }
        }
        return retazec;
    }
user137348
  • 10,166
  • 18
  • 69
  • 89
  • 2
    You should strong type your variables (ie string, int) instead of using var as you do. Var is mainly intended for specific situations involving linq, and should NOT be used in a for loop as you do. – Brann Oct 14 '09 at 11:54
  • @Brann: Why? Resharper itself suggests to do it, and i never had problems with it. – Alex Bagnolini Oct 14 '09 at 11:58
  • 2
    "var" still is strongly typed, Thy type is just infered. – Maximilian Mayerl Oct 14 '09 at 11:58
  • @Brann: Your opinion is highly disputed. Check this conversation for example: http://stackoverflow.com/questions/236878/what-to-use-var-or-object-name-type – HuBeZa Oct 14 '09 at 12:03
  • @Brann Why? Personally, I'd have explicitly written the types of int, string etc, as you suggest, but in practise it has /no/ impact on the code; the variables /are/ strongly-typed, because the compiler swaps "var" for the expected type definition. Other than a minor impact on ease of reading and refactoring, using var here has made no difference at all. – FacticiusVir Oct 14 '09 at 12:07
  • There is indeed debate about it. The general consensus is that people that prefer strict typing also prefer to use classnames instead of `var`. People that favor loose typing (i.e., use `Variant` in old VB6 days a lot), love `var`. The advantage above `object` is that `var` is typed, it's just not visible and imo, that makes code less clear. If possible, use a type to make your code more readable (my opinion and Microsoft's iirc). Don't always believe automatic tools like ReSharper, think what works best for you and that makes your code most readable :) – Abel Oct 14 '09 at 12:11
  • Granted var is strongly typed. That being said, I have yet to see a single post advising to write "For(var i" instead of "For(int i" (including in the thread HubezA pointed to ( http://stackoverflow.com/questions/236878/what-to-use-var-or-object-name-type ) – Brann Oct 14 '09 at 14:30

5 Answers5

2

Resharper doesn't give advices on public members of your classes, since they can be used by other classes.

But it's still a sign (not to say 'smell') for you if public instance methods don't need instance at all.

elder_george
  • 7,849
  • 24
  • 31
0

I can't see any reason why the second method can't be static, as to why Resharper suggests one and not the other... you'd have to ask the Resharper developers. Remember, it's a tool, not a rule book.

Lazarus
  • 41,906
  • 4
  • 43
  • 54
0

I don't see why your second method couldn't be static. As far as I can tell, it doesn't access any instance fields.

It wouldn't be a problem mnaking it static. So much to your question why it couldn't be static. As for ReSharper, I don't know why it doesn't recommend it as static whereas it does this for the other method.

Maximilian Mayerl
  • 11,253
  • 2
  • 33
  • 40
0

Your first method is private, and probably used statically inside the class. Eg: var ehy = Prehod("testing");.

The second method is public. Probably you use it already somewhere like:

   var okBaby = new MyClass();
   Console.WriteLine(okBaby.HashToString("something", 10));

And resharper probably thinks there is a reason for it to be so, and doesn't suggest the change.

Just a blind shot, though.

Alex Bagnolini
  • 21,990
  • 3
  • 41
  • 41
0

Both methods can be static: they don't use any instance members of your class. And both methods can be instance methods. But a method being static or not should be implied by the possible uses of it, not by ReSharper. Do you expect your method to be called as part of an instance:

YourClass instance = new YourClass();
string x = instance.Prehod(...);

or do you expect your method to be called as part of the class:

string x = YourClass.Prehod(...);

It can be much later in the development process before you realize which is best. If you want to give users of your class the "feel" that it acts on the instance (new ClassName()), you must choose instance method. If you really need the method to be used without an instance (ClassName.YourMethod()) you must choose static.

UPDATE: it is uncommon to make a private method static, unless you need other static methods to call the private static methods. If only other instance methods use your private method, then there's no reason to make it a static method whatsoever.

Abel
  • 56,041
  • 24
  • 146
  • 247
  • Resharper suggests making private methods static because it is a (very slight) performance increase - the 'this' parameter isn't used so it doesn't need to be given to the method. – configurator Oct 14 '09 at 12:38
  • The performance increase is much debated and is hard or even impossible to proof in practice. Semantics, use and clarity should drive the choice, not the `callvirt` vs. `call` difference. More here, which also discusses the related FxCop rule: http://gregbeech.com/blogs/tech/archive/2007/01/11/static-vs-instance-method-performance.aspx – Abel Oct 14 '09 at 12:59