Jump to content

shorthand coding: if elseif elseif elseif elseif


Mollymews
 Share

You are about to reply to a thread that has been inactive for 1658 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

i saw the following code elsewhere. Is quite common to see this kind of code in any language, not just in LSL, when there is a wide body of coders at different levels of experience

i am not link to where, as the thought here is to examine the code and think about how we can reduce the code, by removing all the if elseif elseif clauses incurred. With the idea that the readers, if they don't know already, will think about how they can do the same with their own code in similar situations going forward
 
example of if elseif elseif elseif code:

list stations = ["Zero", "One", "Two", "Three", "Four", "Five"];

string getSchedule() {
    integer now = (integer)llGetWallclock();
    integer hours = now / 3600;
    
    if (hours >= 23) return llList2String(stations,0);
    else if (hours >= 21) return llList2String(stations,5);
    else if (hours >= 19) return llList2String(stations,4);
    else if (hours >= 17) return llList2String(stations,3);
    else if (hours >= 15) return llList2String(stations,2);
    else if (hours >= 13) return llList2String(stations,1);
    else if (hours >= 11) return llList2String(stations,0);
    else if (hours >= 9) return llList2String(stations,5);
    else if (hours >= 7) return llList2String(stations,4);
    else if (hours >= 5) return llList2String(stations,3);
    else if (hours >= 3) return llList2String(stations,2);
    else if (hours >= 1) return llList2String(stations,1);
    else return llList2String(stations,0);
}

what this code does is play 6 different radio stations for 2 hours twice a day. The schedule is:

Station One plays from 1am-3am
Station Two plays from 3am-5am
Station Three plays from 5am-7am
Station Four plays from 7am-9am
Station Five plays from 9am-11am
Station Zero plays from 11am-1pm
Station One plays from 1pm-3pm
Station Two plays from 3pm-5pm
Station Three plays from 5pm-7pm
Station Four plays from 7pm-9pm
Station Five plays from 9pm-11pm
Station Zero plays from 11pm-1am
     

a way to shorten the code is to calculate the stations list index based on what we know. What we know is as described above. Example calculation method:

list stations = ["Zero", "One", "Two", "Three", "Four", "Five"];

// inline shorthand
string getSchedule_shorthand() 
{
   return llList2String(stations,
       ((integer)llGetWallClock() / 3600 + 1) % 12 / 2);
}

// longhand explanation
string getSchedule_longhand() 
{
   integer now = (integer)llGetWallClock();  // clock time in seconds
   integer hour = now / 3600;  // hour is 24-hour clock [0..23]
   hour = hour + 1;            // add 1 hour offset as radio Zero is to play between 2300hrs and 0100hrs
   integer index = hour % 12;  // take the remainder of hour divided by 12. result in [0..11]
                               // the divisor is 12. 6 stations for 2 hours each. 6 * 2
   integer index = index / 2;  // [0,1] = 0  [2,3] = 1 ... [10,11] = 5 

   return llList2String(stations, index);
}

 

Edited by Mollymews
type
  • Like 3
Link to comment
Share on other sites

What is perhaps important here is not how it looks to us eyeballers, but what the resultant bytecode looks like. It might be a series of test condition followed by jump to a common return point or execute code then jump to a common return point, in which case it is equivalent to a case statement. I've written assembler and Fortan 4 code before where there isn't even an else available, let alone case-switch. The code I wrote them was often smaller in size and just as quick as much later programs where we had 32-bits to throw around and all sorts of clever conditionals and branching instructions.

 

It is easy to get hung up on how neat or untidy the human-readable code appears, but I suspect (played around with various home-built compilers before) that inline code is about as good as you can get. 

 

Now if we had vectored execution (an extra list storage element being the name of a function or code label to go to) more complicated looking code might result in the same degree of efficiency in the byte code. 

Edited by Profaitchikenz Haiku
Because I can
  • Like 2
Link to comment
Share on other sites

3 hours ago, CoffeeDujour said:

I'd rather have switch and case.

switch/case can also lead us to do the same kind of verbose coding when a calculation can be more efficient.  Where switch/case and if/elseif are used is when the clauses do different things. Example:

if (index == 1)
   start();
else if (index == 2)
   pause();
else if (index == 3)
   stop();

switch (index)
{
    case 1: start();
    case 2: pause();
    case 3: stop();
}

 

 

     

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 1658 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...