转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> Delphi程序 >> 正文
Simple Programming Tip #1 by Charlie Calvert         ★★★★

Simple Programming Tip #1 by Charlie Calvert

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1998 更新时间:2009/4/23 18:38:06
 
Welcome bear www.borland.com  AppServer   C++   CORBA   CaliberRM   Delphi & Kylix   InterBase   JDataStore   Java   Star Team   Together   


Simple Programming Tip #1 by Charlie Calvert

Abstract: This article contains the explication of a simple programming tip that can be read in a few minutes: Avoid writing code that does anything substantial inside a visual container. Instead, create separate classes for your working code.

Simple Programming Tip #1 by Charlie Calvert

Copyright ?2003 by Charlie Calvert


In this article I will describe the first of several simple programming tips. The discussion and code in this article is generic, and applies equally to C++, CSharp, Delphi or Java programmers.

The tip for this article is easy to describe: Avoid writing code that does anything substantial inside a visual container such as a Java JPanel or JDialog, or a Delphi/C++Builder TForm or TFrame. Instead, write code inside separate classes designed for a single, simple purpose.

Of course there are times when it is appropriate to break any rule. After all, what is the point of having a rule, if there won''''t be occasions when you break it? Needless to say, the smaller and simpler your application, the less need there is for rules of this type. But in big applications, rules like this are the keys to the kingdom!

The Danger of Click and Code

Borland was among the companies that pioneered the now popular options that allow you to drop a component such as button on a visual container, then double click on that component, and immediately begin writing event handling code. The beauty of this system is that it allows you to write code quickly and easily. The danger is that it can lead you down the primrose path that leads to unmaintainable "write once, read never" code.

The solution, of course, is not to abandon visual programming, but rather to adopt intelligent programming. When you create a new visual container, the first thing you should do is create at least one secondary class designed to contain your real code.

Suppose, for instance, that you want to create a visual container that will allow the user to view data. In particular, suppose your container will show the results of parsing an XML file that contains information about the Voyager I and II spacecraft.

Data of this kind is likely to be fairly complex. For instance, you would want to provide controls for seeing data that applies to Voyager I only, or to Voyager II only. Likewise, you would want to allow the user to see data about a particular planet visited by the spacecraft, such as Jupiter, Saturn, or Neptune.

Without going into details, it is easy to see that you are going to have to write a substantial amount of code that involves extracting data from your XML file. The neophyte visual programmer will write that code directly inside a button response method:

void MyButtonResponseMethod_actionPerformed(ActionEvent e)
{
	// Code to parse file added here
}

Suppose the code inside this method allows you to sort data according to the planet you are interested in investigating. The programmer happily writes a few lines of SAX based code, and seemingly has a solution to their problem.

But it is precisely at this moment that the ax falls. Whomp! Down it comes, cutting off the programmer of a large project from any hope of ever writing code that can be maintained or reused.

Let''''s follow this hapless programmer just a little further, to see the extend of the damage. The next step in this application''''s development might be to add a routine for displaying data sorted by findings made exclusively by either Voyager I or Voyager II. So another button response method is created, and now the user has two options:

    1. Sort by planet

    2. Sort by spacecraft

So far so good. But what if the user wants to simultaneously sort by spacecraft and by planet? The all too common solution to this problem is to create a third button response method that contains rewrites of the code in the Sort by planet response method and the Sort by spacecraft response method! This is a disastrous situation, in which duplicate chunks of code begin to appear. If a bug is found in the Sort by planet method, then the fix will also have to be applied to the duplicate code found in the Sort by spacecraft and planet method.

Some will laugh and say: "Oh, but I would never do anything so stupid!" And yet, I assure you, intelligent people do things exactly that foolish.

But let us suppose that the programmer is wise enough not to make such an error. Seeing the problem on the horizon, they move the code for sorting by spacecraft and planet to their own unique methods inside the visual container class. In pseudo code, the declaration for such a class might look something like this:

public class MyVisualContainer
{
	void _SortByPlanet_actionPerformed(ActionEvent e)		
	void _SortBySpacecraft_actionPerformed(ActionEvent e)
	void SortByPlanet()
	void SortBySpacecraft()
	void SortByPlanetAndSpaceCraft_actionPerformed(ActionEvent e)
}

The problem here is that the MyVisualContainer class is already starting to take on too heavy a burden! Like Dr. Jekyll and Mr. Hyde, it is beginning to lead a double life. Is it a simple visual container, or is it a XML parser? Only its hairdresser knows for sure!

The proper structure for code of this type should look something like this:

public class MyVisualContainer
{
	void _SortByPlanet_actionPerformed(ActionEvent e)		
	void _SortBySpacecraft_actionPerformed(ActionEvent e)
	void SortByPlanetAndSpaceCraft_actionPerformed(ActionEvent e)
}

public class MyVoyagerParser
{
	void SortByPlanet()
	void SortBySpacecraft()
	void SortByPlanetAndSpaceCraft();
}



The methods inside the container should feature only a few lines of code that call the working code in MyVoyagerParser. With today''''s compilers and CPU''''s, the overhead of adding this extra class will not cause a noticeable, or in many cases even measurable, hit to the performance of your application. As mentioned earlier, the best course of action is to block out the declaration fro MyVoyagerParser right at the start, as soon as you open the source for MyVisualContainer.

The Advantages of Intelligent Programming

In the previous section, I outlined some of the dangers of adding complex code to the button or menu event handlers. Let''''s now talk about some of the advantages of this type of coding.

Suppose you want to reuse the code you have written in a new application with a different interface. If you wrote your code properly, then your port to a new type of application will be easy. For instance, in the example at the end of the last section, you could simply reuse the MyVoyagerParse class. Just plug it in to your new app, calling it with a few simple lines of code:

MyVoyageParse->SortByPlanet();

But reuse is only half the story. The other big advantage of this type of programming is that it helps you simplify your code.

We have already seen that a visual container class can quickly become overburdened by trying to support too much functionality. Consider what happens if you want to

[1] [2]  下一页


[系统软件]什么是Extreme Programming(极限编程,简称XP)  [Delphi程序]Why Pascal is Not My Favourite Programming Lan…
[VB.NET程序]Socket Programming with VB  [VB.NET程序]DAO Advanced Programming
[Web开发]Javascript Tip(1) 操作剪贴板  [MySql]learn linux programming with arden:00 铺垫
[MySql]Linux程式设计入门 - socket/inetd programming  [电脑技术]Programming in Lua翻译--Expressions
[电脑技术]Create a simple Delphi Expert(创建一个简单的De…  [Sql Server]使用sql的一些tip
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台