getters

If you are using getters and setters, how should you name the private member variables?

As kind of a follow up to this question about prefixes, I agree with most people on the thread that prefixes are bad. But what about if you are using getters and setters? Then you need to differeniate the publicly accessible getter name from the privately stored variable. I normally just use an underscore, but is there a better way? ...

Which is more appropriate: getters and setters or functions?

Is it ever appropriate to abandon the "getMyValue()" and "setMyValue()" pattern of getters and setters if alternative function names make the API more obvious? For example, imagine I have this class in C++: public class SomeClass { private: bool mIsVisible; public: void draw(); void erase(); } I could add functions to g...

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 method names? ...

Entity objects getters and setters for data properties

I recently started working in Java and was introduced to the wild and crazy world of getters and setters for everything. I hated it at first, but quickly got used to it. Too used to it. I have been spending a lot of time lately thinking more about class design. One of the things I am trying to do is avoid the trap of doing getters and s...

How to trace a NullPointerException in a chain of getters

If I get a NullPointerException in a call like this: someObject.getSomething().getSomethingElse(). getAnotherThing().getYetAnotherObject().getValue(); I get a rather useless exception text like: Exception in thread "main" java.lang.NullPointerException at package.SomeClass.someMethod(SomeClass.java:12) I find it rather hard to ...

Java: Are Getters and Setters evil?

I'm currently working on a simple game in Java with several different modes. I've extended a main Game class to put the main logic within the other classes. Despite this, the main game class is still pretty hefty. After taking a quick look at my code the majority of it was Getters and Setters (60%) compared to the rest that is truly nee...

Using getters within class methods

If you have a class with some plain get/set properties, is there any reason to use the getters within the class methods, or should you just use the private member variables? I think there could be more of an argument over setters (validation logic?), but I'm wondering just about getters. For example (in Java) - is there any reason to u...

Rhino Mocks AssertWasCalled (multiple times) on property getter using AAA.

I have a mocked object that is passed as a constructor argument to another object. How can I test that a mocked object's property has been called? This is code I am using currently: INewContactAttributes newContact = MockRepository.GenerateMock<INewContactAttributes>(); newContact.Stub(x => x.Forenames).Return("One Two Three"); someobj...

Why I should use __get() and __set()-magic methods in php?

Duplicate: When do/should I use __construct(), __get(), __set(), and __call() in PHP? Well, the title says it all. This is one area that I have not found any good answer. Can anyone explain to me why/how to use them correctly? Aren't they just a bad design decision? ...

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 I may end up with handling with very a high number of fields - and for each I would have...

Getters/setters in Java

I'm new to Java, but have some OOP experience with ActionScript 3, so I'm trying to migrate relying on stuff I know. In ActionScript 3 you can create getters and setters using the get and set keywords, meaning you create a method in the class and access data through a property of an instance of that class. I might sound complicated, but...

Allen Holub wrote "You should never use get/set functions", is he correct?

Allen Holub wrote the following, You can't have a program without some coupling. Nonetheless, you can minimize coupling considerably by slavishly following OO (object-oriented) precepts (the most important is that the implementation of an object should be completely hidden from the objects that use it). For example, an object's insta...

Simple Getter/Setter comments

What convention do you use to comment getters and setters? This is something I've wondered for quite some time, for instance: /** * (1a) what do you put here? * @param salary (1b) what do you put here? */ public void setSalary(float salary); /* * (2a) what do you put here? * @return (2b) */ public float salary(); I always find...

Code Layout for getters and setters

I am writing a class that has lots of getters and setters and was wondering on what people thought about the following: The normal way is to code like the following: public function setChangeSort($changeSort) { self::$changeSort = $changeSort; } public function getChangeSort() { return self::$changeSort; } What are your opin...

How often do you see abuse of C# shorthand getters/setters?

In C# you can create getter/setters in a simpler way than other languages: public int FooBar { get; set; } This creates an internal private variable which you can't address directly, with the external property 'FooBar' to access it directly. My question is - how often do you see this abused? It seems like it has a high potential to v...

Javascript getters/setters in IE?

For whatever reason, Javascript getters/setters for custom objects seem to work with any browser but IE. Does IE have any other non-standard mechanism for this? (As with many other features) If not, are there any workarounds to achieve the same functionality? ...

C# How to make public getters and setters and private methods for a collection?

Hello All, I'd like to have a class "A" with a (for example) SortedList collection "SrtdLst" property, and inside this class "A" allow the addition or subtraction of "SrtdLst" items. But in a instance of the class "A", only allow to get or set the content of the items, not to add new items or subtract the existing ones. In code: class ...

Java Interface Usage Guidelines -- Are getters and setters in an interface bad?

What do people think of the best guidelines to use in an interface? What should and shouldn't go into an interface? I've heard people say that, as a general rule, an interface must only define behavior and not state. Does this mean that an interface shouldn't contain getters and setters? My opinion: Maybe not so for setters, but some...

Java Getters: Always execute specific method

Is there a way to define a method, which is called everytime I call a get? I have an Object Contact and don't want to set updateLastUsed(); in about 30 Getters for my Members. ...

TDD, DDD and Encapsulation

Hi all, After several years of following the bad practice handed down from 'architects' at my place of work and thinking that there must be a better way, I've recently been reading up around TDD and DDD and I think the principles and practices would be a great fit for the complexity of the software we write. However, many of the TDD sa...