在方法中修改变量是不好的做法吗?
哪种方法风格更好? 在方法中修改变量通常是不好的做法吗?
public class Person
{
public string Name { get; set;}
}
//Style 1
public void App()
{
Person p = new Person();
p.Name = GetName();
}
public string GetName()
{
return "daniel";
}
//Style 2
public void App()
{
Person p = new Person();
LoadName(p)
}
public void LoadName(Person p)
{
p.Name = "daniel";
}
Which method style is better?
Is it generally bad practice to modify the variable within a method?
public class Person
{
public string Name { get; set;}
}
//Style 1
public void App()
{
Person p = new Person();
p.Name = GetName();
}
public string GetName()
{
return "daniel";
}
//Style 2
public void App()
{
Person p = new Person();
LoadName(p)
}
public void LoadName(Person p)
{
p.Name = "daniel";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有时两种风格都可能有意义。例如,如果您只是设置名称,那么您可能会选择第一种样式。不要将对象传递给方法来改变一件事,只需检索一件事。作为附带好处,此方法现在更可重用。可以将其视为德墨忒尔定律或最少知识原则。
在其他情况下,也许您需要根据用户输入进行批量更新。如果您要显示一个人的属性并允许用户进行修改,也许您将该对象传递到一个方法中,以便所有更新都可以应用在一个位置。
这两种方法在不同的时间都可以得到保证。
There are times when both styles may make sense. For example, if you're simply setting the name, then perhaps you go with the first style. Don't pass an object into a method to mutate one thing, simply retrieve the one thing. This method is now more reusable as a side benefit. Think of it like the Law of Demeter or the principle of least knowledge.
In other cases, maybe you need to do a wholesale update based on user input. If you're displaying a person's attributes and allowing the user to make modifications, maybe you pass the object into a single method so that all updates can be applied in one spot.
Either approach can be warranted at different times.
我认为当方法不更改传递的对象时,代码会更加清晰和可读。特别是传递对象的内部字段。
有时可能需要这样做。但一般来说我会避免它。
根据评论更新(好点)
I think the code is more clear and readable when methods don't change objects passed. Especially internal fields of passed object.
This might be needed sometimes. But in general I would avoid it.
Updated based on comment (good point)
我同意安东尼的回答。
有时两种样式都可能有意义。
此外,为了提高可读性,您可以在 person 类中添加
LoadName
函数。I agree with Anthony's answer.
There are times when both styles may make sense.
Also, for more readability you can add the
LoadName
function in person class.您正在使用属性来访问数据,这在技术上是通过方法来访问的。您担心的是访问 iVar 或内部变量的属性。允许访问 iVar 通常不好的原因是,任何人都可以在您不知情或未经您许可的情况下修改变量,如果通过方法(属性),您有能力在获取或设置消息时拦截消息,或阻止它被读取或写入,因此通常认为这是最佳实践。
You are accessing the data using properties which technically is by a methods. What you are worried is property accessing iVar or internal variable. There reason why it is generally bad to allow access of iVar is because anyone can modify the variables without your knowledge or without your permission, if its through a methods (properties), you have the ability to intercept the message when it get or set, or prevent it from getting read or write, thus it is generally said to be the best practice.
我同意罗恩的观点。虽然您的特定示例可能因发布原因而略有设计,但我将有一个用于 Name 的公共 getter 和一个私有 setter。将名称传递给构造函数,Name 属性将在那里设置,但之后不能再修改。
例如:
I agree with Ron. Although your particular example could be slightly contrived for posting reasons, I would have a public getter for Name, and a private setter. Pass the name to the constructor, and the Name property will get set there, but afterwards can no longer be modified.
For example: