Programming Style. Linear VS Structured
-
- Lord of Gaming
- Posts: 230
- Joined: Sat Feb 24, 2007 1:07 pm
- Location: Searing in South Florida
Programming Style. Linear VS Structured
Whats your programming style.If its structured,reply STRUC,
if its linear,reply LINE,then I rank it up in 10 days.
if its linear,reply LINE,then I rank it up in 10 days.
- Larry Laffer
- Admin
- Posts: 4143
- Joined: Sun Feb 19, 2006 8:06 am
- Location: Romania
It's extremely usefull.
Think about a coded block, you can call it several times and jump back to the next %% line.
If you need, let's say, a specific process 20 times ( suppose here you need 500 lines or so for that process) , in stead of writing those 500 lines 20x you simply jump to the subroutine and back where you were (now yeah a line down or whatever if there is a variable input ... whatever).
It is very much used in industrial coding ( PLC - Automatisation ...) where repetitive actions are needed.
A good example are electric motors that need a starting/stopping slope , or a check if they are running/stopped before you can safely go to the next step.
You don't code such routine for 2000 motors seperately, you code one and call that one when needed.
Another example ?
Emergency stop buttons, if you ever seen those big red knobs in industry ...
Some complex machinery have hundreds of those, and some machinery needs a specific stopping routine, first shut that motor, next that one, break 1 on, next motor , break 2 on ...
You will NOT write that whole sequence for every emergency stop button, no, you write it once, and call a subroutine with an IF or an OR when such a button is hit.
Think about a coded block, you can call it several times and jump back to the next %% line.
If you need, let's say, a specific process 20 times ( suppose here you need 500 lines or so for that process) , in stead of writing those 500 lines 20x you simply jump to the subroutine and back where you were (now yeah a line down or whatever if there is a variable input ... whatever).
It is very much used in industrial coding ( PLC - Automatisation ...) where repetitive actions are needed.
A good example are electric motors that need a starting/stopping slope , or a check if they are running/stopped before you can safely go to the next step.
You don't code such routine for 2000 motors seperately, you code one and call that one when needed.
Another example ?
Emergency stop buttons, if you ever seen those big red knobs in industry ...
Some complex machinery have hundreds of those, and some machinery needs a specific stopping routine, first shut that motor, next that one, break 1 on, next motor , break 2 on ...
You will NOT write that whole sequence for every emergency stop button, no, you write it once, and call a subroutine with an IF or an OR when such a button is hit.
Linear = any old book
Structured = Choose Your Own Adventure
Or, a better example... say you want to program walking to the store and back to get some peanut butter and bread...
you'll have to walk to the store open a door walk around in the store to find the bread then take the bread and walk more to find the PB, which you will also take. Then you'll want to pay for the goods, walk to the door and open it again. Then you're going to walk home...
Now, from a linear POV [missing a lot of validation checks]:
And now, from a structured POV:
Structured = Choose Your Own Adventure
Or, a better example... say you want to program walking to the store and back to get some peanut butter and bread...
you'll have to walk to the store open a door walk around in the store to find the bread then take the bread and walk more to find the PB, which you will also take. Then you'll want to pay for the goods, walk to the door and open it again. Then you're going to walk home...
Now, from a linear POV [missing a lot of validation checks]:
Code: Select all
do until location = store
Move left foot
move right foot
get location
repeat
move left arm until hand is on door
push with arm
move left foot through door
move right foot through door
release hand from door
lower arm
do until location = peanut butter
move left foot
move right foot
get location
repeat
raise right arm until right hand is on the peanut butter
place right hand on peanut butter
tighten right-hand fingers around peanut butter
raise peanut butter off shelf
bring right hand to bag
put pb in the bag
loosen right-hand fingers
take right hand off of pb
raise right arm until hand is not in bag
do until location = bread
move left foot
move right foot
get location
repeat
raise right arm until right hand is on the bread
place right hand on bread
tighten right-hand fingers around bread
raise bread off shelf
bring right hand to bag
put bread in the bag
loosen right-hand fingers
take right hand off of bread
raise right arm until hand is not in bag
do until location = cash
move left foot
move right foot
get location
repeat
lower left arm until left hand is in pocket
do until money in left hand = bill total
put left hand on coin
tighten left-hand fingers around coin
raise arm until hand is level with cashier
align left hand to cashier's hand
loosen left-hand fingers off of coin
lower left arm until left hand is in pocket
repeat
raise right arm until right hand is on bag handle
tighten right-hand fingers
raise right arm until bag is off counter
move left foot
move right foot
lower right arm
do until location = door
move left foot
move right foot
check location
repeat
move left arm until hand is on door
push with arm
move left foot through door
move right foot through door
release hand from door
lower arm
And now, from a structured POV:
Code: Select all
do until location = door
Walk
repeat
position arm(right, down, pocket)
do until location = pb
Walk
repeat
open(left, up)
position arm(right, up, pb)
grab(right)
position arm(right, down, bag)
release(right)
do until location = bread
Walk
repeat
position arm(right, up, head)
position arm(right, down, bread)
grab(right)
position arm(right, down, bag)
release(right)
do until location = cash
Walk
repeat
do until money = bill total
money = pocket change - coin
position arm(left, down, coin)
position arm(left, up, cashier hand)
repeat
position arm(right, up, bag)
grab(right)
do until position = door
Walk
repeat
open(left, up)
do until location = home
walk
repeat
Walk
move left foot
move right foot
check location
return location
---
position arm(arm, direction, object)
move arm direction until it is level with object
---
grab(arm)
tighten arm fingers
---
release(arm)
loosen arm fingers
---
open(arm, direction)
position(arm, direction, door)
push door with arm
Walk
---
- Larry Laffer
- Admin
- Posts: 4143
- Joined: Sun Feb 19, 2006 8:06 am
- Location: Romania
Thanks... feel free to share it. It's one of my best ones yet.
. I remember my grade 10 programming teacher I think it was asking me "how would you program yourself to make a peanut butter sandwich, assuming you have to go to the store to get what you need".
I went on for 2 solid minutes and had only JUST made it to the part where I reach the store. lol. She was impressed... all the other newbs were like "First I walk to the store" totally undefining how to walk, and leaving out all sorts of easily overlooked steps. lol.
Too bad I didn't know about methods back then![Tbbbbbth! :P](./images/smilies/tounge.gif)
![Tbbbbbth! :P](./images/smilies/tounge.gif)
I went on for 2 solid minutes and had only JUST made it to the part where I reach the store. lol. She was impressed... all the other newbs were like "First I walk to the store" totally undefining how to walk, and leaving out all sorts of easily overlooked steps. lol.
Too bad I didn't know about methods back then
![Tbbbbbth! :P](./images/smilies/tounge.gif)