功能长度 [英] function length

查看:51
本文介绍了功能长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您对功能长度有何看法?超长功能

风格差吗?

我一直在研究我的winprocs,而且有些时间长得很长

长度。我可以将它们分解成碎片,但据我所知,它只能使大量的切换语句变得更容易。/ b $ b见。当然,那我就需要拿出很多额外的功能

名字。


好​​奇。

What is your opinion on function length? Are extra long functions
poor style?

I have been working on my winprocs and some are getting rather long in
length. I could break them up into pieces, but as far as I can tell
it would only serve to make the massive switch statement easier to
see. Of course, then I''d need to come up with lots of extra function
names.

Just curious.

推荐答案

3月29日,10:24,koolj96 ... @ yahoo.com写道:
On 29 mar, 10:24, koolj96...@yahoo.com wrote:

您对功能长度有何看法?超长功能

风格差吗?

我一直在研究我的winprocs,而且有些时间长得很长

长度。我可以将它们分解成碎片,但据我所知,它只能使大量的切换语句变得更容易。/ b $ b见。当然,那么我需要提出许多额外的功能

名称。


只是好奇。
What is your opinion on function length? Are extra long functions
poor style?

I have been working on my winprocs and some are getting rather long in
length. I could break them up into pieces, but as far as I can tell
it would only serve to make the massive switch statement easier to
see. Of course, then I''d need to come up with lots of extra function
names.

Just curious.





我认为一个好的做法是让功能不大于可见的

编辑页面(30-50行)。

我也认为如下:你的功能越多

嵌套,你的软件就越容易演变。


Hi,
I think a good practice is to have functions not bigger than a visible
editor page (30-50 lines).
I also think the following : the more your functionnalities are
nested, the more your software can evolve easily.


ko ******** @ yahoo.com 写道:

您对功能长度有何看法?
What is your opinion on function length?



不超过30行。

No more than 30 lines.


超长功能是不是很差的风格?
Are extra long functions poor style?



非常糟糕的风格和要保持的屁股。

Very poor style and a pin in the arse to maintain.


我一直在努力winprocs和一些在

长度上变得相当长。我可以将它们分解成碎片,但据我所知,它只能使大量的切换语句变得更容易。/ b $ b见。当然,那么我需要提出许多额外的功能

名称。
I have been working on my winprocs and some are getting rather long in
length. I could break them up into pieces, but as far as I can tell
it would only serve to make the massive switch statement easier to
see. Of course, then I''d need to come up with lots of extra function
names.



让名字描述行动是什么执行,然后大量的

开关成为函数的文档。


-

Ian Collins。

Make the names describe the action being performed, then the massive
switch becomes the function''s documentation.

--
Ian Collins.


ko ******** @ yahoo.com 说:

您对功能长度有何看法?超长功能

风格差吗?

我一直在研究我的winprocs,而且有些时间长得很长

长度。我可以将它们分解成碎片,但据我所知,它只能使大量的切换语句变得更容易。/ b $ b见。当然,那么我需要提出许多额外的功能

名称。


只是好奇。
What is your opinion on function length? Are extra long functions
poor style?

I have been working on my winprocs and some are getting rather long in
length. I could break them up into pieces, but as far as I can tell
it would only serve to make the massive switch statement easier to
see. Of course, then I''d need to come up with lots of extra function
names.

Just curious.



这里是我如何做winproc(在主题警察触发快乐之前,

我应该强调所有的以下语法是ISO C,或者可以使用简单的typedef或#define来制作




我从一个或三个宏开始,其中我保持一个共同的标题:


#define MW_CRACK_MESSAGE(MW_m,MW_f)\

案例MW_m:\

rc = MW_f(WindowHandle,Message,Major,Minor); \\ b
休息


#define MW_START_CRACKING_MESSAGES开关(留言)

#define MW_STOP_CRACKING_MESSAGES \

默认值:\

rc = DefWindowProc(WindowHandle,\

消息,\

Major,\

次要); \\ b
休息


#define MW_START_CRACKING_COMMAND_MESSAGES开关(LOWORD(主要))


typedef LRESULT MW_MESSAGE_HANDLER(HWND ,

UINT,

WPARAM,

LPARAM);


顺便说一句,LRESULT, HWND,UINT,WPARAM和LPARAM都只是类型

,可以很容易地从一个

comp.lang中获得FOO,BAR,BAZ,QUUX和QUUUX。 c透视。


在特定于应用程序的头文件中,我们有一些函数原型。

以下内容取自我为<编写的一个非常简单的Windows示例br />
解释目的,一个Vigenere加密程序:


/ *消息处理程序* /

MW_MESSAGE_HANDLER VigenereCreate;

MW_MESSAGE_HANDLER VigenereResize;

MW_MESSAGE_HANDLER VigenereCommand;

MW_MESSAGE_HANDLER VigenereDestroy;


足够的标题内容。转到C代码和wndproc本身:


LRESULT CALLBACK ProcessVigenereMessages(HWND WindowHandle,

UINT消息,

WPARAM Major,

LPARAM Minor)

{

LRESULT rc = {0};


MW_START_CRACKING_MESSAGES

{

MW_CRACK_MESSAGE(WM_CREATE,VigenereCreate);

MW_CRACK_MESSAGE(WM_SIZE,VigenereResize);

MW_CRACK_MESSAGE(WM_COMMAND ,VigenereCommand);

MW_CRACK_MESSAGE(WM_DESTROY,VigenereDestroy);

MW_STOP_CRACKING_MESSAGES;

}


返回rc;

}


如您所见,每条新消息现在只需要一行新代码。

And ,奇怪的是,这个函数有多长时间并不重要!

如果它开始变得太长,只需将命令按字母顺序排序

顺序,并保持他们那样。 :-)这样,你可以很快找到你需要的任何东西,这才真正重要。

函数的长度并不妨碍理解,因为这里没有任何东西需要理解。它只是一种路由机制,即

所有。


当然,你也可以类似地处理控制消息。


你现在已经把问题简化为如何获得所有/其他/

函数,那些进行实际处理的函数是可管理的

长度?",答案是三方面的:


1)模块化;

2)模块化; < br $>
3)模块化。


-

Richard Heathfield

Usenet是一个奇怪的地方 ; - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

Here''s how I do a winproc (and before the topic cops get trigger-happy,
I should stress that all the following syntax is ISO C, or can be made
so with a simple typedef or #define).

I start off with a macro or three, which I keep in a common header:

#define MW_CRACK_MESSAGE(MW_m, MW_f) \
case MW_m: \
rc = MW_f(WindowHandle, Message, Major, Minor); \
break

#define MW_START_CRACKING_MESSAGES switch(Message)

#define MW_STOP_CRACKING_MESSAGES \
default: \
rc = DefWindowProc(WindowHandle, \
Message, \
Major, \
Minor); \
break

#define MW_START_CRACKING_COMMAND_MESSAGES switch(LOWORD(Major))

typedef LRESULT MW_MESSAGE_HANDLER(HWND,
UINT,
WPARAM,
LPARAM);

By the way, LRESULT, HWND, UINT, WPARAM, and LPARAM are all just types
which could just as easily be FOO, BAR, BAZ, QUUX, and QUUUX from a
comp.lang.c perspective.

In the application-specific header, we have some function prototypes.
The following are taken from a very simple Windows example I wrote for
exegetic purposes, a Vigenere encryption program:

/* Message handlers */
MW_MESSAGE_HANDLER VigenereCreate;
MW_MESSAGE_HANDLER VigenereResize;
MW_MESSAGE_HANDLER VigenereCommand;
MW_MESSAGE_HANDLER VigenereDestroy;

Enough header stuff. On to the C code, and the wndproc itself:

LRESULT CALLBACK ProcessVigenereMessages(HWND WindowHandle,
UINT Message,
WPARAM Major,
LPARAM Minor)
{
LRESULT rc = {0};

MW_START_CRACKING_MESSAGES
{
MW_CRACK_MESSAGE(WM_CREATE, VigenereCreate);
MW_CRACK_MESSAGE(WM_SIZE, VigenereResize);
MW_CRACK_MESSAGE(WM_COMMAND, VigenereCommand);
MW_CRACK_MESSAGE(WM_DESTROY, VigenereDestroy);
MW_STOP_CRACKING_MESSAGES;
}

return rc;
}

As you can see, each new message now takes only one new line of code.
And, curiously, it doesn''t actually matter how long this function gets!
If it starts to get too long, just sort the commands into alphabetical
order, and keep them that way. :-) That way, you can find anything you
need very quickly, which is all that really matters. The length of the
function doesn''t impede understanding, because there''s nothing here
that really needs understanding. It''s just a routing mechanism, that''s
all.

You can deal with control messages similarly, of course.

You have now reduced the problem to "how do I get all the /other/
functions, the ones that do the actual processing, to be of manageable
length?", and the answer to that is three-fold:

1) modularise;
2) modularise;
3) modularise.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


这篇关于功能长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆