Re: Ideal way to separate GUI and logic?

Discussion in 'Python' started by asimjalis@gmail.com, Jul 15, 2013.

  1. Guest

    wrote:
    > So as a general idea, I should at the very least separate the GUI from the program logic by defining the logic as a function, correct? And the next level of separation is to define the logic as a class in one or more separate files, and then import it to the file with the GUI, correct?
    >
    > My next question is, to what degree should I 'slice' my logic into functions? How small or how large should one function be, as a rule of thumb?


    The way I do this is to write unit tests against the class and the functions (take a look at the unittest module). The functions methods (take a look at the unittest module). Each function should contain the smallest bit of testable logic.

    Another way to think about this is that each function should contain the smallest piece of logic that you can describe as one action.

    -
    Asim Jalis
    , Jul 15, 2013
    #1
    1. Advertising

  2. Guest

    On Tuesday, July 16, 2013 1:06:30 AM UTC+8, wrote:
    > wrote:
    >
    > > So as a general idea, I should at the very least separate the GUI from the program logic by defining the logic as a function, correct? And the next level of separation is to define the logic as a class in one or more separate files, and then import it to the file with the GUI, correct?

    >
    > >

    >
    > > My next question is, to what degree should I 'slice' my logic into functions? How small or how large should one function be, as a rule of thumb?

    >
    >
    >
    > The way I do this is to write unit tests against the class and the functions (take a look at the unittest module). The functions methods (take a look at the unittest module). Each function should contain the smallest bit oftestable logic.
    >
    >
    >
    > Another way to think about this is that each function should contain the smallest piece of logic that you can describe as one action.
    >
    >
    >
    > -
    >
    > Asim Jalis


    Again, thanks for all the responses. I'm curious, though, what exactly is the rationale for making functions so small? (I've heard that the function calling of Python has relatively high overhead?)
    , Jul 16, 2013
    #2
    1. Advertising

  3. On 2013-07-16, <> wrote:
    > On Tuesday, July 16, 2013 1:06:30 AM UTC+8, wrote:
    >> wrote:
    >>
    >> > So as a general idea, I should at the very least separate the GUI
    >> > from the program logic by defining the logic as a function,
    >> > correct? And the next level of separation is to define the logic as
    >> > a class in one or more separate files, and then import it to the
    >> > file with the GUI, correct?

    >>
    >> >

    >>
    >> > My next question is, to what degree should I 'slice' my logic into
    >> > functions? How small or how large should one function be, as a rule
    >> > of thumb?

    >>
    >>
    >>
    >> The way I do this is to write unit tests against the class and the
    >> functions (take a look at the unittest module). The functions methods
    >> (take a look at the unittest module). Each function should contain
    >> the smallest bit of testable logic.
    >>
    >>
    >>
    >> Another way to think about this is that each function should contain
    >> the smallest piece of logic that you can describe as one action.
    >>
    >>
    >>
    >> -
    >>
    >> Asim Jalis

    >
    > Again, thanks for all the responses. I'm curious, though, what exactly
    > is the rationale for making functions so small? (I've heard that the
    > function calling of Python has relatively high overhead?)


    Small functions are _always_ encouraged for every language. This is best
    practice for everything, not just Python. Has nothing to do with
    overhead.

    --
    -owen
    Owen Marshall, Jul 16, 2013
    #3
  4. Asim Jalis Guest

    On Mon, Jul 15, 2013 at 5:25 PM, <> wrote:

    > Again, thanks for all the responses. I'm curious, though, what exactly is
    > the rationale for making functions so small? (I've heard that the function
    > calling of Python has relatively high overhead?)
    >


    There is a small overhead, but it makes the code easier to read and
    understand. You can look at the function name and get and idea of _what_
    the function is doing instead of having to figure out _how_ it is doing it.

    Regarding optimization, after you have written your application if you see
    performance issues you can surgically optimize the spots that have the
    issues and leave most of the code untouched.

    To quote Don Knuth, "premature optimization is the root of all evil". Also
    the article at http://en.wikipedia.org/wiki/Program_optimization makes some
    good points.
    Asim Jalis, Jul 16, 2013
    #4
  5. On Tue, Jul 16, 2013 at 10:25 AM, <> wrote:
    > Again, thanks for all the responses. I'm curious, though, what exactly is the rationale for making functions so small? (I've heard that the function calling of Python has relatively high overhead?)


    A function should be as long as it needs to be - neither longer nor
    shorter. If you can name a function appropriately, and it's doing
    exactly what its name suggests, it's the right length. This generally
    produces short functions rather than long ones, but if the right
    length for a function exceeds some arbitrary limit, let the function
    be longer. For instance, I have a single function that's a bit over a
    page in length, because it's one big switch block (this isn't in
    Python, obviously), doing one thing fairly cleanly. Larger than that
    would have to be called code smell, but there's certainly nothing
    wrong with having the odd function here or there that's over Steven's
    dozen-line estimate. There'll also be plenty of really short functions
    - even one-liners.

    The largest single function in any of my code, I think, is a gigantic
    double-nested switch block in PHP .In any decent language, that would
    be divided up not just into functions but into files, but PHP has some
    stupid restrictions on its include directive that make that
    impractical. So syntactically it's one massive function, but logically
    it's about seven or eight separate sub-blocks, and the code is laid
    out in those blocks. It's just that the technical nesting level never
    actually hits zero in between them :) Have to confess, though, I've
    had some fairly large functions in C++ (not as big as the
    aforementioned, but still fairly large - what's the next one down from
    gigantic, megantic? [1] would suggest so), which in some cases could
    be split if I felt like putting in the time to do it.

    ChrisA

    [1] http://gatherer.wizards.com/Pages/Card/Details.aspx?multiverseid=370794
    Chris Angelico, Jul 16, 2013
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Satish Appasani

    Ideal way of posting a value to a new page?

    Satish Appasani, Jul 29, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    362
    Natty Gur
    Jul 29, 2003
  2. D. Susman
    Replies:
    25
    Views:
    594
    James Kanze
    Mar 23, 2008
  3. Roland Koebler

    Re: Ideal way to separate GUI and logic?

    Roland Koebler, Jul 13, 2013, in forum: Python
    Replies:
    0
    Views:
    79
    Roland Koebler
    Jul 13, 2013
  4. Dave Cook
    Replies:
    0
    Views:
    85
    Dave Cook
    Jul 13, 2013
  5. Wayne Werner

    Re: Ideal way to separate GUI and logic?

    Wayne Werner, Jul 13, 2013, in forum: Python
    Replies:
    4
    Views:
    100
    Steven D'Aprano
    Jul 15, 2013
Loading...

Share This Page