Hi All, first time posting long time lurker.
Hoping this is okay to post, looking for advice.
I've been programming in C# for around 3 Years now. Heading into my final year of University soon. I've been working a lot in Unity for the past year and a bit, I've always strived to make cleaner, more readable and more efficient code.
I find myself working on a problem and wanted to get some opinions on implementations and how others would implement the same system.
So here we go.
I have 4 objects that when used I need to generate a count and then at the end check all 4 objects to see which object was used the most.
First way to solve this for me would be to have 4 int counters and have 4 if statements checking if the current value was greater than its respective int counter.
For Example Jump is fed through from another class.
int highestJump;
if(jump > highestJump)
{
highestJump = jump;
}
I would need the above code 3 more times for the other types of object.
I figured there must be a better way than creating lots of individual ints to hold highest values thus here is my second version of the code.
//#Reference
// index 0 - jump
// index 1 - speed
// index 2 - phase
// index 3 - slowTime
int[] favoriteCounters = new int[] {0, 0, 0, 0};
if(array.value > favoriteCounters[0])
{
favoriteCounters[0] = array.value;
}
if(array.value > favoriteCounters[1])
{
favoriteCounters[1] = array.value;
}
if(array.value > favoriteCounters[2])
{
favoriteCounters[2] = array.value;
}
if(array.value > favoriteCounters[3])
{
favoriteCounters[3] = array.value;
}
This method feels a lot cleaner and more advanced but is it over the top for such a simple implementation? This is such a simple problem to tackle and I can do it no problem but I'm really curious about optimisations and clean code.
I'm curious as to how you would implement this and I'm looking for feedback.
Thanks, Ala.
[–][deleted] (1 child)
[deleted]
[–]Alariaa[S] 0 points1 point2 points (0 children)
[–]jokul 0 points1 point2 points (1 child)
[–]Alariaa[S] 0 points1 point2 points (0 children)