Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I have the following piece of code:
protected StoreDetailModel GetSelectedStore()
{
if (StoresWithDepartmentType != null && StoresWithDepartmentType.Any())
{
StoreDetailModel currentUserStore = WebsiteContext.GetCurrentUserStore();
if (currentUserStore != null && currentUserStore.Item != null)
{
StoreDetailModel store =
StoresWithDepartmentType.FirstOrDefault(x => x.Item.ID ==
currentUserStore.Item.ID);
if (store == null)
{
store = StoresWithDepartmentType.First();
}
return store;
}
}
return null;
}
Too many ifs make this code hardly readable. How can I optimize it and make it clearer?
You could write
return StoresWithDepartmentType.FirstOrDefault(x => x.Item.ID == currentUserStore.Item.ID) ?? StoresWithDepartmentType.First();
instead of
StoreDetailModel store =
StoresWithDepartmentType.FirstOrDefault(x => x.Item.ID ==
currentUserStore.Item.ID);
if (store == null)
{
store = StoresWithDepartmentType.First();
}
return store;
but some people don't like that. Ifs are necessary by the look of it, they don't bother me.
Your code is expecting to return the current users store or null if one cannot be found. It can be simplified as follows.
protected StoreDetailModel GetSelectedStore()
{
if (StoresWithDepartmentType == null) return null; // There are no stores
// Get the user store or return the first available store
StoreDetailModel currentUserStore = WebsiteContext.GetCurrentUserStore();
return currentUserStore ?? StoresWithDepartmentType.First();
}
If there are no stores to lookup, then nothing can be returned.
Otherwise return the user's store, or If user's store cannot be found, then the first available can be returned.
The simplification works because the lookup on StoresWithDepartmentType appears to return the same object.
It is not necessary to nest if-blocks, so i flatted structure for readability.
Line of last 'return' statement comes from the answer of artm.
protected StoreDetailModel GetSelectedStore()
{
if (StoresWithDepartmentType == null || !StoresWithDepartmentType.Any())
{
return null;
}
StoreDetailModel currentUserStore = WebsiteContext.GetCurrentUserStore();
if (currentUserStore == null || currentUserStore.Item == null)
{
return null;
}
return StoresWithDepartmentType.FirstOrDefault(x => x.Item.ID == currentUserStore.Item.ID) ?? StoresWithDepartmentType.First();
}
I'd split it up into a number of well-named methods.
This won't compile (because I don't know all the types you're using) but hopefully it will give you the idea:
protected StoreDetailModel GetSelectedStore()
{
if (anyStoresWithDepartmentType())
return storeWithCurrentlySelectedItem();
return null;
}
private bool anyStoresWithDepartmentType()
{
return (StoresWithDepartmentType != null) && StoresWithDepartmentType.Any();
}
private StoreDetailModel storeWithCurrentlySelectedItem()
{
var itemId = currentUserStoreItemId();
if (itemId == null)
return null;
return storeWithItem(itemId);
}
private StoreItemId currentUserStoreItemId()
{
StoreDetailModel currentUserStore = WebsiteContext.GetCurrentUserStore();
if (currentUserStore != null && currentUserStore.Item != null)
return currentUserStore.Item.ID;
return null;
}
private StoreDetailModel storeWithItem(StoreItemId itemId)
{
StoreDetailModel store = StoresWithDepartmentType.FirstOrDefault(x => x.Item.ID == itemId);
if (store != null)
return store;
return StoresWithDepartmentType.First();
}
Related
I have some flawed logic here..my goal is if all of the fields have "" value then return true, otherwise return false. How can I fix this in C# ?
public bool CheckFieldsAreEmpty()
{
Driver.WaitForElementValueNull(smtpHostInputField);
if (smtpHostInputField.GetAttribute("value") == "")
return true;
Driver.WaitForElementValueNull(smtpPortInputField);
if (smtpPortInputField.GetAttribute("value") == "")
return true;
Driver.WaitForElementValueNull(usernameInputField);
if (usernameInputField.GetAttribute("value") == "")
return true;
Driver.WaitForElementValueNull(passwordInputField);
if (passwordInputField.GetAttribute("value") == "")
return true;
Driver.WaitForElementValueNull(senderInputField);
if (senderInputField.GetAttribute("value") == "")
return true;
Driver.WaitForElementValueNull(receiverInputField);
if (receiverInputField.GetAttribute("value") == "")
return true;
else return false;
I believe something like this should work. I couldn't find what class all of those variables belong to, but replace var with the proper class and I think this should work.
logic: Add all variables to a List, use a lambda function to return if all of the list match GetAttribute("value") == ""
// Replace var with class type, or parent
List<var> allFields = new List<var> { smtpHostInputField, smtpPortInputField, sernameInputField, passwordInputField, senderInputField, receiverInputField };
foreach (var field in allFields) { Driver.WaitForElementValueNull(field); }
return allFields.All(t => t.GetAttribute("value") == "");
To illustrate #madreflection’s (correct) suggestion from the comments, consider this code:
public bool CheckFieldsAreEmpty()
{
Driver.WaitForElementValueNull(smtpHostInputField);
if (smtpHostInputField.GetAttribute("value") != "")
return false;
Driver.WaitForElementValueNull(smtpPortInputField);
if (smtpPortInputField.GetAttribute("value") != "")
return false;
Driver.WaitForElementValueNull(usernameInputField);
if (usernameInputField.GetAttribute("value") != "")
return false;
Driver.WaitForElementValueNull(passwordInputField);
if (passwordInputField.GetAttribute("value") != "")
return false;
Driver.WaitForElementValueNull(senderInputField);
if (senderInputField.GetAttribute("value") != "")
return false;
Driver.WaitForElementValueNull(receiverInputField);
if (receiverInputField.GetAttribute("value") != "")
return false;
return true;
}
Of course, as #Daniel-Lord’s answer highlights, you can centralize this repetitive logic using either a loop or a LINQ query.
Another way to solve this would be to extract WaitForElementValueNull call and empty string check to a separate method. Something like that:
private static bool WaitForElementValueNullAndEmpty(InputFieldType inputField)
{
Driver.WaitForElementValueNull(inputField);
return inputField.GetAttribute("value") == "";
}
public bool CheckFieldsAreEmpty()
{
return WaitForElementValueNullAndEmpty(smtpHostInputField) ||
WaitForElementValueNullAndEmpty(smtpPortInputField) ||
WaitForElementValueNullAndEmpty(usernameInputField) ||
WaitForElementValueNullAndEmpty(passwordInputField) ||
WaitForElementValueNullAndEmpty(senderInputField) ||
WaitForElementValueNullAndEmpty(receiverInputField);
}
The code above will match the algorithm that was provided, however if you want to check if ALL the values is empty then || should be replaced with && (As it was noticed by Jeremy Caney)
I'm swapping my values in List Object on some conditions and update List Object value.
Currently, what I'm doing is
- Looping on each object through List
- Check If condition is net
- Swap values
public static void SwapMinMaxIfNull<T>(this IEnumerable<T> rows, string reportfor)
{
if (reportfor.Equals("Comparison"))
{
var row = rows as IEnumerable<RiskBoardDataToExport>;
try
{
if (rows.Any())
{
var Tests = row.Where(min => min.MinGaitSpeed == null && min.MaxGaitSpeed != null).ToList();
if (Tests != null)
{
foreach (RiskBoardDataToExport test in Tests)
{
test.MinGaitSpeed = test.MaxGaitSpeed;
test.MaxGaitSpeed = null;
}
}
// again check for next object
Tests = row.Where(min => min.MinTUGTime == null && min.MaxTUGTime != null).ToList();
if (Tests != null)
{
foreach (RiskBoardDataToExport test in Tests)
{
test.MinTUGTime = test.MaxTUGTime;
test.MaxTUGTime = null;
}
}
// again check for next object
Tests = row.Where(min => min.MinBergScoreSpeed == null && min.MaxBergScoreSpeed != null).ToList();
if (Tests != null)
{
foreach (RiskBoardDataToExport test in Tests)
{
test.MinBergScoreSpeed = test.MaxBergScoreSpeed;
test.MaxBergScoreSpeed = null;
}
}
//.. for brevity
}
}
}
Can I do it in better way? I know about PropertyInfo i.e. Can check property name and get value etc, but, not having any hint to get this done.
Thanks
It's not exactly what you're asking for, but you can combine the clauses in your Where statements and then have a few if statements in the body:
public static void SwapMinMaxIfNull(this IEnumerable<RiskBoardDataToExport> rows,
string reportfor)
{
if (rows = null) return;
if (reportfor.Equals("Comparison", StringComparison.OrdinalIgnoreCase))
{
foreach (var row in rows.Where(r =>
(r.MinGaitSpeed == null && r.MaxGaitSpeed != null) ||
(r.MinBergScoreSpeed == null && r.MaxBergScoreSpeed != null) ||
(r.MinBergScoreSpeed == null && r.MaxBergScoreSpeed != null)))
{
if (row.MinGaitSpeed == null)
{
row.MinGaitSpeed = row.MaxGaitSpeed;
row.MaxGaitSpeed = null;
}
if (row.MinTUGTime == null)
{
row.MinTUGTime = row.MaxTUGTime;
row.MaxTUGTime = null;
}
if (row.MinBergScoreSpeed == null)
{
row.MinBergScoreSpeed = row.MaxBergScoreSpeed;
row.MaxBergScoreSpeed = null;
}
}
}
}
As this is an operation where order of the items in the list does not matter, you can easily speed this up by parallelization (you can read up on that here).
So, what you should do, is handle this foreach loop in a parallel way and combine it with Rufus L's optimized code for the fastest result.
var rows = rows.Where(r =>
(r.MinGaitSpeed == null && r.MaxGaitSpeed != null) ||
(r.MinBergScoreSpeed == null && r.MaxBergScoreSpeed != null) ||
(r.MinBergScoreSpeed == null && r.MaxBergScoreSpeed != null))
Parallel.ForEach(rows, (row) => {
{
if (row.MinGaitSpeed == null)
{
row.MinGaitSpeed = row.MaxGaitSpeed;
row.MaxGaitSpeed = null;
}
if (row.MinTUGTime == null)
{
row.MinTUGTime = row.MaxTUGTime;
row.MaxTUGTime = null;
}
if (row.MinBergScoreSpeed == null)
{
row.MinBergScoreSpeed = row.MaxBergScoreSpeed;
row.MaxBergScoreSpeed = null;
}
}
Note that this requires the System.Threading.Tasks namespace, that's where the Parallel class is.
I have a 'Invoice' WinForm C# where it contains the usual textboxes and an unbound datagridview.
I used a 'Invoice' class object to store the form fields' values. The datagridview rows are stored to a List < SubInvoice> properties in the 'Invoice'.
public static bool CompareObjects(object original, object altered)
{
Type o = original.GetType();
Type a = altered.GetType();
foreach (PropertyInfo p in o.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
Console.WriteLine("Original: {0} = {1}", p.Name, p.GetValue(original, null));
Console.WriteLine("Altered: {0} = {1}", p.Name, p.GetValue(altered, null));
if (p.GetValue(original, null).ToString() != p.GetValue(altered, null).ToString())
{
//Console.WriteLine("Not Equal");
return false;
}
}
return true;
}
I am using a global class with a method that uses Reflection to loop through the original 'invoice' object and compared it with the altered 'invoice'. If there is any difference, the user will be notified to save the invoice.
The above method works fine with all the properties of 'Invoice' class but I do not know how to check for the List in which it stores the value of each datagridrow into a 'SubInvoice' class object that stored it as an object in the list.
Can someone please give me some help here? I have also checked for similar thread in stackoverflow and other forums but in vain.
Update: I need to create a global generic method that will check for all type of classes. It can be 'Invoice', 'Customer'. The purpose is to track any changes made to the form at any particular instance and prompt the user to save.
thank you all for your help.
I have stumbled into the following article while still eager to create a global generic method through reflection.
http://www.c-sharpcorner.com/UploadFile/1a81c5/custom-extension-method-to-compare-list-in-C-Sharp/
Here's the code if anyone is interested.
public static bool CompareObjects(object original, object altered)
{
bool result = true;
//Get the class
Type o = original.GetType();
Type a = altered.GetType();
//Cycle through the properties.
foreach (PropertyInfo p in o.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
Console.WriteLine("Original: {0} = {1}", p.Name, p.GetValue(original, null));
Console.WriteLine("Altered: {0} = {1}", p.Name, p.GetValue(altered, null));
if (!p.PropertyType.IsGenericType)
{
if (p.GetValue(original, null) != null && p.GetValue(altered, null) != null)
{
if (!p.GetValue(original, null).ToString().Equals(p.GetValue(altered, null).ToString()))
{
result = false;
break;
}
}
else
{
//If one is null, the other is not
if ((p.GetValue(original, null) == null && p.GetValue(altered, null) != null) || (p.GetValue(original, null) != null && p.GetValue(altered, null) == null))
{
result = false;
break;
}
}
}
}
return result;
}
public static bool CompareLists<T>(this List<T> original, List<T> altered)
{
bool result = true;
if (original.Count != altered.Count)
{
return false;
}
else
{
if (original != null && altered != null)
{
foreach (T item in original)
{
T object1 = item;
T object2 = altered.ElementAt(original.IndexOf(item));
Type objectType = typeof(T);
if ((object1 == null && object2 != null) || (object1 != null && object2 == null))
{
result = false;
break;
}
if (!CompareObjects(object1, object2))
{
result = false;
break;
}
}
}
else
{
if ((original == null && altered != null) || (original != null && altered == null))
{
return false;
}
}
}
return result;
}
Usage: if (!xx.CompareObjects(searchedInvoice, alteredInvoice) | !xx.CompareLists(searchedInvoice.SubInvoice, alteredInvoice.SubInvoice))
If you only whant to inform the user to save the pending changes then you should implement INotifyPropertyChanged instead of doing some black magic.
One trick I've seen before is to mark the objects as serializable, then serialize them in to strings and compare the strings. It may not be the most efficient method, but it is quick, easy, and works for almost any kind of data class. It even works for "complex" nested classes that have references to other classes.
One down-side is that it won't easily tell you what specifically changed, just that something is different.
This is the code giving a stack overflow it only happens about half the time and i have no idea why it's doing it. From what I seen it only happens with the Coms(TopCom, etc) are in a mass of numbers so around 5+ then it stack overflows.
public bool getConnected(int d) {
if (topCom.connection != null) {
if (d != topCom.connection.id) {
if (topCom.connection.id == 0) {
return true;
} else if (topCom.connection.connected == true) {
if (Development.instance.currentDos.buttons[topCom.connection.id].getConnected(id)) {
return true;
}
}
}
}
if (leftCom.connection != null) {
if (d != leftCom.connection.id) {
if (leftCom.connection.id == 0) {
return true;
} else if (leftCom.connection.connected == true) {
if (Development.instance.currentDos.buttons[leftCom.connection.id].getConnected(id)) {
return true;
}
}
}
}
if (rightCom.connection != null) {
if (d != rightCom.connection.id) {
if (rightCom.connection.id == 0) {
return true;
} else if (rightCom.connection.connected == true) {
if (Development.instance.currentDos.buttons[rightCom.connection.id].getConnected(id)) {
return true;
}
}
}
}
if (botCom.connection != null) {
if (d != botCom.connection.id) {
if (botCom.connection.id == 0) {
return true;
} else if (botCom.connection.connected == true) {
if (Development.instance.currentDos.buttons[botCom.connection.id].getConnected(id)) {
return true;
}
}
}
}
return false;
}
This happens in recursive functions where you don't have a base condition for ending the recursion. You basically keep calling the function until you reach stack overflow.. Trace your code through and figure out why it calls itself endlessly.
The fact that people here can't really tell what you're trying to accomplish is a code smell of sorts.
A big part of that is the fact that you have an incredible amount of nesting in your code. Nested conditionals increase the difficulty of debugging code, as you're discovering now. Additionally, you could easily combine some of your conditionals - all of your conditionals in any top-level branch can actually be combined into one statement, as follows:
if ((topCom.connection != null && d != topCom.connection.id && topCom.connection.id == 0) ||
(topCom.connection.connected == true &&
Development.instance.currentDos.buttons[topCom.connection.id].getConnected(id)))
{
return true;
}
return false;
As far as I can imagine, there's no point in having separate conditional branches that perform the same function, e.g. if (a) { return true; } else if (b) { return true; }. Just move the logic from else if into the original if conditional.
However, I'd recommend encapsulating some or all of this logic into a separate function, given that it seems like you're performing the same logic on each of your connections. You could create a function like so:
public bool ConnectionIsValid(connectionObject // replace with the actual type)
{
if (topCom.connection != null && d != topCom.connection.id && topCom.connection.id == 0)
|| (topCom.connection.connected == true
&& Development.instance.currentDos.buttons[topCom.connection.id].getConnected(id))
return true;
return false;
}
So that you could then just call ConnectionIsValid on each of your connections, rather than using 80-some lines on conditionals for each connection.
It also seems doubtful that there's a StackOverflowException occurring in this code. Unless you have a circular reference related to any of the objects referenced in this code (in which case, there's a decent chance you used a setter accessor to assign a value to the same variable:
object A
{
set
{
this.A = value;
}
}
which will always cause a stack overflow, it's likely you've introduced some sort of recursion outside the scope of the included code.
is there a way for bellow lambda expression to move the method call ConvertFilterType(filter.FilterTypeId) into a variable so it is not called repeatedly for every condition ?
This if statement is making sure collection consist of all required filters.
if (run.Filters.All(
filter => (ConvertFilterType(filter.FilterTypeId) != FilterType.A)
&& (ConvertFilterType(filter.FilterTypeId) != FilterType.B)
&& (ConvertFilterType(filter.FilterTypeId) != FilterType.C)
&& (ConvertFilterType(filter.FilterTypeId) != FilterType.D)
&& (ConvertFilterType(filter.FilterTypeId) != FilterType.E)))
{
throw new ArgumentException();
}
if (run.Filters.All(
filter => {
FilterType t = ConvertFilterType(filter.FilterTypeId);
return
t != FilterType.A && t != FilterType.B && t != FilterType.C && t != FilterType.D && t != FilterType.E;
}))
{
throw new ArgumentException();
}
King beat me to it. Although it is not what you asked, I would also recommend that for improved readability, that you create a separate method with a more meaningful name. This chain of logic can obscure the intent of what you are doing. Extracting it into a method would help clarify the method's intent. I would have put this in the comment's to King's post, but don't have the reputation yet.
Here is some sample code, though I don't know what the type of the items in your Filters collection to know what the parameter of the method would need to be.
if (run.Filters.All(filter => { return IsFilterAllowed(filter); } )
{
throw new ArgumentException();
}
private bool IsFilterAllowed(FiltersItemType filter)
{
FilterType t = ConvertFilterType(filter.FilterTypeId);
return
t != FilterType.A &&
t != FilterType.B &&
t != FilterType.C &&
t != FilterType.D &&
t != FilterType.E;
}
King's answer is the way to go but could you could also do this it seems.
FilterType[] notTheseFilters = new FilterType[] { FilterType.A, FilterType.B...};
bool result = !Filters.Any(f =>
notTheseFilters.Contains(ConvertFilterType(f.FilterTypeId)));
If my logic serves me right, you could also do this
var filterTypesToAvoid = new[]{
FilterType.A,
FilterType.B,
FilterType.C,
FilterType.D,
FilterType.E
};
if (run.Filters.All(
filter => !filterTypesToAvoid.Contains(ConvertFilterType(filter.FilterTypeId))
))
{
throw new ArgumentException();
}
This way there is no need for brackets in the LINQ query.
To extend what brader24 proposed, you could try :
if (run.Filters.All(IsFilterAllowed))
{
throw new ArgumentException();
}
private bool IsFilterAllowed(FiltersItemType filter)
{
FilterType t = ConvertFilterType(filter.FilterTypeId);
return
t != FilterType.A &&
t != FilterType.B &&
t != FilterType.C &&
t != FilterType.D &&
t != FilterType.E;
}