Using property access over using getters for better performance - drools

In the Drools documentation it says that accessing a property through a getter is the same as accessing it directly. For example:
Person( age == 50 )
// this is the same as:
Person( getAge() == 50 )
But then there is a note saying "We recommend using property access (age) over using getters explicitly (getAge()) because of performance enhancements through field indexing."
What is the performance benefit of accessing the property directly?
What would happen if "getAge" would not just return the property value but instead does some calculation which is based on the values from the other properties of the object and then returns a value?

Related

How do we create custom getters and setters, and what’s the advantage of doing so in Flutter?

What is the reason of using custom getters and setters in an application.
That's fairly very simple
First let me show you a sample of how getters and setters in Dart look like, which is essentially the language behind Flutter
class Foo {
// Creating a field/instance variable
String _fooName; //Keeping it private always
// Using the getter
String get foo_name {
//We can do something else here, like saving the variable somewhere and then returning it to the caller function
return _fooName;// private variable return for use in outside class
}
// Using the setter method
set foo_name (String name) {
// We can do something else, like update another variable based on fooName
this._fooName = name;//private variable being assigned new value
}
}
From the name, setters are involved in setting the values to an instance variable in an object oriented programming paradigm whereas getters are involved in getting the value of an instance variable
Now you would ask why not return the instance variable directly and why having such a roundabout approach to setting and getting the value
Well the answer is while getting as well as setting, we might want to do some other operation too other than just setting or getting the value and it's always better not to give admin access to the variables and that's why they are private so as to promote consistency within the objects accessing the field
It's a matter of preference, but you really shouldn't needlessly create one for a single field
https://dart.dev/guides/language/effective-dart/usage#dont-wrap-a-field-in-a-getter-and-setter-unnecessarily
One use case for creating a setter would be to perform some type of validation
For a getter, it'd be useful for a calculated field based on other properties, rather than a single property alone

Cannot Set Getters and Setters in Swift

I have created a custom class and I want to add associated getters and setters. I have read the official documentation from Apple and notice that when setting a getter and setter, you actually create a separate variable, which seems really confusing. For instance, in the documentation linked above, getters and setters are applied to a new variable called perimeter to obtain values from another variable called sidelength. Below is my code:
Getters and setters like you are implementing them are only used for computed variables in Swift. A variable with a getter and setter doesn't actually store anything, but does something when assigned or used for its value with other variables. When appropriate to use, the usage would be more like this.
var _key: String?
var key: String? {
get {
return _key
}
set {
_key = newValue
}
}
You would rarely if ever use this on an identical data type though. I use these for communication with CoreData often, and setting things like dates as a string or back to a date. Occasionally I might use it to handle the change in an optional to a non optional.
Getters and setters are not needed in order to use the variable in Swift though. If you are used to languages with them, I can understand the confusion here, but forget the correlation and start over with how you think about it. I am sure after a short amount of time, you will like the way Swift works with its datatypes!
Edit:
In order to do what you want, all you need is this:
class PollCell: UICollectionViewCell {
var key: String?
}
Now you can assign any value you want to cell.key and retrieve any value you want from cell.key.

replacing drools getter lookup for non-pojo

Drools supports the usage of properties of Pojos using a simple name. For example Person(age==10) to match a Person instance that has a getAge() method returning 10.
My problem now is that I have to handle something that is not a pure Pojo and instead has a generic getter. So for the Person example above I need a transformation to Person(myGenericPropertyLookupMethod("age")==10). And I need this for all such property usages, which includes for example the usage of from and chaining like $street : String() from $person.address.street. where at least street must be looked up using myGenericPropertyLookupMethod.

Selecting list of objects where property of a specific type in entityframework

Currently struggling to filter a list of objects (called Field) where these objects have an inherited property, this property is called FieldReference. This is an abstract class and all Field objects have this property but it can be of different types such as;
DebtorFieldReference
What we're trying to do is query all fields where the Property is of the type DebtorFieldReference and further more we also want to then filter on a value in this DebtorFieldReference object called DebtorId.
We tried the following snippet but EF doesnt allow us to cast, we also tried to query on GetType without success.
var results = FieldProvider.Get(f => f.FieldOwnerTypeId == (byte)Enum.FieldReferenceType.Debtor && ((DebtorFieldReference)f.FieldReference).DebtorId == message.Reference);
I've in the past done this with NHibernate without any problems, please tell me i'm just doing something wrong and EF isn't hugely limited in this way :-(
Thank you for your help.
After some trial and error I actually got it working, not entirely sure why this would work but the Cast would not but here is the line that did it;
var results = FieldProvider.Get(f => (f.FieldReference as DebtorFieldReference).DebtorId == message.Reference);
So in other words, use 'as' and not cast, maybe cast and as behave differently when it concerns null values.

NDepend query to consider the getter and setter for a property as one single method

I have followed the link Can I find number of methods without number of getters via CQL? and excluded the properties from one of my NDepend queries.
One of our coding guidelines is also to ensure that the number of properties in a class(including auto-properties) should not exceed 10.(To be consistent with another guideline that no more than 20 methods can be defined for a class.)
The problem is even if we have 10 properties in a class, which is within the defined limits, the number of methods is being shown as 20. I understand that this is because a get_ and set_ for a single property are being counted as two different methods. But is there any way, by which we can change the NDepend query to have the get_ and set_ methods for a property to be counted as a single method?
Thanks
Here is a CQLinq rules that warn for types having more than 10 properties and that lists the properties through a getter or (exclusive) a setter. The astute consists in using a Lookup table where getters and/or setters are indexed by the property name, inferred from getter/setter names:
// <Name>A class should not have more than 10 properties</Name>
warnif count > 0
from t in Application.Types
let accessers = t.Methods.Where(m => m.IsPropertyGetter || m.IsPropertySetter)
where accessers.Count() > 0 // Optimization!
// Here we build a lookup that associate for each property name, the getter, the setter, or both.
// The property name is getter and/or setter simple names, without "get_" or "set_" prefixes (4 chars).
let propertiesLookup = accessers.ToLookup(a => a.SimpleName.Substring(4, a.SimpleName.Length -4))
where propertiesLookup.Count() > 10
let properties = propertiesLookup.Select(p => p.First())
select new { t, properties }
This rule also lists properties for each matched class.
We have demand on our User Voice for creating an NDepend.CodeModel.IProperty interface, that will make such rule easier to develop, don't hesitate to vote for this!