The prefix "Is" in the method name for validation methods

I read Code Complete lately, based on many links here, as well as on another, and had a question about community names. Should the prefix "Is" be used in logical methods that determine if the event was successful? Here is a sample code of two different naming schemes I tried:

migrationSuccessful = CopyData(); if (VerifyCopyData()) migrationSuccessful = CleanupData(); 

against

 migrationSuccessful = CopyData(); if (IsDataCopied()) migrationSuccessful = CleanupData(); 

Note the difference between VerifyCopyData and IsDataCopied. For me, IsDataCopied is more meaningful and makes the code flow more descriptive.

Thanks for your thoughts!

EDIT: based on some comments, I thought I would clarify what the IsDataCopied method does. It moves across multiple directories and files and ensures that source and target directories / files match.

+6
naming-conventions
source share
11 answers

I agree with Matthew Jones that it is preferable to call the IsDataCopied method. I would like to add, however, that when naming code elements associated with other code elements, I usually think that it is better to place Is inside the name and not in front, because it will be closer in Intellisense to this element to (and therefore easier to find). For example, in Winforms Form has the Handle and IsHandleCreated . If IsHandleCreated was named HandleIsCreated , it will be close to the Handle property and will be easier to find (not a hidden path in Intellisense).

+5
source share

I agree with you. For me, IsDataCopied is much more readable.

More generally, VerifyCopyData is ambiguous for me whether it is strictly a validation method or actually does something. IsDataCopied is very clear that it only checks if the data is copied.

+8
source share

"Must" is a broad term. Do what makes sense to you. In many cases, the use of “Is” is explained, but not always.

Again, do what makes sense.

+1
source share

I find “Is” more understandable. A function named "VerifyCopyData ()" may throw an exception when data has not been copied or has side effects. Probably not, but if I read it, I would not be sure what to expect from it. The function with the Is prefix is ​​clear: this function will have no side effects and will simply return a logical answer to the question.

The presence of an action verb (check) means that the function is really doing something. Using "Is" does not have an action verb, so it is understood that the function simply checks the status and does nothing.

+1
source share

This is definitely subjective, and at different times (working with different libraries and even different languages) I always used these prefixes and never used them. Mixing this convention with its use was often a mistake. Decide which one is most clear based on context, including what is already being done in this project / library, etc.

For example, obj.DataIsCopied() better for the native English speaker, but then you have !obj.DataIsCopied() (or not ). You should be faced with the fact that you are writing code and need conventions, and these conventions subjectively add meaning (in a descriptive sense).

In addition, I would expect that any method named Verify will do some “real” work for verification, and Is should either return a previously computed / already available value, or calculate it trivially. In languages ​​where you have properties, it is much easier to express, because you can separate things that are “actions” from those that are not. For example. using if (obj.data_copied) whether this is a data element or a trivial function such as IsDataCopied is encapsulated.

+1
source share

IsDataCopied less readable in that the intent of the method is unknown. If it just returns a boolean and does nothing, then that’s good. But, if it does the verification, then there may be a lot of code hidden behind an innocent looking conditional statement.

My advice:

 bool isDataProperlyCopied = VerifyCopiedData(); if ( isDataProperlyCopied ) { ... } 
+1
source share

data.isCopied ()

Because English is not VSO, but SVO.

+1
source share

It’s good if your methods may contain a verb that helps describe what this method does. VerifyCopyData sounds like it is checking data, while isDataCopied sounds like it is checking for the existence of a fact. This is a subtle difference. Nitpick: I would name the first VerifyCopiedData method.

0
source share

if it were a different name, I would have thought, but VerifyCopyData definitely less readable than isDataCopied .

0
source share

I agree. The presence of the prefix "Is" makes it more clear that this is a logical function. In fact, this is the standard in many naming conventions for prefixing not only methods, but also properties and variables.

The Verify prefix is ​​not so clear, because it may well be a void function or a subroutine that returns nothing. For example, Verify may mean that it checks several things and throws an exception if something is wrong. In the case of the prefix Is, it is understood that you are asking a question and therefore the answer will be returned.

0
source share

I prefer Is when it comes to variables and properties.

 if(o.IsDataCopied) { } 

But in this case, with a method call, I would choose something like

 if(DataIsCopied()) { } 
0
source share

All Articles