3月1日
These examples show two kinds of the same code that speak for itself:
Figure 1
static class Program
{
public static string[] Args = null;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
if (Properties.Settings.Default.DebugLog)
{
/// Copy all Trace and Debug reports to columnAttrib file:
///
string file = Properties.Settings.Default.DebugFile;
FileStream stream = new FileStream(file, FileMode.Create);
stream.Close();
stream.Dispose();
TextWriterTraceListener listener = new TextWriterTraceListener(file);
Trace.Listeners.Add(listener);
Trace.AutoFlush = true;
Debug.Listeners.Add(listener);
Debug.AutoFlush = true;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
foreach (string s in args)
{
if (s.StartsWith("-locale:", StringComparison.OrdinalIgnoreCase))
{
string culture = s.Remove(0, 8);
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(culture);
}
}
Args = args;
AppLauncher launcher = new AppLauncher();
launcher.Run(args);
}
}
Figure 2
static class Program
{
public static string[] Args = null;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
PreprareToLogDebugMessages();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
SetCultureFromParams(args);
Args = args;
AppLauncher launcher = new AppLauncher();
launcher.Run(args);
}
/// <summary>
/// If Specified in the Properites Settings, all Debug and Trace outputs are
/// redirected to a file.
/// </summary>
private static void PreprareToLogDebugMessages()
{
if (Properties.Settings.Default.DebugLog)
{
/// Copy all Trace and Debug reports to columnAttrib file:
///
string file = Properties.Settings.Default.DebugFile;
FileStream stream = new FileStream(file, FileMode.Create);
stream.Close();
stream.Dispose();
TextWriterTraceListener listener = new TextWriterTraceListener(file);
Trace.Listeners.Add(listener);
Trace.AutoFlush = true;
Debug.Listeners.Add(listener);
Debug.AutoFlush = true;
}
}
/// <summary>
/// Sets the culture for the application if specified by the args.
/// </summary>
/// <param name="args"></param>
private static void SetCultureFromParams(string[] args)
{
foreach (string s in args)
{
if (s.StartsWith("-locale:", StringComparison.OrdinalIgnoreCase))
{
string culture = s.Remove(0, 8);
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(culture);
}
}
}
}
As you see in these code snippets, figure 1 looks very complicated because it's one long function that do various sub parts.
In contrast figure 2, although it contains more individual functions, it's easier to understand the meaning of the code, and as a side effect, as logical function parts are separated to a method, you can create an xml header and describe the functionality of the sub part which can be used later for automatic documentating.