0

In OOP coding, there is a long standing naming convention to name accessor/mutator methods getThing and setThing. These directly manipulate the properties, which are often private.

Should these naming conventions be 'reserved' solely for manipulating the object properties? or can they be used in other methods - getFileContents ?

I think readFileContents is much clearer as it implies the objects property in this case isn't being accessed, but rather a files contents are being read. However, it is a little ambiguous in that it doesn't imply the results are being returned like getFileContents would.

1
  • 2
    I would point out that many tools and frameworks in the Java world expect the Java Beans specification. If you aren't interacting with those or passing the information to/from them as a Java Bean, then its probably a non-issue. However, this is a convention for one segment of the OOP world. Commented Jul 15, 2015 at 14:06

2 Answers 2

1

Naming conventions are simply suggestions. There are no set rules for anything.

The only guideline you need to follow is to name your function according to what it does. If your function readFileContents both reads and returns parts of the file, consider splitting it up into two functions, readFileContents and getFileContents, for example. readFileContents can peruse the file for interesting data and then send it to getFileContents when it wishes to return something. This will separate the concerns so that each function is only doing one thing.

2
  • So in this case, readFileContents could save something to the fileContents property and then you would use the getFileContents function to access it? Commented Jul 15, 2015 at 14:23
  • 1
    Right. That separates the concerns so if you have a problem with reading or writing, they are in two separate functions. Commented Jul 15, 2015 at 14:27
1

As opposite examples, in Objective-C or Swift you won't see any setThing anymore, and there never was a getThing. In modern use, you have properties in Objective-C and variables in Swift, which are used exactly as if they were plain old member variables that you can read and write. What's going on behind the scenes is then something entirely different. Your getter and setter may just read or write a value, it might do lazy evaluation, it can get notifications just before and just after a setter is called, it can implement getter, setter or both in code. The user of a class sees nothing but a variable.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.