DEV Community

Cover image for No Need to Maintainability Anymore with AI?
Mehran Davoudi
Mehran Davoudi

Posted on

No Need to Maintainability Anymore with AI?

Look at this bad code which is considered really hard to maintain:

public class ClassA
{
    public string FirstName { get; set; }
    public int Age { get; set; } = 20;
}

public class MyProgram
{
    private static int GlobalVariable;

    public void BadMethod()
    {
        var t= typeof(ClassA);
        var i = Activator.CreateInstance(t);
        var firstName = type.GetProperty("FirstName");
        firstName.SetValue(i, "John Doe");

        var ageOfPerson = type.GetProperty("Age");
        var age = property2.GetValue(i);

        Console.WriteLine("Hello World");

        GlobalVariable = 0
    Loop:
        Console.WriteLine($"{property1} was at age {GlobalVariable}.");
        GlobalVariable++;

        if (GlobalVariable < age)
        {
            goto Loop;
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

It is hard to maintain because it is difficult for humans to modify it. But is it difficult for AI too!?
In this post I'm going to demonstrate how AI can refactor an unmaintainable code much better than humans!

For this I'm using Bing AI in the following examples.

Why is it not maintainable?

Let's see what makes this code unmaintainable for humans.

Reflection is bad

As you see, this code uses reflection to work with the objects. The first and biggest pitfall is that if for example you want to rename the FirstName to Title, you probably will forget to change the hard-coded literal string in this line as a human:

type.GetProperty("FirstName")`
Enter fullscreen mode Exit fullscreen mode

But let's see if Bing AI does the same mistake or not?

Refactored with Reflection

How Amaziiiing... the AI didn't need the code to be maintainable. It just worked fine and changed that line too:

type.GetProperty("Title")`
Enter fullscreen mode Exit fullscreen mode

Bad Naming

As you see ClassA has a really bad name. It can be perceived that Person would be a better name for it. For a human it takes time to figure it out. But not for AI. Look how Bing AI perceives this:

Refactored with bad naming

Amaaazing again... As you see the Bing AI perfectly guessed that Person is a good name for ClassA.

Hard to Understand Loop with goto

As you see this code used goto to implement a loop, instead of a for, foreach, or even a while statement. Understanding such loop is difficult for humans and difficult to modify.
The loop is printing all the ages that the person has lived so far! If a one has a requirement to print just half of the ages of a person, it is really hard to apply the proper change by a human.
But let's see how Bing AI do it?

Refactored the bad loop

Wow, as you see AI did it great, it just applied the proper changes this bad loop. It decided to modify this line amazingly:

GlobalVariable += 2;
Enter fullscreen mode Exit fullscreen mode

Conclusion

In the samples above, I've demonstrated some situations which is difficult for humans to understand and act right. For that reason, humans need a code to be maintainable. But as you see Bing AI just worked fine with no need to maintainability.
I know my examples are not enough or even satisfying. One might argue the type of situations that I made example of. But the whole idea was to show how AI is less dependent to maintainable code that humans.
So here's my final conclusion:

AI does not need a code to be maintainable to be able to refactor it. In fact, AI can refactor an unmaintainable code much better than humans.

So, can we say in the future the need for maintainable code will decrease eventually!?

Top comments (0)