Idea for Numbering your large if statements

Studies that have been contributed to the community by other users. If you’ve got something useful to share, that’s great!
bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Idea for Numbering your large if statements

Postby bowlesj3 » 15 Feb 2010

Not a contributed study but rather something to consider to help with coding your studies and maintaining them. Specifically to get back to any section of the if statement very fast.

Each if statement (that is large or that I think might become very large) has a code number I just randomly grab out of my head. I potentially apply the number to 5 places in the if statement. As you can see below there is a sub code structure too (B1, E1, B2, E2). I apply these where it seems appropriate. I included all four subcodes in the example below just to make it clear as to why I chose these subcode numbers.

Code: Select all

if bartype = 1 and barinterval = 1 then {IF:745 - purpose comments}
begin {IF:745B1}
{large amounts of code in here with deep nested if statements}
end {IF:745E1 - if bartype = 1 and barinterval = 1 then}
else
begin {IF:745B2 - if bartype = 1 and barinterval = 1 then}
{large amounts of code in here with deep nested if statements}
end; {IF:745E2 - if bartype = 1 and barinterval = 1 then}
It should be very clear now why I used (B1, E1, B2, E2) for the subcodes.
B1 is Begin1.
E1 is End1.
B2 is Begin2.
E2 is End2.
This allows you to jump to any section of the if statement fast.
Duplicating the actual if statement within the comment code as I did above is useful when you have a large if statement that is way off your screen. If you change the actual if statement such as from > to >= then the numbering system lets you update the comments faster to match this change.

If you want to quickly jump through the large if statements you have assigned a code to, then search on "IF:".

IF you want to jump around just this one if statement use "IF:745"

It is easy to memorize the if numbers or just quckly jot the number down on some scrap paper at your desk to get back to any section of the if statement fast. You could even list the codes at the top of your study with the comment for each if statement. Next time you work on the study just cut/paste the top section out to a scrap word document and within 30 seconds you are moving around your study you completed 3 months ago just as fast as if you wrote it yesterday.

As an after thought, I am working on two studies that have similar if statements (one is 1 minute bars and the other is 10 second bars). I am using the same if code number for the statement that has exactly the same function in both studies.

I find structuring my if statements with the code indent method above keeps the code cleaner as well as lending itself to this search technique.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Postby TJ » 17 Feb 2010

thanks for the tip
that's a good way of tracking multiple Begin and Ends.
A few of my large indicators would benefit from this coding habit.

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Postby bowlesj3 » 17 Feb 2010

Hi TJ,

Your welcome.

I was using it for two days (back to trading again). I found it also helps when I get those large stacks of deep nested if "end;" statements at the bottom of a deep nested if structure. Specifically less confusion. Less confusion is good :D Especially when I want to add a statement just before one of those "End;" statements and I need to be sure I get the right one.

John.

bowlesj3
Posts: 2180
Joined: 21 Jul 2007
Has thanked: 227 times
Been thanked: 429 times

Re: Idea for Numbering your large if statements

Postby bowlesj3 » 11 Sep 2010

I just came up with a unique way of formatting the if statement so that it lines up the comment at the very bottom with the if statement itself as some of the better languages do. It does this while retaining the normal allignment with the "begin" and "end" components of the statement.

Code: Select all

if InteractiveProcessDone = "Y" then
begin
{Lots of user code}
end
else
begin
{Lots of user code}
end;
{END: if InteractiveProcessDone = "Y" then}
If you want to apply the numbering for very large statements it would be done like this.

Code: Select all

if InteractiveProcessDone = "Y" then {If:134 - comment}
begin
{Lots of user code}
end
else
begin
{Lots of user code}
end;
{END: If:134 - if InteractiveProcessDone = "Y" then}
This allignment technique makes it easier to scan up and down and find the start and end of the statements. The numbering of course can be used for an instant jump.


Return to “User Contributed Studies and Indicator Library”