在for循环中声明太多变量。 [英] Declaring Too many Variables inside a for loop.

查看:219
本文介绍了在for循环中声明太多变量。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我担心在for / foreach / while / dowhile循环中声明变量会持续超过1000~10000次迭代。



我们不会打扰声明任意数量的变量(string [],List< t> ;, Dictionary< key,value>)(忘了创建自定义键入并添加到列表中)并在循环迭代时每次都实例化它。我们没有(知道)任何工具来指出这是正确还是错误。



我的问题是,有人对此有任何担忧吗?我们是否按照正确的方式使用这样的东西?



这篇文章的原因。因为我这样做,我想知道我是否遵循正确的编码方式。



如果有人不能理解这个问题,请告诉我。



Hi,
I have a concern about declaring variable(s) inside a for/foreach/while/dowhile loop which would last for more than 1000~10000 iterations.

We won’t bother about declaring any number of variables (string[],List<t>, Dictionary<key,value>) (forgot about creating a custom type and adding to a list) and keep on instantiating it for each and every time when the loops iterates. And we don’t have (know either) any tool to point out if this was correct or wrong.

My question here is, anyone have any concern about this? Are we following the correct way of using something like this?

The reason for this post. Because I do this and I want to know whether I was following a correct way of coding.

Please let me know if any one can't understand this question.

foreach (var cItem in DataQualityMonitoringViewDataGrid.VisibleColumns.Where(lwhr => lwhr.SortDirection != SortDirection.None))
                {
                    //Inner scope variable
                    var statusProcessing = string.Format("Processing {0} Row", row);

                    //Inner scope variable
                    var fName = cItem.FieldName.Replace(".ValueText", "").ToUpper();
                    if (fName == Constants.DG_COL_STATUS) continue;
                    fName = cItem.FieldName.Replace(".ValueText", "");

                    //Inner scope variable
                    string xmlDbApiFName;
                    CommonUtils.ColumnCollection.TryGetValue(cItem.FieldName.ToUpper(), out xmlDbApiFName);

                    if (string.IsNullOrEmpty(xmlDbApiFName)) break;

                    //Inner scope variable
                    var currstatusDQmCell = currentCol.Columns(fName) as DQMCell;
                    //Inner scope variable
                    var currStatusVal = currstatusDQmCell.Columns("ValueText");

                    if (string.IsNullOrEmpty(currStatusVal.ToString()))
                        condition += @" not(" + xmlDbApiFName + " != '') and ";
                    else
                        condition += @" contains(" + xmlDbApiFName + ",'" + currStatusVal + "') and ";

                    //Inner scope variable
                    const string conditionRed = " contains(_x0031_5010,'0')";

                    //Inner scope variable
                    var forRed = CommonUtils.XmlDocument.SelectNodes("//Distinct_DQM[" + condition + conditionRed + "]");

                    if (forRed != null && forRed.Count >= 1)
                        ApplyCellColor(currentCol, CommonUtils.GetColor("0"), fName.ToUpper(), DQMFlag.red);    // Local Member
                    else
                    {
                        //Inner scope variable
                        const string conditionAmber = " contains(_x0031_5010,'1')";

                        //Inner scope variable
                        var foramberd = CommonUtils.XmlDocument.SelectNodes("//Distinct_DQM[" + condition + conditionAmber + "]");  //Static member
                        if (foramberd != null && foramberd.Count >= 1)

                            ApplyCellColor(currentCol, CommonUtils.GetColor("1"), fName.ToUpper(), DQMFlag.amber);      // Local Member
                        else
                        {
                            //Inner scope variable
                            var dQmFlag = DQMFlag.amber;
                            Enum.TryParse(currentCol.Status_15010.ValueText, out dQmFlag);
                            ApplyCellColor(currentCol, currentCol.Status_15010.Background, fName.ToUpper(), dQmFlag);       // Local Member
                        }
                    }
                    column++;
                    currentCol.IsHeatMapApplied = true;
                }

推荐答案

在循环中声明许多变量没有任何问题,因为优化器可以轻松处理它。如果所有这些变量都合理,则不应进行手动优化,例如将它们带出循环。恰恰相反,最好将所有变量的格式上下文限制为真正需要它们的代码片段。



同时,你的许多变量可以是算法错误的结果很差,而且可能很糟糕。没有您的代码示例,很难添加更多。



-SA
There is nothing wrong with declaring many variables inside loop, as the optimizer can easily take care of it. If all those variables are reasonable, you should not to "manual optimization", such as carrying them out of the loop. Just the opposite, it's good to limit format context of all variable to the fragment of code where they are really needed.

At the same time, your many variables can be a result of poorly algorithmic mistakes, and that could be bad. Without your code sample, it's hard to add more.

—SA


这篇关于在for循环中声明太多变量。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆