stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

This can be shortened this way
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=5130
Page 1 of 1
Author:  hopfi2k [ Fri Mar 31, 2017 4:32 pm ]
Post subject:  This can be shortened this way

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!
Author:  SteveHopwood [ Fri Mar 31, 2017 10:02 pm ]
Post subject:  This can be shortened this way

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.
All times are UTC Page 1 of 1