I had a recent code review that gave me some slightly surprising feedback. It was in an instantiable service class that had a number of fully encapsulated private methods. In other words, methods that didn't reference any higher level functions or variables. For a trivial example:

private decimal IncreaseByPercent(decimal original, int percentageIncrease)
    return original + ((original / 100) * percentageIncrease);

The feedback suggested that methods like this should be marked as static. It's not a suggestion I've come across before: indeed I've rarely used static methods outside of static classes. What would be the advantages of doing so?

  • 2
    Unfortunately this question was closed before I could finish writing my answer. In addition to the answers in the "duplicate", it looks like you are using C#. If so, there's a very good reason to mark a method as static if you can: performance. "After you mark the methods as static, the compiler will emit non-virtual call sites to these members ... [which] can achieve a measurable performance gain for performance-sensitive code.": msdn.microsoft.com/en-us/library/ms245046.aspx – David Arno Apr 17 '18 at 16:18
  • 4
    @DavidArno: The performance benefits are almost inconsequential in most cases. "Performance-sensitive code" is a special case. The real reason you mark private methods as static is to indicate that you won't be touching instance state. – Robert Harvey Apr 17 '18 at 16:52
  • @RobertHarvey, performance-sensitive code used to be a special case. The mass of crap added to C# 7.1 and 7.2 around passing structs by reference sadly shows those days are gone. Games and mobile (both of which are performance sensitive) is a big thing for MS these days and drives a lot of the C# language development. – David Arno Apr 17 '18 at 17:33
  • @DavidArno: I don't know anything about C# 7.x, but based on your description it seems that these new constructs are better to be avoided (at least in game development), not papered over with static calls. – Robert Harvey Apr 17 '18 at 17:38
  • In the future, specify your language, as "static" can have several meanings. – Frank Hileman Apr 18 '18 at 0:00

Browse other questions tagged or ask your own question.