Questions tagged [getter]

A getter is a public accessor method, used in object-oriented programming, which returns the value associated with a private member of a class.

A getter is public method, used in object-oriented programming, which returns the value associated with a private member of a class. A getter often refers to the get portion of a property. The getter method may perform other tasks such as validation, but is usually considered non-mutating.

1882 questions
11
votes
10 answers

What is the best practice for using "get" in method names?

I've noticed in many places in Java (C# included), that many "getter" methods are prefixed with "get" while many other aren't. I never noticed any kind of pattern Sun seems to be following. What are some guidelines or rules for using "get" in getter…
mcjabberz
  • 9,788
  • 10
  • 36
  • 38
11
votes
3 answers

Swift property - getter ivar

Is there an ivar property we should use in a Swift getter? My code is causing the getter to call the getter until the program crashes: var document: UIDocument? { get { return self.document } set { self.document =…
Adam Carter
  • 4,741
  • 5
  • 42
  • 103
11
votes
2 answers

Getter & setter support with ng-model in AngularJs

I am trying to get getter/setter support for ng-model by implementing a directive that will take care of getting and setting the values to/from the view/model. I am almost there, but I end up in infinite $digest loops. The idea is to set…
sboisse
  • 4,860
  • 3
  • 37
  • 48
11
votes
1 answer

Why is a simple get-statement so slow?

A few years back, I got an assignment at school, where I had to parallelize a Raytracer. It was an easy assignment, and I really enjoyed working on it. Today, I felt like profiling the raytracer, to see if I could get it to run any faster (without…
Nolonar
  • 5,962
  • 3
  • 36
  • 55
11
votes
6 answers

PHP Setters/Getters and Constructor

I have been searching for this online, but I can't seem to find something that is clear enough for me to understand. I have seen "similiar" questions on here about this in Java. class animal{ private $name; // traditional setters and…
Paolo Scamardella
  • 245
  • 2
  • 6
  • 14
10
votes
5 answers

Public property VS Private property with getter?

This question has puzzled me for a while. A public property that can be accessed directly or a private property with getter? Which one is better/correct and why?
Pouya
  • 1,266
  • 3
  • 18
  • 44
10
votes
10 answers

Java: how to handle a LOT of fields and their encapsulation cleanly?

Let's say I am tasked with coding some kind of an RPG. This means that, for example, I'll want to track a Character GameCharacter and stats thereof, like intelligence, damage bonuses or hitpoints. I'm positively scared that by the end of the project…
badp
  • 11,409
  • 3
  • 61
  • 89
10
votes
2 answers

Kotlin Renaming Generated Getters and Setters

Is there a way to rename the default getters and setters in Kotlin? I have a property named in snake_case, but I still want the getters and setters to be named in camelCase. The closest I've gotten is something like private var property_name =…
Jeffmagma
  • 452
  • 2
  • 8
  • 20
10
votes
1 answer

Kotlin - nonnull getter for a nullable field

I'm new with Kotlin and I try to rework a small Java project to this new language. I use mongodb in my project and I have a class, for example: class PlayerEntity { constructor() {} //for mongodb to create an instance constructor(id: ObjectId,…
awfun
  • 2,316
  • 4
  • 31
  • 52
10
votes
3 answers

Can I use const references instead of getter functions?

I just wondered if I could bypass using getters if I just allowed a const reference variable, as follows #include class cTest { private: int m_i; std::string m_str; public: const int & i; const…
Fabian
  • 4,001
  • 4
  • 28
  • 59
9
votes
6 answers

Accessor Method Performance and Optimization

Often, I come across code where the Getter method is repeatedly used/abused to get some value or pass it as a method parameter, for ex: public class Test { public void someMethod() { if(person.getName() != null &&…
bchetty
  • 2,231
  • 1
  • 19
  • 26
9
votes
7 answers

Ruby : how to prevent modification of an array instance variable through an attribute reader

sorry for this noob question... let's say we have : class TestMe attr_reader :array def initialize @array = (1..10).to_a end end it is then possible to do : >> a = TestMe.new => #
m_x
  • 12,357
  • 7
  • 46
  • 60
9
votes
2 answers

php automated setter and getter

I'm trying to implement some automated getter and setter for php objects. My target is to automatically have for each properties the methods getProperty() and setProperty(value), that way if the method is not implemented for a property the script…
sebataz
  • 1,025
  • 2
  • 11
  • 35
9
votes
2 answers

Should I use "get"-prefix in my method name?

I have this abstract class that is used as data repository. public abstract class AbstractDataSource { public abstract DataRow getDataRow(Key key); //or just dataRow(Key key)??? } public class CSVDataSource extends AbstractDataSource { …
Simon
  • 9,255
  • 4
  • 37
  • 54
9
votes
2 answers

Strange "getter" behaviour in IE9 when accessing property of `Number.prototype` from a number literal

Object.defineProperty(Number.prototype, 'foo', { get: function () { return this } }) console.log(10.5.foo) console.log(10..foo) // 0 in IE9! console.log(10.0.foo) // 0 in IE9! console.log(10.01.foo) console.log((10).foo) // 0 in IE9! var x =…