Question
When a user asks a question and provides a code sample, should I try to answer the question without changing his/her code too much, or can I also provide some small constructive feedback on the given code (if small enough / relevant) with an updated snippet.
Origin
I remember reading that SE is not meant for direct code feedback as that would make questions / answers very specific to a situation, but I do feel that some code feedback (like the example below) could increase someones knowledge.
I am talking about answering the question first and then have an extra note with something like 'As a hint, you could also achieve the same result with less code, making it more readable for yourself and others' and then providing a snippet with changes. (I am not talking about any personal aspects such as code conventions).
example: (C# Unity)
private void Update()
{
StartCoroutine("Changecolor", 3f);
}
IEnumerator Changecolor()
{
yield return new WaitForSeconds(3);
if(startstop == true)
{
int random = Random.Range(1, 4);
if (random == 1)
{
m_SpriteRenderer = GetComponent<SpriteRenderer>();
m_SpriteRenderer.color = Color.blue;
}
else if(random == 2)
{
m_SpriteRenderer = GetComponent<SpriteRenderer>();
m_SpriteRenderer.color = Color.red;
}
else if(random == 3)
{
m_SpriteRenderer = GetComponent<SpriteRenderer>();
m_SpriteRenderer.color = Color.green;
}
else
{
m_SpriteRenderer = GetComponent<SpriteRenderer>();
m_SpriteRenderer.color = Color.yellow;
}
}
}
private void OnMouseDown()
{
startstop = !startstop;
}
Which could be done with something like:
// store all colors in an array.
Color[] m_Colors = new Color[] { Color.blue, Color.red, Color.green, Color.yellow };
private void Update()
{
if (startstop)
{
StartCoroutine("Changecolor", 3f);
startstop = false;
}
}
IEnumerator Changecolor()
{
yield return new WaitForSeconds(3);
m_SpriteRenderer = GetComponent<SpriteRenderer>();
// select a random color from the array and apply it.
// Count() - 1 as arrays start counting at 0.
int random = Random.Range(0, m_Colors.Count() - 1);
m_SpriteRenderer.color = m_Colors[random];
}
The question I used as an example: changing color of sprite not every frame
final note: My first (and last) post on GDMeta has been a while ago, so if anything about my question is incorrect / wrong, please let me know.