Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 My Minesweeper.
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Nemo296




PostPosted: Wed May 19, 2010 8:50 am   Post subject: My Minesweeper.

This is a project I had to do for my programming class.

I was hoping some people would be able to give me some suggestions to make my coding more effecient.

This is a source code.



Minesweeper.t
 Description:
Minesweeper.

Download
 Filename:  Minesweeper.t
 Filesize:  6.16 KB
 Downloaded:  340 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Nemo296




PostPosted: Thu May 20, 2010 1:02 pm   Post subject: RE:My Minesweeper.

40 views and no comments, sad.

All I did was ask for some advice on how to make it more efficent.
Tony




PostPosted: Thu May 20, 2010 1:27 pm   Post subject: RE:My Minesweeper.

Just 7 downloads of the source file though.

Anyway, good use of constants, arrays, procedures. Recursion! This is quite well done.

Some of the naming could be better chosen ("var Thing" ?). And there is some magic in certain places. What's going on here?
code:

Revealx := floor ((x3 / 16) + 0.74)

So maybe some comments regarding the intent would be useful.

What is it that you are looking to make "more efficent[sic]"? If you are talking about speed of execution, don't say "everything".
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
DemonWasp




PostPosted: Thu May 20, 2010 1:31 pm   Post subject: RE:My Minesweeper.

It's pretty efficient as-is; you've used arrays, procedures and Turing's API pretty effectively. Some tips:


  • More comments. Comment the purpose of each variable or group of variables, of each procedure, and of any non-trivial loops / branches (if, select).
  • Choose a variable naming convention that makes sense. You've named all your procedures intelligently enough (it's called "CamelCasing"), but your variable names are all over the place. Don't use Num1, Num2, x3, y3, and so forth because they don't really describe what their purpose is.
  • Avoid "magic numbers" - you have numbers like 399, 16, 0.74 and 16.2 strewn through your code without any obvious significance attached to these numbers. It looks like you found these by trial-and-error trying to align with the text output in the run window...what will you do if you want to run with a different font or size? You'd have to change it everywhere...or use a FONT_HEIGHT variable declared as const.


Other than that, this is markedly superior to the typical output of high-school-level CS classes.
chrisbrown




PostPosted: Thu May 20, 2010 1:35 pm   Post subject: RE:My Minesweeper.

If you remove delay (200), your program will finish 1/5th of a second faster.

Seriously though, if by efficiency you mean minimizing the number of operations, I don't see anything that stands out. The only costly block is in your Display procedure, and since you only call that when the mouse is clicked, it's already pretty optimal. If it runs in realtime (without any lag), there's no need to improve.

If you want some advice in terms of good coding practices, avoid using magic numbers throughout your code. What if I want the size of each cell to be 20 pixels instead of 16? Or red text instead of black? Those should be constants.

There is more to be said about the general structure of the program, but that's probably beyond your skill level at this point. Keep coding, and you'll pick up on better ways to do things.
Nemo296




PostPosted: Fri May 21, 2010 8:12 am   Post subject: Re: RE:My Minesweeper.

DemonWasp @ Thu May 20, 2010 1:31 pm wrote:
It's pretty efficient as-is; you've used arrays, procedures and Turing's API pretty effectively. Some tips:


  • More comments. Comment the purpose of each variable or group of variables, of each procedure, and of any non-trivial loops / branches (if, select).
  • Choose a variable naming convention that makes sense. You've named all your procedures intelligently enough (it's called "CamelCasing"), but your variable names are all over the place. Don't use Num1, Num2, x3, y3, and so forth because they don't really describe what their purpose is.
  • Avoid "magic numbers" - you have numbers like 399, 16, 0.74 and 16.2 strewn through your code without any obvious significance attached to these numbers. It looks like you found these by trial-and-error trying to align with the text output in the run window...what will you do if you want to run with a different font or size? You'd have to change it everywhere...or use a FONT_HEIGHT variable declared as const.


Other than that, this is markedly superior to the typical output of high-school-level CS classes.


What are these "Magic Numbers" you speak of? Is it like numbers that should probably be constants but arn't?
DemonWasp




PostPosted: Fri May 21, 2010 9:22 am   Post subject: Re: RE:My Minesweeper.

Nemo296 @ Fri May 21, 2010 8:12 am wrote:
What are these "Magic Numbers" you speak of? Is it like numbers that should probably be constants but arn't?


Yes, exactly. While it's usually okay to have a number like +1 or 0 or -1 just laying about in your program, it's probably best to put anything like 17.2 into a variable that can then be used to modify your program's behaviour without making edits everywhere.
Tony




PostPosted: Fri May 21, 2010 11:03 am   Post subject: RE:My Minesweeper.

http://en.wikipedia.org/wiki/Magic_number_%28programming%29
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: