-1
\$\begingroup\$

I have two methods which are GetMinimumSize() and GetMaximumSize(). They return the maximum size of the window, and they take one parameter, which is an enum either WindowSizeLimitParam::Width, or WindowSizeLimitParam::Height, because of course, we want to specify which maximum or minimum we want to get.

Here is the enum

enum class WindowSizeLimitParam
{
    Width = 0,
    Height = 1
};

Some time ago, I didn't have this enum, and I just had 4 methods which were just for min size width, min size height, max size width, and max size height, but for the sake of shortness, I decided to do it with an enum. So my question is, is this a good design choice? Using an enum to specify the size type which we want to get? Keep in mind I have other methods like setting size or getting size, but they don't use enums, they are GetWidth(), GetHeight(), SetWidth(), SetHeight(). If I'm using an enum for the size limit, should i use an enum here and just have SetSize() and GetSize() in which i pass an enum?

Is it generally a good idea to use enums if you are developing an API, which is kind of what I am doing here.

\$\endgroup\$
3
\$\begingroup\$

It's important to be consistent - if you already have GetWidth, GetHeight, SetWidth and SetHeight then it makes sense to have GetMinimumWidth, GetMinimumHeight, GetMaximumWidth, and GetMaximumHeight.

An alternative approach is to retrieve every value in a struct, which further simplifies your API surface and may improve performance if you expect a consumer to call most other getters:

struct WindowSizeInfo
{
    const int width, minWidth, maxWidth;
    const int height, minHeight, maxHeight;
}

WindowSizeInfo Window::GetSize()
{
    return WindowSizeInfo( ... );
}
\$\endgroup\$
  • \$\begingroup\$ Thank you very much. It really is a better way of doing it when I give it a second thought \$\endgroup\$ – Christian Panov Feb 18 at 10:56

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.