Getter VS Computed Property. What Would Warrant Using One of These Approaches Over the Other

Getter vs Computed property. What would warrant using one of these approaches over the other?

Both forms are exactly the same thing, a read-only computed property.

From the documentation:

You can simplify the declaration of a read-only computed property by
removing the get keyword and its braces.

Difference between computed property and property set with closure in swift 3?

There is no difference.

From the documentation

You can simplify the declaration of a read-only computed property by removing the get keyword and its braces.

Need clarification regarding computed properties

There is no difference at all. This is a mere shortcut.

When you write

var testVariable {
get{
return _privateVariable
}
}

you can add setter at any point:

var testVariable {
set{
_privateVariable = newValue
}
get{
return _privateVariable
}
}

While the other case is useful to shorten your code when you don't need a setter.

Properties vs Public member variables

Properties can have side-effects, They provide syntactic sugar around 'getter' and 'setter' methods.

public class MyClass {

int sizeValue = 0;

public int Size {
get {
return sizeValue;
}
set {
if ( value < 10 ) throw new Exception("Size too small");
sizeValue = value;
}
}
}

Properties can also have different levels of protection for get and set, you cannot do that with fields.

public class MyOtherClass {

// only this object can set this.
public int Level {
get; private set;
}

// only things in the same assembly can set this.
public string Name {
get; internal set;
}
}

Field vs Property. Optimisation of performance

As others have already mentioned, the getters are inlined.

If you want to avoid inlining, you have to

  • replace the automatic properties with manual ones:

    class A 
    {
    private double p;
    public double P
    {
    get { return p; }
    set { p = value; }
    }
    }
  • and tell the compiler not to inline the getter (or both, if you feel like it):

            [MethodImpl(MethodImplOptions.NoInlining)]
    get { return p; }

Note that the first change does not make a difference in performance, whereas the second change shows a clear method call overhead:

Manual properties:

auto getter. 519005. 10000971,0237547.
field. 514235. 20001942,0475098.

No inlining of the getter:

auto getter. 785997. 10000476,0385552.
field. 531552. 20000952,077111.

JAVAFX - Unable to set text of a textfield

JavaFX is a single thread GUI toolkit, so every update of a GUI component has to be done on the main application (JavaFX) thread.

What you are doing there, is trying to update a TextField from a background thread and an IllegalStateException will get thrown.

The Task and Service classes are meant to compute something in the background and do a GUI update afterwards.

Like explained over here and over here, you should create a Task<Integer> and return the computed value. If this succeeds, you can retrieve the value in the succeeded() method with getValue() and set the value to the TextField.
The succeeded() method is getting called from the GUI Thread, so its safe to update the TextField here.

Is it possible to compare private attributes in Ruby?

There are several methods

Getter:

class X
attr_reader :a
def m( other )
a == other.a
end
end

instance_eval:

class X
def m( other )
@a == other.instance_eval { @a }
end
end

instance_variable_get:

class X
def m( other )
@a == other.instance_variable_get :@a
end
end

I don't think ruby has a concept of "friend" or "protected" access, and even "private" is easily hacked around. Using a getter creates a read-only property, and instance_eval means you have to know the name of the instance variable, so the connotation is similar.



Related Topics



Leave a reply



Submit