Page 1 of 1

Programming Style. Linear VS Structured

Posted: Thu May 03, 2007 6:05 pm
by tinman47
Whats your programming style.If its structured,reply STRUC,
if its linear,reply LINE,then I rank it up in 10 days.

Posted: Thu May 03, 2007 6:06 pm
by tinman47
LINE

Posted: Thu May 03, 2007 6:09 pm
by wardrich
there, I made it a poll. lol

Posted: Thu May 03, 2007 6:18 pm
by tinman47
Wow,thats what the little POLL button was for,thanks wardrich.

Posted: Fri May 04, 2007 12:59 am
by Larry Laffer
lol, yeah it's obvious! :laugh:

Posted: Fri May 04, 2007 1:32 am
by dosraider
Unruly.


Can't poll. :suspicious:

Posted: Fri May 04, 2007 4:38 pm
by abyss
What does linear and structured mean.

Posted: Fri May 04, 2007 4:41 pm
by wardrich
Linear programming starts at the top and runs down the code until the bottom line.

Structured programming uses classes and methods. Line 3 may tell the interpreter to jump to line 300, then jump back to line 4.

Posted: Fri May 04, 2007 4:56 pm
by abyss
I don't get it.

Posted: Fri May 04, 2007 5:56 pm
by wardrich
what don't you get?

Posted: Fri May 04, 2007 11:25 pm
by tinman47
Linear programming is when you just write a big list of code that is just stuck together and is not organized and just executes straight through,structured programming is when you organize your code into big and small blocks or paragraphs that you can call and use seperately.Do you understand now? B)

Posted: Sat May 05, 2007 6:46 am
by abyss
How is that useful. I really don't understand it.

Posted: Sat May 05, 2007 7:46 am
by dosraider
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.

Posted: Sat May 05, 2007 5:06 pm
by abyss
I am a completely inept moron, so I still have no idea what you're talking about despite your crystal clear explaination. Forget about it.

Posted: Sun May 06, 2007 1:15 am
by wardrich
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]:

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
---

Posted: Sun May 06, 2007 6:14 am
by abyss
I understand it now i prefer structured.

Posted: Sun May 06, 2007 6:14 am
by Larry Laffer
Now, that IS a good example (rofl)

Posted: Sun May 06, 2007 4:34 pm
by wardrich
Thanks... feel free to share it. It's one of my best ones yet. :P. 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 :P