Monday, October 30, 2006

Reading/Writing Data from a Text File: VFP and DotNet

(note: the title of this post is based on what I started to write about which is comparing functions between VFP and DotNet but then went off onto a tangent about code readability)

While I do work in Visual Studio and DotNet a fair bit, my main development environment is Visual FoxPro. One of the things that I always remember from past Devcons was when Microsoft was really trying hard to rally the FoxPro-faithful to use DotNet and ADO.Net, and even at the first DevTeach when there were a fair number of FoxPro developers, (before it turned into a Fox-less event), was the justification for the way most things were objects in DotNet and how that made it better than the single function approach in VFP.

It always struck me as a "this is the right way to do it - so switch to using this approach, regardless of ease of use" type of argument. I know it wasn't intended that way - but that's the impression I always got. And I got it again recently.

Rhonda Tipton explains here how to use the StreamReader class in .Net to read in a simple text file. Just in case there may have been an easier way, a similar approach was noted here and here.

Since I've been doing work on the Code Analyst, I'm always trying to find ways of reducing the number of lines of code in my programs and it just strikes me as crazy that the latest version of a tool actually makes your code longer than it needs to be.

For example, to write to a file using C#, the code displayed was:

StreamWriter sw = new StreamWriter("
string line = "Don't tell anyone!!!";

(and this is without the various Implement statements)

compared with:

STRTOFILE("Don't tell anyone!!!","C:\NewCompanySecret.txt")

in Visual FoxPro.

Reading code:
StreamReader sr = new StreamReader("
string line = sr.ReadLine();
while (line != null)
line = sr.ReadLine();


Compared with:

lc = FILETOSTR("C:\companysecret.txt")

or better yet, my favorite:


(which makes it easier to find specific lines and make changes)

I do like the idea of having a string and doing SUBSTRINGs in them:
line.Substring(0, 21).Trim()

But it kind of reminds me of Word automation (which I also had to do recently), where you have to deal with Selection.Range.text instead of just saying ActiveDocument.text, etc.

I'm not saying it's harder or VFP is better as the functions do the same thing but it reminds me of someone who purposely talks in a strange dialect just to make it seem like they are superior, or maybe just of the Emperor's new clothes.

There are many cases (especially in frameworks) where you have to make things more complex. For example, you create a data manager class to open up files and handle it with error handling:


as opposed to simply saying


So it certainly is a learning curve thing.

But I also think it's useful to look at your tools and code based on a few basic goals (not necessarily in this order):

a) readability
b) maintainability
c) functionality

Readability can be, in my mind, very subjective. ToString() certainly seems far more readable than CStr() (VB) or STR( )(VFP). Likewise a user-defined method named GetAccess() seems more usable than p0acc().

But if you have to read 25 lines of code to understand a procedure or if you have to read 5 lines, which would you prefer?

And I also would say that maintainability also comes from a code's readability factor.

As part of the Code Analyst VFPX project, I'm really interested in finding ways of making suggestions to other developers on how to make their code more readable and more maintainable. Some of the default rules can be seen here.

So join the discussion (or take the poll) and let me know - how maintainable is your code?


Garrett said...

Andrew, just to be persnickety, I'd like to point out that FILETOSTR only works up to a point. If you need to read in a file of over a certain size, you need the LLFFs, which are about as wordy as the StreamReader.

(I hit this at work a while back -- I needed to append some text to the beginning of a file. FILETOSTR worked fine for the files I was testing with, but as soon as someone else tried using my code, boom.)

Andrew MacNeill said...

Very true, Garrett - but in those cases, I would almost recommend creating a cursor or something along those lines to import the file into a table for faster processing.

Anonymous said...

Also to point out that in VB, you can type:

My.Computer.FileSystem.WriteAllText("C:\temp.txt", "Hello there")

Totally agree on the importance of legibility vs. the one true way.


Anonymous said...

Ooohh, I forgot about the My namespace stuff. I gotta remember to look at that while writing some of my new VB code. Thanks yag!