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

Antipattern: Adrenaline Junkie

Antipattern: Adrenaline Junkie

Understanding Project Pressure and Tension

I constantly wonder why situations arise where pressure and tension are generated in projects. One reason is that most projects are simply complex—you have to coordinate several, sometimes dozens of people, anticipate and plan in advance what will happen, and determine what resources will be needed. As a species, we’re not very good at detailed long-term planning (see: David Rock – Your Brain at Work).

Read More

Don't Be Too Quick, Start Thinking!

Don’t Be Too Quick, Start Thinking!

Sometimes I feel like the world has become too fast. Everything happens so quickly that we switch to autopilot and stop thinking about why and for what purpose we are doing a task. Are we doing it in the way we imagined, or the way someone else suggested?

Read More

Why Agile Fails

Introduction

Implementing a methodology from the Agile family is not at all easy. The problem usually lies in management, who upon superficially understanding what it’s all about, perceive the new method as a promise that from now on, everything will magically work better. It doesn’t matter if we have subpar team members and adhere to the principle that “any specialist can be replaced by a finite number of students.” It doesn’t matter if there’s complete disregard for knowledge management and skill development in the team because there’s never time for that. It doesn’t matter if people working on projects are shuffled between projects—after all, it’s about interdisciplinarity, and everyone should know everything.

Read More