0

I have a method in that checks some conditions and if required, will carry out some logic to empty an array of 'selected' elements.

Is there a standard way to name such a method?

I was thinking deselectIfRequired() or is it better to split the logic and the action so you could end up using it like:

if(shouldDeselect())
{
    deselect();
}
2

To a certain extent, it's purely a matter of opinion. Both approaches will have their fans and neither is inherently wrong, save for under one circumstance.

If the test/action code isn't atomic, then that approach should not be used, ie:

if(shouldDeselect())
{
    // could a call to shouldDeselect() ever return false here?
    deselect();
}

So for example, if testing if a file exists before deleting it, the above test/action approach is an anti-pattern and shouldn't be used. If it can't change, then it's a valid approach.

| improve this answer | |
5

I think I would want my function to be more relevant to the actual job/data being processed rather than a generic sounding shouldDeselect(). That doesn't tell you anything really. Maybe something more inclusive of why deselection is flagged?

As an example:

if(martians_have_landed())
{
    cancel_all_shore_leave();
}

How we discover if martians have landed is an implementation detail that we don't need to care about, just the result. Similarly we don't need to know if canceling all shore leave will result in certain records being deselected or not, that's implementation detail.

| improve this answer | |
  • This is an interesting way to approach the issue thanks. – Des Horsley Apr 14 '16 at 0:29
2

As shouldDeselect() doesn't carry out the deselect action itself it can be named checkDeselect(), which I would find more clear (and consider a more common naming pattern), and I would even add what to check, e. g. checkDeselectUsers().

But as DavidArno already mentioned this is as opinion-based as many naming decisions, and I expect the opinion police to strike at any minute...

| improve this answer | |
  • 1
    I was a little concerned about the opinion issue, but there are several naming convention questions that have been left alone, so, what will happen will happen. – Des Horsley Apr 13 '16 at 11:09
  • 1
    where do you see this naming convention? my experience tells me to name boolean returning methods with predicate names, and "action" methods with imperative names. isPrime, shouldExit, needsRepainting VS factorize, exit, repaint. – sara Apr 13 '16 at 19:29
0

Probably method clients don't need to know whether any action was actually performed o no. I think that an idempotent deselect() method is a winner here. It could return true/false to inform whether it actually deselected anything if needed.

| improve this answer | |
  • "deselectIfRequired" tells the caller right in the name that the function can be called whether the item is selected or not. With a name "deselect" I need to consult some documentation. If there is a bug around the code, I'll need to examine what exactly deselect does, wasting my time. – gnasher729 Apr 14 '16 at 8:16
  • Well, this is classic intention not algorithm vs intention revealing names tension. IMHO, as a client of a public method I don't mind about how it does it, but if it's a private method I actually do care about it. That "public vs private interface" is how I use to decide on this kind of situations. – juanignaciosl Apr 14 '16 at 12:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.