Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rule to prefer conditional expression over assignement #1977

Merged
merged 7 commits into from Jul 30, 2019
@@ -103,6 +103,9 @@ csharp_prefer_braces = true:error
#do not suggest readonly fields
dotnet_style_readonly_field = false:error

#prefer conditional expression over assignement
dotnet_style_prefer_conditional_expression_over_assignment = true:error

This comment has been minimized.

Copy link
@molnard

molnard Jul 26, 2019

Collaborator

I think we should not enforce this. Can result in a very complex and unreadable expression in many cases. IMO it should be a suggestion.

This comment has been minimized.

Copy link
@yahiheb

yahiheb Jul 30, 2019

Author Collaborator

I removed the rule because if we add it even as a suggestion and we do not apply it everywhere we will get many suggestions and then we need to use #pragma warning disable IDE0045 // Convert to conditional expression to get rid of them, but that will be a lot.



# name all constant fields using PascalCase
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = error
@@ -163,15 +163,9 @@ protected override void OnAttached()
if (string.IsNullOrEmpty(AssociatedObject.Text))
{
string text = await Application.Current.Clipboard.GetTextAsync();

if (AddressStringParser.TryParse(text, Global.Network, out _))
{
MyTextBoxState = TextBoxState.AddressInsert;
}
else
{
MyTextBoxState = TextBoxState.NormalTextBoxOperation;
}
MyTextBoxState = AddressStringParser.TryParse(text, Global.Network, out _)
? TextBoxState.AddressInsert
: TextBoxState.NormalTextBoxOperation;
}
else
{
@@ -149,18 +149,11 @@ public Uri GetCurrentBackendUri()
return _backendUri;
}

if (Network == Network.Main)
{
_backendUri = new Uri(MainNetBackendUriV3);
}
else if (Network == Network.TestNet)
{
_backendUri = new Uri(TestNetBackendUriV3);
}
else // RegTest
{
_backendUri = new Uri(RegTestBackendUriV3);
}
_backendUri = Network == Network.Main
? new Uri(MainNetBackendUriV3)
: Network == Network.TestNet
? new Uri(TestNetBackendUriV3)
: new Uri(RegTestBackendUriV3);
This conversation was marked as resolved by yahiheb

This comment has been minimized.

Copy link
@molnard

molnard Jul 26, 2019

Collaborator

Beautiful but not readable. Let if-else be.

This comment has been minimized.

Copy link
@yahiheb

yahiheb Jul 27, 2019

Author Collaborator

I agree and I honestly think if else is easier to read, but I did this in the first place only because conditional expression is used a lot when possible in the code base.


return _backendUri;
}
@@ -172,18 +165,11 @@ public Uri GetFallbackBackendUri()
return _fallbackBackendUri;
}

if (Network == Network.Main)
{
_fallbackBackendUri = new Uri(MainNetFallbackBackendUri);
}
else if (Network == Network.TestNet)
{
_fallbackBackendUri = new Uri(TestNetFallbackBackendUri);
}
else // RegTest
{
_fallbackBackendUri = new Uri(RegTestBackendUriV3);
}
_fallbackBackendUri = Network == Network.Main
? new Uri(MainNetFallbackBackendUri)
: Network == Network.TestNet
? new Uri(TestNetFallbackBackendUri)
: new Uri(RegTestBackendUriV3);
This conversation was marked as resolved by yahiheb

This comment has been minimized.

Copy link
@molnard

molnard Jul 26, 2019

Collaborator

Same of above.


return _fallbackBackendUri;
}
@@ -336,14 +336,9 @@ private void UpdateRequiredBtcLabel(CcjClientRound registrableRound)
else
{
var available = Global.WalletService.Coins.Where(x => x.Confirmed && !x.Unavailable);
if (available.Any())
{
RequiredBTC = registrableRound.State.CalculateRequiredAmount(available.Where(x => x.AnonymitySet < Global.Config.PrivacyLevelStrong).Select(x => x.Amount).ToArray());
}
else
{
RequiredBTC = registrableRound.State.CalculateRequiredAmount();
}
RequiredBTC = available.Any()
This conversation was marked as resolved by yahiheb

This comment has been minimized.

Copy link
@molnard

molnard Jul 26, 2019

Collaborator

Not readable.

? registrableRound.State.CalculateRequiredAmount(available.Where(x => x.AnonymitySet < Global.Config.PrivacyLevelStrong).Select(x => x.Amount).ToArray())
: registrableRound.State.CalculateRequiredAmount();
}
}
}
@@ -476,20 +476,15 @@ private void SetSelections()

private void SetCoinJoinStatusWidth()
{
if (Coins.Any(x => x.Status == SmartCoinStatus.MixingConnectionConfirmation
CoinJoinStatusWidth = Coins.Any(x => x.Status == SmartCoinStatus.MixingConnectionConfirmation
|| x.Status == SmartCoinStatus.MixingInputRegistration
|| x.Status == SmartCoinStatus.MixingOnWaitingList
|| x.Status == SmartCoinStatus.MixingOutputRegistration
|| x.Status == SmartCoinStatus.MixingSigning
|| x.Status == SmartCoinStatus.MixingWaitingForConfirmation
|| x.Status == SmartCoinStatus.SpentAccordingToBackend))
{
CoinJoinStatusWidth = new GridLength(180);
}
else
{
CoinJoinStatusWidth = new GridLength(0);
}
|| x.Status == SmartCoinStatus.SpentAccordingToBackend)
? new GridLength(180)
: new GridLength(0);
}

public void OnCoinIsSelectedChanged(CoinViewModel cvm)
@@ -155,22 +155,12 @@ private List<(DateTimeOffset dateTime, Height height, Money amount, string label
continue;
}

DateTimeOffset dateTime;
if (foundTransaction.Height.Type == HeightType.Chain)
{
if (walletService.ProcessedBlocks.Any(x => x.Value.height == foundTransaction.Height))
{
dateTime = walletService.ProcessedBlocks.First(x => x.Value.height == foundTransaction.Height).Value.dateTime;
}
else
{
dateTime = DateTimeOffset.UtcNow;
}
}
else
{
dateTime = foundTransaction.FirstSeenIfMempoolTime ?? DateTimeOffset.UtcNow;
}
DateTimeOffset dateTime = foundTransaction.Height.Type == HeightType.Chain
This conversation was marked as resolved by yahiheb

This comment has been minimized.

Copy link
@molnard

molnard Jul 26, 2019

Collaborator

Not readable.

? walletService.ProcessedBlocks.Any(x => x.Value.height == foundTransaction.Height)
? walletService.ProcessedBlocks.First(x => x.Value.height == foundTransaction.Height).Value.dateTime
: DateTimeOffset.UtcNow
: foundTransaction.FirstSeenIfMempoolTime ?? DateTimeOffset.UtcNow;

if (found != default) // if found
{
txRecordList.Remove(found);
@@ -186,35 +176,16 @@ private List<(DateTimeOffset dateTime, Height height, Money amount, string label
if (coin.SpenderTransactionId != null)
{
SmartTransaction foundSpenderTransaction = walletService.TransactionCache.First(x => x.GetHash() == coin.SpenderTransactionId);
if (foundSpenderTransaction.Height.Type == HeightType.Chain)
{
if (walletService.ProcessedBlocks != null) // NullReferenceException appeared here.
{
if (walletService.ProcessedBlocks.Any(x => x.Value.height == foundSpenderTransaction.Height))
{
if (walletService.ProcessedBlocks != null) // NullReferenceException appeared here.
{
dateTime = walletService.ProcessedBlocks.First(x => x.Value.height == foundSpenderTransaction.Height).Value.dateTime;
}
else
{
dateTime = DateTimeOffset.UtcNow;
}
}
else
{
dateTime = DateTimeOffset.UtcNow;
}
}
else
{
dateTime = DateTimeOffset.UtcNow;
}
}
else
{
dateTime = foundSpenderTransaction.FirstSeenIfMempoolTime ?? DateTimeOffset.UtcNow;
}

dateTime = foundSpenderTransaction.Height.Type == HeightType.Chain
This conversation was marked as resolved by yahiheb

This comment has been minimized.

Copy link
@molnard

molnard Jul 26, 2019

Collaborator

Whooow. Let if else be.

This comment has been minimized.

Copy link
@yahiheb

yahiheb Jul 27, 2019

Author Collaborator

lol I completely agree that it is easier to read with if else.

? walletService.ProcessedBlocks != null // NullReferenceException appeared here.
? walletService.ProcessedBlocks.Any(x => x.Value.height == foundSpenderTransaction.Height)
? walletService.ProcessedBlocks != null // NullReferenceException appeared here.
? walletService.ProcessedBlocks.First(x => x.Value.height == foundSpenderTransaction.Height).Value.dateTime
: DateTimeOffset.UtcNow
: DateTimeOffset.UtcNow
: DateTimeOffset.UtcNow
: foundSpenderTransaction.FirstSeenIfMempoolTime ?? DateTimeOffset.UtcNow;

var foundSpenderCoin = txRecordList.FirstOrDefault(x => x.transactionId == coin.SpenderTransactionId);
if (foundSpenderCoin != default) // if found
@@ -502,18 +502,11 @@ private void SetSendText()
return;
}

if (IsHardwareBusy)
{
BuildTransactionButtonText = WaitingForHardwareWalletButtonTextString;
}
else if (IsBusy)
{
BuildTransactionButtonText = SendingTransactionButtonTextString;
}
else
{
BuildTransactionButtonText = SendTransactionButtonTextString;
}
BuildTransactionButtonText = IsHardwareBusy
? WaitingForHardwareWalletButtonTextString
: IsBusy
? SendingTransactionButtonTextString
: SendTransactionButtonTextString;
}

private void SetAmountWatermark(Money amount)
@@ -533,14 +526,10 @@ private void SetAmountWatermark(Money amount)
{
Logging.Logger.LogTrace<SendTabViewModel>(ex);
}
if (amountUsd != 0)
{
AmountWatermarkText = $"Amount (BTC) ~ ${amountUsd}";
}
else
{
AmountWatermarkText = "Amount (BTC)";
}

AmountWatermarkText = amountUsd != 0
? $"Amount (BTC) ~ ${amountUsd}"
: "Amount (BTC)";
}
}

@@ -645,14 +634,9 @@ private void SetAmountIfMax()
{
if (IsMax)
{
if (AllSelectedAmount == Money.Zero)
{
AmountText = "No Coins Selected";
}
else
{
AmountText = $"~ {AllSelectedAmount.ToString(false, true)}";
}
AmountText = AllSelectedAmount == Money.Zero
? "No Coins Selected"
: $"~ {AllSelectedAmount.ToString(false, true)}";
}
}

@@ -153,14 +153,9 @@ public async Task InitializeNoWalletAsync()
var blocksFolderPath = Path.Combine(DataDir, $"Blocks{Network}");
var connectionParameters = new NodeConnectionParameters { UserAgent = "/Satoshi:0.18.0/" };

if (Config.UseTor.Value)
{
Synchronizer = new WasabiSynchronizer(Network, BitcoinStore, () => Config.GetCurrentBackendUri(), Config.TorSocks5EndPoint);
}
else
{
Synchronizer = new WasabiSynchronizer(Network, BitcoinStore, Config.GetFallbackBackendUri(), null);
}
Synchronizer = Config.UseTor.Value
? new WasabiSynchronizer(Network, BitcoinStore, () => Config.GetCurrentBackendUri(), Config.TorSocks5EndPoint)
: new WasabiSynchronizer(Network, BitcoinStore, Config.GetFallbackBackendUri(), null);

UpdateChecker = new UpdateChecker(Synchronizer.WasabiClient);

@@ -187,14 +182,9 @@ public async Task InitializeNoWalletAsync()

#region TorProcessInitialization

if (Config.UseTor.Value)
{
TorManager = new TorProcessManager(Config.TorSocks5EndPoint, TorLogsFile);
}
else
{
TorManager = TorProcessManager.Mock();
}
TorManager = Config.UseTor.Value
? new TorProcessManager(Config.TorSocks5EndPoint, TorLogsFile)
: TorProcessManager.Mock();
TorManager.Start(false, DataDir);

var fallbackRequestTestUri = new Uri(Config.GetFallbackBackendUri(), "/api/software/versions");
@@ -408,14 +398,9 @@ public async Task InitializeWalletServiceAsync(KeyManager keyManager)
await Task.Delay(100, token);
}

if (Config.UseTor.Value)
{
ChaumianClient = new CcjClient(Synchronizer, Network, keyManager, () => Config.GetCurrentBackendUri(), Config.TorSocks5EndPoint);
}
else
{
ChaumianClient = new CcjClient(Synchronizer, Network, keyManager, Config.GetFallbackBackendUri(), null);
}
ChaumianClient = Config.UseTor.Value
? new CcjClient(Synchronizer, Network, keyManager, () => Config.GetCurrentBackendUri(), Config.TorSocks5EndPoint)
: new CcjClient(Synchronizer, Network, keyManager, Config.GetFallbackBackendUri(), null);

try
{
@@ -18,26 +18,16 @@ public LoadWalletEntry(string walletName)

public LoadWalletEntry(HardwareWalletInfo hwi)
{
if (string.IsNullOrWhiteSpace(hwi.Error))
{
WalletName = hwi.Type.ToString();
}
else if (!hwi.Initialized)
{
WalletName = hwi.Type.ToString() + $" - Not Initialized";
}
else if (!hwi.Ready)
{
WalletName = hwi.Type.ToString() + $" - Device Not Ready";
}
else if (hwi.NeedPin)
{
WalletName = hwi.Type.ToString();
}
else
{
WalletName = hwi.Type.ToString() + $" - Error: {hwi.Error}";
}
WalletName = string.IsNullOrWhiteSpace(hwi.Error)
This conversation was marked as resolved by yahiheb

This comment has been minimized.

Copy link
@molnard

molnard Jul 26, 2019

Collaborator

Let if-else be.

? hwi.Type.ToString()
: !hwi.Initialized
? hwi.Type.ToString() + $" - Not Initialized"
: !hwi.Ready
? hwi.Type.ToString() + $" - Device Not Ready"
: hwi.NeedPin
? hwi.Type.ToString()
: hwi.Type.ToString() + $" - Error: {hwi.Error}";

HardwareWalletInfo = hwi;
}

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.