Jacob L
Moderator (Lawsonator)
I am doing Object Oriented Programming as a module on my Masters at university and our lecturer has incorporated AI into the module, ChatGPT specifically. AI can rewrite code in a more efficient way. It helped at least in my C# in Visual Studio. I know MacroDroid is not written in C# but it works for other languages. See this example:
My original inefficient code:
static void Main(string[] args)
{
string mode;
string exchangeRateInput;
string currencyInput;
int modeUserInput = 1;
double poundsUserInput;
double exchangeRate; // £1 is equal to $exchangerate, this is user configurable
double result;
Console.WriteLine("Please choose the mode");
Console.WriteLine("Press 1 to convert £ to $ or press 2 for $ to £");
mode = Console.ReadLine();
modeUserInput = int.Parse(mode);
Console.WriteLine("You chose mode {0}", mode);
Console.WriteLine("Please enter the exchange rate");
exchangeRateInput = Console.ReadLine();
exchangeRate = double.Parse(exchangeRateInput);
// Check the mode and perform actions based on selection
if (modeUserInput == 1)
{
Console.WriteLine("\nPlease enter the amount in £ that you want to convert to $");
currencyInput = Console.ReadLine();
poundsUserInput = double.Parse(currencyInput);
result = poundsUserInput / exchangeRate;
Console.WriteLine("\nYou want to convert {0} £s to $ using the echange rate of {1} which is: ${2}", poundsUserInput, exchangeRate, result);
}
else
{
Console.WriteLine("\nPlease enter the amount in $ that you want to convert to £");
currencyInput = Console.ReadLine();
poundsUserInput = double.Parse(currencyInput);
result = poundsUserInput * exchangeRate;
Console.WriteLine("\nYou want to convert {0} $s to £ using the echange rate of {1} which is: £{2}", poundsUserInput, exchangeRate, result);
}
Console.ReadLine(); // Holds the program open
}
ChatGPT's more efficient code:
using System;
class Program
{
static void Main(string[] args)
{
string mode;
string exchangeRateInput;
string currencyInput;
double exchangeRate;
double result;
Console.WriteLine("Please choose the mode:");
Console.WriteLine("Press 1 to convert £ to $ or press 2 for $ to £");
mode = Console.ReadLine();
Console.WriteLine($"You chose mode {mode}");
Console.WriteLine("Please enter the exchange rate:");
exchangeRateInput = Console.ReadLine();
exchangeRate = double.Parse(exchangeRateInput);
if (mode == "1")
{
Console.WriteLine("\nPlease enter the amount in £ that you want to convert to $:");
}
else if (mode == "2")
{
Console.WriteLine("\nPlease enter the amount in $ that you want to convert to £:");
}
else
{
Console.WriteLine("Invalid mode selection. Please choose 1 or 2.");
Console.ReadLine();
return;
}
currencyInput = Console.ReadLine();
double amount = double.Parse(currencyInput);
result = (mode == "1") ? amount / exchangeRate : amount * exchangeRate;
string fromCurrency = (mode == "1") ? "£" : "$";
string toCurrency = (mode == "1") ? "$" : "£";
Console.WriteLine($"\nYou want to convert {amount} {fromCurrency} to {toCurrency} using the exchange rate of {exchangeRate}. Result: {toCurrency}{result:F2}");
Console.ReadLine(); // Holds the program open
}
My original inefficient code:
static void Main(string[] args)
{
string mode;
string exchangeRateInput;
string currencyInput;
int modeUserInput = 1;
double poundsUserInput;
double exchangeRate; // £1 is equal to $exchangerate, this is user configurable
double result;
Console.WriteLine("Please choose the mode");
Console.WriteLine("Press 1 to convert £ to $ or press 2 for $ to £");
mode = Console.ReadLine();
modeUserInput = int.Parse(mode);
Console.WriteLine("You chose mode {0}", mode);
Console.WriteLine("Please enter the exchange rate");
exchangeRateInput = Console.ReadLine();
exchangeRate = double.Parse(exchangeRateInput);
// Check the mode and perform actions based on selection
if (modeUserInput == 1)
{
Console.WriteLine("\nPlease enter the amount in £ that you want to convert to $");
currencyInput = Console.ReadLine();
poundsUserInput = double.Parse(currencyInput);
result = poundsUserInput / exchangeRate;
Console.WriteLine("\nYou want to convert {0} £s to $ using the echange rate of {1} which is: ${2}", poundsUserInput, exchangeRate, result);
}
else
{
Console.WriteLine("\nPlease enter the amount in $ that you want to convert to £");
currencyInput = Console.ReadLine();
poundsUserInput = double.Parse(currencyInput);
result = poundsUserInput * exchangeRate;
Console.WriteLine("\nYou want to convert {0} $s to £ using the echange rate of {1} which is: £{2}", poundsUserInput, exchangeRate, result);
}
Console.ReadLine(); // Holds the program open
}
ChatGPT's more efficient code:
using System;
class Program
{
static void Main(string[] args)
{
string mode;
string exchangeRateInput;
string currencyInput;
double exchangeRate;
double result;
Console.WriteLine("Please choose the mode:");
Console.WriteLine("Press 1 to convert £ to $ or press 2 for $ to £");
mode = Console.ReadLine();
Console.WriteLine($"You chose mode {mode}");
Console.WriteLine("Please enter the exchange rate:");
exchangeRateInput = Console.ReadLine();
exchangeRate = double.Parse(exchangeRateInput);
if (mode == "1")
{
Console.WriteLine("\nPlease enter the amount in £ that you want to convert to $:");
}
else if (mode == "2")
{
Console.WriteLine("\nPlease enter the amount in $ that you want to convert to £:");
}
else
{
Console.WriteLine("Invalid mode selection. Please choose 1 or 2.");
Console.ReadLine();
return;
}
currencyInput = Console.ReadLine();
double amount = double.Parse(currencyInput);
result = (mode == "1") ? amount / exchangeRate : amount * exchangeRate;
string fromCurrency = (mode == "1") ? "£" : "$";
string toCurrency = (mode == "1") ? "$" : "£";
Console.WriteLine($"\nYou want to convert {amount} {fromCurrency} to {toCurrency} using the exchange rate of {exchangeRate}. Result: {toCurrency}{result:F2}");
Console.ReadLine(); // Holds the program open
}