I asked the AI to write their instructions in the form of guidelines. I figured I'd use C# since it's both my preferred language and the syntax should be fairly easy to grasp once you understand the basics. My only request was to apply .NET 6's [top-level statements]([Top-level statements tutorial - C# | Microsoft Learn](https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/top-level-statements)) for brevity. Aside from that, it all works flawlessly.
## 1. Write Clear, Well-Structured, and Maintainable Code
- Keep the code simple and coherent. Use meaningful identifiers and limit the complexity of expressions.
- Keep functions or methods short, with a single responsibility.
**Example:**
```csharp
// Good example of a short, focused method
int Sum(int a, int b)
{
return a + b;
}
// Good example of clear, well-structured code
var result = Sum(5, 3);
Console.WriteLine($"The sum is {result}");
```
## 2. Follow Best Practices for Security, Performance, and Reliability
- Validate and sanitize inputs to prevent security vulnerabilities like injection attacks.
- Use try-catch blocks to handle exceptions gracefully.
**Example:**
```csharp
using System.Globalization;
string userInput = "2023-10-30";
if (DateTime.TryParseExact(userInput, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime date))
{
Console.WriteLine($"Parsed date: {date}");
}
else
{
throw new FormatException("Invalid date format.");
}
```
## 3. Ensure Portability and Adaptability Across Different Environments
- Use environment variables or configuration files for environment-specific settings.
- Rely on cross-platform libraries within .NET.
**Example:**
```csharp
// Reading configuration using environment variables
var configValue = Environment.GetEnvironmentVariable("CONFIG_KEY");