Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Style vs Idiot-Proofing 12

Status
Not open for further replies.

CajunCenturion

Programmer
Mar 4, 2002
11,381
0
0
US
A while back, we had a thread dedicated to Idiot Proofing Programs. This thread seemed to run down two paths. The first path was to essentially get a better class of idiots. Amusing and entertaining, this unfortunately also has some truth behind it. The second path dealt with getting accurate functional requirements up front in writing, and so forth. Excellent points were made, but this isn’t really the job of the programmer – it’s the job of the analyst. I am aware that most, if not all of us, wear both hats (analyst and programmer) from time to time, but what the previous thread lacked was a path that dealt with how programmers can write better programs. Assuming that the analyst has done his/her job, and that we have been provided a good, complete, and accurate functional requirements specification, I now pose the following paraphrased question:

How do we write an Idiot-Proof Program that meets those specifications?

Obviously our logic must be correct, and we need to have error handling to deal with unexpected inputs, but does our choice of control structures (Do While vs For Each) make a difference?

Found = False
For Each Item in Items
If Item.Property = “What We Want” Then
Found = True
Exit For
End If
Next

Found = False
Index = 1
Do While (Index <= Items.Count and Not Found)
If Items(Index).Property = “What We Want” Then
Found = True
Else
Index = Index + 1
End If
Loop

Does the complexity of the code make a difference – not just for the sake of performance, but for the sake of program testing and validation? From a purely Software Engineering standpoint, the For Each with Exit For statement is more ‘complex’ because the Exit For is an additional branch that must be dealt with during program testing and validation. It’s another path thru the code that must be taken into account. Or is this simply a matter of style? This is a very simple case, of course, and if your answer is different because it’s a simple case, then at what point does the case not become simple enough to warrant legitimate consideration of alternatives in light of program complexity?

Does having functions and procedures have single entry and exit points affect program complexity, thus in turn leading to easier or more difficult programs to write, test, and/or maintain?

What other programming “guidelines” (indendation, variable nameing, modularity, single-function procedures, program routes, modularity, object oriented, <add your own list>, etc, etc) have their roots in providing mechanism by which we as programmers can write better, (or more idiot-proof) programs? What “guidelines” matter to you?

All opinions and comments welcome. There really are no wrong answers here.
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein


 
Michael - both loops will perform the same number of iterations on the collection and terminate at the same time. The Found Flag achieves this for the Do Loop.

And Please note that in Craig's code, a length check is performed at the end of the routine. Because of the early Exit Function in the case of a Carriage Return, that length check is not being performed. If, for example, a text box were populated using a Cut and Paste, followed by a Return, the length check would not be performed, possibly allowing for string entry being too long. (Knowing Craig, I'm sure he has already taken this account and dealt with it)

But this is an example of the thrust behind the questions - Do our choices with respect to programming style, allow us to write code which inherently make it more difficult to write/maintain 'idiot-proof' code?

Again, thanks to everyone who has taken the time to share their thoughts with us.
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein


 
Ok all, ponder this........

How often are we torn between finding &quot;a way&quot; to do something and finding the &quot;best way&quot; to do it? And, how much time should be dedicatd to looking for the &quot;best way&quot;. I know that I may wade through &quot;help&quot; VB manuals for, what seems like, hours to find that special function only to give up and write my own. Then later I'll be looking for something that seems unrelated and will come upon THE answer I needed earlier. So, if you're looking at my code and are knowledgable about that special function you may say &quot;why did that fool do it this way?&quot; Moreover you may tend to say that I &quot;wasted time&quot; when in fact I probably saved time in that I got the job done &quot;on time&quot;.
When you can't find that excepted way of doing something within the structure/methodology that you may be following, you have to color outside the lines.

I don't have an answer to this. I think that, in general, we all do the best we can with the tools we're given. Forums like this help us all understand issues of code reusability, etc. But, frankly, you almost have to do things the hard way and then had to live with the consequences before you can really understand. Hopefully you are the person that has to live with the problem (not always the case, though).

For example, I've seen more than one comment/warning regarding the use of global variables. When I first started programming I thought globals were great. Then I had to live with the error in my judgement. Chasing problems in my code was awful. Now I only use globals when there is absolutely no reasonable alternative. I had heard that globals weren't good. I just didn't understand....


vbvictim
 
Hi

Plenty of comments there ... I see a lot of:

&quot;I prefer to do it this way&quot; etc.

Isn't an important point, that most people seem to have missed, the fact that the style of coding you should create should entirely depend on the requirements i.e. &quot;what sort of thing am I writing here?&quot;

For instance certain scenarios will call for speed over stability or Size whereas others will be &quot;Mission Critical&quot;

I try to be an &quot;Adaptive Programmer&quot; in that I can adopt different styles depending on my objectives.
e.g. Would you set a ton of Globals when a class object could be passed if memory was a concern.
Give a man a program and tomorrow he will be hungry.
Teach a man to program and he will never hunger again.
--Sunr¿se

 
Excellent point regarding &quot;mission critical&quot; vs not. Requirements and related schedules dictated to programmers definitely need to be driven by whether or not the app is mission critical. This should also influence skill levels of programmers placed on the project.

If you want it real bad. That is how you'll get it. Hope it isn't mission critical.

vbvictim
 
Maybe the cardinal rule in programming style is &quot;Be
consistent!&quot;
 
THOMASNG, I'll vote for you ;-)

Especially when you work in a team it is very important to set a set or rules concerning programming style i.e. object sturctures, calls, coding style etc.
I currently work at a company that has sources that actually do sort of the same, but when you look at the sources, they have been programmed in different ways. This only causes confusion especially for newcomers.

Like in the industry: standardisation is very important to make production easier and most of the time quicker.

Quick and Dirty....the dirty remains and the quick fades away quickly.

Cheers,
Weedz (Edward W.F. Veld)
My private project:Download the CrownBase source code !!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top