功能与子程序 [英] Function Vs. Sub Procedure

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

问题描述

也许这只是一些不良习惯...


但是为什么不把VB中的所有内容写成函数?

-如果以后需要你可以随时传回一个值。

- 你可以根据需要传入变量

我们总是在函数中编写所有内容并试图保持清除

程序。


为什么要创建一个Sub Bla()End Sub

你可以创建一个函数吗?


Miro

解决方案

如果你知道你的方法不需要返回什么,为什么要使它成为一个函数?

-

-iwdu15


我们做的是:


如果一个函数永远不应该必须返回任何东西总是返回真实


Cuase那样...... 2个月后或一年后的下一个人,

如果偶然有些东西需要返回

要退回,然后就可以了。


此外,可以从一个函数调用一个函数nywhere。 Sub不能。

给你更多的自由。

(我是VB的新手 - 自学 - 所以请告诉我,如果不是这样的话
$ b VB.net中的$ b)

-One可能会说...将它变为sub并在需要时将其转换为函数。

-答案是......为什么要让某人在它已经完成时进行转换。


Miro

" iwdu15" < jmmgoalsteratyahoodotcom>在消息中写道

新闻:EC ********************************** @ microsof t.com ...

如果你知道你的方法不需要返回一些东西,为什么要使它成为一个
函数?
-
-iwdu15


为什么不将所有应用程序编写为单个可执行文件?这样你就可以

总是到达任何代码。


为什么不把你服务器中的所有表都写成整数字段和

文本字段并在文本字段中存储XML?这样,您可以更改

存储而无需更改架构。


在您的一个示例中,您提到使用bool创建签名,

返回true。您现在已将签名键入为布尔值。这意味着

如果你真的需要一个整数,你正在改变签名,所以

你在sub上创建函数的优势是没有优势的。


也许你会去一个字符串。所以,你传回真实。你现在

可以将它改成真正的字符串,或者把你的整数(内部)变成1,

等听起来很有希望,但你是拳击和在你的内部

代码和另一方面拆箱,消费者必须将通用的

字符串转换为真实类型。不明智。现在,你可以重新输入正确的类型,

但是你没有优势创建一个sub并稍后将它改成

函数。


如果你想遵循正确的.NET标准 (带上一粒

盐,因为我不确定有明确的规则,直到你明确地检查一个特定的问题),你抛出异常的例外情况/>
条件,不用虚假模糊条件。当你想要返回值时,你还可以预先获得一个好的
想法。这并非总是如此,但前面的一些功课可以帮助你避免大量签名

的变化。


另外需要考虑的是重构是软件开发的一个自然组成部分,即使是一分钱的人都确信事情会被编码一次,而且生活还在继续。如果没有输入时间进行重构,您将打算使用b
创建buggier软件。故事结束。


-

Gregory A. Beamer


******** *****************************************

想想在盒子外面!

************************************** ***********

" Miro" < MI ****** @ golden.net>在消息中写道

新闻:uE ************** @ TK2MSFTNGP04.phx.gbl ...

也许这只是一些捡起坏习惯......

但是为什么不把VB中的所有东西都写成一个函数?
- 如果以后需要你可以随时传回一个值。
- 你可以根据需要传入变量

我们总是在函数中编写所有内容并试图避开
程序。

为什么要创建Sub Bla()End Sub
Miro



Maybe this is just some bad habits picked up...

But why not write everything in VB as a Function ?
-If required later on you can always pass back a value.
-You can pass in variables as required
We always wrote everything in functions and tried to stay clear of
procedures.

Why make a Sub Bla() End Sub
when you can make a Function?

Miro

解决方案

if you know your method wont need to return something, why make it a function?
--
-iwdu15


What we did was this:

If a function should never have to return anything always return True

Cuase that way...the next guy down the road 2 months later or a year later,
if by chance something is requiered
to be returned, then it can.

Also, a Function can be called from anywhere. A Sub cannot.
Gives you more freedom.
(Im new to VB - teaching myself - so please tell me if this is not the case
in VB.net)
-One might say...make it a sub and convert it to a function when required.
-The answer is...why make someone convert it when it can already be done.

Miro
"iwdu15" <jmmgoalsteratyahoodotcom> wrote in message
news:EC**********************************@microsof t.com...

if you know your method wont need to return something, why make it a
function?
--
-iwdu15



Why not write all of your apps as a single executable? That way you can
always reach any of the code.

Why not write all of your tables in your server as a integer field and a
text field and store XML in the text field? That way, you can change the
storage without having to change the schema.

In one of your examples, you mention creating a signature with a bool and
returning a true. You have now typed the signature as a Boolean. That means
if you really need an integer, you are changing the signature anyway, so
your advantage of creating a function over a sub is no advantage.

Perhaps then you go to a string instead. So, you pass back "true". You now
can change it to a real string, or turn your integer (internal) into a "1",
etc. Sounds promising, but you are boxing and unboxing both in your internal
code and on the other side, where the consumer has to convert the generic
string to a real type. Not wise. Now, you could retype to the proper type,
but then you have no advantage over creating a sub and changing it to a
function later.

If you want to follow "proper .NET standards" (take this with a grain of
salt, as I am not certain there are firm rules until you examine a
particular problem explicitly), you throw exceptions for exceptional
conditions, not obfuscate the condition with a false. You also have a good
idea, up front, when you are going to return values. This is not always
true, but a bit of homework up front can help you avoid massive signature
changes.

The other thing to consider is refactoring is a natural part of software
development, even if penny pinchers are convinced that things get coded once
and life goes on. If time is not entered in to refactor, you are going to
create buggier software. End of story.

--
Gregory A. Beamer

*************************************************
Think Outside the Box!
*************************************************
"Miro" <mi******@golden.net> wrote in message
news:uE**************@TK2MSFTNGP04.phx.gbl...

Maybe this is just some bad habits picked up...

But why not write everything in VB as a Function ?
-If required later on you can always pass back a value.
-You can pass in variables as required
We always wrote everything in functions and tried to stay clear of
procedures.

Why make a Sub Bla() End Sub
when you can make a Function?

Miro



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

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