Jim Lynch Codes
  • Blog
  • About Jim

Writings about one coder's stories & experiences.

Name Variables and Functions for Clarity

12/19/2015

0 Comments

 
In an early example in Martin Fowler's book Refactoring: Improving the Design of Existing Code he highlights that renaming a variable or  method is a legitimate form of refactoring. It can also be immensely helpful to the readability if the code. Here's an except from page 15 of the book: 
​
Once I'm done renaming, I compile and test to ensure I haven't broken anything. 
Is renaming worth the effort? Absolutely. Good code should communicate what it is doing clearly, and variable names are a key to clear code. Never be afraid to change the names of things to improve clarity. With good find and replace tools, it is usually not difficult. Strong typing and testing will highlight anything you miss. Remember,

"Any fool can write code a human can understand. Good programmer write code humans can understand."

Code that communicates its purpose is very important. 

This is great advice from the main man of refactoring, Martin Fowler himself. From the examples in the book, it is clear that Fowler is a proponent of camel case variable and function names. He likes to keep it simple and descriptive. He models the software in terms is real objects, and the variables have to do with the world that software is creating. Also, don't be afraid to have long variable names if that's what the program needs. For, example:
int bonusAmountIfCurrentlyRaining;
is perfectly fine. However, if you do find yourself with unusually long variable names- to the point that it's just "getting in the way"- then you may need to do some more refactoring than just renaming variables. ​
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    ​Author

    Picture
    The posts on this site are written and maintained by Jim Lynch. About Jim...
    Follow @JimLynchCodes
    Follow @JimLynchCodes

    Want FREE access to
    my daily stock tips 
    ​newsletter called,
    "The Triple Gainers"?

    Sign up here!

    Categories

    All
    Actionscript 3
    Angular
    AngularJS
    Automated Testing
    AWS Lambda
    Behavior Driven Development
    Blogging
    Business Building
    C#
    C / C++
    ClojureScript / Clojure
    Coding
    Community Service
    CS Philosophy
    Css / Scss
    Dev Ops
    Firebase
    Fitness
    Flash
    Front End
    Functional Programming
    Git
    Go Lang
    Haskell
    Illustrations
    Java
    Javascript
    Lean
    Life
    Linux
    Logic Pro
    Music
    Node.js
    Planning
    Productivity
    Professionalism
    Python
    React
    Redux / Ngrx
    Refactoring
    Reusable Components
    Rust
    Security
    Serverless
    Shell Scripting
    Swift
    Test Driven Development
    Things
    TypeScript
    Useful Sites
    Useful Tools
    Video
    Website Development
    WebStorm
    Writing

    Archives

    October 2020
    September 2020
    May 2020
    April 2020
    February 2020
    January 2020
    December 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015

    RSS Feed

  • Blog
  • About Jim
JimLynchCodes © 2021