CrazyEngineers
  • Tips for writing better code

    Anoop Kumar

    Member

    Updated: Oct 27, 2024
    Views: 1.3K
    I came across the thread on CE itself <a href="https://www.crazyengineers.com/threads/do-you-believe-indians-are-bad-programmers.76745">Do you believe Indians are bad programmers?</a>
    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.
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Anoop Kumar

    MemberSep 25, 2014

    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
    Are you sure? This action cannot be undone.
    Cancel
  • durga ch

    MemberSep 25, 2014

    book mark:

    #-Link-Snipped-#
    Are you sure? This action cannot be undone.
    Cancel
  • HRG

    MemberSep 25, 2014

    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 👍
    Are you sure? This action cannot be undone.
    Cancel
  • avii

    MemberSep 27, 2014

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberSep 27, 2014

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • avii

    MemberSep 27, 2014

    ^Yup, Python follows a bit different coding standards. If you are interested, read PEP8
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register