PDA

View Full Version : Programming made easy


luke
11-01-2004, 03:38 AM
I believe almost all of us do (or forced to do) some programming. Especially in the USA even if you are not a Computer Science major you have to take at least one or two programming courses. And in my own experience I've seen my friends struggling with all those funky-jumbo syntaxes with never-ending { } symbols all ready to swallow you in a snap. So here I am, writing this article with hope that it can lessen your troubles and hopefully help you program like a pro. Well, I myself is not a pro in programming (I'm not even near to that) but with this little knowledge and experience of mine I hope this would help you debug your codes.

1. Check for syntax errors. Programming requires a great deal of precisions. All things must be absolutely right for the program to run properly. You miss an ending semicolon, you mispell a variable name, you forget to close a block with the closing brace } .. all these will result in your program not working ... so look out for these silly-milly errors before you jump out of the window ..

2. A program runs linearly. Which means it runs line by line. So, if you failed to get the right result for a line of code and you are very very sure the line is correct then scroll up. Your trouble might be caused by the previous lines. Sometimes a 50000-lines program will fail to run if the very first line is wrong.

3. Use reasonable variable name. I know some beginners (including me when I started programming) tend to use simple variable name like a, b, c, i, j, or k .. there's nothing wrong with that but if the program fails to run, you are in deep trouble .. why? because most of the time you'll figuring out what this and that variable is and what the value it carries means ... so use variable name like "userName", "inputFile" and save yourself from headache when your program breaks down ... exceptions are variables that are used just as loop counters because they are just used temporarily ...

4. whenever possible, use functions. sometimes when you read your own code you couldn't remember what a bunch of codes does ...

compare this javascript
var txt="the quick brown fox jumps over the lazy dog";
var outtxt="";
strLen=txt.length;

for(i=strLen-1; i>-1; i--)
outtxt += txt.charAt(i);

document.print(outtxt);
with this
function reverse(intxt){
var outtxt="";
strLen=intxt.length;

for(i=strLen-1; i>-1; i--)
outtxt += intxt.charAt(i);

return outtxt;
}

var txt="the quick brown fox jumps over the lazy dog";
outtxt=reverse(txt);
document.print(outtxt);
which one is more understandable? the 2nd, right?
because we know from the function name that the code is transposing the text .. but if we just look at the first one, what the heck is that? who knows .. you have to run it to know what's it doing ... see what I mean? that's one thing .. another is functions save your time because you don't have to have to repeat the codes to process another bunch of data .. just call the function and pass the new data to it ...

I think that's all for now .. I'll write more at another time .. if you have some programming tips don't hesitate to share it here .. you might be saving someone's life ^_^ ..

screw3d
11-01-2004, 04:56 AM
1. Learn to type faster if you can't already do so. Don't be lazy to type in longer but more descriptive variable names as luke mentioned. It WILL save your ass one day.

2. If it's an object oriented language, for goodness sake, use it. Again, it might be a little more code to type, but it is the proper way to do it and it'll make the program a lot easier to understand. Remember, OOP was invented for good reasons!

3. Do not be afraid to use multiple files to organize your program. Believe me, debugging a single page of 2000+ lines is a downright nightmare.

4. This is what your programming instructor will tell you at the beginning of every class - plan before you code. This is especially pertinent for larger programs. It will save you a lot of time even if you start coding later than your other buddies. However, I'm also often guilty of ignoring this ;)

5. Do your program in parts. Do not attempt to code everything and then only try to compile. Test individual functions, classes etc if you are not sure. When you start integrating the parts, you'll be thankful to guarantee that the smaller parts are working properly.

6. And oh, use comments at the right places so you'll know what a part of the code does, unless it's really simple.

To Luke about Point 2: I don't think this applies for all languages.. for some programs, functions and classes are called dynamically.. you can't really tell the flow of the program just by looking from top-bottom. Unless you are talking about within a class/function, then you are correct ;)

luke
11-01-2004, 05:09 AM
yeah OOP (object oriented programming) is damn convenient ... at first I was so afraid of hearing the name .. object oriented what? the name is enough to scare your mouse away .. but when you start getting a hang of it heh you can start programming like a pikachu (i don't know what to put here but you can just replace the word pikachu with the word of your choice ^_^) ...

however to understand the concept of OOP is not an easy thing .. and to explain it is 3 times harder .. so I couldn't really explain what is OOP here .. maybe screw3d or anyone else can explain it to the curious crowd out there ^_^ ..

jiinjoo
11-01-2004, 05:55 AM
Kudos to luke and screw3d for sharing.

Here're more tips: remember your instructor saying "plan before you code"? well, how do you plan? Different people came up with different ways like drawing pictures after pictures, or writing documentation first, or just think very hard in their head. Let me suggest something simple and works very well after some practise, makes your code maintainable and saves you time for doing other stuff: S.O.S.

Source Code Representation

Represent your idea using code itself, especially since most of your assignments, work etc will surround OOP languages. Remember those .h files in C / C++, or your public Interface in Java? Try writing those stuff first - have a good idea of what functions you want to implement, what it does (write your comments into the source file along side with the function declarations), what kind of parameters it takes and what output it gives.

This saves time because it is compilable code! You don't have to translate your plan from picture to code (or at least most of it). This is also less error prine coz the compiler checks the consistency of your framework alongside with you.

Outside-In Development

Simple, start from the outside (test cases + interface) and work your way in. Think about the outcome, write a small test file first so that as you develop you know where you're heading at. My way of doing it is usually to put some dummy return value in my function, make sure everything work given those dummy values, then continue working downwards into the meat of the program.

OID causes the use/test scenarios to be specified early on, making it more likely that you write a program that actually satisfies the requirement of your assignment. As you move into the real world, this also becomes important when working with groups of programmers, as you define the final goal first, then fix the interface accross the group (i.e. what this software is supposed to do), then split up and do your own stuff, developing in parallel.

Short Cycle Development

I think this is the most important of all - since we have this tendency to work last minute. SCD suggest that we build something simple and make it work first, before adding in the features. So instead of sitting there to plan out your grand plan for the project, why not just find the most core requirement and do that first, get a working program and save that as a backup, if you can't finish the n-th part of your project, you can still hand up the (n-1)th part of it that works.

Most software companies practise this because you don't know when the marketing department is going to come to knock your door and say "Ship NOW! or perish we shall!". Another advantage is that you get feedback faster, so that if version 3 of your software didn't work, you might see a problem with your version 2 design, and fix that before releasing version 4. If you lump all of them together, maybe the problem will crop up later, and then you're too late.

Good luck!

luke
11-02-2004, 01:07 AM
Hello World!

do you recognice those 2 words? the mostly used phrase in beginner programming tutorials ... hehehe actually I have nothing to say here .. just pushing this topic up the list ...

luke
11-02-2004, 01:07 AM
Hello World!

do you recognice those 2 words? the mostly used phrase in beginner programming tutorials ... hehehe actually I have nothing to say here .. just pushing this topic up the list ...

qedx
11-02-2004, 08:25 AM
SPAM!

qedx
11-02-2004, 08:25 AM
SPAM!

jiinjoo
11-02-2004, 09:10 AM
It's pretty scary phrase... It is the beginning of a new life form... Poping out from nowhere, this "program" jumps at your face and scream


Hello World!!


At first, you dismiss it, but slowly, this creature evolves to control your life. Each and everyone of you reading this thread is control by this new beast that spawns the world, slowly being consumed by it... Maybe it will eventually lead to humanity's liberation, or maybe it will eventually liberate you forever... muahahahahahahaha :twisted:

jiinjoo
11-02-2004, 09:10 AM
It's pretty scary phrase... It is the beginning of a new life form... Poping out from nowhere, this "program" jumps at your face and scream


Hello World!!


At first, you dismiss it, but slowly, this creature evolves to control your life. Each and everyone of you reading this thread is control by this new beast that spawns the world, slowly being consumed by it... Maybe it will eventually lead to humanity's liberation, or maybe it will eventually liberate you forever... muahahahahahahaha :twisted:

littlebigone
11-02-2004, 01:35 PM
comments. I think comments are good.

For example, for CS majors, remember when you took that datastructures course in your first or second semester. Then 5 semesters down the line, in your OS course, you're asked to implement queues. So you look back at your old files but realize that dechipering what you wrote is impossible and in fact would take longer to do than to just come up with a new implementation.

So comment your code well. Comments also help if you're working with partners so they'll know what to do with your code.

littlebigone
11-02-2004, 01:35 PM
comments. I think comments are good.

For example, for CS majors, remember when you took that datastructures course in your first or second semester. Then 5 semesters down the line, in your OS course, you're asked to implement queues. So you look back at your old files but realize that dechipering what you wrote is impossible and in fact would take longer to do than to just come up with a new implementation.

So comment your code well. Comments also help if you're working with partners so they'll know what to do with your code.

kaiba
08-12-2006, 03:29 PM
hey to all the C++ pros out there.
I encountered a problem for my C++ project.
Plz help me to solve it. Thx in advance.

I need to create a calculator using C++ language.
The user must be able use the calculator for doing addition, subtraction, multiplication and division. Introduce modularity to ur code by writting the code for addition, subtraction, multiplcation and division in four separate functions. The user can enter as many numbers and operator as possible.
Can anyone help me to solve this? thank you so much.

da-hype
08-12-2006, 05:37 PM
kaiba, check out this malaysian website, www.codedb.org

fizzy
15-02-2007, 08:37 PM
Spam/Ads
Deleted by schye

sweetiojj
01-06-2007, 11:06 PM
hi..sorry for trouble..can i know where can i get the useful web site to learn programming?

Excal
03-06-2007, 07:09 AM
Here http://2020ok.com/3608.htm

sAmurAi-X
24-05-2008, 03:16 PM
yeah OOP (object oriented programming) is damn convenient ... at first I was so afraid of hearing the name .. object oriented what? the name is enough to scare your mouse away .. but when you start getting a hang of it heh you can start programming like a pikachu (i don't know what to put here but you can just replace the word pikachu with the word of your choice ^_^) ...

however to understand the concept of OOP is not an easy thing .. and to explain it is 3 times harder .. so I couldn't really explain what is OOP here .. maybe screw3d or anyone else can explain it to the curious crowd out there ^_^ ..


The main advantage of OOP is the concept of inheritance, right? I'm just wondering why C is still the major machine language nowadays. Isn't OOP much better?

luke
24-05-2008, 03:27 PM
The main advantage of OOP is the concept of inheritance, right? I'm just wondering why C is still the major machine language nowadays. Isn't OOP much better?
C is not machine language. A machine language is 00101001010101, that is, all binary. The closest human-readable programming language to machine code is assembly. OOP, SEO blah blah all those freaky stuff in the end will have to compile (translate) into those 0's and 1's, so unless Intel comes up with processor that has built-in support to OOP then what languages with OOP do is translate our objects & methods & all those OOP stuff into 0's and 1's which in structure do not differ much from 0's and 1's generated from non-OOP languages.

All that to say that OOP-related structure in your C++ or Python is going to lose that OOP look when it is translated to machine code, because current machine language, as well as processor, does not automatically support OOP. All those inheritance stuff is simulated.