Expanded up Power of 10 guide

- Slight formatting tweak to  guide
This commit is contained in:
Tony Bark 2025-02-06 12:45:46 -05:00
parent c5960f9f6c
commit 4df8521c58

View file

@ -1,6 +1,8 @@
# NASA-style Programming for C# # NASA-style Programming for C#
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. I asked the AI to write their [NASA-style](https://en.wikipedia.org/wiki/The_Power_of_10:_Rules_for_Developing_Safety-Critical_Code) [coding instructions](Powerof10PPrompt.txt) 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.
Why NASA? Their coding standards have loads of redundancies, and if AI is going to behave like advanced code generation tools, they need to be as fault-tolerant as possible. While my instructions were modified to account for modern languages, the principles are still the same.
My only request was to apply .NET 6's [top-level statements](https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/top-level-statements) for brevity. Aside from a few organizational tweaks with the guidelines and fixing the scope example to be wrapped in a class, it all works flawlessly. My only request was to apply .NET 6's [top-level statements](https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/top-level-statements) for brevity. Aside from a few organizational tweaks with the guidelines and fixing the scope example to be wrapped in a class, it all works flawlessly.
@ -9,7 +11,7 @@ My only request was to apply .NET 6's [top-level statements](https://learn.micro
- Keep the code simple and coherent. Use meaningful identifiers and limit the complexity of expressions. - Keep the code simple and coherent. Use meaningful identifiers and limit the complexity of expressions.
- Keep functions or methods short, with a single responsibility. - Keep functions or methods short, with a single responsibility.
**Example:** ### Example:
```csharp ```csharp
// Good example of a short, focused method // Good example of a short, focused method
@ -28,7 +30,7 @@ Console.WriteLine($"The sum is {result}");
- Validate and sanitize inputs to prevent security vulnerabilities like injection attacks. - Validate and sanitize inputs to prevent security vulnerabilities like injection attacks.
- Use try-catch blocks to handle exceptions gracefully. - Use try-catch blocks to handle exceptions gracefully.
**Example:** ### Example:
```csharp ```csharp
using System.Globalization; using System.Globalization;
@ -49,7 +51,7 @@ else
- Use environment variables or configuration files for environment-specific settings. - Use environment variables or configuration files for environment-specific settings.
- Rely on cross-platform libraries within .NET. - Rely on cross-platform libraries within .NET.
**Example:** ### Example:
```csharp ```csharp
// Reading configuration using environment variables // Reading configuration using environment variables
@ -69,7 +71,7 @@ else
- Prefer `using` statements to safely manage resource cleanup. - Prefer `using` statements to safely manage resource cleanup.
- Avoid excessive memory allocation and prefer stack-allocated variables. - Avoid excessive memory allocation and prefer stack-allocated variables.
**Example:** ### Example:
```csharp ```csharp
// Using statement to ensure safe disposal // Using statement to ensure safe disposal
@ -87,7 +89,7 @@ using (var reader = new StreamReader("example.txt"))
- Limit the visibility of variables and functions to the smallest scope necessary. - Limit the visibility of variables and functions to the smallest scope necessary.
**Example:** ### Example:
```csharp ```csharp
namespace Application; namespace Application;
@ -113,7 +115,7 @@ class Program
- Sanitize and validate all external inputs to secure your application. - Sanitize and validate all external inputs to secure your application.
**Example:** ### Example:
```csharp ```csharp
using System.Web; using System.Web;
@ -134,7 +136,7 @@ else
- Use XML documentation comments for functions or methods to improve maintainability and provide documentation. - Use XML documentation comments for functions or methods to improve maintainability and provide documentation.
**Example:** ### Example:
```csharp ```csharp
/// <summary> /// <summary>
@ -149,6 +151,25 @@ int Sum(int a, int b)
} }
``` ```
### 8. Use Preprocessors, Macros, or Metaprogramming Techniques Sparingly to Maintain Code Clarity
- Preprocessor directives can be useful for conditional compilation but should be used judiciously to avoid obscuring the code.
### Example:
```csharp
#if DEBUG
Console.WriteLine("Running in Debug mode.");
#else
Console.WriteLine("Running in Release mode.");
#endif
int result = Sum(5, 3);
Console.WriteLine($"The sum is {result}");
int Sum(int a, int b) => a + b;
```
## Example Using Top-Level Statements in .NET 6 ## Example Using Top-Level Statements in .NET 6
Combining several of the above principles: Combining several of the above principles: