Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Advice: Simplify your code

Status
Not open for further replies.

ndogg

Programmer
Feb 7, 2000
156
0
0
US
www.crosswinds.net
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 &quot;//&quot; are comments):<br>
&lt;pre&gt;<br>
function DoThis(n) // creates a function called DoThis with n parameters<br>
if n = 0<br>
then print &quot;apples&quot;<br>
// print to the screen the word &quot;apples&quot;<br>
else<br>
if n = 1<br>
then print &quot;oranges&quot;<br>
// same as above except prints &quot;oranges&quot; instead<br>
if n = 2<br>
then print &quot;pears&quot;<br>
else<br>
if n = 3<br>
then print &quot;peaches&quot;<br>
end DoThis(n)<br>
&lt;/pre&gt;<br>
the same code, simplified:<br>
&lt;pre&gt;<br>
function DoThis(n) // creates a function called DoThis with n parameters<br>
array x[] = {&quot;apples&quot;,&quot;oranges&quot;,&quot;pears&quot;,&quot;peaches&quot;} // initializes the array x<br>
print array[n] // prints the value of the array<br>
end DoThis(n)<br>
&lt;/pre&gt;<br>
Now tell me which code is easier to read. <p>REH<br><a href=mailto:hawkdogg@crosswinds.net>hawkdogg@crosswinds.net</a><br><a href= by Linux</a><br>Learn Linux and Leave out the Windows :)
 
REH,<br>
<br>
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.<br>
<br>
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.<br>
<br>
Regards<br>
<br>
Mike<br>
<p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top