473,803 Members | 2,972 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

common practice for creating utility functions?

Just a quickie for today: Is it common (and also preferred, which are
two different things!) to create a function that has the sole job of
calling another function?

Example: for fun and exercise, I'm creating a program that takes a quote
and converts it into a cryptogram. Right now I have four functions:

convert_quote -- the main function that starts it all
make_code -- makes and returns the cryptogram
make_set -- called from make_code, converts the quote into a set so each
letter gets only one coded letter
test_sets -- makes sure a letter isn't assigned to itself

So my first question is this: should I make a Cryptogram class for this,
or are functions fine? If the latter, then back to my original point:
can I do something like this:

def convert_quote(q uote):
return make_code(quote )

Or does it not make sense to have a function just call another function?
May 15 '06 #1
25 1874
John Salerno <jo******@NOSPA Mgmail.com> writes:
So my first question is this: should I make a Cryptogram class for
this, or are functions fine? If the latter, then back to my original
point: can I do something like this:

def convert_quote(q uote):
return make_code(quote )


It's fine to do that if it expresses your intentions more clearly.
It's also normal to write a call for a function you haven't written
yet:

some_quote = convert_quote(a nother_quote)

where you write convert_quote afterwards. It may not be til you
actually write convert_quote that you notice that it simply calls
another function. If that happens, it's fine.

Also, you don't need to make a class if you don't need to do OOP-like
things with the instances. But, sometimes, making a class also
clarifies your intentions. For example, making a class means it
becomes much simpler to have multiple cryptograms active at the same
time, if you want to do that.

Programming is not just creating strings of instructions for a
computer to execute. It's also "literary" in that you are trying to
communicate a program structure to other humans reading the code.

Someone once asked the famous bike racing champion Eddy Merckx (he was
sort of the Lance Armstrong of the 1970's and then some) how to become
a good racer. His answer was "ride a lot". It's the same way with
coding. As you get more experienced these questions get easier to
answer for yourself. But always, do whatever works, and if your code
gets messy or hits technical snags, ask yourself what alternative
approaches avoid those snags, and thereby develop a sense of what to
do.
May 15 '06 #2
On Mon, 15 May 2006 18:26:01 GMT,
John Salerno <jo******@NOSPA Mgmail.com> wrote:
So my first question is this: should I make a Cryptogram class for
this, or are functions fine? ...
Perhaps I'm "old school," but I don't bother with classes unless I'm
going to end up with multiple instances (or I'm pushed into a corner by,
e.g., a GUI framework).
... If the latter, then back to my original point: can I do something
like this: def convert_quote(q uote):
return make_code(quote )
Of course you can. Or, since this is python, you can also do this:

convert_quote = make_quote
Or does it not make sense to have a function just call another
function?


If there's a good design-level reason (like keeping certain objects or
classes unaware of others, or leaving room for something you know you
will add later), then there's nothing wrong with a function consisting
solely of another function call. If you end up with a lot of those tiny
functions, though, and they persist through multiple development cycles,
then you may be making a systematic mistake in your design.

Regards,
Dan

--
Dan Sommers
<http://www.tombstoneze ro.net/dan/>
"I wish people would die in alphabetical order." -- My wife, the genealogist
May 15 '06 #3
John Salerno wrote:
...Is it common ...[and preferred] to create a function that has the sole job of
calling another function?

Example: ... cryptogram. Right now I have four functions:

convert_quote -- the main function that starts it all
make_code -- makes and returns the cryptogram
make_set -- called from make_code, converts the quote into a set so each
letter gets only one coded letter
test_sets -- makes sure a letter isn't assigned to itself

So my first question is this: should I make a Cryptogram class for this,
or are functions fine? Functions are just fine. I'd use a class if they wanted to share state.
... can I do something like this:
def convert_quote(q uote):
return make_code(quote )
Or does it not make sense to have a function just call another function?

Obviously you _can_ do that. I wouldn't, however. If (to you) the four
functions above "mean" something different, I'd implement convert_quote
with:
convert_quote = make_code

--
-Scott David Daniels
sc***********@a cm.org
May 15 '06 #4
John Salerno wrote:
Just a quickie for today


Another quick one I'll combine in this thread: How can I create two
separate conditions in a for loop?

Such as this, which doesn't seem to work beyond string.punctuat ion:

for char in string.punctuat ion or string.whitespa ce:
May 15 '06 #5
Dan Sommers wrote:
Perhaps I'm "old school," but I don't bother with classes unless I'm
going to end up with multiple instances (or I'm pushed into a corner by,
e.g., a GUI framework).


Thanks to all of you! In fact, I sort of came to this same conclusion. I
started with a class, but realized there wasn't much of a point since
I'm just doing some 'work' and returning the cryptogram.

I might try the other suggestion too about convert_quote = make_code,
that might be ok. Or I can simply call make_code.

Thanks!
May 15 '06 #6
John Salerno wrote:
John Salerno wrote:
Just a quickie for today


Another quick one I'll combine in this thread: How can I create two
separate conditions in a for loop?

Such as this, which doesn't seem to work beyond string.punctuat ion:

for char in string.punctuat ion or string.whitespa ce:


I tried this:

punc_space = string.punctuat ion + string.whitespa ce
for char in punc_space:
May 15 '06 #7
QOTW

"Programmin g is not just creating strings of instructions for a
computer to execute. It's also 'literary' in that you are trying to
communicate a program structure to other humans reading the code." Paul
Rubin

rpd

May 15 '06 #8
John Salerno wrote:
How can I create two separate conditions in a for loop?

...
I tried this:

punc_space = string.punctuat ion + string.whitespa ce
for char in punc_space:


That's probably best. If the sources are not so simple, you could use:

import itertools

for char in itertools.chain (onesource, anothersource, yetanother):
...

--Scott David Daniels
sc***********@a cm.org
May 15 '06 #9
Scott David Daniels wrote:
John Salerno wrote:
How can I create two separate conditions in a for loop?

... I tried this:

punc_space = string.punctuat ion + string.whitespa ce
for char in punc_space:


That's probably best. If the sources are not so simple, you could use:

import itertools

for char in itertools.chain (onesource, anothersource, yetanother):
...

--Scott David Daniels
sc***********@a cm.org


Cool. Iterators are fun! :)
May 15 '06 #10

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

Similar topics

12
1800
by: D Witherspoon | last post by:
What is the accepted method of creating a data class or business rules object class with properties that will allow the returning of null values? For example... I have a class named CResults with the following properties. TestID int QuestionID int AnswerID int So, this is a simple example, but I want to be able to know if AnswerID is
6
1676
by: Don Wash | last post by:
Hi All! I'm developing ASP.NET pages using VB.NET language. My background is VB6 and ASP3. Right now, I'm evaluating strategies on creating reusable and common functions and methods for ASP.NET. In ASP 3, it was nothing more than functions and methods coded in #Include Files. Now ASP.NET offers features such as Page Inheritance and full software development support. So I'm just wondering, what should I do if I have 5 functions, that...
5
3203
by: wrecker | last post by:
Hi all, I have a few common methods that I need to use at different points in my web application. I'm wondering where the best place would be to put these? I think that I have three options. 1. I can create a common module like common.vb in my project and put all the functions in there. 2. Create a utility class and create the common functions as shared
22
2513
by: David Mathog | last post by:
One thing that keeps coming up in this forum is that standard C lacks many functions which are required in a workstation or server but not possible in an embedded controller. This results in a plethora of "don't ask here, ask in comp.x.y instead", for queries on functions that from the documentation available to the programmer appear to be part of the C language. I think largely because of this "least common denominator" C language...
0
9565
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
10550
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
10317
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
10295
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
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5501
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...
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2972
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.