Programming as a subject of study

DanM

Поехали!
Joined
May 23, 2010
Messages
1,131
Reaction score
1
Points
38
Location
Chicago
I have a question directed towards the programming and computer science oriented users of the forum.

This semester, as a requirement for my Physics degree, I am taking a course of Unix and C. I've found this to be more difficult than I expected. I can understand most concepts and what they do (data types, control flow, pointers, etc) without much difficulty, but I find myself unable to implement them. Whenever I try to write programs, I feel as if I'm pushing against a brick wall. I've developed a few strategies such as writing out what steps I need to take to complete the program, but even then I feel I can't find the starting point.

So, programmers of O-F, what techniques and tricks do you use when trying to write a program?

Thanks in advance! :tiphat:
 
So, programmers of O-F, what techniques and tricks do you use when trying to write a program?

Well, there are many different approaches. It depends largely what kind of project you are working on. You can work from bottom to top and opposite. Or something completely different. Every strategy or DevOps has its own merits and disadvantages.

I think the first most realization that you need in any case is, to start thinking about how your programming language is executed and what you really want to tell the interpreter or the compiler. While you can already write good programs without too many details, there are special reserved words in many programming language to describe better what you are actually meaning and the compiler can help you by telling you, when you are programming something different to your original intents.

Now, that said, the first question you should always ask yourself: What should your program do, and what should it not do? And must that software really do that, or is there a better solution without it possible?

Once you have settled on that, the answers will create new questions and the answers will help you write your program. You can maybe already by that answers find out, which interfaces your program really need between the different modules and can define them as strict standard in your software.

The "interfaces" (Yes, there is also a programming language concept called that way, ignore it in the context of that post) are the most important part of your program. Anything else depends on your interfaces being defined and you knowing which part of your program depends on which other parts. Interfaces also mean you can test small parts of your program without the rest of your program interfering.
 
I have a question directed towards the programming and computer science oriented users of the forum.

This semester, as a requirement for my Physics degree, I am taking a course of Unix and C. I've found this to be more difficult than I expected. I can understand most concepts and what they do (data types, control flow, pointers, etc) without much difficulty, but I find myself unable to implement them. Whenever I try to write programs, I feel as if I'm pushing against a brick wall. I've developed a few strategies such as writing out what steps I need to take to complete the program, but even then I feel I can't find the starting point.

So, programmers of O-F, what techniques and tricks do you use when trying to write a program?

Thanks in advance! :tiphat:

Sometimes the implementation of the concepts is important, two good points Ive observed so far would be

-Functionalize all the things!!! One of the best strategies for writing good code is to write repetitive operations as functions. The code will look better, and it'll be better. This was one of the first pieces of advice I got from a friend, and it has never let me down.
-Test it! Most big projects will never work the first time around, especially if their individual subcomponents are untested. Trying to find bugs amidst thousands of lines of code is damn near impossible, so test your code as you write it.

But to really help out, we need to know some more specifics. Try posting a more specific problem here so we can help out. Learning by doing is good.

also a good place to ask questions IMO:

http://www.cplusplus.com/forum/

obviously not C specific, but C++ and C are very similar in many many respects, and the people there can often answer a lot of questions you may be having.

If you ever need more specific help, drop me a PM, I know C++ fairly well, and I use Linux, so Unix environments are fairly familiar to me. I would assume you are using Linux or some other more recent OS family, not Unix itself right?
 
Hi Dan,

Don't worry. It's typical for somebody who has never programmed before. You need to learn the confines of a subject, and then, having an easier way of maneuvering in the subject, you'll think in the way that always guarantees a successful path (bumpy or not). Don't try to talk like that though, or people will hate you :)

More to the point: start by writing small programs that do something (un)useful in the area that you study. This will give you a feedback loop. You will know what to expect from a program, and you will modify it until the desired result is reached. For this purpose the initial programs must be small, or you get lost and no feedback loop is created.
Once you have a set of programs that work somehow, you can start studying some simple code organization topics, found here:
[ame="http://en.wikipedia.org/wiki/Category:Programming_principles"]Category:Programming principles - Wikipedia, the free encyclopedia[/ame]
Choose a few and modify your programs until satisfied for the moment. Try to remember what you did and how it helped purifying the code and in turn, how that helped in maintenance of the program. Put special attention to KISS, Don't Repeat Yourself, and Single Responsibility Principle, as they are the de-facto foundation of other, more advanced principles, that you don't need to know. In case of C, the SRP would mean using small functions and structs on which the functions operate.
 
When I first started out, I'd write down how the algorithm would work on paper. Usually that meant a lot of drawing.

Approach it from this angle: How would you solve a problem without the human intuition, experience and sight? Keep in mind that a computer is blind and can only see the variables you're really dealing with on this line of code.


Beyond that, it's just a lot of practice.

http://www.cplusplus.com/doc/tutorial/

This is the tutorial I learned from. It includes some simple examples of how to use C++.

As a classic home work, try writing a program that finds all prime numbers from 2 to a certain number.
 
Practice. Practice. Practice, practice, practice.
Practice for your own fun.
There is no other way.

As to how to do it.
You are making a detailed instruction for the computer to do a specific task.
Get familiar with what can be done, and combine it to make the instruction.

Look closely to things you do along your life.
When you want to fly a DG to the Moon in Orbiter, how does it work?
You need to:
-launch
-get in orbit
-do a TMI
-enter moon orbit
-deorbit
-land

How is each of the steps done?
How to explain how to do each of these steps?
How to explain these steps in a precise, unambiguous algorithmic language?

You think through all these questions, and a pattern forms in your mind.
Large problem broken into many simpler steps.
Steps broken into even simpler steps.
Simpler steps broken down into instructions.

Now, pattern-match on all levels.

The task itself takes steps, so you need a sequencer of steps, and a way to define them.
Each step would be a single sub-program, that should accept parameters and return status.
It would need state saving and loading, a loop function to control the ship, etc.

So, here is your top level class (assuming C++, or just a top level abstraction in any language).

Implementation.
You need a list of steps.
Linked list seems to fit the bill.
An array of pointers can also work, since there will be few rarely changed steps, and it's easier to implement.

That is, get to know your your trade-offs.
What pattern/data type/algorithm does what better and worse.
You should be able to pick one or the other based on the task.

List - functions to add, change order, remove, save generic state, load generic state, functions to draw it to the user and take user input, functions to react to the output and pick the next step.

Top level of your universal autopilot is done.

Now, each sub-task task in turn.
And so on.
I tried to write apart the launch step, but it takes too much math to just explain in a few lines.
But the idea is the same - understand how a thing is done, then explain it in an algorithmic language.
 
One thing I'd suggest is to search the web for examples of code. E.g. I did a quick Google for C Bubble Sort and hit this: http://www.programmingsimplified.com/c/source-code/c-program-bubble-sort

It's a very common trait to never start a new program from a blank sheet and a blinking cursor ... but rather clone a pre-existing piece of code. For sure it brings bad habits along for the ride, but it's more satisfying to start from something that gets the creative juices flowing.

By the way - C is a good place to start. Others would argue to start from an object-oriented language (e.g. C++, Java), but I feel C connects you directly with the essence of human vs machine, in a way that gets much more subtle with higher level languages. So stick with it, and it will stand you in good stead for decades to come. (Also once you learn core concepts of arrays and functions and loops, etc, then you will find these things very quickly in different languages and it's easy to switch into another language in a few hours or days. Of course, mastering the language can take much longer!)
 
By the way - C is a good place to start. Others would argue to start from an object-oriented language (e.g. C++, Java), but I feel C connects you directly with the essence of human vs machine, in a way that gets much more subtle with higher level languages.

Depends. I am usually not the one to suggest that the choice of a programming language has a big impact, but there are concepts that some programming languages have and others have not.

C is the most popular programming language according to the obscure TIOBE index for years now for a good reason. But still, OOP languages are forming the bulk of the top 20 programming languages because doing larger projects without object-orientation and modularity is a very tough job.

But it is sure the best choice to start with one programming language, learn it to the point that you understood its tools, and then learn a second one with different concepts. For example combining Java and C.
 
Depends. I am usually not the one to suggest that the choice of a programming language has a big impact, but there are concepts that some programming languages have and others have not.

C is the most popular programming language according to the obscure TIOBE index for years now for a good reason. But still, OOP languages are forming the bulk of the top 20 programming languages because doing larger projects without object-orientation and modularity is a very tough job.

But it is sure the best choice to start with one programming language, learn it to the point that you understood its tools, and then learn a second one with different concepts. For example combining Java and C.

Sure - doing Java next would be good, or C++, or JavaScript or Python. My point was just the simplicity of C allows you to get used to for, do, while, if then else, simple data structures, your first function calls, a printf, etc, and write a few little console apps. From there - it's a big world out there, so take your pick.
 
Sure - doing Java next would be good, or C++, or JavaScript or Python. My point was just the simplicity of C allows you to get used to for, do, while, if then else, simple data structures, your first function calls, a printf, etc, and write a few little console apps. From there - it's a big world out there, so take your pick.

I would definitely second that learning C first is good. From the point of view of Physics, I believe the OP would benefit the most from also learning C++ and Python. The upper years that I know in Physics at Waterloo use C++ for computational tasks, and Python is becoming a very common language in scientific applications (frequently as a front end for loading C library functions so that the implementation is in a more friendly language than C I think?)

Java is also an option, but I would strongly reccomend C++ and Python first in this case. Javascript is a bit of a stretch for scientific tasks, but still a useful language to know for CS jobs.
 
Back
Top