Sunday, February 12, 2023

Use ChatGPT to Convert Other Language Source Code to Delphi

If you've read some of my previous posts you know how much I've struggled to make the paradigm shift from being a top-down, procedural programmer to an OOP programmer. I've purchased several books on OOP. And one book series I thought would hold some promise are "The Object-Oriented Thought Process" books written by Matt Weisfeld. I have four different editions on my book shelf.

I struggle with OOP for several reasons. One very frustrating reason is the lack of Delphi/Pascal examples used by authors. Most of the code examples are written in Java, or C, or C# or some language other than Delphi/Pascal.

The last bullet point on page 3 of Weisfled's fourth edition, listed under the heading of "What's New in the Fourth Edition", states:

  • Expanded code examples, for certain chapters, in Java, C# .NET, VB .NET, and Objective-C available online on the publisher's website

No Object Pascal or Delphi examples!

No Object Pascal or Delphi examples. I reached out to Weisfeld asking if he might include Object Pascal examples and received a negative response.

I love Pascal. It's elegant. It's easy to read and understand. I've been using Pascal/Delphi since 1985. (2023-1985=38 years). Besides SQL, it's my main programming language. Ok, I confess, I did Active Server Pages (APS) and VB Script for about 10 years.

I've tried loading Java onto my machine so I could follow along and learn the OOP concepts but I just can't wrap my head around the Java syntax.

Today, I discovered the answer that works for me. ChatGPT!

I asked ChatGPT to convert the following Java code from page 24 of "The Object-Oriented Thought Process" to Delphi and the results were very hopeful.

public class IntSquare {

  // private attribute
  private int squareValue;
  
  //public interface
  public in getSquare (int value) {
    
    squareValue = calculateSquare(value);
    return SquareValue
  
  }
  
  //private implementation
  private int calculation (int value) {
    return Math.pow(value,2);
  }
}

Here are the results returned from ChatGPT:

type
TIntSquare = class
private
FSquareValue: Integer;
function CalculateSquare(value: Integer): Integer;
public
function GetSquare(value: Integer): Integer;
end;

implementation

function TIntSquare.CalculateSquare(value: Integer): Integer;
begin
Result := value * value;
end;

function TIntSquare.GetSquare(value: Integer): Integer;
begin
FSquareValue := CalculateSquare(value);
Result := FSquareValue;
end;

end.

I know the results are not the indented and formatted code you would get from inside the IDE. This was my first attempt at using ChatGPT. I took several screen shots of my interaction with ChatGPT so I can show you what happened.

Here is my entire ChatGPT session:






Here is the final output from ChatGPT.

uses
  System.Math;
  
type
  TIntSquare = class
  private
    FSquareValue: Integer;
    function GetSquareValue: Integer;
    procedure SetSquareValue(const Value: Integer);
    function CalculateSquare(value: Integer): Integer;
  public
    property SquareValue: Integer read GetSquareValue write SetSquareValue;
    function GetSquare(value: Integer): Integer;
  end;

implementation

function TIntSquare.CalculateSquare(value: Integer): Integer;
begin
  Result := Round(Power(value, 2));
end;

function TIntSquare.GetSquare(value: Integer): Integer;
begin
  SquareValue := CalculateSquare(value);
  Result := SquareValue;
end;

function TIntSquare.GetSquareValue: Integer;
begin
  Result := FSquareValue;
end;

procedure TIntSquare.SetSquareValue(const Value: Integer);
begin
  FSquareValue := Value;
end;

end.

This looks very promising! Give ChatGPT a try converting code.
https://openai.com/blog/chatgpt/


Enjoy!
Semper Fi
Gunny Mike https://zilchworks.com

3 comments:

  1. The problem with ChatGPT creating / converting code is that it might look plausible but contain subtle errors which will be a pain to find and correct.
    That's not to say that human created code (that is: Created by myself), won't contain such errors.

    ReplyDelete
    Replies
    1. At this point my main objective is to understand the underlying concept others are trying to convey using code examples. At this point in my life I do not wish to learn a new programming language just so I can understand the concept.

      A quick Google search of code conversion from C, Java, or fill-in-the-blank to Pascal/Delphi yields minimal results. If ChatGPT can provide Pascal?Delphi code that is good enough for me to see and understand the concept that is good enough for me.

      Delete
  2. This is indeed very useful to understand any algorithm in delphi. There are so many examples on the internet which are written in C#,Java or C. we can convert and study those or use it for delphi.

    ReplyDelete