Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...The enviroment is simple, natural and efficient. The members are competent, educated and professionals..."

Geography

Where in the world do Tek-Tips members come from?

Advice: Simplify your code

ndogg (Programmer)
14 Feb 00 16:35
I have seen lots of code for lots of different things. Much of what I've seen has lots of redundancy and in some languages can actually slow the program down. This redundancy also has a tendancy to make code harder to read. Take for example the following two (they're not language specific, they just take attributes almost universal to any language (these "//" are comments):
<pre>
function DoThis(n) // creates a function called DoThis with n parameters
if n = 0
then print "apples"
// print to the screen the word "apples"
else
if n = 1
then print "oranges"
// same as above except prints "oranges" instead
if n = 2
then print "pears"
else
if n = 3
then print "peaches"
end DoThis(n)
</pre>
the same code, simplified:
<pre>
function DoThis(n) // creates a function called DoThis with n parameters
array x[] = {"apples","oranges","pears","peaches"} // initializes the array x
print array[n] // prints the value of the array
end DoThis(n)
</pre>
Now tell me which code is easier to read.

REH
hawkdogg@crosswinds.net
Powered by Linux
Learn Linux and Leave out the Windows :)

ssingh (Programmer)
27 Mar 00 5:43
Although you are right but the better way to do is use switch case method instead of doing both the other ways. For eg.

switch(n) {
case 1:
print(...);
break;
case 2:
print(...);
break;
case 3:
print(...);
break;
case 4:
print(...);
break;
case 5:
print(...);
break;
default:
print(...);
break;
}

Now if you look on the code. Here you can have a default clause also. Which says that even if the values are not 1 to 5 , you can do something. While in the array case where you initialized you have to specifically check for boundary limit of the array.
i.e, What about value beyond 3 .
This would check the run time error it would've caused if you pass value beyond 3 in the array.

Siddhartha Singh
ssingh@aztecsoft.com

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close