I was wondering if it was good to have this since it makes code less portable.
Thanks
I was wondering if it was good to have this since it makes code less portable.
Thanks
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.
Yes. This is the basis of programming. It in no way makes the code less portable, rather the reverse.
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:
So please call functions that you wrote from other functions, it's how it works, it is just great.
It's good practice not to write very long functions. That usually requires the use of other self written functions.
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.
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.
You just described programming. Familiarize yourself with:
Reusing "stuff" is one of the primary tenets of any engineering discipline, not only computer science.