суббота, 16 июня 2012 г.

Shitty Code Spagetti Говнокод

Набрел тут на кошмарный код. Проверил по Code Metrics (есть такая вкусность в Visual Studio), индекс поддержки - желтый, значение 14 (должен быть как минимум 20, а лучше - стремиться к 100). Цикломатическая сложность кода (зависит от вложенности условных операторов и циклов) - 58, должна быть не более 15.

Весь код привожу под катом.

Говнокод

        private void FillDocuments()
        {
            this.Panel1.Controls.Clear();
            Guid idList = Guid.Empty;
            try
            {
                idList = new Guid(this.IDDocumentLibrary);
                Guid _web = Helper.GetCurrentWeb(this.WEBURL, SPContext.Current).ID;
                Guid _site = Helper.GetCurrentWeb(this.WEBURL, SPContext.Current).Site.ID;
                SPSecurity.RunWithElevatedPrivileges(delegate
                {
                    using (SPSite sPSite = new SPSite(_site))
                    {
                        using (SPWeb sPWeb = sPSite.OpenWeb(_web))
                        {
                            SPList sPList = sPWeb.Lists[idList];
                            bool flag = sPList.Fields.ContainsField(Helper.ListData.Fields.Group);
                            bool flag2 = sPList.Fields.ContainsField(Helper.ListData.Fields.Priority);
                            if ((this.SortByDate || this.ShowDate) && !sPList.Fields.ContainsField(Helper.ListData.Fields.ViewDate))
                            {
                                this.Panel1.Controls.Add(new LiteralControl(string.Format("У данной библиотеки нет поля '{0}'", Helper.ListData.Fields.ViewDate)));
                            }
                            else
                            {
                                if (!sPList.Fields.ContainsField(Helper.ListData.Fields.Description) && !this.SimpleView)
                                {
                                    this.Panel1.Controls.Add(new LiteralControl(string.Format("У данной библиотеки нет поля '{0}', либо установите галочку 'Простой вид'", Helper.ListData.Fields.Description)));
                                }
                                else
                                {
                                    SPQuery sPQuery = new SPQuery();
                                    if (this.Request.Params["f"] == Helper.DDLNoFilter || string.IsNullOrEmpty(this.Request.Params["f"]))
                                    {
                                        sPQuery.ViewAttributes = "Scope=\"RecursiveAll\"";
                                    }
                                    else
                                    {
                                        try
                                        {
                                            sPQuery.Folder = sPList.Folders[new Guid(this.Request.Params["f"])].Folder;
                                        }
                                        catch (Exception ex2)
                                        {
                                            Logger.Write(ex2);
                                        }
                                    }
                                    string text = string.Empty;
                                    string text2 = string.Empty;
                                    if (flag2)
                                    {
                                        text2 = string.Format("", sPList.Fields[Helper.ListData.Fields.Priority].InternalName);
                                    }
                                    if (flag)
                                    {
                                        text = string.Format("", sPList.Fields[Helper.ListData.Fields.Group].InternalName);
                                    }
                                    if (this.SortByDate)
                                    {
                                        sPQuery.Query = string.Format("{2}{1}", sPList.Fields[Helper.ListData.Fields.ViewDate].InternalName, text, text2);
                                    }
                                    else
                                    {
                                        sPQuery.Query = " " + text2 + text + "";
                                    }
                                    sPQuery.RowLimit = 100u;
                                    List list = new List();
                                    int num = 1;
                                    do
                                    {
                                        SPListItemCollection items = sPList.GetItems(sPQuery);
                                        foreach (SPListItem sPListItem in items)
                                        {
                                            list.Add(sPListItem);
                                        }
                                        sPQuery.ListItemCollectionPosition = items.ListItemCollectionPosition;
                                        num++;
                                    }
                                    while (sPQuery.ListItemCollectionPosition != null);
                                    if (!this.SimpleView)
                                    {
                                        Dictionary dictionary = new Dictionary();
                                        foreach (SPListItem sPListItem in list)
                                        {
                                            if (sPListItem.Folder == null)
                                            {
                                                int num2 = 0;
                                                try
                                                {
                                                    if (sPListItem.Fields.ContainsField(Helper.ListData.Fields.FileSize))
                                                    {
                                                        int.TryParse(sPListItem[Helper.ListData.Fields.FileSize].ToString(), out num2);
                                                    }
                                                    else
                                                    {
                                                        int.TryParse(sPListItem[Helper.ListData.Fields.FileSizeEn].ToString(), out num2);
                                                    }
                                                }
                                                catch (Exception ex2)
                                                {
                                                    Logger.Write(ex2);
                                                }
                                                string key = string.Format("{0}", sPListItem[Helper.ListData.Fields.Title]);
                                                if (flag)
                                                {
                                                    key = string.Format("{1}{0}", sPListItem[Helper.ListData.Fields.Title], sPListItem[Helper.ListData.Fields.Group]);
                                                }
                                                if (!dictionary.ContainsKey(key))
                                                {
                                                    ItemsFilesOnPage itemsFilesOnPage = new ItemsFilesOnPage();
                                                    itemsFilesOnPage.ParamDescription = string.Format("{0}", sPListItem[Helper.ListData.Fields.Description]);
                                                    List list2 = new List();
                                                    if (sPListItem.Fields.ContainsField(Helper.ListData.Fields.FileType))
                                                    {
                                                        list2.Add(new FileURL(this.FileURL(sPListItem), string.Format("{0}", sPListItem[Helper.ListData.Fields.FileType]), (float)num2 / 1024f));
                                                    }
                                                    else
                                                    {
                                                        list2.Add(new FileURL(this.FileURL(sPListItem), string.Format("{0}", sPListItem[Helper.ListData.Fields.FileTypeEn]), (float)num2 / 1024f));
                                                    }
                                                    itemsFilesOnPage.ParamURL = list2.ToArray();
                                                    itemsFilesOnPage.ParamTitle = string.Format("{0}", sPListItem[Helper.ListData.Fields.Title]);
                                                    if (flag)
                                                    {
                                                        itemsFilesOnPage.ParamGroup = string.Format("{0}", sPListItem[Helper.ListData.Fields.Group]);
                                                    }
                                                    dictionary.Add(key, itemsFilesOnPage);
                                                }
                                                else
                                                {
                                                    ItemsFilesOnPage itemsFilesOnPage = dictionary[key];
                                                    itemsFilesOnPage.ParamDescription = string.Format("{0}", sPListItem[Helper.ListData.Fields.Description]);
                                                    List list2 = new List();
                                                    list2.AddRange(itemsFilesOnPage.ParamURL);
                                                    if (sPListItem.Fields.ContainsField(Helper.ListData.Fields.FileType))
                                                    {
                                                        list2.Add(new FileURL(this.FileURL(sPListItem), string.Format("{0}", sPListItem[Helper.ListData.Fields.FileType]), (float)num2 / 1024f));
                                                    }
                                                    else
                                                    {
                                                        list2.Add(new FileURL(this.FileURL(sPListItem), string.Format("{0}", sPListItem[Helper.ListData.Fields.FileTypeEn]), (float)num2 / 1024f));
                                                    }
                                                    itemsFilesOnPage.ParamURL = list2.ToArray();
                                                    itemsFilesOnPage.ParamTitle = string.Format("{0}", sPListItem[Helper.ListData.Fields.Title]);
                                                    if (flag)
                                                    {
                                                        itemsFilesOnPage.ParamGroup = string.Format("{0}", sPListItem[Helper.ListData.Fields.Group]);
                                                    }
                                                }
                                            }
                                        }
                                        if (flag)
                                        {
                                            string a = "&&&&&&&&&&";
                                            int num3 = 11111;
                                            ControlDVGroup controlDVGroup = null;
                                            foreach (KeyValuePair current in dictionary)
                                            {
                                                if (a != current.Value.ParamGroup)
                                                {
                                                    controlDVGroup = (ControlDVGroup)this.Page.LoadControl("~/_CONTROLTEMPLATES/Softline.DocumentView/ControlDVGroup.ascx");
                                                    controlDVGroup.ParamTitle = current.Value.ParamGroup;
                                                    if (string.IsNullOrEmpty(controlDVGroup.ParamTitle))
                                                    {
                                                        controlDVGroup.ParamTitle = Helper.TitleNotGrouping;
                                                    }
                                                    controlDVGroup.ParamNumberGroup = num3.ToString();
                                                    this.Panel1.Controls.Add(controlDVGroup);
                                                    num3++;
                                                    a = current.Value.ParamGroup;
                                                }
                                                if (controlDVGroup != null)
                                                {
                                                    ControlDVItem controlDVItem = (ControlDVItem)this.Page.LoadControl("~/_CONTROLTEMPLATES/Softline.DocumentView/ControlDVItem.ascx");
                                                    controlDVItem.ParamTitle = current.Value.ParamTitle;
                                                    controlDVItem.ParamURL = current.Value.ParamURL;
                                                    controlDVItem.ParamDescription = current.Value.ParamDescription;
                                                    controlDVGroup.PanelControls.Controls.Add(controlDVItem);
                                                }
                                            }
                                            if (num3 > 11111)
                                            {
                                                this.Panel1.Controls.Add(new LiteralControl(""));
                                            }
                                        }
                                        else
                                        {
                                            foreach (KeyValuePair current in dictionary)
                                            {
                                                ControlDVItem controlDVItem = (ControlDVItem)this.Page.LoadControl("~/_CONTROLTEMPLATES/Softline.DocumentView/ControlDVItem.ascx");
                                                controlDVItem.ParamDescription = current.Value.ParamDescription;
                                                controlDVItem.ParamTitle = current.Key;
                                                controlDVItem.ParamURL = current.Value.ParamURL;
                                                this.Panel1.Controls.Add(controlDVItem);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        Dictionary dictionary = new Dictionary();
                                        foreach (SPListItem sPListItem in list)
                                        {
                                            if (sPListItem.Folder == null)
                                            {
                                                int num2 = 0;
                                                try
                                                {
                                                    if (sPListItem.Fields.ContainsField(Helper.ListData.Fields.FileSize))
                                                    {
                                                        int.TryParse(sPListItem[Helper.ListData.Fields.FileSize].ToString(), out num2);
                                                    }
                                                    else
                                                    {
                                                        int.TryParse(sPListItem[Helper.ListData.Fields.FileSizeEn].ToString(), out num2);
                                                    }
                                                }
                                                catch (Exception ex2)
                                                {
                                                    Logger.Write(ex2);
                                                }
                                                string key = string.Format("{1}{0}", this.FileURL(sPListItem), sPListItem[Helper.ListData.Fields.Title]);
                                                if (flag)
                                                {
                                                    key = string.Format("{2}{0}{1}", sPListItem[Helper.ListData.Fields.Title], this.FileURL(sPListItem), sPListItem[Helper.ListData.Fields.Group]);
                                                }
                                                if (!dictionary.ContainsKey(key))
                                                {
                                                    ItemsFilesOnPage itemsFilesOnPage = new ItemsFilesOnPage();
                                                    itemsFilesOnPage.ParamDescription = string.Format("{0}", sPListItem[Helper.ListData.Fields.Description]);
                                                    List list2 = new List();
                                                    if (sPListItem.Fields.ContainsField(Helper.ListData.Fields.FileType))
                                                    {
                                                        list2.Add(new FileURL(this.FileURL(sPListItem), string.Format("{0}", sPListItem[Helper.ListData.Fields.FileType]), (float)num2 / 1024f));
                                                    }
                                                    else
                                                    {
                                                        list2.Add(new FileURL(this.FileURL(sPListItem), string.Format("{0}", sPListItem[Helper.ListData.Fields.FileTypeEn]), (float)num2 / 1024f));
                                                    }
                                                    itemsFilesOnPage.ParamURL = list2.ToArray();
                                                    if (this.ShowDate)
                                                    {
                                                        itemsFilesOnPage.ParamDate = string.Format("{0:dd.MM.yyyy}", sPListItem[Helper.ListData.Fields.ViewDate]);
                                                    }
                                                    itemsFilesOnPage.ParamTitle = string.Format("{0}", sPListItem[Helper.ListData.Fields.Title]);
                                                    dictionary.Add(key, itemsFilesOnPage);
                                                    if (flag)
                                                    {
                                                        itemsFilesOnPage.ParamGroup = string.Format("{0}", sPListItem[Helper.ListData.Fields.Group]);
                                                    }
                                                }
                                                else
                                                {
                                                    ItemsFilesOnPage itemsFilesOnPage = dictionary[key];
                                                    itemsFilesOnPage.ParamDescription = string.Format("{0}", sPListItem[Helper.ListData.Fields.Description]);
                                                    List list2 = new List();
                                                    if (sPListItem.Fields.ContainsField(Helper.ListData.Fields.FileType))
                                                    {
                                                        list2.Add(new FileURL(this.FileURL(sPListItem), string.Format("{0}", sPListItem[Helper.ListData.Fields.FileType]), (float)num2 / 1024f));
                                                    }
                                                    else
                                                    {
                                                        list2.Add(new FileURL(this.FileURL(sPListItem), string.Format("{0}", sPListItem[Helper.ListData.Fields.FileTypeEn]), (float)num2 / 1024f));
                                                    }
                                                    itemsFilesOnPage.ParamURL = list2.ToArray();
                                                    if (this.ShowDate)
                                                    {
                                                        itemsFilesOnPage.ParamDate = string.Format("{0:dd.MM.yyyy}", sPListItem[Helper.ListData.Fields.ViewDate]);
                                                    }
                                                    itemsFilesOnPage.ParamTitle = string.Format("{0}", sPListItem[Helper.ListData.Fields.Title]);
                                                    if (flag)
                                                    {
                                                        itemsFilesOnPage.ParamGroup = string.Format("{0}", sPListItem[Helper.ListData.Fields.Group]);
                                                    }
                                                }
                                            }
                                        }
                                        string a = "&&&&&&&&&&";
                                        if (flag)
                                        {
                                            int num3 = 11111;
                                            ControlDVGroup controlDVGroup = null;
                                            foreach (KeyValuePair current in dictionary)
                                            {
                                                if (a != current.Value.ParamGroup)
                                                {
                                                    controlDVGroup = (ControlDVGroup)this.Page.LoadControl("~/_CONTROLTEMPLATES/Softline.DocumentView/ControlDVGroup.ascx");
                                                    controlDVGroup.ParamTitle = current.Value.ParamGroup;
                                                    if (string.IsNullOrEmpty(controlDVGroup.ParamTitle))
                                                    {
                                                        controlDVGroup.ParamTitle = Helper.TitleNotGrouping;
                                                    }
                                                    controlDVGroup.ParamNumberGroup = num3.ToString();
                                                    this.Panel1.Controls.Add(controlDVGroup);
                                                    num3++;
                                                    a = current.Value.ParamGroup;
                                                }
                                                if (controlDVGroup != null)
                                                {
                                                    ControlDVItemSimple controlDVItemSimple = (ControlDVItemSimple)this.Page.LoadControl("~/_CONTROLTEMPLATES/Softline.DocumentView/ControlDVItemSimple.ascx");
                                                    if (!this.ShowDate)
                                                    {
                                                        controlDVItemSimple.ParamTitle = current.Value.ParamTitle;
                                                    }
                                                    else
                                                    {
                                                        controlDVItemSimple.ParamTitle = string.Format(" {0} {1}", current.Value.ParamDate, current.Value.ParamTitle);
                                                    }
                                                    controlDVItemSimple.ParamURL = current.Value.ParamURL[0];
                                                    controlDVGroup.PanelControls.Controls.Add(controlDVItemSimple);
                                                }
                                            }
                                            if (num3 > 11111)
                                            {
                                                this.Panel1.Controls.Add(new LiteralControl(""));
                                            }
                                        }
                                        else
                                        {
                                            foreach (KeyValuePair current in dictionary)
                                            {
                                                ControlDVItemSimple controlDVItemSimple = (ControlDVItemSimple)this.Page.LoadControl("~/_CONTROLTEMPLATES/Softline.DocumentView/ControlDVItemSimple.ascx");
                                                controlDVItemSimple.ParamDescription = current.Value.ParamDescription;
                                                if (!this.ShowDate)
                                                {
                                                    controlDVItemSimple.ParamTitle = current.Value.ParamTitle;
                                                }
                                                else
                                                {
                                                    controlDVItemSimple.ParamTitle = string.Format(" {0} {1}", current.Value.ParamDate, current.Value.ParamTitle);
                                                }
                                                controlDVItemSimple.ParamURL = current.Value.ParamURL[0];
                                                this.Panel1.Controls.Add(controlDVItemSimple);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
            }
        }

0 коммент.:

Отправить комментарий