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

Programmers vs. Developers vs. Architects

I received an email this morning from Brandon Savage's newsletter. Brandon's a PHP guru (works at Mozilla) but his newsletter and books have some great overall perspectives for developers of all languages. However, this last one (What's the difference between developers and architects?) kind of rubs me the wrong way. Either that, or I've just missed the natural inflation of job descriptions. (maybe, it's like the change in terminology between Garbage man and Waste Engineer or Secretary and Office Administrator)

So maybe it's just me - but I think there's still a big difference between Programmer, Developer and then of course, architect. The key thing here is that every role has a different perspective and every one of those perspectives has value. The original MSF create roles like Product Manager, Program Manager, Developer, Tester, etc - so every concept may pigeon hole people into different roles. But the statements Brandon makes are often distinctions I…

Security in Windows 10

http://www.slate.com/articles/technology/bitwise/2015/08/windows_10_privacy_problems_here_s_how_bad_they_are_and_how_to_plug_them.single.html

 discusses some Windows 10 privacy settings and their implications.

"Finally, we will access, disclose and preserve personal data, including your content (such as the content of your emails, other private communications or files in private folders), when we have a good faith belief that doing so is necessary." "In other words, Microsoft won't treat your local data with any more privacy than it treats your data on its servers and may upload your local data to its servers arbitrarily"
I did a quick install on a VM choosing the Express settings. When I fully deploy this on a real workstation, I will likely choose to wade through all of the individual pages, as David recommends.

Of course, losing one's privacy is nothing new - it's happening all over the place (despite Santa Ana's police force's lawsu…

AppleSoft

I'm not TRYING to be "fanboy-flame bait" but what I saw yesterday was a typical "Do it this way, now do it this way and then we'll go back to this way" all over again.... a move similar to what Microsoft does to developers on an ongoing basis.

Remember the first iPhone? Smooth and curved, at least as far as it could be back then. I still pull out my 3G and can see the curves on it.

Then the 4 came out and "boxy" was all the rage. Everything should be "tight with corners"

Now iPhone 6.... smooth and curvy is back. Granted I don't have the actual device yet, but that's the message.

Guess that means the iPhone 8 will be back to boxy.

And honestly, Apple Watch is not worth "one more thing" --- especially when everyone knows it's going to be shown. "One more thing" would be something no one saw coming.  The device itself ? Very interesting and yes, definitely lots of potential but "one more thing" wor…