DEV Community

CodingBlocks

Episode 47 – Clean Code – Writing Meaningful Names

In this episode, we take our first dive into the book Clean Code by Robert Martin and specifically we talk about writing meaningful names for all things code related.  You’ll be amazed at how following some decent rules that you can start naming things that will help you and fellow coders understand your code at a glance.

Clean Code - Writing Meaningful Names

You can see the original show notes and put your own stamp on our survey here:
http://www.codingblocks.net/episode47

News

Samsung 960 Pro
http://www.anandtech.com/show/10698/samsung-announces-960-pro-and-960-evo-m2-pcie-ssds

SQL Server 2016 Columnstore for real time operational analytics
https://msdn.microsoft.com/en-us/library/dn817827.aspx

Krebs site taken off Akamai
http://www.zdnet.com/article/krebs-on-security-booted-off-akamai-network-after-ddos-attack-proves-pricey/

The best Android distribution is iOS?
Outlaw’s thoughts on various phone OS’s

Survey

#yop-poll-container-23_yp57eb26ad08208 { width: 360; background:#fff; padding:10px; color:#555; overflow:hidden; font-size:12px; } #yop-poll-container-23_yp57eb26ad08208 input[type='text'] { margin:0px 0px 5px 0px; padding:2%; width:96%; text-indent:2%; font-size:12px; } .yop-poll-name-23_yp57eb26ad08208 { font-weight:bold; background:#327BD6; color:#fff; padding:5px; text-align:center; font-size:12px; } #yop-poll-questions-container-23_yp57eb26ad08208 { font-size:14px; margin:5px 0px; } .yop-poll-question-container-23_yp57eb26ad08208 { padding: 2px; } .yop-poll-question-23_yp57eb26ad08208 { background:#327BD6; color:#fff; margin-bottom: 21px; margin-top: -10px; font-style: italic; text-align: center; width: 100%; padding:5px; } .yop-poll-answers-23_yp57eb26ad08208 { } .yop-poll-answers-23_yp57eb26ad08208 ul { list-style: none outside none; margin: 0; padding: 0; } .yop-poll-li-answer-23_yp57eb26ad08208 { font-style:normal; margin:0px 0px 10px 0px; padding:0px; font-size:12px; margin-bottom:20px; } .yop-poll-li-answer-23_yp57eb26ad08208 input { margin:0px; float:none; } .yop-poll-li-answer-23_yp57eb26ad08208 label { margin:0px; font-style:normal; font-weight:normal; font-size:12px; float:none; } .yop-poll-results-23_yp57eb26ad08208 { font-size: 12px; font-style: italic; font-weight: normal; margin-left: 15px; } .yop-poll-customs-23_yp57eb26ad08208 { } .yop-poll-customs-23_yp57eb26ad08208 ul { list-style: none outside none; margin: 0; padding: 0; } .yop-poll-li-custom-23_yp57eb26ad08208 { padding:0px; margin:0px; font-size:14px; } /* Start CAPTCHA div style*/ #yop-poll-captcha-input-div-23_yp57eb26ad08208 { margin-top:5px; } #yop-poll-captcha-helpers-div-23_yp57eb26ad08208 { width:30px; float:left; margin-left:5px; height:0px; } #yop-poll-captcha-helpers-div-23_yp57eb26ad08208 img { margin-bottom:2px; } #yop-poll-captcha-image-div-23_yp57eb26ad08208 { margin-bottom:5px; } #yop_poll_captcha_image_23_yp57eb26ad08208 { float:left; } /* End CAPTCHA div style*/ .yop-poll-clear-23_yp57eb26ad08208 { clear:both; } #yop-poll-vote-23_yp57eb26ad08208 { } /* Start Result bar*/ .yop-poll-results-bar-23_yp57eb26ad08208 { background:#f5f5f5; height:10px; } .yop-poll-results-bar-23_yp57eb26ad08208 div { background:#555; height:10px; } /* End Result bar*/ /* Start Vote Button*/ #yop-poll-vote-23_yp57eb26ad08208 div#yop-poll-vote-23_yp57eb26ad08208 button { float:left; } #yop-poll-vote-23_yp57eb26ad08208 div#yop-poll-results-23_yp57eb26ad08208 { float: right; margin-bottom: 20px; margin-top: -20px; width: auto; } #yop-poll-vote-23_yp57eb26ad08208 div#yop-poll-results-23_yp57eb26ad08208 a { color:#fff; text-decoration:underline; font-size:12px; } #yop-poll-vote-23_yp57eb26ad08208 div#yop-poll-back-23_yp57eb26ad08208 a { color:#555; text-decoration:underline; font-size:12px; } #yop-poll-vote-23_yp57eb26ad08208 div#yop-poll-archive-23_yp57eb26ad08208 a { color:#555; text-decoration:underline; font-size:12px; } #yop-poll-vote-23_yp57eb26ad08208 div { float:left; width:100%; } /* End Vote Button*/ /* Start Messages*/ #yop-poll-container-error-23_yp57eb26ad08208 { font-size:12px; font-style:italic; color:red; text-transform:lowercase; margin-bottom:20px; text-align:center; } #yop-poll-container-success-23_yp57eb26ad08208 { font-size:12px; font-style:italic; color:green; margin-bottom:20px; text-align:center; } /* End Messages*/#yop-poll-container-23_yp57eb26ad08208 img { max-width: 360; } .yop-poll-forms-display{}
Do you regularly attend meetups or conferences?
  • Yes, I need to get out of my cave every now and then
  • No, ain't nobody got time for that
  • No, I live light years away from any meetups / conferences

Meaningful Names – Clean Code Chapter 2

“If a name requires a comment, then the name does not reveal its intent”

  • Write explicit code – naming variables and methods can reveal the entire intent of the code
  • Avoid using words that would be confusing like “List” as they refer to programming types and could be misleading : accountList should be accounts
  • Avoid using characters that look like numbers i and L or capital o
  • disinformative vs noninformative
    • noise words “data” “info” – noninformative
  • Types should almost never be in a name “table” “string” “object”
  • Names should be distinguished so a user can look at them and understand the differences
  • Use pronounceable names
  • Use searcheable names – longer names trump shorter names
  • Author’s pref – single letter names should only be used as local variables inside small methods – length of the name should correspond to the size of its scope
  • Avoid encoding names
  • Avoid Hungarian Notation with typing as part of the variable name – simply not needed nowadays
  • Stop prefixing member (instance) variables with m_ or _
  • Decorating Interfaces vs Classes with a prefix / suffix – opinion – he prefers
    • ClassImp or vs IType
  • Don’t force someone to map variable names in their mind – n = username…smart programmer vs professional programmer – clarity is king
  • Class names should be nouns – English 101 – NOT VERBS
  • Method names should be verbs
  • Use get, set, is – javabean standard
  • When constructors are overloaded, use static factory methods with explicit names – liked this one, possibly make the constructors private
  • Don’t get cute with naming by means of jokes (inside or well known)
  • Use consistent naming – Get, Set, Controller – makes it easier to understand and code various parts of an application
  • Avoid puns – add for a collection vs add for setting a value – two different meanings with the same name
  • Use technical names such as pattern names or CS terms in your names – other programmers will understand them better than the problem domain in some cases
  • Fall back to the problem domain for a name if there is no suitable technical name
  • Adding context to naming can clarify their use – prefixes can work but putting variables into classes may work out better

“Hardest thing about choosing good names is that it requires good descriptive skills and a shared cultural background”

Renaming things that don’t make sense as you work in code is a good thing.

Resources we Like

Clean Code

Clean Code by Robert C. Martin

Even though we’re giving our thoughts on the various ideas throughout the book, Clean Code has tons of excellent sample code that really helps drive the points home. We can’t recommend it enough – it’s probably one of the few books EVERY developer should read and revisit from time to time.
http://amzn.to/2cryvJR

Tip of the Week

Allen: Implementing OAuth in ASP.NET for a number of providers
http://www.oauthforaspnet.com/

Michael: Get out there! Go to conferences, meetups, do it all!
http://www.connect.tech/
https://www.atlantacodecamp.com/2016

Episode source