This can be shortened this way

Locked
User avatar
hopfi2k
Trader
Posts: 35
Joined: Tue Nov 15, 2011 11:03 pm
Location: Germany

This can be shortened this way

Post by hopfi2k »

Hi codes,

While browsing the HGBnD core library I saw some areas that can be shortened.
One example is the following function:

Code: Select all

string GetTimeFrameDisplay(int tf)
{
   if (tf == 0)
      tf = Period();
      
   
   if (tf == PERIOD_M1)
      return "M1";
      
   if (tf == PERIOD_M5)
      return "M5";
      
   if (tf == PERIOD_M15)
      return "M15";
      
   if (tf == PERIOD_M30)
      return "M30";
      
   if (tf == PERIOD_H1)
      return "H1";
      
   if (tf == PERIOD_H4)
      return "H4";
      
   if (tf == PERIOD_D1)
      return "D1";
      
   if (tf == PERIOD_W1)
      return "W1";
      
   if (tf == PERIOD_MN1)
      return "Monthly";
      
   return("No recognisable time frame selected");

}//string GetTimeFrameDisplay()
This can be rewritten into just two lines of code:

Code: Select all

string GetTimeFrameDisplay(int tf)
{
   const string p[9] = { "M1", "M5", "M15", "M30", "H1", "H4", , "D1", , "W1", "Monthly" };
   return p[(int)floor(log(tf))];
}//string GetTimeFrameDisplay()
I know my version is missing the return string for unrecognized time frames, but realistically this isn't need as it should never happen when properly coding ;)

Cheers,
Andy

P.S. The two extra commas in p[9] are necessary!
User avatar
SteveHopwood
Owner
Posts: 9904
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

This can be shortened this way

Post by SteveHopwood »

You are suggesting replacing code that works under all circumstances with code that will work under most circumstances? Great Plan.

I may have mentioned in other threads that I am not a fan of people questioning code that has worked flawlessly and seamlessly for years? I trust I did so. For sure, I seem to recall this. Just in case not then here goes: I am not a fan of people questioning code that has worked flawlessly and seamlessly for years. In contemplating my previous, you might also care to contemplate my love of Masterly British Understatement, and that I indulge said love all the time. Starting to get where we are going yet?

Do feel free to set up your own forum for dimwits with about as much coding experience as you have. No doubt you will be able to attract the expertise of the likes of the contributors here after several years of your contribution to forums elsewhere. Just like me several years ago.

This idiotic thread is locked and the OP is one lunatic post away from being unable to post here again, ever.
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
Locked

Return to “Coders Hangout”