31 August, 2007 | Written by Vincent Comments Off

Code responsibly or your work can haunt you

Have you ever written code that was below your average quality? I have, and sometimes I wish I could have write it better then.

There had been times when I came across some of my older work, and I’ll be tempted to rewrite or refactor or rework the code or logic. Then I told myself that I did the best I could then, and I should let it go. Focusing my energy on future projects is a better use.

That said, sometimes your creations can come back and haunt you. Like in this animator versus animation video.

30 August, 2007 | Written by Vincent Comments Off

A void in humanity

With the advent of more powerful communication tools enabled by technology, will the congregation of social groups suddenly create voids in our humanity? Let’s time travel to the past for a while.

I remember a time when connecting with family and friends was done either face to face or using the phone (the landed one). Then people around me started carrying these things called mobile phones, and it was considered hip to actually own one. I was oblivious to this accessory, since I haven’t found a need to be constantly contactable.

Then I started studying in a university, and through force of pressure, I was strongly persuaded to get a mobile phone. It was a flip phone, one of the earlier models that I got from a friend.

I also found out about the Internet. The hip thing then was ICQ and MSN Messenger, two instant messaging software available. Through peer pressure, I was again strongly persuaded to sign up, so I could go home and chat with my friends online. The conversation usually degenerated into near-monosyllabic words and arcane short forms like gtg (got to go) and lol (laugh out loud).

When we’re free in between attending lessons or doing homework and research, my friends (and I, you got it, strongly persuaded) logged on to Neopets, and practically bashed on the refresh button on the web browser so we could get our virtual hands on the store items when the Neopets server restocked.

Then people found the Internet to be a fantastic communication medium. Community web sites sprung forth, empowering people to reach beyond to the world. Forums and other online groups became connecting points.

Technology advanced. Better features. Faster upload/download speeds. And Web 2.0 came. Digg, Reddit, Technorati, StumbleUpon, Facebook, MySpace, Flickr, YouTube and other social media sites appeared. And people flocked to them in droves.

We seem to have this innate need to connect with other people. Like this elderly gentlemen whom Darren met while shopping for inspiration. Text and images on web sites aren’t enough anymore. We want to see and hear people through the Internet too. Audio and video are becoming more important. My blogging mentor, Yaro Starak, is also experimenting with more video posts, along with Darren and other bloggers.

Then I read about this article on a hole in the universe. Astronomers found a void in space, and their explanation was that gravity from high density masses are attracting nearby matter. When enough of these attractors came together, a void was formed because of the absence of matter between the attractors.

Will this happen to us? Will our new generation social media groups unwittingly create voids in our humanity, by pulling in masses of people to them?

29 August, 2007 | Written by Vincent 1 Comment

Beginning C# – Reading Input Writing Output

There’s a saying in programming that goes something like this

Be liberal with what you receive. Be strict with what you produce.

What it means is that your program should be lenient with the input it receives, to assume that all sorts of rubbish data can (and will be) fed to your program, and it’s your duty to make sure your program can handle it.

BUT, your program must adhere strictly to the output format it’s supposed to produce. If your program’s supposed to write out integers, it better write out only integers.

It’s not fair, I know. It also makes it easier for programs to talk to each other, since input and output of programs are what they communicate with. The most common communication methods is through file input/output or file IO as they’re usually referred to.

I personally find the StreamReader and StreamWriter classes to be easy to use for file IO. The .NET framework views file IO as a form of data stream. Other classes dealing with data streams include NetworkStream (working with data across networks) and MemoryStream (working with data within computer memory).

So how do we read input and write output? Let’s look at some source code first. [code formatting looks better from web site]

StreamWriter sw;
sw = new StreamWriter("greetings.txt");
sw.WriteLine("Hi!");
sw.WriteLine("Good morning!");
sw.WriteLine("Splendid day isn't it?");
sw.Close();

string[] flowerlist = new string[] { "daffodil", "lily", "orchid", "rose" };
sw = new StreamWriter("flowers.txt");
foreach (string s in flowerlist)
{
    sw.WriteLine(s);
}
sw.Close();

// the second parameter indicates if file should be appended or not.
// If the second parameter is false, it would have overwritten the
// contents from above, and flowers.txt would only have 2 flowers.
sw = new StreamWriter("flowers.txt", true);
sw.WriteLine("sunflower");
sw.WriteLine("tulip");
sw.Close();

File.WriteAllLines("greetings2.txt", new string[] { "Hello!", "How are you doing?" });

// we start reading the files and writing their content here
sw = new StreamWriter("result.txt");
StreamReader sr;

sw.WriteLine("{0}Reading from greetings.txt", "=".PadRight(20, '='));
sr = new StreamReader("greetings.txt");
while (sr.Peek() > -1)
{
   // we read one line and write one line
   sw.WriteLine(sr.ReadLine());
}
sr.Close();
sw.WriteLine("{0}End of greetings.txt", "=".PadRight(20, '='));

sw.WriteLine("{0}Reading from flowers.txt", "=".PadRight(20, '='));
sr = new StreamReader("flowers.txt");
// we use sw.Write() instead of sw.WriteLine() because
// flowers.txt already contained a newline character at the end.
sw.Write(sr.ReadToEnd());
sr.Close();
sw.WriteLine("{0}End of flowers.txt", "=".PadRight(20, '='));

sw.WriteLine("{0}Reading from greetings2.txt", "=".PadRight(20, '='));
sw.WriteLine(File.ReadAllText("greetings2.txt"));
sw.WriteLine("{0}End of greetings2.txt", "=".PadRight(20, '='));

sw.Close();

Console.WriteLine("End of program");
Console.ReadLine();

The code's quite straight forward. I want to highlight a small code section

"=".PadRight(20, '=')

This is a shortcut to generate 20 equal signs. It beats writing a for loop or manually typing in 20 equal signs.

The StreamReader.Peek() function returns the next character but doesn't read it into memory. If there's nothing left to read, like End Of File (EOF), then a -1 is returned. This makes the function a suitable termination condition for a while loop reading in file content.

The StreamReader.ReadLine() function reads in input until it hits a newline character, and returns all input read thus far in a string. The StreamReader.ReadToEnd() function basically dumps the entire file content into a string.

With .NET framework 2.0 comes another way to rapidly and easily read data from files. The File class has been beefed up with some nifty new functions. The following functions allow reading in input from a file with just one line of code

  • File.ReadAllBytes() - returns file content in a byte array
  • File.ReadAllLines() - returns file content in a string array
  • File.ReadAllText() - returns file content in just one string (newlines and all)

Then there's the corresponding one-liners for writing output

  • File.WriteAllBytes()
  • File.WriteAllLines()
  • File.WriteAllText()

These one-liners read/write content to/from a file and closes the file, all in that one line. It's basically a shortcut compressing one line to open a file, one line to read/write file content, and one line to close the file.

As always, you are encouraged to explore the (online) MSDN documentation for more details. Study how to instantiate a class and what are the public properties and functions available.

You don't have to memorise and learn how to use every single function. Just remember what kinds of functions are available. Then when the time comes where you need a particular feature, you'll know where to look.

Download the source code.

Next Page →