Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • 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!

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

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Advice: Simplify your code

Advice: Simplify your code

Advice: Simplify your code

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):
function DoThis(n) // creates a function called DoThis with n parameters
if n = 0
then print "apples"
// print to the screen the word "apples"
if n = 1
then print "oranges"
// same as above except prints "oranges" instead
if n = 2
then print "pears"
if n = 3
then print "peaches"
end DoThis(n)
the same code, simplified:
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)
Now tell me which code is easier to read.

Powered by Linux
Learn Linux and Leave out the Windows :)

RE: Advice: Simplify your code


With respect - could I suggest that you may be confusing conciseness with readability? My position would be that for all but the most trivial of cases future maintainability is key - rather than speed of execution or brevity of code.

With regard to your hypothetical function DoThis you are quite correct; the second example is easier to read. As a general principle however you are quite (IMHO) wrong. The more english-like your code (the more verbose) the easier it will be to read and maintain.



Mike Lacey
Cargill's Corporate Web Site

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

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! Already a Member? Login

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