Shop OBEX P1 Docs P2 Docs Learn Events
Best convention for coding IF THEN in SX/B? — Parallax Forums

Best convention for coding IF THEN in SX/B?

RobotWorkshopRobotWorkshop Posts: 2,307
edited 2007-01-28 05:26 in General Discussion
From source examples and the SX/B help file I can see several different ways of structuring IF THEN statements. Is there a preferred method for readability of SX/B code? I can write it several different ways and if there is best practice and established convention i'd rather follow that to start with. In particular the example below:

Wait_Ack:
IF ACK = 0 THEN
GOTO Wait_Ack:
ENDIF

or

Wait_Ack:
IF ACK = 0 THEN Wait_Ack

or

Wait_Ack:
IF ACK = 0 GOTO Wait_Ack

I've got my programs working as expected and just want to make sure they are going to be easy to follow for someone else.

Best Regards,

Robert

Comments

  • PJMontyPJMonty Posts: 983
    edited 2007-01-26 22:56
    Robert,

    Personally I would use the 2nd version. It leaves out the extraneous and redundant GOTO, and it is very compact without sacrificing readability. If you had more going on in the loop other than the check on ACK, then the first version makes sense since it has an ENDIF to contain anything that needs to be grouped within the loop.

    Others may feel differently.

    Thanks,
    PeterM
  • JonnyMacJonnyMac Posts: 8,942
    edited 2007-01-26 23:00
    I agree with Peter and would only use the first form if there was an ELSE section. If you're doing more than one thing after the [noparse][[/noparse]implicit] THEN you're forced into the first form, but that's not the case here. Another form you could take, that creates [noparse][[/noparse]internal] labels for you, is this one:

    DO WHILE Ack = 0
    LOOP
    

    Post Edited (JonnyMac) : 1/26/2007 11:04:49 PM GMT
  • RobotWorkshopRobotWorkshop Posts: 2,307
    edited 2007-01-28 05:26
    Thank you both for the replies. I had read through the style guide but didn't see that there was a preferred method. I'll go with the suggestion on using the 2nd method except when I have other things to do and in that case go with the first one.

    Robert
Sign In or Register to comment.