Jump to content


- - - - -

Programming Tips


  • Please log in to reply
3 replies to this topic

#1 sharkbate24

sharkbate24

    Private Pilot - IFR

  • Members
  • PipPipPipPip
  • 943 posts

Posted 01 July 2009 - 08:59 AM

Hi everyone,
Just thought I'd give you some programming tips.

1. Comment what you need to do first before programming.
Believe me, this really does help. My programming teacher asked me to write a program where the computer generates a random number between 1 and 20 and the user has to guess the number with 5 tries only, and actually; it was quite difficult. But then he said to me, "Try commenting out what you need to do first, then add the code after."

So I came up with the following first (in Perl):

# Introduce the game to the user
# Generate a random number between 0 and 19 then add 1 to make it 1-20
# Generate a variable with 5 tries
# While loop (while $tries is NOT 0)
# Tell user what we need from them
# Ask user to input a guess
# Check if guess is equal to the actual number
# Check if guess is less than the actual number
# Check if guess is more than the actual number
# Take away 1 try
# Check if there are any more tries remaining

Now, after each of these comments, I can write the code for it.
For example, after:

# Introduce the game to the user

I can put:

print "Welcome to the number guessing game (1-20)!";

Because I know that that's what I need to do. So ideally, you need to know what to do first; don't just jump into writing code as it'll just be a waste of time and probably won't work.

2. Use indents
You should already have an IDE which automatically indends sections of your code. It makes it so much easier to read.

Look at the difference:

Without indents:
# Check if guess is equal to the actual number
if($guess == $actual) {
print "You guessed correct! Well done!\n";
$tries = 0;
}

With indents:
# Check if guess is equal to the actual number
if($guess == $actual) {
print "You guessed correct! Well done!\n";
$tries = 0;
}

It saves time, and helps you know where the block of code goes etc.

3. Make mistakes
As daft as it sounds, the only way to learn programming properly is to make mistakes. The best way of learning programming is to learn from your mistakes. If you want to try something, try it, don't be afraid to do it. You can read don't's from a book, but you won't understand what it does etc, and it won't sink in unless you try it.

4. Improvements
Don't just write a program and quit. Ask your friends to test it, and see if you can make improvements to it. My 1-20 number guessing game can be improved with maybe more tries, a higher range, more if statements to see if maybe the guess was half of the actual number, or double etc. It's also important not to copy and paste script from tutorials because you won't understand it. When you write your own programs, you just can't copy and paste, you have to write them yourselves. Don't depend on others to write your code.

5. Take criticisms
While I do like it when people say "Good job" to me, it won't help me at all. I'd rather someone says "I don't like it because the amount of tries you get is too low" or something. I can them use the criticisms I receive to improve my program, and learn from my mistakes. Remember; quite often with programming, you'll be writing code for others, not yourself; so it's important that others like your code / program.

6. Edit existing source code
When you need a program such as a content management system, there's really no point to create your own one from scratch unless you have the time to do so, and none of the ones out there already offer you what you need. Just go to an open source website, and edit the source code to fit your needs (make sure you read the licensing terms and keep the copyright information intact); it saves more time and effort than creating your own one from scratch.

I hope those tips have helped you out!
Thanks and good luck!

Edited by sharkbate24, 01 July 2009 - 09:00 AM.


#2 sharkbate24

sharkbate24

    Private Pilot - IFR

  • Members
  • PipPipPipPip
  • 943 posts

Posted 01 July 2009 - 10:47 AM

Sorry guys, with the indents, I just realised that the forum will just skip the indents.

#3 Prash

Prash

    Orville Reincarnate

  • Members
  • PipPipPipPipPipPipPip
  • 5,317 posts
  • Location:UK, England

Posted 01 July 2009 - 11:30 AM

Awesome stuff John!

I wish you had posted that when I started :hrmm:

#4 sharkbate24

sharkbate24

    Private Pilot - IFR

  • Members
  • PipPipPipPip
  • 943 posts

Posted 01 July 2009 - 12:05 PM

View PostPrash, on Jul 1 2009, 11:30 AM, said:

Awesome stuff John!

I wish you had posted that when I started :hrmm:

haha thanks :hrmm:, hope it helps!