Tips for writing better code

I came across the thread on CE itself Do you believe Indians are bad programmers?
Almost everyone belief that programming and writing better code is an art.

a reply from above thread
durga
most of the good programmers might have put lot of effort in learning (as I realised, programming cant be taught, you learn) and rest are just mediocre.
In starting of coding, I use to write just to get things done. Even now when I look back my a year old code, sometime curse myself that I wrote that thing.
I think writing better code, evolve with practice and focus on logic.
My intention here to invide CEians their coding practices to help other and help others to learn quickly to be better at coding.

Replies

  • Anoop Kumar
    Anoop Kumar
    My 2 cents here:

    1. Write your workable logic on paper.
    2. Write code as fastest as you can as dirtiest it can be.
    3. test if it working.
    4. Refractor and go to step 1, revise your logic, is it optimal or not.
    5. Standardize your code.
    Look back if your code is really readable or if you have code review practices you will get this 😁

    wtfs_per_minute_thumb
  • durga ch
    durga ch
    book mark:

    #-Link-Snipped-#
  • HRG
    HRG
    This is a good thread !! The code we write must be optimized after several reviews before porting into the module we are working .

    In embedded systems this code optimization has more significance because sometimes it directly deals with the power consumption aspect while working in low energy applications..( currently experiencing now !!😒 ).
    I think it will be great , if we continue this thread by posting some basic optimization techniques with some examples!!! It will be useful for many CEans 👍
  • avii
    avii
    You guys are getting all wrong. In the linked thread, see the context and 4chan thread.

    simply put,

    a good code does not mean its optimized or works. A good code, may fail also. In this context bad code exactly means, coding like a monkey.

    Here is a simple code which calculates compound interest with following formula:

    Final value = Current value * (1+rate)^period

    Good python code:

    #!/usr/bin/env python
    
    import math
    
    def get_compund_interest(current_value, rate, period):
        return current_value * math.pow(1+rate, period)
    
    print get_compund_interest(1000, 10, 5)

    Horrible/bad code:

    #/usr/bin/python
    import math
    
    def My_supa_function(a, b, time):
        tempVal=math.pow(1+b, time)
        Answer = a * tempVal
        return Answer
    print My_supa_function(1000, 10, 5)
    PS: I chose Python, because it reads like English.
  • Anoop Kumar
    Anoop Kumar
    avii
    Good python code:

    #!/usr/bin/env python
    
    import math
    
    def get_compund_interest(current_value, rate, period):
        return current_value * math.pow(1+rate, period)
    
    print get_compund_interest(1000, 10, 5)
    Nice example. Reusable and concise.
    The thread is just meant to share good coding practices so everyone can learn and improve.

    I don't know python, just out of curiosity : defining variable and methods like get_compound_interest is standard of python like SQL?
    In JavaScript, HTML, Java we use like getCompoundInterest.
  • avii
    avii
    ^Yup, Python follows a bit different coding standards. If you are interested, read PEP8

You are reading an archived discussion.

Related Posts

Raghav Sood started coding when he was just 9 years old. At 15, he went on to start Appaholics in the year 2011 and is currently studying in Standard XII....
Karbonn Mobiles is back with a brand new phone, an elder brother to the Titanium S5, with the 5-inch Android KitKat smartphone titled 'Karbonn Titanium S5 Ultra'. Launched with an...
The Entrepreneurship Awareness Drive is being conducted by IIT-KGP at YCCE, Nagpur (SDMP Auditorium). I'm one of the speakers and would be at the venue from 2:15 pm onwards. If...
Quote: Bike wrecks happen fast. In two milliseconds—that's one-hundred-times quicker than a blink of an eye—a regular ride can turn into a disastrous noggin-buster. If you ever doubted the importance...
After scrapping Night Unlimited plans without any warning, state run telecom and broadband service provider BSNL has this time informed and will be changing the base tariffs of various plans...