CommunityToolkit / CommunityToolkit/dotnet

Guard-like helper methods for warnings

Aperta
#533 1 commento 0 reazioni 0 assegnatari Vedi su GitHub
feature request :mailbox_with_mail:
Lingua principale
C#
Stelle
3.8k
Fork
400
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

### Overview

The `Guard` static class is great! But it only makes sense when we are willing to throw and handle exceptions.
Sometimes, for debugging purposes, we would like to safely assert some assumptions and report them in the output.

Therefore, I would like to suggest a new class similar to `Guard`, maybe named `Test` or `Trace` or `LogIf`, that does pretty much the same thing than `Guard` except that it does *not* throw exceptions, but instead display a message through `Debug.WriteLine` and would act only with the DEBUG build constant.

### API breakdown

```csharp
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace CommunityToolkit.Diagnostics;

///
/// Helper methods to verify conditions when running code.
///
[DebuggerStepThrough]
public static partial class Trace
{
///
/// Asserts that the input value is not .
///
/// The type of reference value type being tested.
/// The input value to test.
/// The message to display when value is not null.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Conditional("DEBUG")]
public static void IsNotNull(T? value, string message)
{
if (value is null)
{
Warn(message);
}
}

[...]

[Conditional("DEBUG")] // => https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.conditionalattribute?view=net-7.0
private void Warn(string message)
{
Debug.WriteLine(message);
Debug.Assert(false, message);
}
}
```

### Usage example

```csharp
void Foo(object param)
{
Trace.IsNotNull(param, "param is null, it means X is happening");
}
```

### Breaking change?

No

### Alternatives

```csharp
void Foo(object param)
{
LogIf.IsNull(param, "param is null, it means X is happening");
}
```

### Additional context

_No response_

### Help us help you

No, just wanted to propose this

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Inizia esaminando la classe statica Guard esistente e confrontando la sua API con le alternative proposte Trace o LogIf. Chiarisci il nome della classe, l’ambito dei metodi helper e il comportamento DEBUG previsto, quindi definisci test che dimostrino che i controlli falliti riportano diagnostica senza generare eccezioni e sono inattivi nelle build non-DEBUG.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
csharp
Ambito
developer-experience
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.