No Need to Maintainability Anymore with AI?
Tags: chatgpt, csharp, bing, ai
Published: 2023-04-12 23:36:54

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

``` csharp
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;
        }
    }
}
 
```

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:

```csharp
type.GetProperty("FirstName")`
```

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

![Refactored with Reflection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yseh09lr267w4b90qois.png)

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

```csharp
type.GetProperty("Title")`
```

### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yiuv3tx5vi6l426ec2a7.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yma3v4yyysqtumuwvfrh.png)

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

```csharp
GlobalVariable += 2;
```

## 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!?