简体   繁体   English

公共变量是否比使用 getter 和 setter 更快?

[英]Are public variables faster than using getters and setters?

I am writing a custom physics engine for a game that I am making, my physics object class has tons of variables (distance, velocity, acceleration, mass, gravity, force, impulse duration etc....).我正在为我正在制作的游戏编写自定义物理引擎,我的物理对象类有大量变量(距离、速度、加速度、质量、重力、力、脉冲持续时间等......)。 Will creating a setter and getter function for each of these variables impact performance?为每个变量创建一个 setter 和 getter 函数会影响性能吗? (There will be at least 100 instances of this class at a given time) (在给定时间至少有 100 个此类的实例)

Also should I create setters and getters?我还应该创建 setter 和 getter 吗? I heard public variables are really bad practice but there are a lot of variables, can this be an exception?我听说公共变量是非常糟糕的做法,但是有很多变量,这可以是一个例外吗?

Will creating a setter and getter function for each of these variables impact performance?为每个变量创建一个 setter 和 getter 函数会影响性能吗?

As for simple getter/setter functions, they can be usually inlined by the compiler so there won't be a performance impact.至于简单的 getter/setter 函数,它们通常可以由编译器内联,因此不会对性能产生影响。

Also should I create setters and getters?我还应该创建 setter 和 getter 吗? I heard public variables are really bad practice我听说公共变量是非常糟糕的做法

Public variables aren't inherently a bad practice, though usually you want to encapsulate your data in a class, especially if there are calulations applied on them.公共变量本质上并不是一种不好的做法,尽管通常您希望将数据封装在一个类中,尤其是在对它们应用了计算的情况下。

Classes which only provide getter/setter functions are useless, these can usually simply replaced with a struct with all public variables.只提供 getter/setter 函数的类是无用的,它们通常可以简单地替换为具有所有公共变量的struct

Will creating a setter and getter function for each of these variables impact performance?为每个变量创建一个 setter 和 getter 函数会影响性能吗? (There will be at least 100 instances of this class at a given time) (在给定时间至少有 100 个此类的实例)

Probably no.可能没有。 Theoretically yes, but in practice, the cost of calling an extra function to get a particular value is negligible.理论上是的,但在实践中,调用额外函数来获取特定值的成本可以忽略不计。 The only way it will impact performance is if you end up calling these methods all the time (say ... 50000 times per second).它会影响性能的唯一方法是,如果你最终调用这些方法的时候(比如说......每秒50000次)。

Also should I create setters and getters?我还应该创建 setter 和 getter 吗?

Probably no.可能没有。 Good OO design follows a guideline called "tell, don't ask".好的 OO 设计遵循称为“告诉,不要问”的准则。 Usually you should see what operations you need these variables for, then either implement those operations in the class, or implement them in a class that has access, or use a different model (visitor pattern comes to mind).通常你应该看到你需要这些变量的哪些操作,然后要么在类中实现这些操作,要么在具有访问权限的类中实现它们,或者使用不同的模型(访问者模式)。

I heard public variables are really bad practice but there are a lot of variables, can this be an exception?我听说公共变量是非常糟糕的做法,但是有很多变量,这可以是一个例外吗?

Having public variables is not bad practice.拥有公共变量并不是坏习惯。 Having public variables when you have invariants on them, is bad practice.当你有不变量时,拥有公共变量是不好的做法。

For example, if you have a variable measuring the weight of an object, you will want to make sure this cannot be set to an invalid value (such as a negative amount, or a ridiculously large amount).例如,如果您有一个测量物体重量的变量,您将需要确保它不能设置为无效值(例如负值或大得离谱的值)。 If you make the variable public, you will either have to check the value you set everywhere in client code where you modify it, or give up on validating that value.如果您将变量设为公开,您将不得不检查您在客户端代码中修改它的任何地方设置的值,或者放弃验证该值。

Both are bad, as they are errors that couldn't exist if you had a propper setter, with validation.两者都不好,因为它们是错误,如果您有正确的 setter 和验证,它们就不会存在。

In short, having public variables is only acceptable if you have no invariants on them.简而言之,只有当您没有不变量时,才可以接受公共变量。

Considerations:注意事项:

  • use public variables, if setting any value permitted by the variable type is OK, at any point (you have no invariants)使用公共变量,如果设置变量类型允许的任何值都可以,在任何时候(你没有不变量)

  • use private variables if you have invariants.如果您有不变量,请使用私有变量。

  • use public methods to define operations, not "access to internal variables".使用公共方法来定义操作,而不是“访问内部变量”。

  • design your public API in terms of operations you want to perform when you look at it from the client code , not in terms of variables present in the internal implementation .当您从客户端代码查看公共 API 时,根据您希望执行操作来设计您的公共 API,而不是根据内部实现中存在变量

In this context, getters and setters make sense some times (rarely), but not because you have the variables in the class.在这种情况下,getter 和 setter 有时(很少)有意义,但不是因为类中有变量。

  • getters and setters are a symptom of trying to solve a problem in the wrong place: if you have an operation X in class A that uses variables from class B, then you should probably define operation X in class B, then call it from class A. getter 和 setter 是试图在错误的地方解决问题的症状:如果类 A 中的操作 X 使用类 B 中的变量,那么您可能应该在类 B 中定义操作 X,然后从类 A 中调用它.

简单的公共变量违反了 OO 设计原则,但 getter setter 没有,这就是为什么我们需要用户总是 getter 和 setter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM