Darian Miller 366 Posted February 18, 2023 Provide ChatGPT with table schema and ask it to generate the Delphi class, along with Create/Read/Update/Delete methods. https://www.ideasawakened.com/post/leveraging-chatgpt-to-generate-a-delphi-class-along-with-crud-code-from-a-table-schema Is there a IDE Expert or stand alone tool that does this sort of codegen? 1 Share this post Link to post
David Schwartz 430 Posted February 18, 2023 ummm ... all that comes to my mind is ... HOLY SHIT! Share this post Link to post
David Schwartz 430 Posted February 18, 2023 (edited) I asked ChatGPT for a sample class in Delphi. Then asked it to extend the class to show how to move the data to and from a JSON message. This is what it produced. type TPerson = class private FFirstName: string; FLastName: string; FAge: Integer; public property FirstName: string read FFirstName write FFirstName; property LastName: string read FLastName write FLastName; property Age: Integer read FAge write FAge; function ToJSON: string; class function FromJSON(const AJSON: string): TPerson; end; function TPerson.ToJSON: string; var JSONObj: TJSONObject; begin JSONObj := TJSONObject.Create; try JSONObj.AddPair('FirstName', FirstName); JSONObj.AddPair('LastName', LastName); JSONObj.AddPair('Age', TJSONNumber.Create(Age)); Result := JSONObj.ToString; finally JSONObj.Free; end; end; class function TPerson.FromJSON(const AJSON: string): TPerson; var JSONObj: TJSONObject; begin JSONObj := TJSONObject.ParseJSONValue(AJSON) as TJSONObject; try Result := TPerson.Create; Result.FirstName := JSONObj.GetValue('FirstName').Value; Result.LastName := JSONObj.GetValue('LastName').Value; Result.Age := JSONObj.GetValue('Age').Value.ToInteger; finally JSONObj.Free; end; end; This could be a HUGE TIME-SAVER for stuff like this! It's like having a team of interns on staff who write at the speed of light. This took less than two minutes. I asked it to add the uses clause and it generated an entire unit: unit Person; interface uses System.SysUtils, System.JSON; type TPerson = class private FFirstName: string; FLastName: string; FAge: Integer; public property FirstName: string read FFirstName write FFirstName; property LastName: string read FLastName write FLastName; property Age: Integer read FAge write FAge; function ToJSON: string; class function FromJSON(const AJSON: string): TPerson; end; implementation { TPerson } function TPerson.ToJSON: string; var JSONObj: TJSONObject; begin JSONObj := TJSONObject.Create; try JSONObj.AddPair('FirstName', FirstName); JSONObj.AddPair('LastName', LastName); JSONObj.AddPair('Age', TJSONNumber.Create(Age)); Result := JSONObj.ToString; finally JSONObj.Free; end; end; class function TPerson.FromJSON(const AJSON: string): TPerson; var JSONObj: TJSONObject; begin JSONObj := TJSONObject.ParseJSONValue(AJSON) as TJSONObject; try Result := TPerson.Create; Result.FirstName := JSONObj.GetValue('FirstName').Value; Result.LastName := JSONObj.GetValue('LastName').Value; Result.Age := JSONObj.GetValue('Age').Value.ToInteger; finally JSONObj.Free; end; end; end. It also added this: The uses clause includes the System.SysUtils unit for the TJSONNumber.ToInteger method, as well as the System.JSON unit for the TJSONObject and TJSONValue classes and related functions. Edited February 18, 2023 by David Schwartz Share this post Link to post
David Schwartz 430 Posted February 18, 2023 (edited) 7 hours ago, Darian Miller said: Provide ChatGPT with table schema and ask it to generate the Delphi class, along with Create/Read/Update/Delete methods. https://www.ideasawakened.com/post/leveraging-chatgpt-to-generate-a-delphi-class-along-with-crud-code-from-a-table-schema Is there a IDE Expert or stand alone tool that does this sort of codegen? I gave it a class and asked it to generate code needed to move data to and from a JSON message and it did a bang-up job. But it stopped at one point in the toJSON function. I just asked: can you just show the ToJSON function and ... it did! So that's one way to get past this limitation. if it gives you a list of methods and stops at some point, just ask for them individually. Edited February 18, 2023 by David Schwartz Share this post Link to post
Rollo62 539 Posted February 18, 2023 14 hours ago, Darian Miller said: Provide ChatGPT with table schema Is there a IDE Expert or stand alone tool that does this sort of codegen? Im sure it will be available soon. I am only curious what the ChatGPT service might cost after its free period, ending in April. Share this post Link to post
Anders Melander 1815 Posted February 18, 2023 Once again, it's just a party trick; A complete waste of time. What's demonstrated here is not how a professional solves a problem. If you're lucky the generated code is one possible solution, but regardless of how correct the code is there's almost no chance of it being the best solution to a given problem. Share this post Link to post
David Schwartz 430 Posted February 19, 2023 10 hours ago, Rollo62 said: Im sure it will be available soon. I am only curious what the ChatGPT service might cost after its free period, ending in April. It seems it's free with limitations, and you can upgrade now for $20 / mo. 1 Share this post Link to post
David Schwartz 430 Posted February 19, 2023 (edited) 7 hours ago, Anders Melander said: Once again, it's just a party trick; A complete waste of time. What's demonstrated here is not how a professional solves a problem. If you're lucky the generated code is one possible solution, but regardless of how correct the code is there's almost no chance of it being the best solution to a given problem. I spent an hour or so playing with it. There are definitely some limitations and drawbacks. I was trying to get it to generate some code I could use, but it kept giving me stuff that would not compile. I told it "not xyz" and it just kept apologizing and then showing xyz in the code anyway. Instead of repeating "NOT xyz" I said, "Try a different approach" and that got it unstuck. What surprised me was the variety of approaches it took when I asked it to regenerate the answer. Saying it's "not how a professional solves a problem" is disengenuous at least -- I've seen far worse coding in tons of production code! I think it's great as a learning tool, to see how certain problems might be solved. I mean, it showed quite a range of solutions! I also think it could be great for generating code to do imports and exports to/from classes ... I think it's called "ETL" stuff? There's a LOT of basic boilerplate code that needs to be generated whenever you're working with data tables and you need to get the field values into and out of some other collection of things. It's really boring and repetative stuff to code; it's too complex to do with regex in an editor, and requires knowledge of what's in the source and destination to get everything matched-up correctly. This sort of thing is the bane of my work a lot of times. If this is something that can solve this type of problem easily, then I'm all for it! I plan to explore this usage further. I had it generate code to save and load a class to JSON. Because I used an array previously, it generated toArray and fromArray methods that were different from what I did, but equally useful. I found it interesting that it made an assumption that the contents of the array were in a specific order (based on the ordering of fields in the class) and created methods that assumed each field was always present. My approach was to use name=value pairs and look for a given name to see if it was there and then use the value from it. I told ChatGPT to take that approach and it did. But trying to get smaller refinements didn't work well. One time I asked it about javascript and then the next attempt it displayed some code in javascript. Something interesting there was the use of a string as the discriminant in a case statement, which Delphi doesn't support (sadly), and it was half the size of the Delphi code. (Are there ANY contemporary languages used today, other than Dephi, that do NOT support strings in case statements?) Edited February 19, 2023 by David Schwartz Share this post Link to post
david_navigator 12 Posted February 19, 2023 (edited) Something I found it really useful is explaining code that I don't understand. For example last week someone sent me a function in Python (a new language for me) which even with the comments I couldn't work out what it was doing. I asked ChatGPT to explain the code, and it did, in a way that then meant I could code something in delphi very quickly. For example, with your code. Quote This code defines a Delphi unit called "Person", which contains the definition of a class called "TPerson". This class represents a person and has three properties: FirstName, LastName, and Age. The properties are implemented with getter and setter methods that read and write private fields. The class also has two methods that are used to convert the person object to and from JSON format. The ToJSON method takes the person object and returns a JSON string that represents the object's properties. The FromJSON method takes a JSON string and creates a new person object with properties that match the values in the JSON string. The implementation of the ToJSON method creates a new TJSONObject and adds the person's properties to it as name-value pairs. The method then returns the JSON string representation of the TJSONObject. The implementation of the FromJSON method first parses the JSON string into a TJSONObject, and then creates a new TPerson object. The method then sets the properties of the new TPerson object to the values of the corresponding name-value pairs in the TJSONObject. The "uses" clause at the top of the unit indicates that the System.SysUtils and System.JSON units are required for this code to compile. Edited February 19, 2023 by david_navigator Share this post Link to post
Anders Melander 1815 Posted February 19, 2023 18 hours ago, David Schwartz said: Saying it's "not how a professional solves a problem" is disengenuous at least -- I've seen far worse coding in tons of production code! How is it disingenuous? I'm stating pretty clearly that I believe it's an amateurish approach. A professional would look at the problem and then think about how best to solve it within the given constraints. The problem with ChatGPT is not the quality of the code. The problem is that you're delegating the most important element of software development, namely design and architecture, to a construct that has no grasp of these concepts. But okay, what do I know. 1 Share this post Link to post
David Schwartz 430 Posted February 19, 2023 (edited) In my mind, a "professional" is simply someone who gets paid for their time and effort. I think you're confusing skill level with compensation. College Professors write lots of high-quality code, but often do it for no additional compensation. And there are plenty of people fresh out of a "bootcamp" who get paid to write code who barely know what they're doing. While I do agree with your point to a certain extent, there are plenty of problems that don't require much context to reach a viable solution. In my experience, 30%-50% of most coding involves moving data from here to there -- ETL if you will. That kind of thing can be done by a robot that has no understanding of the larger context. It's self-contained, and if you spend too much time on it, you're wasting your time. For example, the code to move things in one direction looks like this: aaa := bbb; while the code to copy the other way looks like this: bbb := aaa; Show me something that will take two classes with 5, 10, or 100 fields and generate the code for Assign methods both ways, especially if some of the members are records or objects themselves? This kind of stuff is B-O-R-I-N-G and SHOULD BE AUTOMATED, IMHO, but I don't know of anything that does it. If you then inject a data transfer in between, like aaa --> send --> rcv --> aaa and the data transfer might be JSON or an array, or Base64 encoding, with or without encryption/decryption ... again, it's a lot of work, and totally mechanical. The problem with this extremely common coding pattern is that it does not lend itself to anything we have in our current toolboxes. Copy-and-paste is out; regular expression search-and-replace won't work; and writing something that's general enough is just awfully complicated unless there's a lot more meta-data available from the compiler or IDE. Most vendors I know refuse to provide access to that level of meta-data even though their compilers gather it regularly and the IDEs often make use of it. So if something like ChatGPT can do it, I'm all for that. This is not something that needs to be "optimal" since there's not much to optimize; it needs to be done quickly and easily updated. I've been playig around wiht ChatGPT and am really surprised at how quickly it can generate useful code for specific needs. It might not compile or run properly right off the bat, but there's both "bones" and a lot of viable "meat" on them. I'd typically reorganize things before using it, but it's like having someone write a draft of an article and then using that material as the basis for a more complete article. That kind of thing is done all the time in the written world (magazines, books, ad copy, web sites, etc.). From what I've seen so far, ChatGPT is far more skilled than your basic intern at writing code that solves a specific problem, as long as you can describe what you want clearly. I would not expect the results to be drop-in ready. However, the time it takes to go from "problem statement" to "working code" is WAY SHORTER than doing it all yourself by hand. And THAT is where the VALUE lies here. Edited February 19, 2023 by David Schwartz 3 Share this post Link to post
david_navigator 12 Posted February 19, 2023 1 hour ago, David Schwartz said: I've been playig around wiht ChatGPT and am really surprised at how quickly it can generate useful code for specific needs. Me too and there's definitely a learning curve in working out what ChatGPT is good at and what it's not. Tonight I got it to do, like you, the boring part of writing some class code (I was going to say creating, but I think I'm creating, it's just writing), but I also asked it to write a simple function to remove tags from some HTML to just leave plain text. Even after 8 iterations the code wouldn't still wouldn't compile and even after I'd fixed those issues, the code didn't do what was requested. I'm guessing too many StackOverflow wrong answers have been analysed as code sampled ! Share this post Link to post
Rollo62 539 Posted February 20, 2023 I would see this AI not a "coder" yet but as "idea generator" or "interactive dictionary". I'm pretty much impressed how fast easy ChatGPT can produce ideas and tips from a very broad perspective and in 90% spot-on the topic and complete, only if you ask the right questions. This is what I expected though, since ChatGPT has pretty much the whole web as source, so it might probably win against humans in creating inspiring solutions of any kind (not only coding). These "solutions" are thankfully NOT out-of-the-box, otherwise you could ask: "Please program a full-fledged ERP system perfectly tailored to my company's needs" 🙂 . The people who eagerly deny such amazing breakthroughs are probably the same people who made fun of the automobile around 1900, when it began to replace horse-drawn carriages. You shouldn't oppose what you can't stop anyway, especially because ChatGPT has gone from 0 to 100 across society in absolute record time. Better to learn early to work with it, instead of against it, I think this should apply especially to programmers and technophiles who made such developments possible in the end. Share this post Link to post
Fr0sT.Brutal 900 Posted February 20, 2023 (edited) On 2/18/2023 at 10:27 AM, David Schwartz said: I asked ChatGPT for a sample class in Delphi. Then asked it to extend the class to show how to move the data to and from a JSON message While I'm impressed on ChatGPT's abilities to generate mechanic code, this very example is only good for quick MVP and not serious production. Modifying a field requires changes in 3 places. The best option would be universal RTTI-based approach or the class should be generated always based on JSON schema. Anyway it's kind of new generation RAD tool. However, just like with Delphi RAD features, there's a moment when you throw out almost all of them and just create/init components in code 🙂 Edited February 20, 2023 by Fr0sT.Brutal 1 Share this post Link to post
Lars Fosdal 1793 Posted February 20, 2023 Personally, I think ChatGPT in it's current form is nearly useless for code generation. The examples it produces are trivial, and in many cases incorrect, incomplete or dangerous. Take the SQL - I've been indoctrinated with the notion that you don't do insert/update/delete statements from applications. You create SQL functions that do that job, and send the parameters. Why? Because you can grant execute rights to the user for these functions, and deny them the option of doing direct insert/update/delete - and hence reduce the risk of SQL injection significantly. The ability of ChatGPT to analyze and describe code is handy, although, I would not consider passing proprietary code to ChatGPT. It does not forget, and you risk that code, model or algorithm will be exposed in somebody elses response. And, although it is alluring, if you use ChatGPT to educate you in code that you have not used before, you have very little insight into the correctness or completeness of the code. I am going to wait a generation or two until the flaws have been addressed, and stick with using the AI to create interesting images. Edit: It struck me - ChatGPT for code is like Autotune for vocalists. It can be used for interesting effects, or making the mediocre presentable. Share this post Link to post
Dalija Prasnikar 1404 Posted February 20, 2023 11 hours ago, David Schwartz said: This kind of stuff is B-O-R-I-N-G and SHOULD BE AUTOMATED, IMHO, but I don't know of anything that does it. It takes 10 minutes to create such generator. I have few that generate boring code based on my coding patterns. I just need to write field declaration, copy-paste that to generator and I get all property declarations and assign method out of it. Yes, I need to adjust some of them afterwards, depending on their type, but that is only because my generator is extremely dumb parser and I never bothered to improve it as I don't need it that often. I mean, if you find AI useful for some tasks, then by all means use it. But it is not a magical tool, and it has serious limitations. If you are aware of those, then you can get the most of it, but don't expect any intelligent solutions out of it. 2 Share this post Link to post
Attila Kovacs 631 Posted February 20, 2023 Just now, Dalija Prasnikar said: It takes 10 minutes to create such generator. I have few that generate boring code based on my coding patterns. I just need to write field declaration, copy-paste that to generator and I get all property declarations and assign method out of it. Same here. Like SQL Schema in the clipboard, paste as class menu in the IDE. Share this post Link to post
Anders Melander 1815 Posted February 20, 2023 11 hours ago, David Schwartz said: In my mind, a "professional" is simply someone who gets paid for their time and effort. Yes, that's the formal definition of it. I'm more referring to professionalism as an attitude toward solving problems; Don't be lazy is the first thing I try to teach new developers. The vast majority of the work I do is horribly boring, but I don't get paid to write "interesting" code. I get paid to write high-quality code and if that means I have to write yet another damned factory, observer, or visitor class so be it. The effort spent will benefit everybody, myself included, down the road. I'm not against using a tool like ChatGPT as long as it is used as what it actually is. Share this post Link to post
Fr0sT.Brutal 900 Posted February 20, 2023 (edited) One of useful applications I see: Q: - write unit tests for function SplitString that cover all possible cases A: - Here is an example of a unit test for this function that covers all possible cases: procedure TestSplitString; var arr: TStringArray; begin arr := SplitString('abc,def,ghi', ','); Assert(arr[0] = 'abc'); Assert(arr[1] = 'def'); Assert(arr[2] = 'ghi'); arr := SplitString('abc', ','); Assert(arr[0] = 'abc'); Assert(Length(arr) = 1); arr := SplitString('', ','); Assert(Length(arr) = 0); arr := SplitString('abc,def,ghi,jkl', ','); Assert(arr[0] = 'abc'); Assert(arr[1] = 'def'); Assert(arr[2] = 'ghi'); Assert(arr[3] = 'jkl'); end; This test verifies that the SplitString function correctly splits a string into an array using a given separator, regardless of the length of the string or the number of separators present. == Somewhat awkward but with some adjustments could simplify the most boring task. Btw, the function itself was written in the most dumb and non-optimal way I can imagine 🙂 Edited February 20, 2023 by Fr0sT.Brutal Share this post Link to post
Lars Fosdal 1793 Posted February 20, 2023 43 minutes ago, Dalija Prasnikar said: It takes 10 minutes to create such generator. Mine takes the SQL table create statement and creates the wrapper class in Delphi, as well as the database stored procs. 1 Share this post Link to post
Brandon Staggs 285 Posted February 22, 2023 (edited) The OP links to an article that shows how this can be useful. As one option for generating simple base classes it looks good. Also produced by ChatGPT today: When my wife was working with our daughter on a math problem, we wanted to check an answer that was clearly wrong in the answer key. Out of curiosity I typed it into ChatGPT. I attempted to tell ChatGPT it was in error and the session crashed on me. Edited February 22, 2023 by Brandon Staggs Share this post Link to post
skyzoframe[hun] 4 Posted February 22, 2023 creativity the key, AI can not handle that... Share this post Link to post