Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How to get faster at coding in a short amount of time?

  1. Jun 10, 2016 #1
    So I am completely new to coding. I told my advisor that I wanted to work with him despite my lack of experience so I can get some experience. He said yes, and in fact he is a very nice and helpful advisor. He does think I need to work faster, and I felt that way too but I don't know how to.

    I feel like learning how to code takes me a while and that's what is slowing me down. I did well in my previous two physics research projects, but those were pure pen/paper theory. For some reason I was soo much better at it. I want to get better at coding, but I am feeling demotivated.

    I'm learning python! The thing is I feel like my prof thinks I'm focusing too much time on trying to understand the physics/math and less time coding. I think I am doing that because I feel like how I can set up a code that performs something if I don't understand what it should perform?

    I feel like a half assed programmer because so far I've done coding without even having a good grasp of it. When I take the time to understand something it slows down my research.

    How can I become good at python in a short amount of time?

    *I do ask for help from other people, but I don't always want to do that* I was so independent in my previous research and now I just feel dumb. People are like "coding is so easy"... I find physics so much easier.

    Thanks for any tips!
  2. jcsd
  3. Jun 10, 2016 #2


    Staff: Mentor

    Your attitude seems entirely reasonable to me. If you don't understand the underlying math or physics, any program you create will probably not work correctly.
    If your research involves writing a program to do some calculation, but you aren't very knowledgeable about the programming end of things, it will definitely take time to get up to speed.
    Do a web search for "python tutorial" Here's a link to one that I found helpful: https://www.codecademy.com/learn/python
    There are many more out there.
    The people who say "coding is so easy" have probably been doing it for many years. If you don't have much experience it will take some time before you reach the level that you think it's easy.

    In addition to the link I provided and others that you can find, look at the documentation that comes with a python installation. It's reasonably good, and can be helpful when you're getting started.
  4. Jun 10, 2016 #3


    User Avatar
    Science Advisor

    Practice, practice, practice. The more you do, the easier and quicker it will get. You will have the methods at your fingertips instead of having to look them up and think about them. I know one person who found it helpful to practice coding up these Project Euler problems as a way to gain experience.
  5. Jun 10, 2016 #4
    The way to be faster at coding is to slow down during design.

    I know, weird right? You're inexperienced at programming and algorithms, so what you need to do is fully understand what you are coding before you code it. You should have a complete design before you start anything, this will allow you to see problem areas and do a redesign before you actually write any code. If you've fully designed it properly, code should be pretty easy.

  6. Jun 10, 2016 #5
    Thank you everyone for the tips and advice!! I'm definitely going to work hard tonight and this weekend (and the rest of my life) to improve.

    Thank you! I'll definitely check out codeacademy and I've also heard great things about this from other people. I started using Sololearn which is pretty interactive as well. My strange problem is my level of coding is still at basic but what i need for my research is advanced. So it's stressful and weird. I don't have the time to fully gasp the basics but I need to google stuff and use all these code I practically don't understand and somehow weave it together.

    My professor seems to get annoyed when I spend a lot of our meetings asking him questions that involve getting deeper into things. I think it's an unfortunate nature of mine ( in this case). Especially for physics, I care too much for a deeper understanding. It pains me to just not... and I guess for this summer I have to not care and just do do do like a robot.

    I definitely agree about the practice. It's just frustrating because every time I spend trying to learn the basics and practice, I'm not doing my research and I'm slowed down. My professor thinks I need to get things done faster, I just wish I could get better at this faster. There are some things I pick up easily ( like math), but this is one those that I don't. =( Maybe coding is not for me, but it is something I care about learning and improving.

    This is great and very true!! My goal today is to have some kind of design for what I need to code. I guess this what people mean by writing pseudocode? Or is that something completely different.
  7. Jun 10, 2016 #6


    User Avatar
    Homework Helper

    It's possible to convert an algorithm or a mathematical formula into code, without understanding how the algorithm or formula was derived, if the algorithm or formula is clearly defined and you have a general idea of what it's supposed to do, and perhaps some example cases that show expected outputs for given inputs. As an example of this, coding a program to solve a quadratic equation with the 3 coefficients as inputs, if given the formula to solve a quadratic equation and a description of the exception cases, without understanding how to derive the quadratic equation solution.

    The normal process of initially learning to code is to convert an algorithm description or pseudo code or actual code from another language into Python. Some common examples include simple sorts like bubble sort or quick sort, generating factorial, generating Fibonacci numbers, ... . See if you can find tutorial oriented web sites that would include other relatively simple algorithms to learn how to convert to code. Perhaps others reading this could suggest some sites.

    The next step would be learning to develop an algorithm or process or program(s) given a basic idea of what the purpose of the program(s) will be, then implementing that algorithm or process as code.
  8. Jun 10, 2016 #7
    In addition to the excellent points already made: try to find time to get regular feedback on your code from skilled programmers. This may involve posting snippets on this site or others like stackoverflow for criticism. You run the risk of being bombarded with more detail than you need, but a likely reward is having bad habits nipped in the bud early.
  9. Jun 10, 2016 #8
    I agree that you can definitely write code without really understanding much of the algorithm and you are right, I don't have practice doing that. I actually spent a lot of time looking for code in python for Baum-Welch algorithm and forward-backward algorithm ( I have an okay grasp of these because I spent three days reading lecture notes/examples), but no luck. I know what they are supposed to do, translating into code and making it happen is the hard part.

    So it was a it disheartening that my advisor expects me to grasp all of this faster, but I guess I better get on it. I think I'm realizing that coding being a major part of my career might not be for me, but having the skill is important to me.

    You are right, I should start posting my code and my errors online for feedback. I feel like I spend soo much time debugging and sometimes it's something simple, but I just don't realize it.
  10. Jun 10, 2016 #9


    Staff: Mentor

    If you think of your code like an outline that can help you begin your design ie in steps where each step is mapped to a function and successively refines what you need to do:
    Code (Text):

    main function {

        step 1 do this --> maps to function 1

        step 2 do this --> maps to function 2

    Between steps you need to think about how to organize your data as a list or NxN array... programming is all about transforming data from one form to another to make it easier to work with.

    I find this helps when I start a program. I also scour the network looking for related programs in the same language. Real programmers don't write from scratch they beg barrow and steal ideas from other programmers. Please make sure to attribute where you got the code. It helps to document in code where you found it for licensing and copyright issues in case you're doing commercial products.

    Another strategy is to find a similarly written program in your group perhaps by another student that is actively used.

    Don't feel bad about asking questions, consider that you are an investigative journalist and your program encapsulates the knowledge of your professor. Its something that can be used to determine how good the resultant data from your experiment is.

    Lastly, look for tips from more experienced programmers as an example:


    There are a lot of sites that provide code samples that you can read and you should read what you need for what you're doing at the moment.

    Lastly, checkout the http://www.processing.org website. Processing is a simple programming tool IDE with modes for Java and for Python programming. It comes with many examples and can be a good resource for the future as well as something to play with to learn programming skills.

    One such site is http://www.rosettacode.org with numerous examples coded a large variety of languages.

    Hope this helps, come back to PF for more insight too. We are the best resource...
  11. Jun 11, 2016 #10
    Managing your time can be difficult. There are only so many hours in a day and you have to decide how much of your time you want to invest into each subject. Unfortunately there is no rule as to what the right balance is. It's different from person to person.

    Deeper understanding is the only way to become truly good at something. So you definitely shouldn't give up on that. You just have to decide how much time you can afford to invest in it.

    It's strange that you can pick up math much more easily than programming. Because fundamentally programming is a form of math.
    One difference is that in math concepts are expressed more directly, often through a single symbol, while in a program the same concepts may be realized through a combination of different instructions that could be located in different parts of the code interwoven with other instructions that are not part of that concept. You just have to learn how to do the translation and separate the different parts of the logic.
    Here is an example. It's part of the python code for the forward backward algorithm on Wikipedia.

    Code (Python):
    fwd = []
    f_prev = {}
    for i, x_i in enumerate(x):
        f_curr = {}
        for st in states:
            if i == 0:
                prev_f_sum = a_0[st]
                prev_f_sum = sum(f_prev[k]*a[k][st] for k in states)

            f_curr[st] = e[st][x_i] * prev_f_sum

        f_prev = f_curr

    p_fwd = sum(f_curr[k]*a[k][end_st] for k in states)
    One way of making the code less interwoven is to separate the different logical concepts into individual functions. That could look like this:
    Code (Python):
    def calc_f_curr_st(i, st, f_prev):
        if i == 0:
            prev_f_sum =  a_0[st]
            prev_f_sum = sum(f_prev[k]*a[k][st] for k in states)
        return e[st][x[i]] * prev_f_sum

    def calc_f_curr(i, f_prev):
        f_curr = {}
        for st in states:
            f_curr[st] = calc_f_curr_st(i, st, f_prev)
        return f_curr

    def calc_fwd():
        fwd = []
        f_curr = {}
        for i in range(len(x)):
            f_curr = calc_f_curr(i, f_curr)

        return fwd

    fwd = calc_fwd()
    p_fwd = sum(fwd[-1][k]*a[k][end_st] for k in states)
    This makes the code longer. But it becomes more clear which parts of the logic belong together.

    btw. here is another Python course that could be helpful.

    Also knowing the syntax of Python is actually not that important to being able to translate mathematical algorithms into code. I mean of course you need it to write down the code but syntax is really superficial.

    There is an interesting course here that was given at MIT in the 80s. It's called "structure and interpretation of computer programs". It uses a programming language that has nearly no syntax whatsoever (a Lisp dialect called Scheme). Yet it is actually a very powerful language and I feel like the connection between math and programming is much more clear in that course.
    And to be honest I watched quite a few programming courses but these old lectures made me feel like I was finally starting to grasp the true spirit of computer science.
    Last edited: Jun 11, 2016
  12. Jun 11, 2016 #11
    Thank for the advice about thinking of code like an outline. I think I'm going to write sot of a pseudocode outline and then translate into python language. I do feel bad about asking questions now because my professor thinks I'm too focused on understanding the "basics" and what not. Which is true I do want to know the basics because I don't really know anything.

    I'll definitely check out the websites you suggested and I am probably going to bombard this forum with coding questions soon. First I have to write/test it out on my own a decent amount of times. =)

    I think this is because I've been doing math most of my life now, so learning something new is not as hard once you know the basics. Of course certain areas in mathematics are a lot more difficult than others.

    I've been looking for a good course to watch or learn from! I regret not taking classes back in undergrad now. I'll check this course out for sure! In fact one of the books I'm using , to learn Python, is written by an MIT prof. Thank you for the all the advice DrZoidberg, I'm going to spend this weekend on improving. I know it won't happen over night though.
  13. Jun 21, 2016 #12

    Stephen Tashi

    User Avatar
    Science Advisor

    I'd say you should focus on the "weave it together" problem. If you professor expects speed, he's probably assuming your task is to utilize lots of code that other people have written. "Weave it together" is a different problem in different computer languages. For example, using code other people have written in FORTRAN is different that using other code people have written in Python.

    The typical evolution of a computer programmer is to begin with "procedural programs" which are self contained. Then one begins to understand the utility of writing code that is broken up into functions ( or "procedures" or "subroutines". Then (some people) progress to using mechanisms such as "classes" or "objects" that allow one part of code to "inherit" code from another part.

    If you are coding in Python, you need to understand how to use objects that other people have coded.

    The outlook that "I want to understand every detail in the program" is somewhat at odds with the task of efficiently using code that other people have written. Using other peoples' code involves trust - or you can think of it as "delegation". You are the boss and you can't do all the work yourself, so you have to delegate jobs to others - or you are the assembler of a the computer and you don't have time to make the CPU from scratch yourself, so you accept it as pre-made component.
  14. Jun 21, 2016 #13


    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    I second this. If you can find implementations that are already done, tested, and reliable, you should reuse it as much as possible. It can take many orders of magnitude more time if you try to understand, implement, and test everything that you use. Python is good at calling other programs, even if they are in a different language. Or you may have to translate an implementation from another language into Python. In the case of HMM, there are already implementations in R, Java, and Python.

    That being said, you need to be aware of any licensing restrictions and follow them. Ignoring license restrictions is just asking for trouble later.
  15. Jun 21, 2016 #14
    You are right, looking at other people's code and using will definitely make me do this project efficiently. I guess my objection for that was that I feel like "cheating" since I didn't come up with it. The advice you are giving me is the one I seem to get the most. I guess for this project i should do as you suggest while working on getting better at programming so someday I can write my own more efficiently.

    Suppose I had a friend help me? So that code partially belongs to the friend? Also I have a track of all the papers( and lectures) I've been looking to understand algorithms that I need to use. I will definitely cite them if this turns into a paper. However, at the rate I'm going that might not happen.
  16. Jun 21, 2016 #15


    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    Giving credit where credit is due is just common courtesy. What I had in mind were legal restrictions due to official licenses and copyrights on any code you mimic / translate / copy / link in.
  17. Jun 21, 2016 #16


    User Avatar
    Science Advisor

    If you need help just ask for it.

    You have enough resources at your disposal and you shouldn't feel bad for asking for help - you are just in your early stages of learning a particular skills and there is no shame in that.

    Most members here are very willing to give you a hand so take advantage of it - if your professor is acting a certain way then don't think that everyone else will.
  18. Jun 23, 2016 #17
    Thanks! I will definitely ask for help once I feel like I tried enough times and I am completely stuck. So far, I've had an old friend help me along the way.
  19. Jun 27, 2016 #18


    User Avatar
    Science Advisor

    If you start by describing what you want the program to do (in plain English) you are off to a good start. If you start coding straight away without any clear design, you are headed for trouble. And, yes, I know that most code jockeys just barge in and start coding - which is why they need a large amount of time just to debug the mess. So:
    1. Write down what the customer wants (and have him agree to your description). This is called the specification phase, and is likely to be long and boring. I know, just getting a clear statement from a customer is a nightmare.
    2. Write a short description of what your program is going to do. List input parameter, output values etc.
    3. Now divide your description into blocks (like: Get input - do something - provide output)
    4. After you have refined the description down to something an idiot could follow, it is time to start coding the blocks. Use lots of small routines that does one specific thing and ensure that it does exactly that.
    5. Never expect sensible input - always check!
  20. Jun 27, 2016 #19
    In addition to the great input from the previous posts, I have an additional coding technique that I have always found very effective: Write the code in small bite-size chunks, and run the whole code with each new chunk included to validate what you have done so far. Each new chunk could involve only a few lines of new code (to do some logical part of the calculation) before proceeding to the next chunk. This way, at each point in the development, you will already have some significant accomplishments under your belt. And everything you have done so far will have already been tested. Under no circumstances should you write the entire code without any intermediate testing. This is virtually guaranteed to give you a whole lot of time consuming debugging.
  21. Jun 27, 2016 #20


    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    I second this as a very good practice. I almost always develop code in baby steps. Write a simple loop -- test run it. Add a simple function -- test run it. Put a couple of equations in the function -- test run it. etc. etc. etc. Doing test runs is quick these days, especially with scripting languages like Python. Much faster than trying to locate a bug in a mass of untested code.
  22. Jun 27, 2016 #21
    Thanks Svein, Chestermiller, and FactChecker for the additional advice. I just finished my online course in Python! However, I would still call myself a basic coder.

    The thing I am realizing is that it's hard for me to code something when I don't really understand what's going on. I have been asking people for help and following advice on my other post, but still it doesn't feel good to not understand. The problem is...if I ask my professor too many questions he thinks I am slow and behind because I am too hung up on something.

    For example, now I am trying to figure out how to get confidence intervals of the Hidden Markov Model paramters. That's not easy at all ( to me), because I have to first figure out how to get that. Then code it. I don't know how to code (or even start) without understanding the problem. Reading and understanding the Hidden Markov Model, Baum Welch algorithm took some time, and then writing a code to implement those took more time.

    Slightly off topic from my original post, but I don't want to start a new thread. What if coding is just not for me? I mean it's something I definitely care about to learn and get better at, but I feel like I'm not someone one would hire. I'm not that great.
  23. Jun 27, 2016 #22


    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    Figuring out the formula for the confidence intervals is not a programming problem. It is an algorithm problem. You need some clear documentation of the equations first. If you can find documentation or the code in some computer language, that would help greatly.
  24. Jun 28, 2016 #23


    User Avatar
    Science Advisor

    Try breaking up the information bit by bit and the flow control bit by bit.

    Once you have the structures in memory (not necessarily 1's and 0's but data structures in the language you use) then think about how these will change and why you want them to change in your natural intuition before you translate that to code.

    I'd also look at the instructions. Most code doesn't have a lot of instructions and once you get the intuition for loops, functions, arithmetic, comparing things and other common tasks like setting variables and declaring them then you will look at the code in a different way.

    Find your way of describing the code - don't try and think you have to use the way other people use. Find how your natural intuition can help you so that instead of seeing the code (which is probably like Latin for you) just use another language that you're comfortable with and try and translate it.

    Doing this is a part of learning that always helps once you can find that bridge and crack that code and it's a reminder that natural intuition for things you are already good at (and can develop on) is there to be used and can be relevant for things that aren't as intuitive.
  25. Jun 28, 2016 #24


    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    You don't have to be hired as a programmer to benefit from programming skills. There is a wide range of programming use. A lot of programming is by people who are doing it for their own use. Any time you have a lot of data or information, there is a good chance that some tedious, repetitive task can be easily done with a program. Then you can either spend days doing that task by hand (and double checking your work), or you can start a program and let the computer do it for you.
  26. Jun 28, 2016 #25
    Took me a while to find a clear detailed explanation for what I need, but now just need to work on turning into code. Unfortunately no such example with code in Python (still a beginner)exists so I'll have to try and understand another language.

    This is awesome advice! I've been spending so much time trying to match other people's level that I keep getting lost and frustrated. I should just focus on writing and doing it the way that makes the most sense to me. Thanks!

    I definitely agree that it is a useful skill to have and I want to continue building on this knowledge. I think for me, it will just take more time than other people. I met up with a few people who wanted to meet weekly and go through a course to learn. I think starting something new with other people who are also learning will be helpful. Thanks so much for all the great advice! I really appreciate it =)
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted