为什么使用过程来创建优先于函数的对象? [英] Why is using procedures to create objects preferred over functions?

查看:133
本文介绍了为什么使用过程来创建优先于函数的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与这一点类似问题。我问为什么? 最受欢迎响应,但我不知道有人会再次看到它。至少没有及时的方式。

This is similar to this question. I asked "Why?" to the most popular response but I don't know that anyone would ever look at it again. At least not in any timely manner.

无论如何,我的问题是关于将对象创建到函数或过程的责任的最佳做法,而不会导致内存泄漏。似乎这样:

Anyway, my question is about best practices for delegating responsibility for creation of objects to functions or procedures, without causing memory leaks. It seems that this:

procedure FillObject(MyObject: TMyObject; SomeParam: Integer);
begin
  //Database operations to fill object
end;

procedure CallUsingProcedure();
var
  MyObject: TMyObject;
begin
  MyObject = TMyObject.Create();
  try
    FillObject(MyObject, 1);
    //use object
  finally
    MyObject.Free();
  end;
end;

优先于此:

function CreateMyObject(DBID: Integer): TMyObject;
begin
  Result := TMyObject.Create();
  try
    //Database operations to fill object
  except on E: Exception do
    begin
      Result.Free();
      raise;
    end;
  end;
end;

procedure CallUsingFunction();
var
  MyObject: TMyObject;
begin
  MyObject = CreateMyObject(1);
  try
    //use object
  finally
    MyObject.Free();
  end;
end;

为什么?

我比较新的Delphi,以前在Java和PHP以及C ++中工作最多,但在较小程度上。直观地,我倾向于函数方法,因为:

I'm relatively new to Delp having previously worked most with Java and PHP, as well as C++, though to a lesser extent. Intuitively, I lean toward the function method because:


  • 它将对象创建代码封装在函数中,而不是单独创建对象想使用这个程序。

  • 我不喜欢改变参数的方法。

  • 暧昧,但诚然,这只是闻起来对我不好。

我不是说我是对的我只是想了解社区为什么选择这种方法,如果有更好的理由让我改变。

I'm not saying I'm right. I just want to understand why the community chooses this method and if there is good reason for me to change.

编辑:
参考@ E-Rock的评论是给我的(Eric G)。我更改了我的显示名称。

References to @E-Rock in comments are to me(Eric G). I changed my display name.

推荐答案

创建对象实例并将其传递给另一个过程,使其清楚哪些代码负责释放

Creating the object instance and passing it into another procedure makes it clear which code is responsible for freeing the instance.

在第一种情况(使用一个程序来填写它):

In the first case (using a procedure to fill it):

MyObj := TMyObject.Create;
try
  // Do whatever with MyObj
finally
  MyObj.Free;
end;

这很清楚,这块代码负责释放 MyObj 完成使用后。

This is clear that this block of code is responsible for freeing MyObj when it's finished being used.

MyObj := CreateMyObject(DBID);

应该释放什么代码?什么时候可以安全释放它?谁负责异常处理?您如何知道(作为其他人的代码的用户)?

What code is supposed to free it? When can you safely free it? Who is responsible for exception handling? How do you know (as a user of someone else's code)?

作为一般规则,您应该在需要的地方创建,使用和释放对象实例。这使得您的代码更容易维护,并且肯定会使以后的人更容易,并且必须尝试弄清楚。 :)

As a general rule, you should create, use, and free object instances where they're needed. This makes your code easier to maintain, and definitely makes it easier for someone who comes along later and has to try and figure it out. :)

这篇关于为什么使用过程来创建优先于函数的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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