1

Is it good practice to create functions which use other functions that you built?

I was wondering if it was good to have this since it makes code less portable.

Thanks

alain.janinm
  • 19,951
  • 10
  • 65
  • 112
jmasterx
  • 52,639
  • 96
  • 311
  • 557
  • 1
    How does this make code less portable? – Will A Jul 31 '10 at 19:25
  • 7
    Isn't that the whole purpose of, you know, creating your own functions? To *use* them? – BoltClock Jul 31 '10 at 19:26
  • It is less portable because you might need to drag in the whole class to use the function – jmasterx Jul 31 '10 at 19:27
  • If you frequently find yourself dragging in a behemoth class just to use a single function (and it's not some form of "utility class" - a class just containing useful functions) then you're most likely doing something wrong. – Will A Jul 31 '10 at 19:33
  • 3
    @Jex I think you don't understand what is meant by "portable". –  Jul 31 '10 at 19:34
  • Unless you like functions thousands of lines long, which cannot be understood by mere mortals. – Oded Jul 31 '10 at 19:36

7 Answers7

13

Very good practice.

It is called code reuse and this is what programming is all about.

As for your argument about it making "code less portable", that only makes sense in a very low level language such as assembly, and even then it makes it more portable as you can isolate platform specific code into functions.

Good code is made of small, understandable functions. Some people say that a function that is longer than 30 lines is too long.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
4

Yes. This is the basis of programming. It in no way makes the code less portable, rather the reverse.

2

In procedural and Object Oriented code - yes, though at some point you'll want to review what you've got & see if a library/etc should be dedicated to the functionality you need.

In SQL, no. SQL is set based, and abstracted functions/views/stored procedures are brittle and tend not to perform as well as re-writing with as little function/etc use as possible.

OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
  • +1 for SQL being set based. Although some DBs allow inlining of pure-SQL functions which fixes this in some cases. – rfusca Jul 31 '10 at 19:42
  • 2
    I'd upvaote this a million times if I could. Practices good in OOP are not always appropriate or good in the set-based database environment. – HLGEM Aug 02 '10 at 18:58
1

Unless you are going to have one just one big function that is your whole program (and you will be able to do complex things only with spaghetti code) you will be forced to invoke other functions so I don't see the point of your question.

Actually if you encapsulate functionality inside a function and use it all over your program you will:

  • save a lot of lines of code (reuse)
  • avoid having to fix many things instead that one
  • keep high readability
  • keep high maitainability: change just the function and it will change whenever you call it)

So please call functions that you wrote from other functions, it's how it works, it is just great.

Jack
  • 131,802
  • 30
  • 241
  • 343
1

It's good practice not to write very long functions. That usually requires the use of other self written functions.

Kaniu
  • 454
  • 2
  • 5
1

You just described programming. Familiarize yourself with:

Reusing "stuff" is one of the primary tenets of any engineering discipline, not only computer science.

David Titarenco
  • 32,662
  • 13
  • 66
  • 111
0

I would say yes.

If you have a functionality that can be broken down into 2 logical pieces, possibly reusable, do it.

All big APIs use internal components, and often even public functionality makes use of other public functions.

For example:

ShowPage(url) {
    request = new Request(url)
    response = request.Send()
    page = response.GetHTML()
    browser.Load(page)
}

can become:

RetrievePage(url) {
    request = new Request(url)
    response = request.Send()
    return response.GetHTML()
}

ShowPage(url) {
    page = Retrieve(url)
    browser.Load(page)
}

Now RetrieveUrl can be reused, for example by a function searching a website for some kind of content.

Mau
  • 14,234
  • 2
  • 31
  • 52