首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

小弟我这段代码 还可以怎么优化?真心请问

2012-11-07 
我这段代码 还可以如何优化?真心请教C# code/// summary/// PDFToSWF/// /summary/// param nameup

我这段代码 还可以如何优化?真心请教

C# code
/// <summary>        /// PDFToSWF        /// </summary>        /// <param name="uploadFilepath"></param>        /// <param name="file"></param>        public static void AsConvertFile(string uploadFilepath, string file)        {            if (uploadFilepath == null || string.IsNullOrEmpty(AsFileHelper.AsCheckFileType(uploadFilepath)))            {                return;            }            else            {                if (uploadFilepath.EndsWith(".pdf"))//如果上传的文件是PDF格式                {                    string swfpath = uploadFilepath.Replace(".pdf", ".swf");                    if (AsConvertHelper.AsConvertToSwf(uploadFilepath, swfpath))                    {                        GetResponseUrl(Path.GetFileName(swfpath));                    }                }                else                {                    string uploadFile = AsFileHelper.AsCheckFileType(uploadFilepath);//如果上传的文件不是DPF格式的文件                    file = uploadFilepath.Replace(uploadFile, ".pdf");                    if (!File.Exists(file))                    {                        string PDFFolder = file.Replace("UploadFile", "PdfFile");//PDF文件保存的文件夹路径                        bool isconvert = AsConvertHelper.AsConvertToPDF(uploadFilepath, PDFFolder);                        if (isconvert)                        {                            string swfpath = PDFFolder.Replace(".pdf", ".swf");                            if (AsConvertHelper.AsConvertToSwf(PDFFolder, swfpath))                            {                                GetResponseUrl(Path.GetFileName(swfpath));                            }                        }                    }                }            }        }


我自己重构了一下 到了这一段优化不下去了 能力有限哈。嘿。各位看看

推荐一点代码重构的书籍给我。谢谢!

[解决办法]
1.首先,只要通过测试,代码就不需要修改了;
2.对以前的代码的总结和改进有利于以后的工作中用更高效的编程手段通过测试,应当受到鼓励;
3.大概提2点我的建议,仅供参考
1)对参数的验证,可以采用断言的形式,比如:
C# code
if (uploadFilepath == null     || string.IsNullOrEmpty(    AsFileHelper.AsCheckFileType(uploadFilepath))){    return;    }替换如下:MyHelper.Assert(uploadFilepath!=null,new ArgumentNullException("errMsg1"));string _s=AsFileHelper.AsCheckFileType(uploadFilepath);MyHelper.Assert(!string.IsNullOrEmpty(_s),new ArgumentException("errMsg2"));
[解决办法]
C# code
        #region  private static String ConvertToPDF(String uploadFilePath) //转为PDF文件        //-----------------------------------------------------        /// <summary>        /// 转为PDF文件        /// </summary>        /// <param name="uploadFilePath"></param>        /// <returns></returns>        private static String ConvertToPDF(String uploadFilePath)        {            String strPDFPath = String.Empty;            if (AsConvertHelper.AsConvertPDF(uploadFilePath, strPDFPath))            {                return strPDFPath;            }            return String.Empty;        }        //-----------------------------------------------------        #endregion        #region  private static String ConvertToSwf(String uploadFilePath) //转为SWF文件        //-----------------------------------------------------        /// <summary>        /// 转为SWF文件        /// </summary>        /// <param name="uploadFilePath"></param>        /// <returns></returns>        private static String ConvertToSwf(String uploadFilePath)        {            String strSwfPath = uploadFilePath.Replace(".pdf", ".swf");            if (AsConvertHelper.AsConvertToSwf(uploadFilePath, strSwfPath))            {                return strSwfPath;            }            return String.Empty;        }        //-----------------------------------------------------        #endregion        #region  public static void AsConvertFile(String uploadFilePath) //PDFToSWF        //-----------------------------------------------------        /// <summary>        /// PDFToSWF        /// </summary>        /// <param name="uploadFilepath"></param>        public static void AsConvertFile(String uploadFilePath)        {            if (String.IsNullOrEmpty(uploadFilePath) || String.IsNullOrEmpty(AsFileHelper.AsCheckFileType(uploadFilePath)))            {                return;            }            String strSwfPath = String.Empty;            // 如果上传的文件是PDF格式            if (uploadFilePath.EndsWith(".pdf"))            {                strSwfPath = ConvertToSwf(uploadFilePath);            }            else            {                String strPDF = ConvertToPDF(uploadFilePath);                if (!File.Exists(strPDF))                {                    strSwfPath = ConvertToSwf(strPDF);                }            }            if (!File.Exists(strSwfPath))            {                GetResponseUrl(Path.GetFileName(strSwfPath));            }        }        //-----------------------------------------------------        #endregion 


[解决办法]
分析下你的逻辑哈。。
1.是.pdf 文件 就转成.swf ..... 
2.不是的话 先要变成.pdf 再转成.swf 

这思路不知道对不对..
如果对的话,你有些代码就是重复了..
至少这个可以重用
if (AsConvertHelper.AsConvertToSwf(PDFFolder, swfpath))
{
GetResponseUrl(Path.GetFileName(swfpath));
}
其次你不管怎样都是要转成.swf 
,所以这里完全可以独立出来

热点排行