tecHindustan






    With whom are we having a Buzz?












    Pleasure to meet you,

    What’s on the foreground of your mind? Tell us something about your business?












    How can we succor you?












    Any valuation of the budget for your venture/expedition?












    Where shall we come back to you? Please mention your email address.




    Invalid email









    How about your digits?












    Could you please elaborate your pursuit? Tell us more…


































     

    To rocket the business growth by providing IT services and Business Solutions worldwide.

    0172-4660048, +918570810001
    Email: care@techindustan.com

    tecHindustan Solutions Pvt. Ltd.
    902, Tower B, Bestech Business Tower Sector 66, Mohali, Punjab 160066

    Open in Google Maps

     

    To rocket the business growth by providing IT services and Business Solutions worldwide.

    0172-4660048, +918570810001
    Email: care@techindustan.com

    tecHindustan Solutions Pvt. Ltd.
    902, Tower B, Bestech Business Tower Sector 66, Mohali, Punjab 160066

    Open in Google Maps
    FREEQUOTE
    • Services
      • Internet-of-Things (IoT) Development
      • Web Development
        • PHP Development
        • ASP.Net Development
        • JAVA Web Development
        • Python Web Development
        • Ruby on Rails Development
        • Node JS Development
        • HTML5 CANVAS Development
      • Mobile App Development
        • iPhone App Development
        • iPad App Development
        • Android App Development
        • Hybrid App Development
      • CMS Development
        • WordPress Development
        • Drupal Development
        • Moodle Development
        • Joomla Development
      • E-Commerce Development
        • Magento Development
        • Custom E-Commerce Development
        • Prestashop Development
        • Shopify Development
        • BigCommerce Development
        • Opencart Development
      • Cloud Saas Development
        • Saas Application Development
        • Google Cloud Development
        • AWS Development
      • Digital Marketing
        • Search Engine Optimization
        • Content Development
        • Social Media Marketing
      • CRM Development
        • SugarCRM Development
        • Zoho CRM Development
        • Vtiger CRM Development
      • Product Development
      • User Experience (UX) Design
    • About Us
    • Team
    • Clients
    • Blog
    • Careers
    • Contact Us
    • Home
    • Technology
    • Ways to Write a Cleaner Code | Become a Better Programmer
    January 30, 2023

    Ways to Write a Cleaner Code | Become a Better Programmer

    Ways to Write a Cleaner Code | Become a Better Programmer

    by Vivek Kaushal / Tuesday, 27 March 2018 / Published in Technology
    Ways to Write a Cleaner Code - Become a Better Programmer
    Ways to Write a Cleaner Code | Become a Better Programmer

    If you have ever worked on a development team, or have looked back at some of your old codes probably you would have come across a certain block of code that looked like if someone had a fight on the keyboard while the text editor was open. The messy syntax, cramped code, and unclear variable definitions could result in a pain to read. The situation worsens when you are stuck by the deadlines and are tasked with someone else’s sloppy line of codes. Sometimes, it just moves with the flow as you are thinking and coding at the same time. Later, there is nothing that stops you from going back cleaning the code once again. With the help of a little more efforts, let’s spend some time in learning how to become a better programmer by keeping your code neat, easily readable and well worth. Here are some tips that will help you to write a cleaner code naturally:

    Conventions

    Using a naming convention is a great way to start with. It helps in keeping things clear and lets you know exactly what you are working with.

    Naming convention basically means that you are going to call your variables by name that adhere to a certain set of rules. There are preset rules that people could go into but it can get hairy and most of the people don’t agree on which is the best. Accordingly, for keeping it simple just try making it as clear as possible what type of variable it is and keeping the naming consistent. It can be as simple as adding variable names with their data type:

    int numberOfChildren = 1;
    float heightInCms = 70.54;

    360 No Scope

    The next big thing that follows nicely after naming conventions, is the usage of a convention for indicating variable scope. Again, there aren’t any rules defined and every coder can have his own way of doing it as long as these are consistent throughout the code. It will make it simple for what can be used from where.

    One of the most common conventions is as under:

    // Private and protected variables are prefixed with an underscore private
    int _iScreenWidth = 900;
    
    // Public variables are left as they would be normally public
    int screenWidth = 900;
    
    // Constant values are in all caps and separated with underscores
    int SCREEN_WIDTH = 900;

    Most importantly, feel free to follow any convention but make sure that you are clear with your code and have a consistent style as well. If someone else is looking at your code then they shouldn’t be caught by the things for not being as they were expecting.


    Say What You Mean

    This becomes pretty straightforward and is probably the most common thing that you would see but also the easiest to forget. Eventually, the most frustrating thing that a developer faces looking at your code, is seeing a variable with a misleading name, or worse, named with a single letter.

    Let us try to understand this with the help of an example:

    int getReaderType()
    {
        if(r.type == t.new)
        {
            return 1;
        }
    
        return 0;
    }

    While this could look acceptable and is also functional but if someone found this in the sea of codes with minimum or no comments; it would sound like ‘why’ any of it is happening at all. The reader of the code shouldn’t have to read the function’s contents to know more about what it exactly does.

    Here, the user hardly has any idea what ‘r’ or ‘t’ could be. Moreover, this depends a little on the program’s architecture. If in any case, you are returning something for indicating a boolean value, then you must return a boolean and not a number.

    Let’s take a look at how we can clean it up:

    bool getReaderType()
    {
        return reader.type == ReaderType.New; 
    }

    Now, we rapidly know wh