473,786 Members | 2,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

good design & method calls

I know the answer to this is going to be "It depends . . .", but I want
to get my mind right. In Fowler's *Refactoring* I read: "Older
languages carried an overhead in subroutine calls, which deterred
people from small methods" (followed by the basic "Extract Method"
advice). In Skip Montanaro's "Python Performance Tips"
(http://manatee.mojam.com/~skip/python/fastpython.html) I read: ". . .
use local variables wherever possible. If the above loop is cast as a
function, append and upper become local variables. Python accesses
local variables much more efficiently than global variables."

These two pieces of advice imply opposite kinds of code revisions.
Obviously they have different purposes, and both are right at different
times. I wonder if anyone has some wisdom about how to think about when
or how often to do which, how to balance them ultimately, and so on.

Charles Hartman
Professor of English, Poet in Residence
the Scandroid is at: http://cherry.conncoll.edu/cohar/Programs
http://villex.blogspot.com

Jul 18 '05 #1
3 1370
Charles Hartman wrote:
I know the answer to this is going to be "It depends . . .", but I want
to get my mind right. In Fowler's *Refactoring* I read: "Older languages
carried an overhead in subroutine calls, which deterred people from
small methods" (followed by the basic "Extract Method" advice). In Skip
Montanaro's "Python Performance Tips"
(http://manatee.mojam.com/~skip/python/fastpython.html) I read: ". . .
use local variables wherever possible. If the above loop is cast as a
function, append and upper become local variables. Python accesses local
variables much more efficiently than global variables."

These two pieces of advice imply opposite kinds of code revisions.
Obviously they have different purposes, and both are right at different
times. I wonder if anyone has some wisdom about how to think about when
or how often to do which, how to balance them ultimately, and so on.


While I don't see that Skip's specific comment in any way
implies that one should not write functions (doesn't it
*say* to write a function?), you are correct at least in
the implication that Fowler's advice about creating lots
of small functions can carry a performance cost in Python.
Python functions have a relatively high setup overhead.

On the other hand, most of refactoring is or should be
focused on improving the structure of code (removing
duplication, improving readability, generalizing, etc)
and *not* on improving performance. Why "should be?"
Because in general focusing on improving performance is
wasted effort, often with negative results when you
look at the big picture.

I suspect you've seen the repeated comments in this
comp.lang.pytho n about premature optimization ("the
root of all evil in programming" etc. etc.).
Optimization rarely *improves* code readability, and
often involves a decrease in generality or other
tradeoffs. If it conflicts this much with advice
from the refactoring world, you'd do well to pay
close attention to *why* you are trying to change
your code. If it's for performance reasons, then
don't pay a lot of attention to the Fowler stuff.
Otherwise ignore things like Skip's comment above,
and focus on making the code readable.

The thing that has helped me most in writing efficient
Python is to remember that *I'm using Python*, and
it's already rather slower than C for many of the
things I do. Why am I using Python? Not for speed,
clearly, so worrying about speed is probably a little
silly. I use Python because of the readability,
because I can write it about ten times faster than
I can write in other languages, and because I have
access to such a wide array of outstanding libraries
that someone else has already optimized. So in
the end, my definition of "efficient" gets revised,
I lower my standards to the point where my current
code is "plenty fast", and I pay attention to the
other things Skip says :-) but don't spend a lot of
time trying hard to use local variables unless it
makes my code more readable in some way.

Sorry for the rant... I didn't intend it to head
that way when I started out, but I seem to be on a
bit of an anti-optimization bent today. :-)

-Peter
Jul 18 '05 #2
On Tue, 29 Mar 2005 09:09:37 -0500, Charles Hartman
<ch************ *@conncoll.edu> wrote:
I know the answer to this is going to be "It depends . . .", but I want
to get my mind right. In Fowler's *Refactoring* I read: "Older
languages carried an overhead in subroutine calls, which deterred
people from small methods" (followed by the basic "Extract Method"
advice). In Skip Montanaro's "Python Performance Tips"
(http://manatee.mojam.com/~skip/python/fastpython.html) I read: ". . .
use local variables wherever possible. If the above loop is cast as a
function, append and upper become local variables. Python accesses
local variables much more efficiently than global variables."

These two pieces of advice imply opposite kinds of code revisions.
Obviously they have different purposes, and both are right at different
times. I wonder if anyone has some wisdom about how to think about when
or how often to do which, how to balance them ultimately, and so on.

Charles Hartman
Professor of English, Poet in Residence
the Scandroid is at: http://cherry.conncoll.edu/cohar/Programs
http://villex.blogspot.com

It depends... ;)

Converting small functions to inline, usually should only be done in
the inner most loops to optimize performance if it's needed. Moving
calculations out of those loops by doing them ahead of time is also
good.

It's good practice in python to put all of your code in functions or
class's even if it's a single main() function.

def main()
(program code)

main()

Then you avoid the slower globals unless you declare them with the
global statement.

If main, or any other functions, get too big or complex, split them up
as needed.

Ron


Jul 18 '05 #3
In article <58************ *************** *****@4ax.com>,
Ron_Adam <ra****@tampaba y.rr.com> wrote:
On Tue, 29 Mar 2005 09:09:37 -0500, Charles Hartman
<ch*********** **@conncoll.edu > wrote:
I know the answer to this is going to be "It depends . . .", but I want
to get my mind right. In Fowler's *Refactoring* I read: "Older
languages carried an overhead in subroutine calls, which deterred
people from small methods" (followed by the basic "Extract Method"
advice). In Skip Montanaro's "Python Performance Tips"
(http://manatee.mojam.com/~skip/python/fastpython.html) I read: ". . .
use local variables wherever possible. If the above loop is cast as a
function, append and upper become local variables. Python accesses
local variables much more efficiently than global variables."

These two pieces of advice imply opposite kinds of code revisions.
Obviously they have different purposes, and both are right at different
times. I wonder if anyone has some wisdom about how to think about when
or how often to do which, how to balance them ultimately, and so on.

Charles Hartman
Professor of English, Poet in Residence
the Scandroid is at: http://cherry.conncoll.edu/cohar/Programs
http://villex.blogspot.com

It depends... ;)

Converting small functions to inline, usually should only be done in
the inner most loops to optimize performance if it's needed. Moving
calculations out of those loops by doing them ahead of time is also
good.

It's good practice in python to put all of your code in functions or
class's even if it's a single main() function.

def main()
(program code)

main()

Then you avoid the slower globals unless you declare them with the
global statement.

If main, or any other functions, get too big or complex, split them up
as needed.

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
3615
by: matty | last post by:
Go away for a few days and you miss it all... A few opinions... Programming is a craft more than an art (software engineering, not black magic) and as such, is about writing code that works, first and foremost. If it works well, even better. The same goes for ease of maintenance, memory footprint, speed, etc, etc. Most of the time, people are writing code for a use in the *real world*, and not just as an academic exercise. Look at...
2
3476
by: ggg | last post by:
I'm looking for a complete project/application done with heavy use of of object-oriented programming & design. Preferably something well documented and/or commented so that I can pick it apart and learn how/why they designed it they way they did. Any suggestions?
30
3483
by: Christian Seberino | last post by:
How does Ruby compare to Python?? How good is DESIGN of Ruby compared to Python? Python's design is godly. I'm wondering if Ruby's is godly too. I've heard it has solid OOP design but then I've also heard there are lots of weird ways to do some things kinda like Perl which is bad for me. Any other ideas?
6
1160
by: Diffident | last post by:
Hello All, If I create a class based on Singleton design pattern, are the methods in this class thread-safe? Consider the following class: Public class Singleton {
12
3040
by: FluffyCat | last post by:
New on November 28, 2005 for www.FluffyCat.com PHP 5 Design Pattern Examples - the Visitor Pattern. In the Visitor pattern, one class calls a function in another class and passes an instance of itself. The called class has special functions for each class that can call it. With the visitor pattern, the calling class can have new operations added without being changed itself.
8
5119
by: tomsiw | last post by:
Hello, I am trying to create a user control which allows the user to enter a collection of items at design-time. Code example: public class MyControl : System.Windows.Forms.UserControl { .... MyItem itemList;
0
11630
weaknessforcats
by: weaknessforcats | last post by:
Design Patterns: Visitor Introduction Polymorphism requires a class hierarchy where the interface to the hierarchy is in the base class. Virtual functions allow derived classes to override base class functions. Applications using polymorphism typically have functions with base class pointers or references as arguments. Then derived objects are created and used as arguments to these functions. Inside the function, only the base class methods...
1
1574
by: Jeroen | last post by:
For a hobby project, I'm discovering the "undo/redo"- and "Command objects"- design patterns. In my specific project, I want: 1. The command objects to just hold the data; and 2. The receiving/executing objects to perform the action. Now please consider the following code: /************ Receiving/executing object code snippet ****************/
3
3644
by: Gav | last post by:
I have just started to rewrite my application using the Presenter First Design Pattern to make sure my business logic is not in the Gui itself. I've got the general idea but I'm a bit unsure as to how I should be launching a dialogue window from my main application window. What i have done is this in a nutshell. My main application window comprises of a Model, Presenter and a View. The Popup window also has a Model, Presenter and View....
0
9497
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10169
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.