Functions vs If-Then-Else program flow

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

Functions vs If-Then-Else program flow

Post by Goatboy on Fri Sep 25, 2009 1:23 pm
([msg=30095]see Functions vs If-Then-Else program flow[/msg])

I was writing some simple PHP files today when I noticed something. Almost exclusively, the programs I write (in any language) are either 95% functions or 95% if-then-else clauses. It had never occurred to me before that I do this. This has to be bad form.

Since nobody ever really told me I was doing it wrong, it took me a long time to catch this. Do you guys have any general rules you use to determine if you should use if-then-else or functions? Since there is no excuse for this noobishness, don't assume I know anything :(
Assume that everything I say is or could be a lie.
1UHQ15HqBRZFykqx7mKHpYroxanLjJcUk
User avatar
Goatboy
Expert
Expert
 
Posts: 2782
Joined: Mon Jul 07, 2008 9:35 pm
Blog: View Blog (0)


Re: Functions vs If-Then-Else program flow

Post by insomaniacal on Sat Sep 26, 2009 7:26 am
([msg=30125]see Re: Functions vs If-Then-Else program flow[/msg])

I also find myself writing mostly functions and wrapping them in side each other to create program flow, it's probably no problem for small-scale programs.

I assume you mean something like this:

Code: Select all
def func1
[code here] -- calls func2 near end or at a if-then sequence
def func2
[code here]

func1
It's not who votes that counts, it's who counts the votes
insomaniacal.blog.com
User avatar
insomaniacal
Addict
Addict
 
Posts: 1210
Joined: Sun May 24, 2009 10:21 am
Blog: View Blog (0)


Re: Functions vs If-Then-Else program flow

Post by thedotmaster on Sat Sep 26, 2009 1:23 pm
([msg=30129]see Re: Functions vs If-Then-Else program flow[/msg])

It's very good form to write entirely using functions, then with a small block at the end that links the functions together. Using functions reduces the use of global variables.
When I write in Python I generally write in blocks, rather than functions - though I do use functions as part of my code, of course. I will have a block of code that sets a few global variables, uses them in some way, then at the end of the block of code (which is usually a particular part of an application, such as getting a username from a website before moving onto another block of code that might do something with it), I will clean up and delete (or free) any variables that are unneeded.
Any variables that are needed in future can be kept, then deleted when appropriate.
Doing this makes the code far more readable as you don't need to keep on scrolling up to find another function - it goes from the top to the bottom, with any functions being close to where they are referenced.
For larger scale projects, however, I wouldn't do this - it is only for projects of below 500 lines or so, as there is always going to be the odd variable that you forget to free.
Image
User avatar
thedotmaster
Contributor
Contributor
 
Posts: 984
Joined: Sun May 04, 2008 4:39 pm
Location: North West UK
Blog: View Blog (1)


Re: Functions vs If-Then-Else program flow

Post by r-ID on Mon Sep 28, 2009 11:57 am
([msg=30177]see Re: Functions vs If-Then-Else program flow[/msg])

you do not need to write a function if you use it once in the code.
functions are for avoiding same code writing over and over again.
Once same code repeats write a function
r-ID
Poster
Poster
 
Posts: 172
Joined: Mon Dec 29, 2008 6:04 pm
Blog: View Blog (0)


Re: Functions vs If-Then-Else program flow

Post by thedotmaster on Mon Sep 28, 2009 12:08 pm
([msg=30178]see Re: Functions vs If-Then-Else program flow[/msg])

r-ID wrote:you do not need to write a function if you use it once in the code.
functions are for avoiding same code writing over and over again.
Once same code repeats write a function


That's not true. Using a function saves memory as variables are localised, rather than globalised.
Image
User avatar
thedotmaster
Contributor
Contributor
 
Posts: 984
Joined: Sun May 04, 2008 4:39 pm
Location: North West UK
Blog: View Blog (1)


Re: Functions vs If-Then-Else program flow

Post by r-ID on Mon Sep 28, 2009 1:17 pm
([msg=30180]see Re: Functions vs If-Then-Else program flow[/msg])

1) every time you call a function variables are initialized
2) you have to declarate additional variables, such as counters, you can't reuse same variables.
3) functions are slow, everytime you call a function there are: address pushing, variables creation, jump to the function, result writing, poping variables, pop'ing memory address and jumping back to the code.

the main reason why functions or so called procedures was born is to allow to use same code twice.

and, yes, it might save some memory during the execution process.

-- Mon Sep 28, 2009 1:20 pm --

but to do that just to save memory? what kind of programs do you write, and what language? php i guess
r-ID
Poster
Poster
 
Posts: 172
Joined: Mon Dec 29, 2008 6:04 pm
Blog: View Blog (0)


Re: Functions vs If-Then-Else program flow

Post by thedotmaster on Mon Sep 28, 2009 1:42 pm
([msg=30181]see Re: Functions vs If-Then-Else program flow[/msg])

r-ID wrote:1) every time you call a function variables are initialized
2) you have to declarate additional variables, such as counters, you can't reuse same variables.
3) functions are slow, everytime you call a function there are: address pushing, variables creation, jump to the function, result writing, poping variables, pop'ing memory address and jumping back to the code.

the main reason why functions or so called procedures was born is to allow to use same code twice.

and, yes, it might save some memory during the execution process.

-- Mon Sep 28, 2009 1:20 pm --

but to do that just to save memory? what kind of programs do you write, and what language? php i guess


No, I program in Python and I'm studying C.
A function is just a block of instructions in memory. Extra memory for variables has to be requested from the CPU, yes, but that's better than assigning memory globally - that's how memory leaks begin to occur.
One of the uses of functions is to reuse code, the other is to separate code off into blocks.

Programming languages such as C and C++ all use functions to encapsulate code - there is no 'body', just a function that is initialised at the start.

It is standard practice and good programming style to do this.
Image
User avatar
thedotmaster
Contributor
Contributor
 
Posts: 984
Joined: Sun May 04, 2008 4:39 pm
Location: North West UK
Blog: View Blog (1)


Re: Functions vs If-Then-Else program flow

Post by r-ID on Mon Sep 28, 2009 4:02 pm
([msg=30187]see Re: Functions vs If-Then-Else program flow[/msg])

Extra memory for variables has to be requested from the CPU, yes, but that's better than assigning memory globally - that's how memory leaks begin to occur.

I don't understand this at all.

"Extra memory for variables has to be requested from the CPU".
Could you explain this? CPU does have memory, called registers, but it's for mashine instructions only, it has nothing to do with allocated memory.

"that's better than assigning memory globally"
Who said about global variables? There is no need to use global variables at all (depends on programming language)

"that's how memory leaks begin to occur"
Doesn't make sense. There is no leak about using global variables.

C++ is object-oriented programming. There are very little functions in it. There are methods and objects mostly.

Oh and i can code in 10 programming languages.
r-ID
Poster
Poster
 
Posts: 172
Joined: Mon Dec 29, 2008 6:04 pm
Blog: View Blog (0)


Re: Functions vs If-Then-Else program flow

Post by thedotmaster on Mon Sep 28, 2009 4:29 pm
([msg=30193]see Re: Functions vs If-Then-Else program flow[/msg])

The CPU is responsible for allocating memory (i.e. RAM).
If a variable isn't local, it's global.
Yes it does make sense, as soon as you get a build up of memory that is not freed then the size of the program in memory increases, the buildup of this over time is called a memory leak.
You're joking, right? Of course it uses functions. A method is a function! It's a function that's attached to an class.
Finally, awesome - but maybe if you'd spent your time learning just one or two languages then you'd know a little more about how things work, rather than being able to tick boxes.
Image
User avatar
thedotmaster
Contributor
Contributor
 
Posts: 984
Joined: Sun May 04, 2008 4:39 pm
Location: North West UK
Blog: View Blog (1)


Re: Functions vs If-Then-Else program flow

Post by r-ID on Mon Sep 28, 2009 4:40 pm
([msg=30194]see Re: Functions vs If-Then-Else program flow[/msg])

The CPU is responsible for allocating memory (i.e. RAM). Since when?

If a variable isn't local, it's global. doh

You're joking, right? Of course it uses functions. A method is a function! TRUE,
It's a function that's attached to an class. (first time heard such sequence) class funtion is method (you know that), but speaking about class it is better to say method :P

I know any of that programming language better then you do :P
r-ID
Poster
Poster
 
Posts: 172
Joined: Mon Dec 29, 2008 6:04 pm
Blog: View Blog (0)


Next

Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests