Archive for March, 2007|Monthly archive page

Coding vs Programming

Coding is a mere typing exercise to express the logical sequence of steps in the form of a well indented paragraph. It is specific to syntax and the language.

Programming is problem solving exercise, which forces you think continuously. Its a two step process.

First and the most crucial step is to “Define the Problem Statement”. This is the most important and difficult step as all the use cases which needs to be satisfied must be clearly identified. The accuracy of the solution will depend on this step. This doesn’t depend on how you express. It could be a flow chart or diagrams depicting the problem statement.

Second step is to provide a logical sequence of the steps. The elegance, re-usability and scalability of the solution will depend on this step, as domain/technology specific knowledge is essential.

After some experience in using the msdn, I am confident that any software technology can be easily learnt by spending some time with a well documented API.

Hence a solid first step, is essential for a rock solid solution.

Count your LOC!!

How many Lines of Code(LOC), have you produced? For those wondering, how many lines of code have they generated, here is a sample code in C#.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace LOC
{
class Loc
{
private const string fileExtension = ".cs";

private static void Main(string[] args)
{
string path = "";
if (args.Length == 0)
{
//PrintUsageAndExit();
Console.WriteLine("Enter the path of the root folder containing the source code" + Environment.NewLine);
path = Console.ReadLine();
}
else
{
path = args[0];
}

if (string.IsNullOrEmpty(path) || !Directory.Exists(path))
{
PrintUsageAndExit();
}

DirectoryInfo parent = new DirectoryInfo(path);

int lines = CountLines(parent);
Console.WriteLine("The Total Number of Lines is :" + lines);
Console.ReadLine();
}

private static void PrintUsageAndExit()
{
Console.WriteLine("Expecting the path of a Directory containing Files or Sub Folders with file extension as " + fileExtension);

Console.WriteLine(Environment.NewLine + "Press any key to exit...");
Console.ReadLine();
Environment.Exit(1);
}

private static int CountLines(DirectoryInfo dir)
{
int lines = 0;
//process all the files in the directory
foreach (FileInfo f in dir.GetFiles())
{
//process only the files with the required file extension
if (f.Name.EndsWith(fileExtension))
{
lines += CountLines(f);
}

}

//process all the sub directories in the current directory
DirectoryInfo[] children = dir.GetDirectories();
foreach (DirectoryInfo d in children)
{
lines += CountLines(d);
}
return lines;
}

private static int CountLines(FileInfo file)
{
StreamReader reader = null;
int lines = 0;
try
{
reader = new StreamReader(file.FullName);

while (!reader.EndOfStream)
{
string line = reader.ReadLine().Trim();

//ignore blank lines and comments...TODO: Also ignore commented code with in /* ... */
if (string.IsNullOrEmpty(line) || line.StartsWith("//"))
{
continue;
}
lines++;
}
}
catch { }
finally
{
if (reader != null)
{ reader.Close(); }
}
return lines;
}
}
}