Properties VS Methods

Properties vs Methods

From the Choosing Between Properties and Methods section of Design Guidelines for Developing Class Libraries:

In general, methods represent actions and properties represent data. Properties are meant to be used like fields, meaning that properties should not be computationally complex or produce side effects. When it does not violate the following guidelines, consider using a property, rather than a method, because less experienced developers find properties easier to use.


Method vs Property in C# - what's the difference

Here is a good set of guidelines for when to use properties vs methods from Bill Wagner (fixed link)

  • Use a Property when all these are true:
    The getters should be simple and thus unlikely to throw exceptions. Note that this implies no network (or database) access. Either might fail, and therefore would throw an exception.
  • They should not have dependencies on each other. Note that this would include setting one property and having it affect another. (For example, setting the FirstName property would affect a read-only FullName property that composed the first name + last name properties implies such a dependency )
  • They should be settable in any order
  • The getter does not have an observable side effect Note this guideline doesn't preclude some forms of lazy evaluation in a property.
  • The method must always return immediately. (Note that this precludes a property that makes a database access call, web service call, or other similar operation).
  • Use a method if the member returns an array.
  • Repeated calls to the getter (without intervening code) should return the same value.
  • Repeated calls to the setter (with the same value) should yield no difference from a single call.

  • The get should not return a reference to internal data structures (See item 23). A method could return a deep copy, and could avoid this issue.

Example of Properties vs. Methods in JS

Really, you need to back up and read some of the links posted above. But as a quick example:

var house = {} ;

house.isDoorOpen = false ;

house.openDoor = function(){
house.isDoorOpen = true ;
}

Here house is the object. It has a property: house.isDoorOpen. Here, it is more like an adjective. Either the door is open (true) or closed (false). As it sounds, it describes a property of the house.

Also, it has a method openDoor (which is used like this: house.openDoor() ). That's something that it can do. In this case, the action openDoor affects the isDoorOpen property, making it true.

When to use a property vs a method?

The general standard is about side effects. If by calling a member to get a value you only get that value it's a property. If there are side effects, it should probably be a method.

To put it another way: properties even though they aren't fields should behave very much like fields. This means not causing side effects, not taking too long to execute and not throwing exceptions.

Python property vs method when no access to attribute is needed?

Properties are not intended for providing access to a private attribute. Properties are intended to give you the option of making zero-argument methods accessible as if they were attributes, so that a given "attribute" can be implemented as either a calculation or an actual attribute, without changing the interface of the class.

As such, it's usually not really a question of "better", but rather a design decision with pros and cons that depend on the context.

In this case, whatever this object is supports x.hunger, x.boredom and x.mood. Why not x.mood()? You could, although that exposes permanently in the interface that it is a calculation and is not stored.

If a prior version of the class had a "real" mood attribute, and a required invariant of the object meant that any internal method updating boredom or hunger had to also carefully set mood to be consistent, then introducing the property would be an excellent refactor; the interface stays the same, but the invariant is now guaranteed to always hold, rather than having to be carefully maintained. A whole field of bugs are made impossible to occur.

On the other hand, if mood is expensive to calculate, or has side effects, then it likely would be much better as a normal method. Making it look like an attribute access means that client code programmers will likely think of it as an attribute access, which is cheap and non-destructive; this would be a rich source of bugs or performance problems.

When to use Properties and Methods?

It is pure syntactic sugar. On the back end, it is compiled into plain get and set methods.

Use it because of convention, and that it looks nicer.

Some guidelines are that when it has a high risk of throwing Exceptions or going wrong, don't use properties but explicit getters/setters. But generally even then they are used.

Difference between methods and attributes in python


Terminology

Mental model:

  • A variable stored in an instance or class is called an attribute.
  • A function stored in an instance or class is called a method.

According to Python's glossary:

attribute: A value associated with an object which is referenced by
name using dotted expressions. For example, if an object o has an
attribute a it would be referenced as o.a

method: A function which is defined inside a class body. If called as
an attribute of an instance of that class, the method will get the
instance object as its first argument (which is usually called self).
See function and nested scope.


Examples

Terminology applied to actual code:

a = 10                          # variable

def f(b): # function
return b ** 2

class C:

c = 20 # class attribute

def __init__(self, d): # "dunder" method
self.d = d # instance attribute

def show(self): # method
print(self.c, self.d)

e = C(30)
e.g = 40 # another instance variable

What is the difference between Method, Property and Variable in PHP

Yes, method is a function.

Model property would be a global variable within the class, so you can use it in all the methods. And depending on the access modifier (private, protected, public) the model property can be used from other classes inheriting/instantiating from that class.

While a variable will be something used within a method and has usage only within the body of that method.

Performance of Property Get v Method

Properties are implemented as methods under the hood, so there is absolutely no performance difference. The guidelines for choosing between a property and a method are semantic.



Related Topics



Leave a reply



Submit