What is the use of explicitly calling getters and setters like "get ..." and "install ..."?

Does this mean anyone else? I would prefer:

 block.key(newKey);  // set the key for this block

and

 testKey = block.key();  // look up the key for this block

than

 block.setKey(newKey); // set the key for this block
 testKey = block.getKey(); // look up the key for this block

Firstly, "set" and "get" are redundant (and therefore add noise that reduces readability). The action (set / get) is determined by the syntax of each statement. I understand the overload. In fact, using the same exact identifier confirms that they are the same property of the object. When I read "getKey ()" and "setKey ()", maybe I'm not sure.

Secondly, if "get" and "set" should be strictly interpreted as setter and getter, then if other semantics related to setting / getting the value, for example, side effects, will be amazing.

, Smalltalk, , , "get" "set" , ? , , ?! ( )

- ?

+3
11

"get" "" - , , -, Java. :

  • Python , obj.foo, obj.foo = bar ( ). , Ruby. , "" , "setFoo" "getFoo".
  • , Objective-C, "get", Objective-C [obj foo] [obj setFoo:bar].

Java "get", "set". , "get" / "set" Java, .

+2

#, -, , "#" 2: 1 StackOverflow. , .

+4

. Java getName setName, Qt setName. Java- :

  • , , ? / ?
  • get, . , , .
  • , . , - , get set (, , do?). , , get. , , .
+4

get set . get/set grep , . , get set .

+3

Java , getter/setter -. Java-, Java. , , , Java , Java Bean / .

, Velocity templating engine - :

The answer is $block.key

:

block.getkey();
block.getKey();

block.getKey(), . , .

+3

++

int parameter() const { return m_param }
void parameter(int param) { m_param = param; }
+2

, , "driver", "drive". , , , .

+2

# ( ).

get/set , , Objective-C.

0

getDrive() setDrive(). "", .

0

, .

. CLU :

  • p.x get_x(p).

  • p.x := e () set_x(p, e).

If the interface for the object pdid not export get_xor set_x, you could not perform the corresponding operation. Plain.

Let me hear it for syntactic sugar!

0
source

Source: https://habr.com/ru/post/1714359/


All Articles