📜 ⬆️ ⬇️

How to: Properly call class property methods




In our company, the following is written about this (excerpt from the programmer’s instructions, the part “Recommendation on the formation of names”):

Methods for obtaining a binary attribute
')
The name of the method for obtaining a binary attribute must be a predicate (a question that can be answered with Yes or No). In most cases, the method name must begin with an Is or Has verb.

Methods for getting and setting a non-binary attribute

You can add to the name of the method of obtaining the attribute of the verb Get. If, in addition to the attribute acquisition method, there is also an attribute installation method in the class, the names of these methods should begin with the verbs Get and Set, respectively.

Now, let's take an open “arbitrary” solution, consisting of about 50 projects, mostly related to the product shell, and including about 5,000 header files. And let's see what naming is found there:



The pie chart shows the relative distribution of the various “get” methods. On the rest of the semantic verbs: Is, Can, Has, Need, Should. Just be careful, some diagrams built literate PM.

Analysis of the results showed that there are a small number of paired Is / Set methods, in which the Set method does not accept any parameters. Those. the default value is false, and the set method cocks it, and this transition cannot be canceled. This is meaningful for IsFailed () / SetFailed (). But in many cases, this restriction results more from UI restrictions (which the user cannot do this now) than from the logic of the property.

It should also be noted that the search for someone else's solution showed a radically different distribution. Therefore, the use of specific modal verbs is more likely to be associated with people in a team than with the rules of the organization.

So how to correctly name the method? NeedDoSomething or ShouldDoSomething? Which modal verb to choose? Let's try to answer this question. It is obvious that there should be six such methods, and how to use them is indicated below:



bool Do( Tction type, IObject* obj ) { //     if( !detector.ShouldDo( type, obj ) ) { return false; } //    if( !callback.MayDo( type, obj ) && !DareToDo( type ) ) { //            //     –   ,   check( !detector.MustDo( type, obj ), ERR_MUST_NOT_MAY ); return false; } //   foreach( executor in executors ) { if( executor.OughtToDo( type, obj ) ) { if( executor.CanDo( obj ) ) { executor.Do( obj ); } check( !detector.MustDo( type, obj ), ERR_MUST_NOT_CAN ); return false; } }; // executor   check( false, ERR_EXEC_NOT_FOUND ); } 

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


All Articles