1

I use C# language, but I think this question is not limited with language. I have the next function and method to return image name:

public Func<string> GetImageNameFunc { get; set; }

private string getImageName()
{
    if (GetImageNameFunc != null)
    {
        string imageName = GetImageNameFunc();
        if (!string.IsNullOrEmpty(imageName))
        {
            return imageName;
        }
    }
    return "default.png";
}

In the past the logic was next:

  • use GetImageNameFunc function to override image name
  • return null or empty string to specify, that you need to use default image

Now I'd like to give one more possibility to user: he must be able to specify, that he wouldn't like to have image at all. So, he must be able to return some value from function, which would mean, that he doesn't wish to get image at all: let's say, Image.None.

I guess I can use some constant like

public const string NoImageName = "|";

, which would have a value that is inconsistent for image, and specify this constant in event comments. But I don't really like that way.

Any suggestions?

2
  • Returning a defined string (ie |) means that the consumer of this functionality must be able to understand what that means and do something with it when it sees it. Since that's the case, I'd just stick to returning null and handle the default image inside that consumer. By consumer, it could be another method within the same class. Commented Nov 21, 2014 at 12:54
  • Why not simply add a bool parameter asking if he wants to backout to a default value if no image exists? Commented Nov 21, 2014 at 12:54

1 Answer 1

3

Split the method into two cases:

string TryGetImageName()
{
    if (GetImageNameFunc != null)
    {
        string imageName = GetImageNameFunc();
        if (!string.IsNullOrEmpty(imageName))
        {
            return imageName;
        }
    }
    return null; //no default!
}

string GetImageNameOrDefault() {
 return TryGetImageName() ?? "default.png";
}

Your method was doing two things at once.

Alternative:

string TryGetImageName(string @default = "default.png") {
 //...
 return @default;
}

NoImageName = "|" that is not a good idea. It is a confusing hack. Rather than using magic strings create a class that holds a string and a boolean informing the caller whether the default was used or not.

Sign up to request clarification or add additional context in comments.

8 Comments

Yep, that's what I was trying to get at with my comment on the OP :)
Ok, but like this, how does user specify that he wants no image at all? If he returns null, it'll still be "default.png".
The user simply calls TryGetImageName() and gets null. Or, he can call TryGetImageName(@default: null).
Or you could have the method you pass to GetImageNameFunc return null and remove the string.isnullorempty check
Yes, but the only public thing here is GetImageNameFunc, and internal entry point is private string getImageName(). getImageName is called internally, and GetImageNameFunc is used to specify image name by outer classes.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.