Skip to main content

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("
C:/NewCompanySecret.txt");
string line = "Don't tell anyone!!!";
sw.WriteLine(line);
sw.Close();

(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("
C:/CompanySecret.txt");
string line = sr.ReadLine();
while (line != null)
{
System.Console.WriteLine(line);
line = sr.ReadLine();
}

sr.Close();


Compared with:

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

or better yet, my favorite:

lnLines=ALINES(la,FILETOSTR("C:\companysecret.txt"))

(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:

myData.Open("CUSTOMERS")

as opposed to simply saying

USE CUSTOMERS

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?

Comments

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.

yag
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!

Popular posts from this blog

Blogs and RSS come to Microsoft.com

MS has just introduced their portal and it's pretty comprehensive. Nothing quite like learning that some people use AIM instead of MSN messenger, or that there really may be a need for supporting 4 monitors ( Cyrus Complains ) However, it's really a great sign that MS is serious about supporting the blogging community which seems to have um, exploded in size in the past year. Blogs and RSS come to Microsoft.com

FoxInCloud Stats

FoxInCloud sent this link a while back about their statistics regarding visits to their site: http://foxincloud.com/blog/2017/12/27/VFP-community-lessons-from-foxincloud-site.html What's interesting here is the breakdown of people. Yes, I think it's understandable that the Fox community is getting older. Another factor is the growth of the mobile and web environments taking over development. These environments really do push people towards the newer non-SQL or free SQL/hosted environments but more towards hosted storage options like Amazon and Google. A tool like FoxInCloud that helps MOVE existing applications to the cloud inherently competes with those environments. But FoxInCloud also allows developers to extend their application further by giving them a starting point using Javascript and the basic CSS (such as Bootstrap). If you're not rebuilding your application from scratch, it's certainly a great step forward. FoxPro VFP

5 Great Reasons to attend Virtual FoxFest

What's coming up? Virtual FoxFest is coming up soon (sessions start October 14th). Like last year, the conference is entirely virtual yet includes great breakdown rooms and sessions to add that nice one-on-one feel that you get in person. It's also staggered so you can choose which days you want to attend - October 14th, 20th and 26th. This is great if you can't break away for a consecutive three days. But really, I've gone through the sessions and I see five great sessions that I'm eager to check out. 1. A Decade of Thor (Rick Schummer) Thor has been an extension for Visual FoxPro that many developers swear by, yet many don't know even exists. Visual FoxPro's built-in extensions are great but Jim Nelson's Thor supercharges your IDE. I can't believe it's been ten years - so Rick's session should be able to not just whet your appetite but give you all the reasons you should be using it. 2. VFP C++ compiler.  Last year, we saw DotNetX as well