Re: [OT] My First C# (warning - long post)



Pete Dashwood wrote:
Hi Daniel,

Unfortunately, much as I'd like to, I'm just too busy at the moment to analyse this properly.

Fortunately, Andrew is doing so (I had a quick look at his comments and endorse them 100%) and one set of criticisms is probably all you need on a first attempt :-)

I'm a big guy (well, metaphorically). I can take it.

I formed some first impressions from a quick look (before I saw Andrew's comments.... :-)) and they were as follows:

1. It is a procedural translation into an OO environment. Even down to the "Working-storage" at the top of the Class... :-)

Aw man, cut me some slack - show me a COBOL program with less than 15 working-storage variables! :) (The reason I did this that way, especially for the block of private ones, was just so that the memory didn't have to be loaded every time. Can you imagine loading that array of COBOL keywords every time isCobolKeyword is called?)

2. You show an excellent grasp of using functions and, at the nitty-gritty level you have it sussed. (I think you may be over-using Trim()...
"Convert.ToChar(dr["element_type"].ToString().Trim().Substring(0,1))" seems a bit unnecessary to me, when all you are wanting is one byte, but I haven't checked the details...:-)

You'd think that you could cast a substring of one character TO a character, wouldn't you? Yes, the Trim() is probably extra now, but that was my attempt to get it to quit griping at me that I had given it a "String", when it wanted a "char". (I know in Java you can't do a switch on a string - I'm guessing that it's that way in C# as well. If not, then I spent 20 minutes wasting my time... :> Which wouldn't surprise me to find out...)

3. The embedded SQL is just a hangover from procedural code; it is inefficient and ugly. Think in tiers; separate data access from business logic. Treat db connections as precious; don't hold them any longer than you need to. I posted a link previously which outlines the C# approach and it is quite different from the procedural embedded SQL approach. (Since my own stuff is now working, I can also confirm that it is VERY fast...)

See my post to Andrew on this topic - how?

4. I think you are very lucky to have someone like Andrew looking at it. I read his comments and agree almost entirely. (I think 10 lines is a bit too rigid a rule, but the principle is correct.). In OO, as in most online processing, "small is beautiful"... Andrew's point about refactoring is an excellent one and I wouldn't have thought of that as a good way to improve what you have; I've learned from this interchange :-)

I really appreciate him doing it.

I realized today how much of a geek I am. I have Visual C# 2005 Express Edition, Eclipse (for Java 2 1.5), Fujitsu COBOL, and Visual SlickEdit all on one machine. Wow...

5. You deserve credit for posting ANY code here, and to post a first attempt, is brave and admirable. I know you won't take the comments to heart in a negative way.

I won't. Of course, that's also why I posted my list of caveats before the code - there are things about it I don't like, but when the IDE or shop standards enforce "style", it's hard to take any complaints about it personally.

6. You might think about offloading a lot of the literals into external data. C# provides a number of places where you can put stuff... System collections, XML files, configuration data, to name a few. The COBOL Reserved Word list could be an XML file that gets loaded once at startup, rather than being embedded in the program.

I was thinking that there *had* to be a better way to do that. Can you make a static XML that gets "built into" (whatever the C# term for that would be) the .dll file?

For my money, you need to start thinking in terms of a production line. Think about a series of (small) machines (components) that take scrap metal in one end and push knives and forks out the other. Each machine carries out another step in the process, each machine does exactly what it does, and functionality does not overlap. (Think encapsulation and "black boxes"). Some machines provide support functions to machines on the main line, but for each machine, the goals it must achieve are clear, and the primary focus for it, from which it must not be distracted by having to do ancilliary and support functions (like accessing databases). Think of machines in each layer (tier) of the production process. (A component needs to validate, retrieve, or update certain data? It should focus on what it is going to DO with that data and leave the "lower order" functions to other components who deal with that. It can simply activate a "retriever" and know that what comes back is valid and timely or whatever...) Activate the retriever; take what it returns, get on with what this component is really about...:-)

I'd really love to do this... Gotta get the grey matter aligned properly...

Still, you have demonstrated a really good grasp of the language itself, the program works, and it can be made more elegant fairly easily. All in all, pretty good for a first attempt :-)

Thanks. It really is a piece of cake - I've converted three ASPs to ASP.NET/C# pages in 2 days (6 if you count the little ones I'm working on now). It took me a little over a day on the first one, then it just clicked. Of course, it's probably still procedural C#, but it's fast. :)

This DB abstraction layer would be good for our system as a whole. The entire thing will be ASP.NET/C# when we're (er, I guess that should be "they're") done. I'm guessing that, once I get that built, we would just give it a namespace (maybe CSCS.DataUtilities or something like that), then import, er, "using" it in our classes/pages?

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ / \ / ~ Live from Montgomery, AL! ~
~ / \/ o ~ ~
~ / /\ - | ~ daniel@thebelowdomain ~
~ _____ / \ | ~ http://www.djs-consulting.com ~
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
~ GEEKCODE 3.12 GCS/IT d s-:+ a C++ L++ E--- W++ N++ o? K- w$ ~
~ !O M-- V PS+ PE++ Y? !PGP t+ 5? X+ R* tv b+ DI++ D+ G- e ~
~ h---- r+++ z++++ ~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

"Who is more irrational? A man who believes in a God he doesn't see, or a man who's offended by a God he doesn't believe in?" - Brad Stine
.