Welcome To The Home Of The Visual FoxPro Experts  
home. signup. forum. archives. search. google. articles. downloads. faq. members. weblogs. sponsors. rss.
 From: Marcia Akins
  Where is Marcia Akins?
 Akorn
 Ohio - United States
 Marcia Akins
 To: Dan Baker
  Where is Dan Baker?
 Effort
 Pennsylvania - United States
 Dan Baker
 Tags
Subject: RE: Best Coding Practices for Newbies
Thread ID: 160033 Message ID: 160043 # Views: 209 # Ratings: 7
Version: Visual FoxPro 8 Category: General VFP Topics
Date: Monday, February 11, 2008 10:37:35 PM         
   


So I direct my query to you VFP consultants in Foxite land: What are the common coding practices that newbies make that I should avoid to make your consulting life easier?

Look for Patterns

Much has been said, elsewhere and by better qualified people than me, about Patterns. For the moment let me leave it by saying that Design Patterns can be just as relevant in the context of designing classes as they are to application design. When designing your own classes it is important to look for patterns in your requirements so that your classes themselves can reflect the patterns that you will use when they are deployed. Probably the best reference for getting to grips with Design Patterns is: “Design Patterns – Elements of Reusable Object Oriented Software” by Gamma, Helm, Johnson and Vlissides (Published by Addison Wesley, ISBN 0-201-63361-2)

Code in Methods, not Events
When coding your classes avoid placing code directly in native Visual FoxPro events. Instead, create custom methods and call those from your events. There is, admittedly, no requirement to do things this way, but it will make your life easier for (yet again) three reasons.
• It allows you to give meaningful names to your method code. This may sound trivial, but it really does make maintenance easier when code that produces output is called by ‘This.GenerateOutput()’ rather than ‘Thisform.Pageframe1.Page1.CmdButton1.Click()’
• It allows you change the interface, if necessary, by simply re-locating the single line of code which calls the method instead of having to cut and paste functional code.
• It allows you to break complex code up into multiple, (and potentially re-usable) methods. The Event method then acts merely as the initiator and controller for calling the associated code.

Beware of putting functionality too high in the class hierarchy
This is a trap that I have fallen into many times over the years and still occasionally stub my toe on. You will generally know when you have placed functionality too high because you will spend a lot of time in you subclasses trying to over-ride all the great functionality that you put in the root class. Let me give you an example of how not to do it from bitter personal experience.

I once had a method called SetInputMask() as a method in my root textbox class. It seemed to me that I wanted all my text boxes to have such functionality. This method, called from the text box’s Init(), looked at the ControlSource of the text box and used it to construct an InputMask for the control if one was not specified. It seemed like a good idea at the time because it would eliminate numeric overflow errors that would occur when an InputMask was not supplied for bound numeric fields. It would also stop endless support calls from my end users asking me why, when they typed “Supercalifragilisticexpialidotious” into a text box on the form, all that got saved was “Supercalifragilistic”.

So what was wrong with this idea? The answer is that this behavior did not belong in the root class text box. It was too high in the hierarchy. You can just imagine what happened the first time I dropped one of my custom textboxes into a grid column whose ControlSource was:

 ( IIF( MyRecordSource.MyField, ‘Yes’, ‘No’ ) )


The form blew up! It would have been a simple matter to set an InputMask for the column and its contained text box, but why should I have to? Each time a form with a grid blew up and I had to go back and determine the reason why and then set an InputMask cost several valuable minutes of my time. A much better idea would have been to have SetInputMask as a custom method of a subclass of my root class text box.

Had I applied the “Must, Could, Should” rule to this situation, it never would have happened. Placing code too high in the class hierarchy is probably the most frequent trap that we all, as developers, fall into. At least I know that it has been the bane of my existence.

Methods should do one thing and one thing only
Methods that do “everything” are difficult to reuse. Remember this: “a single line of code is always reusable”. If I have to hit PAGE DOWN more than once to view my method code, the method is probably too long and trying to do too many things. The solution is to break the monolithic method up into smaller, better focused, methods and use a control method to call them individually.

Use template methods for a consistent interface
I stated earlier that we need to avoid placing functionality too high in the hierarchy. However, this doesn’t mean that we shouldn’t define template methods in our root class. These empty methods at the highest level of the hierarchy ensure that all subclasses share the same interface. As long as sub-classes share a common interface, they are interchangeable.
Defining a common interface for subclasses by using template methods in the root class will not hurt anything. Remember, all Visual FoxPro methods return .T. even when they contain no code. Defining the entire interface in the root class makes it much easier to program to interface rather than implementation.

Regards,
Marcia G. Akins
Tightline Computers, Inc.




COMPLETE THREAD
Best Coding Practices for Newbies Posted by Dan Baker @ 2/11/2008 9:45:47 PM
RE: Best Coding Practices for Newbies Posted by Ken Murphy @ 2/11/2008 10:20:17 PM
RE: Best Coding Practices for Newbies Posted by Marcia Akins @ 2/11/2008 10:37:35 PM
RE: Best Coding Practices for Newbies Posted by Bernard Bout @ 2/12/2008 3:13:39 AM
RE: Best Coding Practices for Newbies Posted by tushar @ 2/12/2008 3:44:07 PM
RE: Best Coding Practices for Newbies Posted by Glenn Villar @ 4/26/2008 8:44:34 AM
RE: Best Coding Practices for Newbies Posted by Cetin Basoz @ 2/12/2008 3:52:43 PM
RE: Best Coding Practices for Newbies Posted by Leslie Botchway @ 2/12/2008 11:12:49 PM
RE: Best Coding Practices for Newbies Posted by Ken Murphy @ 2/13/2008 2:12:32 AM
RE: Best Coding Practices for Newbies Posted by Leslie Botchway @ 4/28/2008 1:38:57 PM
RE: Best Coding Practices for Newbies Posted by Jun Tangunan @ 4/28/2008 3:02:59 PM
RE: Best Coding Practices for Newbies Posted by Lyrad Bangoy @ 4/29/2008 9:33:26 AM
RE: Best Coding Practices for Newbies Posted by Christian Tabligan @ 4/29/2008 10:05:09 AM
RE: Best Coding Practices for Newbies Posted by Lyrad Bangoy @ 4/29/2008 10:19:00 AM
RE: Best Coding Practices for Newbies Posted by Cetin Basoz @ 4/29/2008 10:49:48 AM
RE: Best Coding Practices for Newbies Posted by Mike Yearwood @ 4/29/2008 7:31:25 PM
RE: Best Coding Practices for Newbies Posted by Cetin Basoz @ 2/13/2008 11:15:28 PM
RE: Best Coding Practices for Newbies Posted by Mike Yearwood @ 2/14/2008 7:20:53 PM
RE: Best Coding Practices for Newbies Posted by Pamela Thalacker @ 2/12/2008 9:17:58 PM
RE: Best Coding Practices for Newbies Posted by Tamar Granor @ 2/12/2008 11:13:57 PM
RE: Best Coding Practices for Newbies Posted by Ken Murphy @ 2/13/2008 2:17:48 AM
RE: Best Coding Practices for Newbies Posted by Bernard Bout @ 2/13/2008 5:55:54 AM
RE: Best Coding Practices for Newbies Posted by Ken Murphy @ 2/13/2008 10:28:13 AM
RE: Best Coding Practices for Newbies Posted by Christian Tabligan @ 2/13/2008 2:37:30 PM
RE: Best Coding Practices for Newbies Posted by Ilya Rabyy @ 2/13/2008 7:14:07 PM
RE: Best Coding Practices for Newbies Posted by Andy Kramek @ 2/13/2008 7:39:02 PM
RE: Best Coding Practices for Newbies Posted by Ilya Rabyy @ 2/13/2008 8:12:51 PM
RE: Best Coding Practices for Newbies Posted by Christian Tabligan @ 2/13/2008 7:46:29 PM
RE: Best Coding Practices for Newbies Posted by Ilya Rabyy @ 2/13/2008 8:28:26 PM
RE: Best Coding Practices for Newbies Posted by Christian Tabligan @ 2/13/2008 8:48:11 PM
RE: Best Coding Practices for Newbies Posted by Ilya Rabyy @ 2/13/2008 9:01:18 PM
RE: Best Coding Practices for Newbies Posted by Juan Antonio Santana @ 2/13/2008 3:46:59 PM
RE: Best Coding Practices for Newbies Posted by Ilya Rabyy @ 2/13/2008 7:28:57 PM
RE: Best Coding Practices for Newbies Posted by Mike Yearwood @ 2/14/2008 7:31:37 PM
RE: Best Coding Practices for Newbies Posted by Ilya Rabyy @ 2/14/2008 8:45:29 PM
RE: Best Coding Practices for Newbies Posted by Sandeep Misra @ 4/26/2008 4:16:39 AM
RE: Best Coding Practices for Newbies Posted by Ken Murphy @ 4/26/2008 2:36:22 PM
RE: Best Coding Practices for Newbies Posted by Sandeep Misra @ 4/28/2008 5:12:22 PM
RE: Best Coding Practices for Newbies Posted by Tamar Granor @ 4/28/2008 10:51:51 PM
RE: Best Coding Practices for Newbies Posted by Anders Altberg @ 4/27/2008 1:18:05 AM
RE: Best Coding Practices for Newbies Posted by tushar @ 4/27/2008 7:39:42 AM
RE: Best Coding Practices for Newbies Posted by Anders Altberg @ 4/27/2008 12:59:24 PM
RE: Best Coding Practices for Newbies Posted by tushar @ 4/27/2008 2:04:48 PM
RE: Best Coding Practices for Newbies Posted by Ken Murphy @ 4/27/2008 2:21:02 PM
RE: Best Coding Practices for Newbies Posted by Anders Altberg @ 4/28/2008 12:56:54 AM
RE: Best Coding Practices for Newbies Posted by tushar @ 4/28/2008 7:02:40 AM
RE: Best Coding Practices for Newbies Posted by Mike Yearwood @ 4/28/2008 1:33:37 PM
RE: Best Coding Practices for Newbies Posted by Anders Altberg @ 4/28/2008 9:06:34 PM
RE: Best Coding Practices for Newbies Posted by Mike Yearwood @ 4/28/2008 9:40:31 PM
RE: Best Coding Practices for Newbies Posted by tushar @ 4/28/2008 9:54:04 PM
RE: Best Coding Practices for Newbies Posted by Anders Altberg @ 4/29/2008 12:38:25 AM
RE: Best Coding Practices for Newbies Posted by tushar @ 4/29/2008 6:23:59 AM
RE: Best Coding Practices for Newbies Posted by Jun Tangunan @ 4/29/2008 7:33:02 AM
RE: Best Coding Practices for Newbies Posted by Anders Altberg @ 4/29/2008 3:43:15 PM
RE: Best Coding Practices for Newbies Posted by Mike Yearwood @ 4/29/2008 7:39:47 PM
RE: Best Coding Practices for Newbies Posted by Anders Altberg @ 4/29/2008 8:54:01 PM
RE: Best Coding Practices for Newbies Posted by Mike Yearwood @ 4/30/2008 1:08:53 AM
RE: Best Coding Practices for Newbies Posted by tushar @ 4/30/2008 6:51:25 AM
RE: Best Coding Practices for Newbies Posted by Kevin @ 4/26/2008 6:47:17 PM
RE: Best Coding Practices for Newbies Posted by Mike Yearwood @ 4/26/2008 10:09:55 PM
RE: Best Coding Practices for Newbies Posted by Kevin @ 4/27/2008 1:19:56 AM
RE: Best Coding Practices for Newbies Posted by Ken Murphy @ 4/27/2008 1:35:13 AM
RE: Best Coding Practices for Newbies Posted by Kevin @ 4/27/2008 1:45:30 AM
RE: Best Coding Practices for Newbies Posted by Ken Murphy @ 4/27/2008 2:49:46 AM
RE: Best Coding Practices for Newbies Posted by Mike Yearwood @ 4/27/2008 5:09:04 AM
RE: Best Coding Practices for Newbies Posted by Ken Murphy @ 4/27/2008 2:29:00 PM
RE: Best Coding Practices for Newbies Posted by Mike Yearwood @ 4/27/2008 4:54:49 PM
RE: Best Coding Practices for Newbies Posted by anton d @ 4/26/2008 7:47:01 PM
RE: Best Coding Practices for Newbies Posted by Garth Groft @ 4/28/2008 8:59:33 PM
RE: Best Coding Practices for Newbies Posted by Mike Yearwood @ 4/28/2008 9:41:17 PM
RE: Best Coding Practices for Newbies Posted by tushar @ 4/28/2008 10:00:24 PM
RE: Best Coding Practices for Newbies Posted by Mike Yearwood @ 4/28/2008 11:14:16 PM
RE: Best Coding Practices for Newbies Posted by Tamar Granor @ 4/28/2008 10:56:38 PM
RE: Best Coding Practices for Newbies Posted by Mike Yearwood @ 4/29/2008 12:14:37 AM
RE: Best Coding Practices for Newbies Posted by Anders Altberg @ 4/29/2008 12:50:06 AM
RE: Best Coding Practices for Newbies Posted by Mike Yearwood @ 4/29/2008 7:41:24 PM
RE: Best Coding Practices for Newbies Posted by Garth Groft @ 4/29/2008 9:32:04 PM
RE: Best Coding Practices for Newbies Posted by Mike Yearwood @ 4/30/2008 1:11:06 AM
RE: Best Coding Practices for Newbies Posted by Tamar Granor @ 4/30/2008 1:53:41 AM
RE: Best Coding Practices for Newbies Posted by Garth Groft @ 4/30/2008 2:10:22 AM
RE: Best Coding Practices for Newbies Posted by Tamar Granor @ 4/30/2008 1:50:36 AM
RE: Best Coding Practices for Newbies Posted by Bernard Bout @ 4/30/2008 2:43:38 AM
RE: Best Coding Practices for Newbies Posted by Mike Yearwood @ 4/30/2008 3:45:10 PM
RE: Best Coding Practices for Newbies Posted by Lyrad Bangoy @ 4/29/2008 9:37:22 AM