user57962: I've read that the Arduino String is bad as it causes memory fragmentation, so I try to avoid them mostly. But I still have some occurences where I'm not quite sure if they are ok after all.
The first is in print statements:
I have a function which prints a success or error message to the serial port, like so: Serial.println("Success");. Is this ok or should I (for example) pre define the String using #define SUCCESS "SUCCESS"?
Which leads me to the next case, defines:
I'm using MQTT and have the topics defined like above, e.g. #define TOPIC_COLOR "light/color". Is this fine?
A third one is with ArduinoJSON:
I'm using Strings as keys, like so lights["red"] = redVal;. This call is made at multiple places so I assume the string would be created multiple times? A workaround/improvement I've thought of is using an enum defining the properties and using that as key.
Thanks for all comments, tips and explanations!

user58653: +1 to @majenko's comments. I tend to avoid the String class, as I like to know what is actually going on, and I stick to char * and char [] in general (and the stdlib functions like sprintf, strlen, etc).
Couple of notes on your question:

"foo/bar" is fine. Some characters have special meanings in the C language itself, so you need to escape them with the \. For example if " was part of your literal string, you'd write "foo\"bar\""

I tend to use ArduinoJSON to parse. If my app is just sending JSON payloads, I just build it with sprintf or print which results in smaller programs. But, I guess it is a matter of choice. It is a great library.



57962: <p>I've read that the Arduino String is bad as it causes memory fragmentation, so I try to avoid them mostly. But I still have some occurences where I'm not quite sure if they are ok after all.</p>
<p>The first is in print statements:<br />
I have a function which prints a success or error message to the serial port, like so: <code>Serial.println(&quot;Success&quot;);</code>. Is this ok or should I (for example) pre define the String using <code>#define SUCCESS &quot;SUCCESS&quot;</code>?</p>
<p>Which leads me to the next case, defines:<br />
I'm using MQTT and have the topics defined like above, e.g. <code>#define TOPIC_COLOR &quot;light/color&quot;</code>. Is this fine?</p>
<p>A third one is with ArduinoJSON:<br />
I'm using Strings as keys, like so <code>lights[&quot;red&quot;] = redVal;</code>. This call is made at multiple places so I assume the string would be created multiple times? A workaround/improvement I've thought of is using an enum defining the properties and using that as key.</p>
<p>Thanks for all comments, tips and explanations!</p>
 58653: <p>+1 to @majenko's comments. I tend to avoid the <code>String</code> class, as I like to know what is actually going on, and I stick to <code>char *</code> and <code>char []</code> in general (and the <code>stdlib</code> functions like <code>sprintf</code>, <code>strlen</code>, etc).</p>
<p>Couple of notes on your question:</p>
<ol>
<li><p><code>&quot;foo/bar&quot;</code> is fine. Some characters have special meanings in the C language itself, so you need to escape them with the <code>\</code>. For example if <code>&quot;</code> was part of your literal string, you'd write <code>&quot;foo\&quot;bar\&quot;&quot;</code></p>
</li>
<li><p>I tend to use <code>ArduinoJSON</code> to <em>parse</em>. If my app is just <em>sending</em> JSON payloads, I just build it with <code>sprintf</code> or <code>print</code> which results in smaller programs. But, I guess it is a matter of choice. It is a great library.</p>
</li>
</ol>
