-4

Its ok to have hardcoded strings on backend? Example:

Log.e("Error connecting to the database")

The text "Error connecting to the database" should be hardcoded there? Or should it be in a separated file with all strings? Or in a constant at the beginning of the class?

  • 1
    What do you mean by "OK?" (Don't say "best practice"; that's just a synonym for "OK"). – Robert Harvey Jun 9 at 13:32
  • I mean, how is it done in real world business applications. How would you do it? I am interested in learning about your experience. – RangerCode Jun 9 at 13:39
  • It is done the way that best meets the application and business's specific needs. In small applications involving a single language, I usually hard-code them (they tend to be specific to the situation), but you can't hard-code these strings if they need to be internationalized, so some other technique is needed. – Robert Harvey Jun 9 at 13:44
  • You have received a number of down votes because your question is open ended and could get people providing opinions based on the insufficient information provided . You may want to recall your question and ask a more specific one to avoid further down votes. – Jason K. Jun 9 at 23:32
  • In my opinion it will depend on your application. – Ishan Shah Jun 30 at 6:46
0

The answer is, it depends.

It depends on who is supposed to consume the string and what the conventions within the team are.

If the string is a message to the end-user, then it it likely that the string needs to be translated. The actual message for the end-user will live in a translation file and in the code you will only have a key for locating that message. The format of that key can range from a numeric identifier, a textual identifier or even a full sentence. Where those keys are defined depends on local conventions and ranges from a central file (most common with numeric keys) to the point where you need the text (most common with full sentence keys).

If the string is a log message that is intended for the developer/maintainers of the software, then it is most common to have that string hard-coded in the logging statement.

If the string is used as a identifier within the software, then it is common to create a constant for it at the appropriate scope, so that if you need to change the content of the string, you only have to do it at one spot and you can't accidentally forget to update it in a location where it is used.

| improve this answer | |

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.