1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Python: Cutting Code Help

Discussion in 'Programming' started by digital_reality, Aug 13, 2014.

  1. #1
    I am working on a simple tkinter game that contains numerous occurrences of duplicate code.

    Simply put, I can significantly cut down on the total lines of code that my program has by creating

    18 additional functions for these sections.

    The question can be summed up as:
    Is increased functions calls a productive alternative for less lines of code?

    Is this a good idea in (A) making my code faster, (B) making my code more efficient, (C) Making

    my code more flexible to modifications, or (D) simply a good practice to keep because game

    development employers simply like to see less lines of code?

    Anything you can provide would be appreciated, such as personal experience, or topics/subjects

    that you can refer me to study up on, thanks.
    SEMrush
    By the way, I am using python.
     
    digital_reality, Aug 13, 2014 IP
    SEMrush
  2. spujap

    spujap Active Member

    Messages:
    136
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    53
    #2
    Though I am not a Python developer/game programmer but have worked at places where requirements were similar.

    As per my experience as a C/C++ & Java/J2EE developer -

    There is always a trade-off between the parameters

    (A) making my code faster (B) making my code more efficient
    (C) Making my code more flexible to modifications


    Your code should take care of A, B & C but should maintain a balance. i.e. If some changes make your application faster by negligible amount but makes code much uglier and inflexible for maintenance you should avoid it. Again is something makes you app super fast and the requirement of this app is to be super fast then you could compromise a bit on flexibility. All of these should be documented in the code or on a separate page.

    (D) simply a good practice to keep because game development employers simply like to see less lines of code? No but code that could be easily written in 2 lines should not take say 5 lines.

    One more thing is that most of optimizations in any language can be done by using the most appropriate data-structure. And profiling the app helps a lot to identify bottlenecks if any.

    Simply put, I can significantly cut down on the total lines of code that my program has by creating 18 additional functions for these sections. The question can be summed up as: Is increased functions calls a productive alternative for less lines of code?

    Yes , in terms of maintenance/flexibility. e.g. you have a function that sorts some type of data and it's called/used by other codes in your app, tomorrow if you need to change the way sorting happens, you have to make that change in one place i.e. this function only. Otherwise, a new developer has to change the same code where ever it is written which definitely reduces his productivity. Performance reduction is very negligible here if you use functions. I would recommend you to profile the thing that you are developing to find where it's taking more time.

    Python optimization - https://wiki.python.org/moin/PythonSpeed/PerformanceTips#Overview:_Optimize_what_needs_optimizing
     
    spujap, Aug 16, 2014 IP