Open Side Menu Go to the Top
Register
Best Practices of Code Organization Best Practices of Code Organization

06-10-2017 , 08:48 PM
Quote:
Originally Posted by daveT
Disregarding the other topics (oh my, no one knows what state is)...

Turtle is a great place to have math students work on an absolute classic of math: the drunken walk. It would let them see how math turns into simulation.

You wouldn't need much more than a single loop and I'm guessing it would take about 10 LOC. What you have in the OP is just waaaayyyyy too much to be handing someone new to programming. Ouch.
Do you have more material I could read or projects to provide more enlightenment?

I know my example is contrived to fit with the turtle game but I thought they fairly reflected my understanding of state. Admittedly the acceleration part was a stretch but the example seemed legitimate.
Best Practices of Code Organization Quote
06-10-2017 , 08:50 PM
Quote:
Originally Posted by Victor
unless I am misunderstanding what a process is, this is not true for java or c#.
I assume he meant "function" but I dunno
Best Practices of Code Organization Quote
06-10-2017 , 08:55 PM
Quote:
Originally Posted by Aaron W.
Heh... Part of the assignment I had associated with this chunk of code was to try to figure out what was happening and add comments that describe various chunks of the code.

The idea is basically to give them a starting point and let them tinker. That's one of my underlying themes for the course. Half of it is just learning to try stuff out and see what happens. (Again, very hackish.)
Ok I think that is a good way to go.

Quote:
That would be an interesting extension of the code.
My thinking is that if you are doing a simulation you probably want to have some flexibility in the delineating the input parameters for the simulation. As you alluded to earlier thinking about different ways you can have different competitors traverse the course is something that would be useful in a simulation.
Best Practices of Code Organization Quote
06-10-2017 , 08:55 PM
Quote:
Originally Posted by RustyBrooks
I assume he meant "function" but I dunno
A pre-programmed recipe with it's own variable scope, or process/function for short. I take a function to be a process with a return variable where a process doesn't imply whether or not it uses a return variable, though it has it's own resources, etc.

Last edited by leavesofliberty; 06-10-2017 at 08:57 PM. Reason: dictionary nits itt
Best Practices of Code Organization Quote
06-10-2017 , 08:59 PM
Quote:
Originally Posted by leavesofliberty
Well it could work as long as the class is structured with chapters on the fundamentals as it relates to the turtle game. It'd also help if the turtle game was broken into a design document of some kind with some diagrams of the processes so students can look at it meta before digging in.
This could be the hack in me, but I never learned the fundamentals of anything before programming. I learned back in the days where you could get books with BASIC code in them and would just start typing things in and that was how you learned what commands did what. I just played with code and watched what happened. Most of my early learning came from screwing around with changing a line of code here and there and seeing what how it broke.

So that's kind of the approach here. For example, the first in-class activity in turtle will be to give them this code:

Code:
import turtle

screen = turtle.Screen()
my_turtle = turtle.Turtle()

my_turtle.forward(50)
my_turtle.left(45)
my_turtle.forward(100)
They're just going to copy it in and the run it. They'll see an angled line. Then the line of questioning will be something like:

* What do you think my_turtle.forward(50) does?
* What happens if you change that line to my_turtle.forward(100)? Does the outcome agree with your answer to the previous problem?

Then there will be some challenges:
* Can you make a right angle?
* Can you draw a square?
* Can you draw a rectangle?

And that's it. I'm not going to pre-explain everything. I want them to tinker around with the code more than I want to explain the code to them. (There *will* be explanations, but I want to front-load them with discovery experiences.)
Best Practices of Code Organization Quote
06-10-2017 , 09:05 PM
Quote:
Originally Posted by leavesofliberty
Also, you could construct a basic maze game with this, and address state if you have time to return to variables. Advanced students would be able to save the maze in file after re-visiting Input/output, recursion, and state. But, you might not even have time depending on how much time you have and how able the students are at learning programming. Some students are just flat-out not that able, which is why extra credit for a game makes sense.
This is a good point. I think it would certainly be beneficial to go over broadly what problems computation is good at solving and where it falls short if you aren't already.

Sent from my SM-G900R4 using Tapatalk
Best Practices of Code Organization Quote
06-10-2017 , 09:46 PM
Quote:
Originally Posted by Aaron W.
This might work. Resetting things periodically so that everyone is on the same page will certainly simplify things as we get deeper.
just thinking about this. Would you think if there is someone who is very bright/good at coding and you reset that the new code is for everyone would be working or could turn to be challenging for some of the students?

Lets say if everyone develops their own code they are working on their capabilities but if you have a super-student who adds a lot of stuff you cant understand and you need to work on that code, is it going to work if you don't understand or be fun- or is it just a challenge?

I know I'm questioning my own suggestion but what may work with professionals may not work well with students.

Last edited by vento; 06-10-2017 at 09:52 PM.
Best Practices of Code Organization Quote
06-10-2017 , 09:53 PM
Quote:
Originally Posted by leavesofliberty
A pre-programmed recipe with it's own variable scope, or process/function for short. I take a function to be a process with a return variable where a process doesn't imply whether or not it uses a return variable, though it has it's own resources, etc.
That's a distinction I don't think I've heard since my Pascal days. Nowadays it's just functions and when people say "process" they usually mean like an operating system process
Best Practices of Code Organization Quote
06-10-2017 , 09:57 PM
Quote:
Originally Posted by RustyBrooks
That's a distinction I don't think I've heard since my Pascal days. Nowadays it's just functions and when people say "process" they usually mean like an operating system process
My first formal class in programming was in Pascal, btw. I'll bear that in mind next time I use it though, because process has a very different meaning in terms of OS, and it's worth avoiding the confusion.
Best Practices of Code Organization Quote
06-10-2017 , 10:32 PM
Quote:
Originally Posted by vento
just thinking about this. Would you think if there is someone who is very bright/good at coding and you reset that the new code is for everyone would be working or could turn to be challenging for some of the students?

Lets say if everyone develops their own code they are working on their capabilities but if you have a super-student who adds a lot of stuff you cant understand and you need to work on that code, is it going to work if you don't understand or be fun- or is it just a challenge?

I know I'm questioning my own suggestion but what may work with professionals may not work well with students.
Like any good dictator, I have the final say in the classroom. I don't have to incorporate anything that I don't want to. There's definitely some risk of outrunning the class, and I will have to be attentive to that.
Best Practices of Code Organization Quote
06-10-2017 , 10:35 PM
Quote:
Originally Posted by Aaron W.
Like any good dictator, I have the final say in the classroom. I don't have to incorporate anything that I don't want to. There's definitely some risk of outrunning the class, and I will have to be attentive to that.
Absolutely. Fully agree
Best Practices of Code Organization Quote
06-11-2017 , 01:12 AM
Quote:
Originally Posted by Aaron W.
Yes! I've already intended to do both 1D and 2D random walks (on a regular lattice). I'm not sure if I'm going to general 2D random walks.



Most of the code is just drawing the track. The actual racing part is super short. This could be me being overly ambitious. I'll keep letting it roll around in my head.
I'd probably just have the board pre-written an have them import it as a module. Nothing wrong with going over the basics, etc, but that really is a lot of tedious work as presented.
Best Practices of Code Organization Quote
06-11-2017 , 11:27 AM
Quote:
Originally Posted by daveT
I'd probably just have the board pre-written an have them import it as a module. Nothing wrong with going over the basics, etc, but that really is a lot of tedious work as presented.
That's an idea I haven't considered. Thanks!
Best Practices of Code Organization Quote
06-11-2017 , 04:37 PM
Quote:
Originally Posted by RustyBrooks
That's a distinction I don't think I've heard since my Pascal days. Nowadays it's just functions and when people say "process" they usually mean like an operating system process
ya I had never heard that distinction. I always used method and function interchangeably, whether they returned a value or not. and I thought a process was indeed an operating system process which is taken care of by default in java and the programmer does not need to specify it.
Best Practices of Code Organization Quote
06-11-2017 , 05:08 PM
I typically think of a method as a function that's bound to a class/object
Best Practices of Code Organization Quote
06-11-2017 , 05:48 PM
Aaron, if you wouldn't mind, post results on your class after it's done. I am curious.
Best Practices of Code Organization Quote
06-11-2017 , 07:45 PM
Quote:
Originally Posted by leavesofliberty
Aaron, if you wouldn't mind, post results on your class after it's done. I am curious.
I will try to remember. The course isn't until the Fall, and it won't be over until December.
Best Practices of Code Organization Quote
06-11-2017 , 07:52 PM
Quote:
Originally Posted by Aaron W.
Heh... Part of the assignment I had associated with this chunk of code was to try to figure out what was happening and add comments that describe various chunks of the code.
I wanted to just loop back around to this because this seems to be something that I'm getting good feedback on. The way that it's structured in the assignment is that I've already broken the code into "parts" and they need to look through the code and tell me what each part appears to be doing.

I *think* this makes the assignment a little less tedious and overwhelming than it might have appeared because I've defined the transition points for them.

Code:
import random
import turtle
import time

# BEGIN PART 1
screen = turtle.Screen()

track = turtle.Turtle()
track.hideturtle()
track.pensize(3)
track.speed(0)
track.penup()

track.goto(-250,-100)
track.pendown()
track.goto(250,-100)
track.goto(250,-50)
track.goto(-250,-50)
track.goto(-250,0)
track.goto(250,0)
track.goto(250,50)
track.goto(-250,50)
track.goto(-250,100)
track.goto(250,100)
track.goto(250,-100)
track.goto(-250,-100)
track.goto(-250,100)
track.penup()

track.goto(0,150)
track.write('Turtle Race!', font=("Arial", 14, "normal"), align='center')
# END PART 1

# BEGIN PART 2
P1 = turtle.Turtle()
P1.penup()
P1.shape('turtle')
P1.color('red')
P1.speed(0)
P1.goto(-268,75)

P2 = turtle.Turtle()
P2.penup()
P2.shape('turtle')
P2.color('blue')
P2.speed(0)
P2.goto(-268,25)

P3 = turtle.Turtle()
P3.penup()
P3.shape('turtle')
P3.color('orange')
P3.speed(0)
P3.goto(-268,-25)

P4 = turtle.Turtle()
P4.penup()
P4.shape('turtle')
P4.color('purple')
P4.speed(0)
P4.goto(-268,-75)

P1x = -268
P2x = -268
P3x = -268
P4x = -268
# END PART 2

# BEGIN PART 3 
track.goto(0,125)
track.write('Ready...         ', font=("Arial", 14, "normal"), align='center')
time.sleep(random.randint(2,5))
track.write('                GO!', font=("Arial", 14, "normal"), align='center')
# END PART 3

# BEGIN PART 4
done = 0 
while done == 0:
    P1x = P1x + random.randint(3,10)
    P2x = P2x + random.randint(3,10)
    P3x = P3x + random.randint(3,10)
    P4x = P4x + random.randint(3,10)

    P1.setx(P1x)
    P2.setx(P2x)
    P3.setx(P3x)
    P4.setx(P4x)

    if P1x >= 232 or P2x >= 232 or P3x >= 232 or P4x >= 232:
        done = 1
# END PART 4
Best Practices of Code Organization Quote
06-11-2017 , 11:58 PM
Hey Aaron,

Based on those languages, it sounds like your blind spot is functional/declarative programming. I'd recommend learning haskell as your next language -- it should really open up your perspective on programming.

Given my personal leaning, some may be surprised at this advice, but if your expertise is in procedural programming, I think stick to what you know and embrace that perspective, though you should make both your perspective and its limitations explicit to your students. You won't be teaching them "computer programming." You'll be teaching them a small subset of it. And I'd likewise advise someone taking a higher level, functional approach to an intro course to caution students that there's a whole world of lower level detail and other paradigms out there.

RE: the conversation about state: there's a lot to say there, but as a concrete example to give a taste, the following is a stateful, procedural way to sum the elements of an array:

sum = 0
for (i=0; i<arr.length; i++) {
sum += arr[i]
}

vs a functional approach:

sum = arr.reduce(add)

in general, writing stateless code that is a series of transformations of data will be both more concise and easier to reason about. of course if you go down towards the metal far enough, everything is procedural and stateful. but not having to think about those details offers many advantages.
Best Practices of Code Organization Quote
06-12-2017 , 09:18 AM
Quote:
Originally Posted by just_grindin
Do you have more material I could read or projects to provide more enlightenment?

I know my example is contrived to fit with the turtle game but I thought they fairly reflected my understanding of state. Admittedly the acceleration part was a stretch but the example seemed legitimate.
I think your explanation is very good.

I generally think of "state" as "destructive" and "stateless" as a "film."

Code:
turtle.move()
In state would destroy the initial position and each successive position.

In stateless, move() would be feeding a list of pre-computed values into turtle, and turtle[0] would show the initial position, etc. This would allow you to "rewind" the path of the turtle.

I think your ideas about how to do it, computing accelaration on the fly, and considerations of memory, are great insights.

Although the programmer didn't use state, the implementation of the board itself and the movement is likely stateful, as the interaction of the outside world forces some change in the environment, though isolated to that section of the world.

I always say the distinction is rather hand-wavy. there is a sense of time / state in each approach, but the simplest distinction is, if you can't use variable substitution and guarantee a result of a function for each step of the system, then you are in state, if you can, then you are stateless.

In any case, I mostly rewrote what you wrote.
Best Practices of Code Organization Quote
06-12-2017 , 09:29 AM
And seeing that gaming_mouse popped in, I'll point out that he and I have a different interpretation of state and statelessness. I don't consider syntax a distinct feature of stateless programming and focus more on guarantees at a function level.

Not saying that one idea is more correct or incorrect, but my main idea is preventing accidental global mutation.
Best Practices of Code Organization Quote
06-12-2017 , 09:29 AM
@daveT thanks for the response. I remember you mentioning the film analogy before and I like it as a way to conceptualize it.

I had also forgotten to emphasize the state isolation before when I mentioned that state is sometimes necessary. Thanks again!

Sent from my SM-G900R4 using Tapatalk
Best Practices of Code Organization Quote
06-12-2017 , 11:01 AM
Quote:
Originally Posted by gaming_mouse
Hey Aaron,

Based on those languages, it sounds like your blind spot is functional/declarative programming. I'd recommend learning haskell as your next language -- it should really open up your perspective on programming.
Thanks for that. It's true that I don't even know what I don't know.

Quote:
Given my personal leaning, some may be surprised at this advice, but if your expertise is in procedural programming, I think stick to what you know and embrace that perspective, though you should make both your perspective and its limitations explicit to your students. You won't be teaching them "computer programming." You'll be teaching them a small subset of it. And I'd likewise advise someone taking a higher level, functional approach to an intro course to caution students that there's a whole world of lower level detail and other paradigms out there.
Given that this is a first semester course for non-majors, I'm trying not to overwhelm them. Is there a good "framework" or something that I can give them a 10,000 foot overview of the different types of ideas that's sufficiently accurate without giving them too much information or muddling everything together?

Quote:
in general, writing stateless code that is a series of transformations of data will be both more concise and easier to reason about. of course if you go down towards the metal far enough, everything is procedural and stateful. but not having to think about those details offers many advantages.
I'm probably going to step into something deep here and say something that makes no sense and not understand the answer that I'm given, but...

If you go down far enough, isn't everything just about 0s and 1s and either changing the bits or using the bits to jump to another place in memory? And am I correct that the 0s and 1s are kind of what you mean by "stateful" and "changing/jumping" is what you mean by procedural?
Best Practices of Code Organization Quote
06-12-2017 , 04:42 PM
Quote:
Originally Posted by RustyBrooks
That's a distinction I don't think I've heard since my Pascal days. Nowadays it's just functions and when people say "process" they usually mean like an operating system process
It's a long time since wrote any Pascal, but the when I thought it used Functions and Procedures, rather than Functions and Processes.
Best Practices of Code Organization Quote
06-12-2017 , 05:05 PM
Actually, you're right. It's also been a long time since I programmed pascal - basically since I was in high school, uh, 22 years ago
Best Practices of Code Organization Quote

      
m