× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Good code design: naming

Alexey Naumov
Jun 03, 2016
<p>One more time I want to mention importance of good code design. General idea is simple. We write code for humans not for computers. Computers don't care about code. Ideal code for CPU looks like this: </p> <pre><code>0001 00111011 0100 00001001 0000 00011110 1111 00000000 </code></pre> <p>But wait, I can't understand anything! That's true, it's easy for CPU but not for human. Each function should have appropriate name which represents functionallity under the hood.</p> <p>Quick example: </p> <p>try to figure out what is this code doing:</p> <pre><code>showEditLink() if $user.admin OR ($user.user_id AND $user.user_id == $user_id) </code></pre> <p>and next one:</p> <pre><code>userCanUpdateEntity? -&gt; $user.admin OR currecnUserIsEntityOwner() currecnUserIsEntityOwner -&gt; $user.user_id AND $user.user_id == $user_id showEditLink() if userCanUpdateEntity?</code></pre> <p>Which one was more readable? ;)</p> <p> </p> <p>Simple rules:</p> <p> - name of the function/method should explain it's primary function</p> <p> - if you have complex condition - split it into parts with named functions</p> <p> - if you have complex method - split it into parts with named functions</p> <p> </p> <p>Have a great code!</p>
comments powered by Disqus