1
\$\begingroup\$

I would like to arrange a system messages dictionary for an application in JavaScript.

As an initial intention, I thought something about the following structure:

export const SYS_MESSAGES = Object.freeze({

    fileSizeExceeded: {
        code: "FILE_SIZE_EXCEEDED",
        text: "The file size exceeds allowed limit"
    }

});

But then I realized, the structure could be even more simple:

export const SYS_MESSAGES = Object.freeze({

    FILE_SIZE_EXCEEDED: "The file size exceeds allowed limit"

});

Which option of two structures is a preferred-one from maintenance/style perspective?

\$\endgroup\$
1
\$\begingroup\$

I consider that the second option is better for the following:

  • The first option is more verbose than the second, and it is in a way that there is no additional information which allow other developers or even you to obtain meaningful information.
  • The second option has a constant style name which represents what you are trying to tell us, "hey, this is a constant and its value is ..." and so, it clearly reflects a shorter and descriptive code style.
  • Having less properties (in your objects) implies less space, and less space computed implies less execution time. (even if it is very small).
  • Less content helps you to debug faster.

Well, I hope it helped you.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thanks for the opinion, I also considered the second option, but then I realized that with a such structure it will be more difficult to get a code of a message. E.g. SYS_MESSAGES.FILE_SIZE_EXCEEDED returns always a text message ("The file size exceeds allowed limit"), but if I want to get the message code itself (FILE_SIZE_EXCEEDED), then I'll have to play with Object.keys() and rely on a permanent order of messages in the dictionary, which can be changed at any time day and will require adjusting the entire code base. That's why the option #1 is better, although it's more verbose. \$\endgroup\$ – Mike B. Jun 15 at 19:16

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.