{"id":86,"date":"2024-03-07T18:01:00","date_gmt":"2024-03-07T18:01:00","guid":{"rendered":"https:\/\/permutationcity.co.uk\/bp\/?p=86"},"modified":"2025-03-13T10:27:04","modified_gmt":"2025-03-13T10:27:04","slug":"ai-assistance","status":"publish","type":"post","link":"https:\/\/permutationcity.co.uk\/bp\/2024\/03\/07\/ai-assistance\/","title":{"rendered":"AI assistance"},"content":{"rendered":"\n<p>We now have access to new powerful tools like <a href=\"https:\/\/chat.openai.com\/\">ChatGPT<\/a> that can help us write code.\nThere are a number of potential issues with this:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The ethics of using models trained by web scrapping.<\/li>\n\n\n\n<li>The security issues of asking an external bot about internal code.<\/li>\n\n\n\n<li>The correctness and quality of code produced.<\/li>\n<\/ul>\n\n\n\n<p>So far I haven&#8217;t found this useful for situations at work.\nEvery time I had asked for some source there were fundamental flaws with the code.\nIt did sometimes give me ideas that I could then persue by myself but nothing to use directly.<\/p>\n\n\n\n<p>Listening to other stories about people using these tools it seems better to treat them\nas a process rather than a one stop shop.\nThey may not give you the &#8220;right&#8221; answer immediately, or indeed ever, but they are endlessly patient.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"closest-distance-between-point-and-line\">Closest distance between point and line<\/h2>\n\n\n\n<p>Like this:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Give me C++ source to calculate the distance between a point and a line.<\/p>\n<\/blockquote>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">#include &lt;iostream&gt;\n#include &lt;cmath&gt;\n\n\/\/ Structure to represent a point\nstruct Point {\n    double x, y;\n};\n\n\/\/ Function to calculate the distance between two points\ndouble distance(Point p1, Point p2) {\n    return sqrt(pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2));\n}\n\n\/\/ Function to calculate the distance between a point and a line defined by two points\ndouble distancePointLine(Point point, Point lineStart, Point lineEnd) {\n    double lineLength = distance(lineStart, lineEnd);\n    if (lineLength == 0) \/\/ If the line has zero length\n        return distance(point, lineStart); \/\/ Return distance from the point to the line start\n\n    \/\/ Calculate the perpendicular distance from the point to the line\n    double distance = fabs((lineEnd.x - lineStart.x) * (lineStart.y - point.y) - (lineStart.x - point.x) * (lineEnd.y - lineStart.y)) \/ lineLength;\n\n    return distance;\n}\n\nint main() {\n    Point point = {1.0, 2.0}; \/\/ Point coordinates\n    Point lineStart = {0.0, 0.0}; \/\/ Line start coordinates\n    Point lineEnd = {3.0, 4.0}; \/\/ Line end coordinates\n\n    double dist = distancePointLine(point, lineStart, lineEnd);\n    std::cout &lt;&lt; &quot;Distance between point and line: &quot; &lt;&lt; dist &lt;&lt; std::endl;\n\n    return 0;\n}<\/pre><\/div>\n\n\n\n<p>Not bad but not great either.\nFor me this seems overly cluttered.<\/p>\n\n\n\n<p>I&#8217;d prefer it to follow <a href=\"https:\/\/permutationcity.co.uk\/bp\/2024\/02\/27\/good-names\/\">my naming guidelines<\/a>\nbut the model doesn&#8217;t have internet access.\nI&#8217;d have to repeat them all here so I&#8217;ll not worry about that for the moment.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Update this code following this code review:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>There are a lot of comments but they aren&#8217;t adding a lot of value.\n<ul class=\"wp-block-list\">\n<li>We know structs are structures.<\/li>\n\n\n\n<li>We know functions are functions.<\/li>\n\n\n\n<li>We know points are coordinates.<\/li>\n\n\n\n<li>If an identifier can already tells us everything the comment is unnecessary.<\/li>\n\n\n\n<li>Comments are just telling us what not why.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Adding a <code>struct Line<\/code> could be better in the long run.<\/li>\n\n\n\n<li>The identifiers could be better.\n<ul class=\"wp-block-list\">\n<li>Inconsistant usage, <code>dist<\/code> and <code>distance<\/code>.<\/li>\n\n\n\n<li>Using <code>distanceBetween<\/code> emphasises the purpose and removes need for comment.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Return values directly rather than transfering them via a variable (unless required to understand code).<\/li>\n<\/ul>\n<\/blockquote>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">#include &lt;iostream&gt;\n#include &lt;cmath&gt;\n\nstruct Point {\n    double x, y;\n};\n\nstruct Line {\n    Point start, end;\n};\n\ndouble calculateDistance(Point p1, Point p2) {\n    return sqrt(pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2));\n}\n\ndouble distanceBetween(Point point, Line line) {\n    double lineLength = calculateDistance(line.start, line.end);\n    if (lineLength == 0)\n        return calculateDistance(point, line.start);\n\n    return fabs((line.end.x - line.start.x) * (line.start.y - point.y) - (line.start.x - point.x) * (line.end.y - line.start.y)) \/ lineLength;\n}\n\nint main() {\n    Point point = {1.0, 2.0};\n    Line line = {{0.0, 0.0}, {3.0, 4.0}};\n\n    std::cout &lt;&lt; &quot;Distance between point and line: &quot; &lt;&lt; distanceBetween(point, line) &lt;&lt; std::endl;\n\n    return 0;\n}<\/pre><\/div>\n\n\n\n<p>It&#8217;s gotten rid of all the comments.\nI&#8217;d prefer to see some left explaining the more complex parts.<\/p>\n\n\n\n<p>I wonder why it changed the <code>distance<\/code> function to <code>calculateDistance<\/code>?\nI did ask. To summarise,\nit said that re-using function names could cause confusion and <code>calculateDistance<\/code> was more descriptive.\nI don&#8217;t think that&#8217;s really true.\nIt feels more like an explaination you&#8217;d come up with after the fact to justify a change.<\/p>\n\n\n\n<p>I&#8217;ll start combining queries rather than showing almost the same code every time.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>C++ allows function overloading so that the same function name can be distinguished just by types.\nRe-write the code using that.<\/p>\n<\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Use <code>distanceBetween<\/code> instead.<\/p>\n<\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Add short comments to explain the maths heavy lines.<\/p>\n<\/blockquote>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">#include &lt;iostream&gt;\n#include &lt;cmath&gt;\n\nstruct Point {\n    double x, y;\n};\n\nstruct Line {\n    Point start, end;\n};\n\n\/\/ Calculate the Euclidean distance between two points\ndouble distanceBetween(Point p1, Point p2) {\n    return sqrt(pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2));\n}\n\n\/\/ Calculate the distance between a point and a line\ndouble distanceBetween(Point point, Line line) {\n    double lineLength = distanceBetween(line.start, line.end);\n\n    \/\/ If the line has zero length, return the distance between the point and the line start\n    if (lineLength == 0)\n        return distanceBetween(point, line.start);\n\n    \/\/ Calculate the perpendicular distance from the point to the line using the formula for a point-to-line distance\n    return fabs((line.end.x - line.start.x) * (line.start.y - point.y) - (line.start.x - point.x) * (line.end.y - line.start.y)) \/ lineLength;\n}\n\nint main() {\n    Point point = {1.0, 2.0};\n    Line line = {{0.0, 0.0}, {3.0, 4.0}};\n\n    \/\/ Calculate and print the distance between the point and the line\n    std::cout &lt;&lt; &quot;Distance between point and line: &quot; &lt;&lt; distanceBetween(point, line) &lt;&lt; std::endl;\n\n    return 0;\n}\n<\/pre><\/div>\n\n\n\n<p>Oh dear. General comments have re-appeared rather than just explaining the maths.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Can you go back to the previous version of the code before those comments were added.<\/p>\n<\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Add comments immediately before the maths on lines containing sqrt and fabs.<\/p>\n<\/blockquote>\n\n\n\n<p>It can&#8217;t see to help itself with the extra comments.\nThe &#8220;steering&#8221; for ChatGPT seems to be incredibly sensitive.\nEach time I ask for a change it swerves violently in the new direction.<\/p>\n\n\n\n<p>Much easier to take the first version and just update it myself.\nI can change the identifiers and comments at the same time and get exactly what I want.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C++&quot;,&quot;language&quot;:&quot;C++&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">#include &lt;iostream&gt;\n#include &lt;cmath&gt;\n\nstruct Point {\n    double x, y;\n};\n\nstruct Line {\n    Point start, end;\n};\n\ndouble DistanceBetween(Point point, Point other) {\n    return sqrt(pow(other.x - point.x, 2) + pow(other.y - point.y, 2));\n}\n\ndouble DistanceBetween(Point point, Line line) {\n    double lineLength = DistanceBetween(line.start, line.end);\n    \/\/ Use a special case for an empty line to avoid divide by zero.\n    if (lineLength == 0) {\n        return DistanceBetween(point, line.start);\n    }\n    \/\/ Calculate the perpendicular distance from the point to the line.\n    return fabs((line.end.x - line.start.x) * (line.start.y - point.y) - (line.end.y - line.start.y) * (line.start.x - point.x)) \/ lineLength;\n}\n\nint main() {\n    Point point = { 1.0, 2.0 };\n    Line line = {{ 0.0, 0.0 , { 3.0, 4.0 }};\n\n    std::cout &lt;&lt; &quot;Distance between point and line: &quot; &lt;&lt; DistanceBetween(point, line) &lt;&lt; std::endl;\n\n    return 0;\n}<\/pre><\/div>\n\n\n\n<p>This still isn&#8217;t perfect. As part of a larger project I&#8217;d like to have my own library of maths routines. That would provide <code>Square<\/code> and <code>DotProduct<\/code> functions which would make this clearer.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">On balance<\/h2>\n\n\n\n<p>Some people are very excited by these new tools. I&#8217;m wary.\nThese models know a lot but are still limited in how they use that knowledge.\nGiven the legal, ethical and quality issues of using this code I wouldn&#8217;t recommend it.\nThey may be able to give you ideas but I don&#8217;t think they should be used to directly do coding for you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We now have access to new powerful tools like ChatGPT that can help us write code. There are a number of potential issues with this: So far I haven&#8217;t found this useful for situations at work. Every time I had asked for some source there were fundamental flaws with the code. It did sometimes give [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[40,18,21],"class_list":["post-86","post","type-post","status-publish","format-standard","hentry","category-general","tag-geometry","tag-tools","tag-websites"],"_links":{"self":[{"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/posts\/86","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/comments?post=86"}],"version-history":[{"count":3,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/posts\/86\/revisions"}],"predecessor-version":[{"id":194,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/posts\/86\/revisions\/194"}],"wp:attachment":[{"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/media?parent=86"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/categories?post=86"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/permutationcity.co.uk\/bp\/wp-json\/wp\/v2\/tags?post=86"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}