How do you program your Programs? Flow Charts, Top/Bottom or Bottom/Top?
PoundSign2
Posts: 129
Hello friends! While I haven been at the forums for a hop, skip and a jump, in other words not long at all, I have come to learn many things and participate in some good conversations. There are examples for this type of code, how to use this code, how to hook up this component or how to read this or that. Essentially what I see is a lot of programs and code. However I see nothing in the way of how to program. Specifically how to Program and use SPIN. I don't mean the syntax, what I mean is, how do you design your programs? Do you use a flow chart? Do you get out pencil & pad and just write it out? What exactly, are we doing to program our programs?
To be fair there's a bajillion tutorials on how to use white space, how to indent and make our programs look pro or fancy. But where is a good, solid method or methods on how to build the program in the first place? Do you start with the result and work backwards or do it like a book and go from left to right top to bottom? Do you use a lot of comments or do you use little bits here and there so there's less clutter?
I would just like suggestions and documentation on how we as a community go about punching in the code and how we come up with our programs. How does someone know to create this variable or that variable, to call this function or method? Often I have a project or goal in mind, but no clear or obvious way to program something to get the proper result. If I knew how to tackle these methods better I might be much more efficient at programming. As a beginner and a newcomer to the community, I feel that myself and many others might learn a lot more if them and & knew how to get the program going in the first place, and once we start typing syntax, how to best structure it and set everything up.
If there's a thread on this I apologize but the search function did not result in any matches.
To be fair there's a bajillion tutorials on how to use white space, how to indent and make our programs look pro or fancy. But where is a good, solid method or methods on how to build the program in the first place? Do you start with the result and work backwards or do it like a book and go from left to right top to bottom? Do you use a lot of comments or do you use little bits here and there so there's less clutter?
I would just like suggestions and documentation on how we as a community go about punching in the code and how we come up with our programs. How does someone know to create this variable or that variable, to call this function or method? Often I have a project or goal in mind, but no clear or obvious way to program something to get the proper result. If I knew how to tackle these methods better I might be much more efficient at programming. As a beginner and a newcomer to the community, I feel that myself and many others might learn a lot more if them and & knew how to get the program going in the first place, and once we start typing syntax, how to best structure it and set everything up.
If there's a thread on this I apologize but the search function did not result in any matches.
Comments
Here is a sort of example of how my program building goes on the Propeller.
0) I have some vague notion of what I want to do. Most likely not written down at all.
1) OK I need a serial I/O interface, let's get that working.
2) Also need an LCD interface, let's get that working.
3) Looks like some file storage might be cool, get that working.
4)...repeat for as many small parts as required.
5) Ah..now I can stich all thise little working parts together and get my vague notion working.
6) Damn, these things don't fit together quite how I like go back and fix them up, improve their interfaces or whatever.
7) Damn, this kind of works but I would be embarassed to show it to anyone, it's a big jumbly mess, rewrite the whole thing.
8) Repeat 7) a few times.
9) Start thinking about optimizing for speed or size if either of those are a problem.
Profesionally things are supposed to be more organized. There is a requirements document that many people have approved, Many of those steps above are worked through in a design phase where you try to find all the problems before you start coding.
Secretly, a lot of profesional development I have seen goes as per my outline above:)
All in all it comes down to writing a lot of code. Making a lot of mess and mistakes. Eventually it dawns on you why global variables are not a good idea, or GOTO should be avided, or objects are a neat way to organize things. You get a feel for partitioning a design in to independent testable units of some form or other. And so on.
Flow charts can help, even if they are considered old fashioned. But then there are state transition diagrams. Structure charts, UML add nauseum. You will get a feel for what is useful when.
It's like someone who has never driven a car wanting to drive across the country.
First they have to learn to drive. Learning to drive proceeds in baby steps. Starting the engine, stopping the engine How do stop, how to go, how to change gear. how to slow down for corners. Then all that complexity of roads, traffic signs, regulations other road users etc etc etc.
After much practice the driving student becomes fluent at all that. Then one day they can decide "I want to drive across the country" and it just happens.
The project then is not the details of driving but route planning, finding gas stations, places to stay, things to see etc.
In short, start small, practice a lot. Getting little simple things working builds confidence to tackle bigger things.
I suppose I start with drafting my project in an A4 notebook This usually begins with a block schematic of inputs and their data types/ protocols.
I then search this forum and the OBEX for info from similar projects and methods.
And then I start gluing it all with two hard learnt lessons in mind:
1) All variable, constant, array and names should be comprehensible to someone else looking at the code at a later day. By doing this I hope I can come back to my code without too much scratching my head and wondering what a variable or whatever name means. Not always easy.
2) My notebook contains flowcharts of how the program (is supposed to) work(s).
Some on here use state machines as well.
And most of the time this has proved a god send when I have to fix a fault.
And when all else fails, have a beer and read the forums.
Other than that, I compose my programs at the keyboard, I don't use flowcharts (a total waste of time), and I add comments -- very important for PASM code -- after the fact.
-Phil
1) Read the book "Clean Code" and or watch a bunch of videos about clean code on You Tube.
2) Don't listen to anyone who tells you there is one methodology or technique that will solve all you software creation problems.
Anyhow, I almost always start with visual brainstorming on my whiteboard. It helps my mind work out the difference between the steps I need to take and those fly-by thoughts that are not critical to the finished project or the design process. I have actually killed many of my ideas by working out block diagrams or other notes on my whiteboard in the first stage of just getting the idea written down. After that, my projects take two paths: Either "code and go" until it's completed (or killed in the process) or a complete flowchart/block diagram with phases of the project mapped out so that I stay on track for a deadline.
I utilize these two concepts:
Especially useful if you have someone else around that you can explain your ideas to. Simply trying to explain the thing can bring problems to mind or if you are lucky solutions as well.
You know...this is a very deep question.
After many nights of designing, hacking and testing our Propeller coder may have 32K of EEPROM full of his latest creation. That is 262144 bits that have been set to zero or one.
One could look at that and determine that in the act of coding a quarter of a million decisions have been made! For each bit, should this bit be a 1 or a 0? And that's only a small MCU program.
Conversely, one could look at it and ask "That's 2 to the power 262144 - 1 possible programs our coder could have chosen from, how the heck did he come to choose that one?"
Given that we use assemblers and high level languages we clearly did not make all of those decisions. But it's still a huge number.
Now I'm starting to wonder: How do we ever get started?
For a more concrete answer to the question. Variables, functions and objects mostly suggest themselves from the problem you are trying to solve.
Let's say you are building a weather station. You have sensors for temperature, wind speed, rainfall, humidity, etc.
Straight away that suggests you might have objects in your code called "thermometer", "anemometer" etc which are going to deal with taking readings from those instruments.
Then you might consider that you thermometer should have methods like "temperatureNow", "temperatureMax", "temperatureMin", there is always something to initialize so we will have "init". We will want to reset those max and mins so how about a "reset" method.
Similarly for the other instruments.
Of course those instruments are kind of abstract, they may need to talk to the real hardware sensors via I2C or SPI or whatever, so lets have objects for I2C, SPI, whatever. They will suggest their own set of methods.
When you start putting this together you have some data. Well, what are you going to call temperature readings? How about an array of readings called "temperatureReadings" And so on, and so on.
Code just writes itself
2. Start with programming one thing.
3. Test that one thing.
4. Modify that one thing until it works as it should.
5. Test to be sure that one thing works in all situations.
6. Go on to programming next thing which needs to be done.
So say you have a key pad to enter numbers, a microcontroller, and a display. And you are programming a simple calculator which can add two numbers, display these numbers as they are entered, and display the answer.
One of the first tasks is to be able to enter a number and have the microcontroller be able to "see" it.
Another task is for a number to be displayed on the display.
More than likely, you may try programming number entry, you try it by pressing the number one, and the microcontroller "sees" a 4 (instead of a 1). So you then need to figure out what is wrong.
Anyway just break everything down into tasks and do the programming for each and make sure it works, then put it all together.
So far as variable names, with a small simple program, you can get away with variable names like VarOne, VarTwo, etc. But with a program which is thousands of lines long and may have upwards of a hundred variables, something more descriptive can be a BIG help for troubleshooting and tracking down problems. Like a variable called VarVolumeLevel pretty much says what it is for.
-Phil
I pretty much never can name functions and variables properly on the first pass because I don't fully understand either the problem or my solution until I've worked on it for awhile, so renaming code parts to make more sense is a normal part of my program development.
Phil, Mike Green, JonnyMac and Kye are my favorites. (I'm sure there are lots of other good examples (like Tracy Allen) but I mention the programmers I have the most experience reading.)
I've learned a lot from JonnyMac's SpinZone articles (link in post #3 of my index) and programs. I've also learned a lot from Phil's S2 object. Mike Green's various Basic programs written in Spin have lots of great examples of how to do things in Spin.
I have a hard time following some of Kye's stuff (I've recently tried to figure out how the CMUcam4 firmware works) but I still learn a lot by trying to understand it. I think Kye is fanatical about code size since he often works on such large projects. Some of his shortcuts are hard to follow.
IMO, There are a lot of bad examples of code in the Propeler Library. There's a lot of stuff which was written early on and not improved later when the programmers had learned better technique. There are pin number hardcoded into some of the objects and there are even pin numbers which can't be changed easily because of the way they are used in the source field in PASM (the pin mask is hardcoded which limits the mask to 9-bits).
I mention the issues with the Library objects to talk you into choosing other examples to study.
You'll save yourself grief if you only change pin states from one cog. Pick a cog to control specific I/O pins, and don't let any other cog directly modify them (unless you're sure you know what you are doing).
Also I comment things like crazy. I very often leave projects for months or years before I need to look at the code again, so comments are essential.
I use variable names that read like short biographies. The Propeller's SPIN language allows variable names up to 30 characters. IoftenUseEveryCharacterEyeCan .
Oh so true. My current project is on it's 5th iteration and has fewer 'components', both hardware and software and is substantially different from what I first dreamt up.
Same here. I was kind of feeling silly about some of my names until I saw I was in good company.
This is from the section of Kye's CMUcam4 firmware I had open.
Below are the last three variables in the code I'm working one.
While we're discussing programming in Spin, I'd like to add my support for Parallax's Gold Standard conventions. I'll take useful code in any format it's written but it would sure make my life easier if people (particularly the programmers previously mentioned) used the same convention with method, constant and variable names.
-Phil
Imagine I wrote it.
That should keep you on your toes.
- Will anyone besides me be reviewing or using the programs I write?
- How complicated will the program be?
If the program will be utilized by someone else or if the program is going to be very complicated, it is best to use a lot of comments. There has been many times that I have reviewed code that I wrote many years ago, which lacked adequate commenting, and this made the review of the code much more complicated.Some programs are very simple and others can be extremely complicated. If the code is going to be complicated, I often find it easier to write a brief outline of my intentions and key aspects of the program, and then I write my functions to perform the key aspects. When writing functions or methods, I almost always ask myself whether this function or method can be used in another program, and if appropriate, I will make it more generic, so that it can be more easily adapted into another program.
As you build your programming skills, your style will change according to your needs, and only you can determine your needs.
Like many others here, I have also been programming for many years, but since participating in this community, my style has of programming has changed out of necessity and the desire to become a better programmer. One of the discussions that truly affected my programming style was the discussion of the Gold Standard for OBEX objects. There were some very good ideas behinds these discussions that will help you to become a better programmer. I would suggest reading what others had to say about the Gold Standard topic and this will help you to establish a programming style.
Bruce
That's why it's always a good idea to have someone else look over your work. They might find something that you missed or show you a different way that you can do something.
Start with something simple... turning a ledon and off. Then two leds. Varythe time they areon. Binarycount with some. This islearningthe basics. Keep extendingthis until youhave to rewriteit. Now putthe routines into subroutines andcall them with parameters.
Remember,you haveto start somewhere. Andmake sure you document it -at least this makes you remember what you did. Later you will realise youneedless comments,but a summary of what agroupof instructions are doing.
sorry about lack of spaces -my xoom misses them :0
1) Find what works (experiment, read data sheets, study other code)
2) Simplify #1 for your application (remove redundancy)
3) Encapsulate #2 ; make it a SUB routine/function
4) Move to the next section/function/feature of the code and repeat step #1
Note: #4 can often stand alone as a derived function that calls other more simplistic SUB functions to achieve a more complex function.
One thing I often come across is what I want to do, but I am unable of how to actually program that function. The Propeller Manual serves as a great tool, but often I have no idea what syntax to even look for. Is there a method that you have for overcoming this? I'm sure it decreases with time as your knowledge improves, but for the beginner sometimes it's a pain in the butt trying to code something when you aren't sure what code to even use. Any insights for solving that would be appreciated. Although I don't think there exist a strategy other than learning over time, but oh well.
Not really. Recently I had to do a lot of work in JavaScript for the first time. Despite the fact that JS looks a lot like plain old C it has a lot of higher level features over C and a lot of quirks. Programming in JS requires a very different style than C, that I have used for years. Then there is the issue of the environment, all those facilities in the browser that you can use or all those libraries you can use for server side JS.
It was all new to me and like you I had no idea where to start. So I know how you feel.
Here is what I did:
1) Find as many JS tutorials as possible and read them.
2) Try out all the little examples in those tutorials, play with them. make changes, break them;)
3) Find libraries and modules that look like they might be useful. Read their docs, try out their examples, break them:)
4) Try making my own little programs, starting very small. Experiment with those.
5) Find other peoples code and have a read of that. Be aware that much of it is Smile written by people who are not so skilled. Reading the source of libraries is general a good place as their authors tend to be more experienced. Get a feel for common practices and idioms in the language.
6) Lots of experimenting.
After some months I got my project working. after a year I can see how badly I did it and want to rewrite it from scratch!
All of the above you can do with Spin or C on the Propeller. The OBEX is full of code to study, some of it is rather complex but a lot is fairly straight forward. The Prop manual is a good starting point if you try out all the examples and make sure you understand them
Starting out programing can be very daunting. It's like playing chess, so many possible moves you can make but which one? Every move you make gets you deeper into a situation you might not want to be in!
The only thing that helps is playing a lot of games and studying the games of others.
that's got me thinking, or which uses a clever algorithm - this is the initial motivation,
and may be a prerequisite for the rest of the project.
Having committed to getting the first part working and tested, the more boring stuff
is motivated by wanting a working whole - unless something more interesting crops
up and diverts you (pangs of guilt!)...
And whether the most interesting part is low level or high level rather depends - often
low-level with the Propeller since you are probably trying to interface to high
speed hardware - for instance my recent forays into driving SDRAM.
For the less experienced I suspect the order is more dictated by where understanding is
strongest, the part of a project that can actually be visualised in the mind's eye.
I guess PoudSign2 is not at that stage yet but after a few years of trying to remember what the hell one did and why one realizes that a version control system like git or mercurial or whatever is a good idea.