Natural Order of Refactoring Explored Part 2: Compose Method

Table of Contents

Compose Method

Analyzing methods, such as the one presented in Part 1, often leads us to understand the main points of the algorithm contained in them. This insight paves the way for the next step: try to split a large method into smaller steps by extracting them into separate methods (refactoring using the Extract Method). Thus, the original method will consist of a sequence of calls to these new methods. With the right naming conventions, you can achieve code that reads like a book.

While doing this, it is also worth cleaning up a bit, especially regarding enigmatic names. Below you will find an example.

public class TextObfuscator
{
    // ...
    public string Obfuscate(string text)
    {
        String result = ChangeWordsOrderRandomly(text);
        result = AddMeaninglessWordsRandomly(result);
        result = RemoveSeparators(result);
        result = AddSeparatorsRandomly(result);
        result = ReplacePolishCharactersWithNonPolishCharacters(result);
        result = ReplaceUpperAndLowerCharactersRandomly(result);
        result = RemoveSpaces(result);
        return result;
    }
    public String RemoveSpaces(String text)
    {
        return text.Replace(" ", "");
    }

    public String RemoveSeparators(String text)
    {
        return Regex.Replace(text, SEPARATORS_REGEX, "", RegexOptions.CultureInvariant);
    }

    public String ReplacePolishCharactersWithNonPolishCharacters(String text)
    {
        return text
                .Replace("ą", "a")
                .Replace("ł", "l")
                .Replace("ę", "e")
                .Replace("ń", "n")
                .Replace("ż", "z")
                .Replace("ź", "z")
                .Replace("ó", "o")
                .Replace("ś", "s")
                .Replace("ć", "c")
                .Replace("Ą", "A")
                .Replace("Ł", "L")
                .Replace("Ę", "E")
                .Replace("Ń", "N")
                .Replace("Ż", "Z")
                .Replace("Ź", "Z")
                .Replace("Ó", "O")
                .Replace("Ś", "S");
    }

    public String ReplaceUpperAndLowerCharactersRandomly(String text)
    {
        String result = "";
        char[] textArray = text.ToCharArray();

        foreach (char currentCharacter in textArray)
        {
            if (ShouldBeUpperAndLowerReplaced())
            {
                result += ReplaceLowerAndUpper(currentCharacter);
            }
            else
            {
                result += currentCharacter;
            }
        }

        return result;
    }
    // ...
}

(Text translated and moved from original old blog automatically by AI. May contain inaccuracies.)

Related Posts

Self-Organization - A Myth?

The entire Agile community (and not just Agile) loudly discusses organization. I have the impression that for some time, we’ve been trying to find the holy grail that will allow us to answer the question: “How to manage without managing?”. This time, I’ll play the devil’s advocate.

Read More

Simple, Complicated, Complex and Chaotic Systems, in Other Words Cynefin. And How Does It Relate to Software Development?

Intro

You might have already come across terms such as complex systems, complicated systems, or complex adaptive systems; especially, if you have read Management 3.0 by Jurgen Appelo or heard about Ken Schwaber’s ideas about the applicability of Scrum. It might sound intriguing, but finding logic in it is difficult without some background theory. This is the point at which Dave Snowden’s Cynefin concept comes in handy. This concept is based on complex systems theory, anthropology, evolutionary psychology, and, last but not least, cognitive psychology.

Read More

Have You Ever Thought About a Career as a Trainer/Consultant?

As our activities at BNS IT (bnsit.pl) continue to expand, we have openings for individuals eager to work with others to share knowledge and experience. We aim to support teams in increasing their work efficiency.

Read More