Main Board > Programming

Strange if then else behavior

<< < (3/3)

Stéphane Devouard:
After using IF {} ELSE {} for several years
I found it hard to follow especially when having several nested IFs
I started adding remarks to denote the level of nesting such as :

--- Code: ---if some_condition { ! <-
if nested_condition { ! <--
if yet_another_condition { <---
... some code ...
} ! --->
} ! -->
} ! ->
--- End code ---
But it is still hard to follow


I started coding in other languages (C#, PHP, JS) with fancy IDEs that have indentation
But even witht all the bells and whistles, a complicated algorithm is hard to follow
And I have watched numerous coding tutorials where it is recommended to avoid complicated nested if/else structures and use guards instead
i.e.
Do all the tests that prevent the main logic to be processed, and return from the routine/method/functions
Then do the processing
Using Mike's GOSUB technique, that would be :

--- Code: ---GOSUB Do_Some_Logic
...
Do_Some_Logic:
if some_condition then return
if some_other_condition then return
if yet_another_condition then return
! // now do the logic
return

--- End code ---


I also use the simple FOR/NEXT structure A LOT :

--- Code: ---for (1)

if some_condition then break
if some_other_condition then break
if yet_another_condition then break
! // now do the logic
next

--- End code ---

Navigation

[0] Message Index

[*] Previous page

Go to full version