Skip to main content
grammar
Source Link
Caridorc
  • 28.1k
  • 7
  • 55
  • 138

@t3chb0t rightfully noted that isPalindrome still does two things, ifin fact, it is better to implement it like:

@t3chb0t rightfully noted that isPalindrome still does two things, if fact, it is better to implement it like:

@t3chb0t rightfully noted that isPalindrome still does two things, in fact, it is better to implement it like:

scope
Source Link
Caridorc
  • 28.1k
  • 7
  • 55
  • 138

Do not put types into the name of the variables.

I am talking about regexPattern

Use constants

The cleaner RegexThe cleaner Regex should be a class constant.

private const string cleaner = @"(?i)[^a-z]";

And replacement is unlikely to change, just use ""

clean is now simpler:

public static string clean(string s) 
{
    return Regex.Replace(s, cleaner, "").ToLower();
}

Variables should be a class constant. Do not put types intodeclared in the name ofsmallest possible scope, so putting the variables.

private const string cleaner = @"(?i)[^a-z]";

And replacementpattern inside the function is unlikely to changebest. @Mat's Mug suggests inlining it, just use ""

clean is now simpler:and I agree for such small a pattern.

public static string clean(string s) 
{
    return Regex.Replace(s, cleaner, "").ToLower();
}

Use constants

The cleaner Regex should be a class constant. Do not put types into the name of the variables.

private const string cleaner = @"(?i)[^a-z]";

And replacement is unlikely to change, just use ""

clean is now simpler:

public static string clean(string s) 
{
    return Regex.Replace(s, cleaner, "").ToLower();
}

Do not put types into the name of the variables.

I am talking about regexPattern

Use constants

The cleaner Regex should be a class constant.

private const string cleaner = @"(?i)[^a-z]";

And replacement is unlikely to change, just use ""

clean is now simpler:

public static string clean(string s) 
{
    return Regex.Replace(s, cleaner, "").ToLower();
}

Variables should be declared in the smallest possible scope, so putting the pattern inside the function is best. @Mat's Mug suggests inlining it, and I agree for such small a pattern.

single responsability
Source Link
Caridorc
  • 28.1k
  • 7
  • 55
  • 138

Use a for loop where fit

public static string MergeMultipleLinesIntoOneLine(int numberOfLines)
{
    
    var stringResult = new StringBuilder();

    for (var line = 0; line < numberOfLines; line++)
    {
        Console.WriteLine("Please enter phrase #{0}: ", line);
        stringResult.Append(Console.ReadLine() + " ");
    }

    return stringResult.ToString();
}

MergeMultipleLinesIntoOneLine was very weird, when looping a fixed number of times, you should always use a for loop.

Refactor further

isPalindrome does two things:

  • It cleans the string.
  • It actually checks if it a palindrome.

These are two things, so we need two functions:

public static string clean(string s) 
{
    string regexPattern = @"(?i)[^a-z]";
    string replacement = "";
    return Regex.Replace(s, regexPattern, replacement).ToLower();
}

and:

public static bool IsPalindrome(string someString)
{
    var amendedString = clean(someString);
    string reverseAmendedString = new string(amendedString.Reverse().ToArray());
    return amendedString.Equals(reverseAmendedString);
}

Use constants

The cleaner Regex should be a class constant. Do not put types into the name of the variables.

private const string cleaner = @"(?i)[^a-z]";

And replacement is unlikely to change, just use ""

clean is now simpler:

public static string clean(string s) 
{
    return Regex.Replace(s, cleaner, "").ToLower();
}

Avoid unnecessary variables

bool isPalindrome = IsPalindrome(mergedLine);

Delete that line, just use:

Console.WriteLine(isPalindrome(mergedLine) ? "Palindrome" : "Not a palindrome");

Also isPalindrome can use less verbose names and be reduced to:

public static bool IsPalindrome(string someString)
{
    var cleaned = clean(someString);
    return cleaned.Equals(amendedString.Reverse().ToArray());
}

Seriously reducing noise.

Single responsibility system

@t3chb0t rightfully noted that isPalindrome still does two things, if fact, it is better to implement it like:

public static bool IsPalindrome(string text)
{
    return text.Equals(text.Reverse().ToArray());
}

And be careful to pass in cleaned strings. Now it really does only one thing.

Use a for loop where fit

public static string MergeMultipleLinesIntoOneLine(int numberOfLines)
{
    
    var stringResult = new StringBuilder();

    for (var line = 0; line < numberOfLines; line++)
    {
        Console.WriteLine("Please enter phrase #{0}: ", line);
        stringResult.Append(Console.ReadLine() + " ");
    }

    return stringResult.ToString();
}

MergeMultipleLinesIntoOneLine was very weird, when looping a fixed number of times, you should always use a for loop.

Refactor further

isPalindrome does two things:

  • It cleans the string.
  • It actually checks if it a palindrome.

These are two things, so we need two functions:

public static string clean(string s) 
{
    string regexPattern = @"(?i)[^a-z]";
    string replacement = "";
    return Regex.Replace(s, regexPattern, replacement).ToLower();
}

and:

public static bool IsPalindrome(string someString)
{
    var amendedString = clean(someString);
    string reverseAmendedString = new string(amendedString.Reverse().ToArray());
    return amendedString.Equals(reverseAmendedString);
}

Use constants

The cleaner Regex should be a class constant. Do not put types into the name of the variables.

private const string cleaner = @"(?i)[^a-z]";

And replacement is unlikely to change, just use ""

clean is now simpler:

public static string clean(string s) 
{
    return Regex.Replace(s, cleaner, "").ToLower();
}

Avoid unnecessary variables

bool isPalindrome = IsPalindrome(mergedLine);

Delete that line, just use:

Console.WriteLine(isPalindrome(mergedLine) ? "Palindrome" : "Not a palindrome");

Also isPalindrome can use less verbose names and be reduced to:

public static bool IsPalindrome(string someString)
{
    var cleaned = clean(someString);
    return cleaned.Equals(amendedString.Reverse().ToArray());
}

Seriously reducing noise.

Use a for loop where fit

public static string MergeMultipleLinesIntoOneLine(int numberOfLines)
{
    
    var stringResult = new StringBuilder();

    for (var line = 0; line < numberOfLines; line++)
    {
        Console.WriteLine("Please enter phrase #{0}: ", line);
        stringResult.Append(Console.ReadLine() + " ");
    }

    return stringResult.ToString();
}

MergeMultipleLinesIntoOneLine was very weird, when looping a fixed number of times, you should always use a for loop.

Refactor further

isPalindrome does two things:

  • It cleans the string.
  • It actually checks if it a palindrome.

These are two things, so we need two functions:

public static string clean(string s) 
{
    string regexPattern = @"(?i)[^a-z]";
    string replacement = "";
    return Regex.Replace(s, regexPattern, replacement).ToLower();
}

and:

public static bool IsPalindrome(string someString)
{
    var amendedString = clean(someString);
    string reverseAmendedString = new string(amendedString.Reverse().ToArray());
    return amendedString.Equals(reverseAmendedString);
}

Use constants

The cleaner Regex should be a class constant. Do not put types into the name of the variables.

private const string cleaner = @"(?i)[^a-z]";

And replacement is unlikely to change, just use ""

clean is now simpler:

public static string clean(string s) 
{
    return Regex.Replace(s, cleaner, "").ToLower();
}

Avoid unnecessary variables

bool isPalindrome = IsPalindrome(mergedLine);

Delete that line, just use:

Console.WriteLine(isPalindrome(mergedLine) ? "Palindrome" : "Not a palindrome");

Also isPalindrome can use less verbose names and be reduced to:

public static bool IsPalindrome(string someString)
{
    var cleaned = clean(someString);
    return cleaned.Equals(amendedString.Reverse().ToArray());
}

Seriously reducing noise.

Single responsibility system

@t3chb0t rightfully noted that isPalindrome still does two things, if fact, it is better to implement it like:

public static bool IsPalindrome(string text)
{
    return text.Equals(text.Reverse().ToArray());
}

And be careful to pass in cleaned strings. Now it really does only one thing.

clean
Source Link
Caridorc
  • 28.1k
  • 7
  • 55
  • 138
Loading
Source Link
Caridorc
  • 28.1k
  • 7
  • 55
  • 138
Loading