Neatest/most idiomatic way to rewrite this If in C# - c#

I have this if-else statement which does what I want. What it's doing is pretty straightforward as you should be able to tell.
if (width != null && height != null)
{
if (top != null && left != null)
{
ret.type = VMLDimensionType.full;
}
else
{
ret.type = VMLDimensionType.size;
}
}
else
{
if (top != null && left != null)
{
ret.type = VMLDimensionType.positon;
}
else
{
ret.type = VMLDimensionType.unset;
}
}
The enum being referred to is:
private enum VMLDimensionType
{
unset = 0,
full = 1,
size = 2,
position = 3
}
It's so straightforward I'm sure there's a much more terse and more readable way to express this.
NB If it wasn't for the ridiculous 'one-brace per line' rule that VS imposes by default I probably wouldn't be so bothered. Eg in VB I could lose about 10 lines from this code block! (any thoughts on that as an aside?)

bool hasPosition = (top != null && left != null);
bool hasSize = (width != null && height != null);
if (hasSize)
{
ret.type = hasPosition ? VMLDimensionType.full : VMLDimensionType.size;
}
else
{
ret.type = hasPosition ? VMLDimensionType.positon : VMLDimensionType.unset;
}

One option would be to make VMLDimensionType a Flags enumeration:
[Flags]
enum VMLDimensionType
{
Unset = 0,
Size = 1,
Position = 1 << 1,
Full = Size | Position
}
And then:
ret.Type = VMLDimensionType.Unset;
if(width != null && height != null)
ret.Type |= VMLDimensionType.Size;
if (top != null && left != null)
ret.Type |= VMLDimensionType.Position;

How about this:
bool hasSize = width != null && height != null;
bool hasPosition = top != null && left != null;
if (hasSize && hasPosition)
{
ret.type = VMLDimensionType.full;
}
else if (hasSize && !hasPosition)
{
ret.type = VMLDimensionType.size;
}
else if (!hasSize && hasPosition)
{
ret.type = VMLDimensionType.positon;
}
else
{
ret.type = VMLDimensionType.unset;
}

I would like to extract GetDimensionType() method.
And make it not so small, but more readable and self-descriptive.
private VMLDimensionType GetDimensionType()
{
bool hasSize = width != null && height != null;
bool hasPosition = top != null && left != null;
if (hasSize && hasPosition)
return VMLDimensionType.full;
if (hasSize)
return VMLDimensionType.size;
if (hasPosition)
return VMLDimensionType.positon;
return VMLDimensionType.unset;
}
Usage:
ret.type = GetDimensionType();

What about this:
if(width != null && height != null)
ret.type = top != null && left != null ? VMLDimensionType.full : VMLDimensionType.size;
else
ret.type = top != null && left != null ? VMLDimensionType.positon : VMLDimensionType.unset;

Related

Sum two forces with both signs

I'm trying to make a good algorithm for summing up pair of forces. The main problem here is that you can have force with alternative sign which means that force can be with + or - at any time.
For example:
F1 = ±100 kN, F2 = 200 kN --> maxForce = +100+200 = 300 kN, minForce = -100+200 = 100 kN.
I've already made an simple algorithm which combines all possibilities, but I ask for something better than that. As an output of my method I have:
public List<Force> SumForces(Force firstForce, Force secondForce)
{
Force maxForce = new Force();
Force minForce = new Force();
// All possible sumatuons
double sumCaseFirst = firstForce.ForceValue + secondForce.ForceValue;
double sumCaseSecond = firstForce.ForceValue - secondForce.ForceValue;
double sumCaseThird = -firstForce.ForceValue + secondForce.ForceValue;
double sumCaseFourth = -firstForce.ForceValue - secondForce.ForceValue;
// Calculating all posible sumations
if (firstForce.Sign == ForceSign.Alter && secondForce.Sign == ForceSign.Alter)
{
maxForce.ForceValue = sumCaseFirst;
minForce.ForceValue = sumCaseFourth;
}
else if (firstForce.Sign == ForceSign.Alter && secondForce.Sign == ForceSign.Plus)
{
maxForce.ForceValue = sumCaseFirst;
minForce.ForceValue = sumCaseThird;
}
else if (firstForce.Sign == ForceSign.Alter && secondForce.Sign == ForceSign.Minus)
{
maxForce.ForceValue = sumCaseSecond;
minForce.ForceValue = sumCaseFourth;
}
else if (firstForce.Sign == ForceSign.Plus && secondForce.Sign == ForceSign.Alter)
{
maxForce.ForceValue = sumCaseFirst;
minForce.ForceValue = sumCaseSecond;
}
else if (firstForce.Sign == ForceSign.Plus && secondForce.Sign == ForceSign.Plus)
{
maxForce.ForceValue = sumCaseFirst;
minForce.ForceValue = 0;
}
else if (firstForce.Sign == ForceSign.Plus && secondForce.Sign == ForceSign.Minus)
{
maxForce.ForceValue = sumCaseSecond;
minForce.ForceValue = 0;
}
else if (firstForce.Sign == ForceSign.Minus && secondForce.Sign == ForceSign.Alter)
{
maxForce.ForceValue = sumCaseThird;
minForce.ForceValue = sumCaseFourth;
}
else if (firstForce.Sign == ForceSign.Minus && secondForce.Sign == ForceSign.Plus)
{
maxForce.ForceValue = sumCaseThird;
minForce.ForceValue = 0;
}
else
{
maxForce.ForceValue = 0;
minForce.ForceValue = sumCaseFourth;
}
// Ensure that true maximum force value is at index 0
if (maxForce.ForceValue > minForce.ForceValue)
{
Sum.Add(maxForce);
Sum.Add(minForce);
}
else
{
Sum.Add(minForce);
Sum.Add(maxForce);
}
return Sum;
}
The maximum is always when adding the positive values
double maxValue = Math.Abs(firstForce.ForceValue) + Math.Abs(secondForce.ForceValue);
The minimum is always when adding the negative values
double minValue = -Math.Abs(firstForce.ForceValue) - Math.Abs(secondForce.ForceValue);
There is no need to consider the positive and negative combinations.

Simplifying IF statement logic - inner logic overlap

Can the below logical if conditions be simplified ?
I have wrote this code but some parts are overlapping so I thought to seek for some help to see whether it can be simplified...
I have actually three different fields but following the same patterns.
EDIT :
if (Row.ReceivableAmount_IsNull == true && Row.CustomerID == LastCustomerID)
{
if (LastReceivableAmount == null)
{
Row.PreviousReceivableAmount_IsNull = true;
}
else
{
Row.PreviousReceivableAmount = LastReceivableAmount.GetValueOrDefault();
}
}
else
{
Row.PreviousReceivableAmount = LastReceivableAmount.GetValueOrDefault();
LastReceivableAmount = Row.ReceivableAmount;
}
if (Row.SaleAmount_IsNull == true && Row.CustomerID == LastCustomerID)
{
if (LastSaleDate == null)
{
Row.PreviousSaleDate_IsNull = true;
}
else
{
Row.PreviousSaleDate = LastSaleDate.GetValueOrDefault();
}
}
else
{
if (LastSaleDate == null)
{
Row.PreviousSaleDate_IsNull = true;
}
else
{
Row.PreviousSaleDate = LastSaleDate.GetValueOrDefault();
}
LastSaleDate = Row.Date;
}
if (Row.PaymentAmount_IsNull == true && Row.CustomerID == LastCustomerID)
{
if (LastPaymentDate == null)
{
Row.PreviousPaymentDate_IsNull = true;
}
else
{
Row.PreviousPaymentDate = LastPaymentDate.GetValueOrDefault();
}
}
else
{
Row.PreviousPaymentDate = LastPaymentDate.GetValueOrDefault();
LastPaymentDate = Row.Date;
}
Yes, you only care about LastSaleDate in your outer if condition, so move everything else out.
Once you've moved it out, you can invert your original condition, reducing your if/else to just an if.
if (LastReceivableAmount == null)
{
Row.PreviousReceivableAmount_IsNull = true;
}
else
{
Row.PreviousReceivableAmount = LastReceivableAmount.GetValueOrDefault();
}
if (!Row.ReceivableAmount_IsNull || Row.CustomerID != LastCustomerID)
{
Row.PreviousReceivableAmount = LastReceivableAmount.GetValueOrDefault();
LastReceivableAmount = Row.ReceivableAmount;
}
if (LastSaleDate == null)
{
Row.PreviousSaleDate_IsNull = true;
}
else
{
Row.PreviousSaleDate = LastSaleDate.GetValueOrDefault();
}
if (!Row.SaleAmount_IsNull || Row.CustomerID != LastCustomerID)
{
LastSaleDate = Row.Date;
}
if (LastPaymentDate == null)
{
Row.PreviousPaymentDate_IsNull = true;
}
else
{
Row.PreviousPaymentDate = LastPaymentDate.GetValueOrDefault();
}
if (!Row.PaymentAmount_IsNull == true || Row.CustomerID != LastCustomerID)
{
Row.PreviousPaymentDate = LastPaymentDate.GetValueOrDefault();
LastPaymentDate = Row.Date;
}
Since both branches of the if are similar, except one statement, you could use the following approach:
if (LastSaleDate == null)
{
Row.PreviousSaleDate_IsNull = true;
}
else
{
Row.PreviousSaleDate = LastSaleDate.GetValueOrDefault();
}
if (!Row.SaleAmount_IsNull || Row.CustomerID != LastCustomerID)
{
LastSaleDate = Row.Date;
}

Nullable Property throwing NullReferenceException on .HasValue

This line of (C#) code
if (!currentLap.S1.HasValue)
is giving me
System.NullReferenceException: Object reference not set to an instance of an object.
provided I'm sure that currentLap variable is instantiated (because it's being used a few lines before and it is a local variable) and it has following property:
private double? _s1;
[DefaultValue(null)]
[JsonConverter(typeof(ShortDoubleConverter))]
public double? S1
{
get { return _s1; }
set { _s1 = value; }
}
how can it possibly throw NullReferenceException? Can it be something to do with optimization on Release mode?
Thanks,
Stevo
EDIT:
here is full method code.
public void Update(DriverData driverData)
{
LapInfo currentLap = this.CurrentLap;
if (currentLap != null &&
this.LastDriverData != null &&
driverData.TotalLaps != this.LastDriverData.TotalLaps &&
driverData.InPits &&
driverData.Speed < 10 &&
!this.LastDriverData.InPits)
{
currentLap.Escaped = true;
}
this.LastDriverData = driverData;
if ((currentLap == null || currentLap.Lap != driverData.LapNumber) &&
!this.Laps.TryGetValue(driverData.LapNumber, out currentLap))
{
currentLap = new LapInfo() { Lap = driverData.LapNumber, Parent = this, Class = driverData.Class };
this.Laps.Add(driverData.LapNumber, currentLap);
int lapsCount = 0, completedDriverLaps = 0, cleanLaps = 0;
this.TotalLaps = driverData.TotalLaps;
//if it's not the first lap
if (driverData.TotalLaps > 0)
{
//previous lap
if (this.CurrentLap == null || !this.CurrentLap.Escaped)
{
this.CompletedLaps++;
if (this.CurrentLap == null || !this.CurrentLap.MaxIncident.HasValue)
this.CleanLaps++;
}
}
foreach (DriverLapsInfo laps in this.Parent.LapsByVehicle.Values)
{
lapsCount += laps.TotalLaps;
completedDriverLaps += laps.CompletedLaps;
cleanLaps += laps.CleanLaps;
}
this.Parent.Parent.SetLapsCount(driverData, lapsCount, driverData.Class, completedDriverLaps, cleanLaps);
}
this.CurrentLap = currentLap;
//add incidents
if (driverData.Incidents != null)
{
foreach (IncidentScore incident in driverData.Incidents)
{
this.CurrentLap.MaxIncident = Math.Max(this.CurrentLap.MaxIncident ?? 0, incident.Strength);
this.CurrentLap.Incidents++;
this.Incidents++;
}
}
LapInfo previousLap = null;
if ((this.PreviousLap == null || this.PreviousLap.Lap != driverData.TotalLaps) &&
this.Laps.TryGetValue(driverData.TotalLaps, out previousLap))
{
this.PreviousLap = previousLap;
if (!this.PreviousLap.Date.HasValue)
{
this.PreviousLap.Date = DateTime.UtcNow;
}
}
if (currentLap.Position == 0)
currentLap.Position = driverData.Position;
if (driverData.CurrentS1 > 0)
{
**if (!currentLap.S1.HasValue)**
{
this.UpdateBestS1(driverData.BestS1);
this.Parent.Parent.UpdateBestS1(driverData.BestS1, driverData.UniqueName);
currentLap.UpdateS1(driverData.CurrentS1, driverData);
//reset the best split set at the finish line
if (this.PreviousLap != null && this.PreviousLap.SplitBest < 0)
this.PreviousLap.SplitBest = 0;
}
if (driverData.CurrentS2.HasValue && driverData.CurrentS1.HasValue && !currentLap.S2.HasValue)
{
double s2 = driverData.CurrentS2.Value - driverData.CurrentS1.Value;
this.UpdateBestS2(s2);
this.Parent.Parent.UpdateBestS2(s2, driverData.UniqueName);
currentLap.UpdateS2(s2, driverData);
}
}
if (this.PreviousLap != null)
{
if (driverData.LastLap > 0)
{
if (!this.PreviousLap.S3.HasValue && driverData.LastS2.HasValue)
{
double s3 = driverData.LastLap.Value - driverData.LastS2.Value;
this.UpdateBestS3(s3);
this.Parent.Parent.UpdateBestS3(s3, driverData.UniqueName);
this.PreviousLap.UpdateS3(s3, driverData);
}
if (!this.PreviousLap.LapTime.HasValue)
{
double? bestLap = this.Parent.Parent.BestLap;
this.PreviousLap.UpdateLapTime(driverData, 0);
this.Parent.Parent.UpdateBestLap(this.PreviousLap, driverData.BestLap, driverData);
this.UpdateBestLap(driverData.BestLap, this.PreviousLap);
this.PreviousLap.UpdateLapTime(driverData, bestLap);
}
}
else
{
if (this.PreviousLap.SplitBest.HasValue)
this.PreviousLap.UpdateBestSplit();
if (this.PreviousLap.SplitSelf.HasValue)
this.PreviousLap.UpdateSelfSplit();
}
}
if (driverData.InPits)
{
switch (driverData.Sector)
{
case Sectors.Sector1:
if (previousLap != null)
previousLap.InPits = true;
break;
case Sectors.Sector3:
currentLap.InPits = true;
break;
}
}
//lap to speed
if (currentLap.TopSpeed < driverData.Speed)
{
driverData.TopSpeedLap = driverData.Speed;
currentLap.UpdateTopSpeed(driverData.Speed);
}
else
driverData.TopSpeedLap = currentLap.TopSpeed;
//overall top speed
if (this.TopSpeed < driverData.Speed)
{
driverData.TopSpeed = driverData.Speed;
this.TopSpeed = driverData.Speed;
this.Parent.Parent.UpdateTopSpeed(this.TopSpeed, driverData);
}
else
driverData.TopSpeed = this.TopSpeed;
}
There is no way on earth the code can make it to that line and currentLap beeing null.
Or am I going crazy? :)
.HasValue will not throw if the nullable reference is null, but a.b.HasValue will if a is null.
I suspect that currentLap == null. I know you say you're sure that currentLap is not null, but I think that's the most likely explanation. Can you post more code?
Update:
Thanks for posting your code.
This doesn't throw:
void Main() {
var f = new Foo();
Console.WriteLine (f.S1);
Console.WriteLine (f.S1.HasValue);
}
class Foo {
private double? _s1 = null;
public double? S1 {
get { return _s1; }
set { _s1 = value; }
}
}
Could you try to create a minimal reproduction? (minimal code that exhibits the issue)
Maybe have a look at the previous line of code :) - debugger often highlights the next line after the one where the NullReferenceException was actually thrown.

if then else vs the ternary operator ( ? :) in c#

this.value1 and c.value1 can both be either null or non-null. So a total of 4 combinations to test. value2 can also be null or non-null.
Can the if-then-else's below be replaced by something shorter like use the ternary operator ( if then else using the ? : operators) - and would that be a bad practice for this specific case because we are testing 4 combinations for value1 and value2?
public override bool Equals(object obj)
{
bool value1_check = false;
bool value2_check = false;
var c = obj as ObjectType;
if (this.value1 != null)
value1_check = this.value1.Equals(c.value1);
else if ((this.value1 == null) && (c.value1 == null))
value1_check = true;
else if ((this.value1 == null) && (c.value1 != null))
value1_check = c.value1.Equals(this.value1);
if (this.value2 != null)
value2_check = this.value2.Equals(c.value2);
else if ((this.value2 == null) && (c.value2 == null))
value2_check = true;
else if ((this.value2 == null) && (c.value2 != null))
value2_check = c.value2.Equals(this.value2);
return (value1_check && value2_check);
}
You can call Object.Equals(), which already does all that.
return Equals(this.Value1, c.Value1)
&& Equals(this.Value2, c.Value2);
Actually, you might want the ?? Operator.
var lhs= this.value1 ?? c.value1 ?? null;
var rhs = c.value1 ?? this.value1 ?? null;
var value1Check = lhs == rhs
Should do the same thing as yours, but almost 100% less readable!
If your still wondering about the ternary option.
value1_check= this.value1!=null? this.value1.Equals(c.value1):(c.value1!=null?c.value.Equals(this.value):value1_check=true);

Is there a better method to structure this if statement

It just seems a mess to me, my mind tells me there has to be a better way.
I have 6 controls on a web page.
if (printer_make_1.Text != "" && printer_model_1.Text != "" && printer_make_2.Text != "" && printer_model_2.Text != "" && printer_make_3.Text != "" && printer_model_3.Text != "")
{
// Do something
}
What is the best/most efficient way to do this?
You can refactor into a method, if you want to improve the readability or use the same logic elsewhere:
public Boolean AllControlsHaveAValue() {
return (printer_make_1.Text != ""
&& printer_model_1.Text != ""
&& printer_make_2.Text != ""
&& printer_model_2.Text != ""
&& printer_make_3.Text != ""
&& printer_model_3.Text != "");
}
Then just ask:
if (AllControlsHaveAValue()) {
// do something
}
Restructuring starts with your data: avoid printer_make_1, printer_make_2, ...
class PrinterData
{
public string Make { get; set; }
public string Model { get; set; }
}
PrinterData[] printers = new PrinterData[3]; //or use a List<>
printers[0] = new PrinterData { Make = "PH", Model = "1A" };
...
if (printers.All(p => ! (p.Make == "" || p.Model == "")) )
...
if(new[] { printer_make_1, printer_model_1 ...}.All(l => l.Text != string.Empty)
{
//do something
}
You might want to split it up to be more readable:
var labels = new[] { printer_make_1, printer_model_1 ... };
if(labels.All(l => l.Text != string.Empty))
{
//do something
}
I normally put that test into a method and call that to make the if easier to read
private boolean AreAllPrinterFieldsFilled()
{
return (printer_make_1.Text != ""
&& printer_model_1.Text != ""
&& printer_make_2.Text != ""
&& printer_model_2.Text != ""
&& printer_make_3.Text != ""
&& printer_model_3.Text != "");
}
Then in the if:
if (AreAllPrinterFieldsFilled)
{
// Do something
}
There are lots of ways to accomplish this - none of them are elegant. Do what is most readable to you (and those who may come behind you).
I would probably take this approach:
string makeText = String.Concat(printer_make_1.Text, printer_make_2.Text, printer_make_3.Text);
string modelText = String.Concat(printer_model_1.Text, printer_model_2.Text, printer_model_3.Text);
if (makeText.Length != 0 && modelText.Length != 0)
{
// Do something
}
if (!Enumerable.Range(1, 3)
.Any(i => ((TextBox)FindControl("printer_make_" + i)).Text == "" ||
((TextBox)FindControl("printer_model_" + i)).Text == "") {...}
It allows you to later expand the number of printer makes and models, but isn't as strong typed.
private bool CheckAllEmpty(params TextBox[] textBoxes)
{
return textBoxes.All(tb => tb.Text != null);
}
private void Foo()
{
...
if (CheckAllEmpty(tb1, tb2, tb3))
{
mbox("tb1, tb2 and tb3 are all empty");
}
else
{
mbox("tb1, tb2 or tb3 isn't empty");
}
...
}

Categories

Resources