Click here to Skip to main content
13,899,656 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

6K views
5 bookmarked
Posted 11 Feb 2018
Licenced CPOL

"Method can be made static" May Hide OO Design Flaw

, 22 Feb 2019
Rate this:
Please Sign up or sign in to vote.
"Method can be static" ReSharper warning may hide violation of object-oriented design

Introduction

Wandering through codebases, I've encountered some examples of code where ReSharper issues the above mentioned warning. Although the fix seems straightforward, the warning itself may hide a more subtle issue connected to object responsibility assignment.

Toy Example

Let's take a look at the following code:

public class EmailConstructor
    {
        private const string Signature = "Regards";

        public string Construct(User recipient, string body)
        {
            var builder = new StringBuilder();
            builder.Append($"Hello {GetNiceUserName(recipient)}");
            builder.Append(Environment.NewLine);
            builder.Append(body);
            builder.Append(Environment.NewLine);
            builder.Append(Signature);
            return builder.ToString();
        }

        private string GetNiceUserName(User user)
        {
            return $"{user.Name} {user.Surname}";
        }
    }

    public class User
    {
        public string Name { get; set; }
        public string Surname { get; set; }
    }

Indeed ReSharper issues the warning.

Feature Envy

Although things may seem obvious at first glance, actually the code above is the example of Feature envy. As the rule of thumb which would help you to spot such cases without using tools such as ReSharper, you may use one of the principles postulated by Craig Larman in his book "Applying UML and patterns".

Quote:

Information expert will lead to placing the responsibility on the class with the most information required to fulfill it.

According to this principle, you can fix the warning my moving method inside the User class as follows as the User is the one who has more information to represent his name in one or another way:

public class EmailConstructor
    {
        private const string Signature = "Regards";

        public string Construct(User recipient, string body)
        {
            var builder = new StringBuilder();
            builder.Append($"Hello {recipient.GetNiceUserName()}");
            builder.Append(Environment.NewLine);
            builder.Append(body);
            builder.Append(Environment.NewLine);
            builder.Append(Signature);
            return builder.ToString();
        }
    }

    public class User
    {
        public string Name { get; set; }
        public string Surname { get; set; }

        public string GetNiceUserName()
        {
            return $"{Name} {Surname}";
        }
    }

High Cohesion

Naturally, the question arises from the example above: "Won't this lead to bloating User class with lots of responsibilities such as, for example, working with persistent storage?". The answer is that Information expert principle should be used together with high cohesion principle.

Quote:

High cohesion is generally used in support of low coupling. High cohesion means that the responsibilities of a given element are strongly related and highly focused. Breaking programs into classes and subsystems is an example of activities that increase the cohesive properties of a system. Alternatively, low cohesion is a situation in which a given element has too many unrelated responsibilities. Elements with low cohesion often suffer from being hard to comprehend, hard to reuse, hard to maintain and averse to change.

Thus if we, for example, would like to extend our toy code with persistent storage interaction, we would likely extract highly cohesive functions dedicated to that into some sort of Unit of Work pattern.

Conclusion

Static members have some disadvantages such as they complicate unit testing. That's why static should be considered with care. Craig Larman's GRASP patterns in combinination with SOLID allows us to examine such cases in order to reduce support cost of our codebases.

History

  • 11th February, 2018: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Bohdan Stupak
Software Developer
Ukraine Ukraine
https://twitter.com/BohdanStupak1

You may also be interested in...

Comments and Discussions

 
QuestionReSharper is not a holy cow... Pin
Oleg Shilo23-Feb-19 23:32
memberOleg Shilo23-Feb-19 23:32 
PraiseGood point Pin
wmjordan11-Feb-18 13:43
professionalwmjordan11-Feb-18 13:43 
GeneralRe: Good point Pin
Bohdan Stupak13-Feb-18 21:57
professionalBohdan Stupak13-Feb-18 21:57 
AnswerWell, no Pin
pt140111-Feb-18 4:12
memberpt140111-Feb-18 4:12 
GeneralRe: Well, no Pin
Bohdan Stupak11-Feb-18 4:35
professionalBohdan Stupak11-Feb-18 4:35 
GeneralRe: Well, no Pin
pt140111-Feb-18 5:09
memberpt140111-Feb-18 5:09 
GeneralRe: Well, no Pin
Bohdan Stupak11-Feb-18 5:21
professionalBohdan Stupak11-Feb-18 5:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190306.1 | Last Updated 22 Feb 2019
Article Copyright 2018 by Bohdan Stupak
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid