日期:2014-05-17  浏览次数:20466 次

我这段代码 还可以如何优化?真心请教
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.Repl