Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does hard coding of string affect performance?

Whenever I make any app I always hardcode the string instead of referencing it from string resource from XML. App works fine but gives me warning to use @string resource

Example button:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="click here" />

My question is whether it will affect my application performance if I do so or it(@string resource) is just used for internationalization.

like image 528
Shakeeb Ayaz Avatar asked May 08 '13 05:05

Shakeeb Ayaz


People also ask

Why should we avoid hard coding?

Hardcoding is something that should be avoided as much as possible. If you hardcode something on your code it will completely "destroy" the portability of your code in a great extent. Even with a platform independant languages you will not able to say "Compile once, Run anywhere".

What is wrong with hard coding?

Hard-coding has caused quite a bit of pain for folks trying to maintain systems. One of the issues is that changing the configuration information requires a code change, and then a redeployment of the system. Another issue is if the configuration is hard-coded into multiple places within the system.

Is hard coding good?

A best practice in technology is to not reference IDs or text directly, or “hard code” them. That same best practice applies to the declarative side as well. You should not hard code IDs or text in your formulas, validation rules, processes, flows, and so on.

What is hard-coded string?

Hardcoded string are the literal strings. So, What you may be referring to is, literal strings in the data. C#


6 Answers

This is an Android lint warning to help you with localization.

Technically, hard coding strings would make your app perform a little better, as it won't have to look up the string from the corresponding R int each time. However, this performance difference will be negligible, and no human will be able to notice it.

However, you should always keep your String resources in the values folder as it makes localization very easy.

like image 58
Raghav Sood Avatar answered Oct 05 '22 08:10

Raghav Sood


I see no reason that you would get worse performance by using a hard-coded string. There are fewer steps involved with hard-coded strings. However, it is certainly best practice to separate resource strings from application and UI code.

like image 23
Matt Ball Avatar answered Oct 05 '22 08:10

Matt Ball


It will not create any performance issue. But defining strings in strings.xml is encouraged for the ease of maintainance and lolalization. For example consider the below two scenario.

Scenario 1

When you need to change a string used in many places. In your case You will have to change all the "click here" in all layouts. But if you declared in strings.xml then only change made in the xml will change them all.

Scenario 2

For another example if you want to show different language for different locale then you need to use the string.xml.

like image 30
stinepike Avatar answered Oct 05 '22 09:10

stinepike


As others said, it's for localization, but for performance, it depends on how many times per second those strings are getting looked up. I've seen a case where an app is slow in startup, and stack sampling showed 50% of the time was being spent in resource lookup of strings, and the reason the strings were being looked up was to display them on the splash screen that gives the user something to look at while the app is starting up!

like image 34
Mike Dunlavey Avatar answered Oct 05 '22 08:10

Mike Dunlavey


I dont think hardcoding a string will make your program to run any slower.. Infact it will enhance the performance as there's no need for any lookup for the String in the R.java class. Referencing the string from strings.xml is best practice due to 2 reasons:-

1- Localization 2- if you're using the same string in multiple places and would like to edit the same in all the places saves you the overhead of editing all the hard-coded strings individually.

like image 26
d3m0li5h3r Avatar answered Oct 05 '22 10:10

d3m0li5h3r


Hard coding strings will not affect the performance directly. It affects the maintainability.

In case when you hardcode a string and in later stage if you want to change the string "Click me" to "add" or something else, then you need to search your complete project to change the string where and all it is used. So Better to follow strings.xml always. :)

like image 24
Srihari Mahajanam Avatar answered Oct 05 '22 08:10

Srihari Mahajanam