Function is a block of code which performs a specific task.

learn more… | top users | synonyms

1
vote
1answer
65 views

How do multiple levels of inheritance affect virtual function call overhead in C++?

I am considering using a class hierarchy with more than a single level of inheritance, where virtual member functions form a "chain", for example: struct Base { virtual void foo(); }; struct D1 : ...
0
votes
1answer
72 views

How to split a Service class in two but still use them like one?

I've got a class: AuthenticationService findLoggedInUser() Checks session if User is logged in. If not, check client persistent user login cookie and log in. loginUser($email, $pw, $remember = ...
14
votes
7answers
2k views

Function only returns unchanged parameter, useless?

I just found this function in the project I'm working at: -- Just returns the text unchanged. -- Note: <text> may be nil, function must return nil in that case! function Widget:wtr(text) ...
39
votes
9answers
5k views

Beginning a sentence with a function name? [closed]

Occasionally while typing something up that relates to a case-sensitive programming language I end up starting a sentence with a function name. Now the rules of English state that the first word in a ...
0
votes
1answer
296 views

Does this function do one thing only?

Is the following method considered to be doing one thing only? I'm wondering about that since it takes an optional argument. public function findErrors($name = null) { if ($name) { ...
9
votes
4answers
437 views

Are first-class functions a substitute for the Strategy pattern?

The Strategy design pattern is often regarded as a substitute for first-class functions in languages that lack them. So for example say you wanted to pass functionality into an object. In Java you'd ...
28
votes
8answers
5k views

Is splitting up a function into several inner functions an anti-pattern? [duplicate]

Imagine a long and complicated process, which is started by calling function foo(). There are several consecutive steps in this process, each of them depending on result of the previous step. The ...
0
votes
2answers
107 views

Should you avoid adding unecessary stack frames?

I have been working with laravel (PHP) recently, and there is an option when using their fluent ORM to define where clauses using closures. This gives us two, functionally equivalent, ways to specify ...
2
votes
2answers
128 views

Property or Method to obtain information about a collection?

Suppose I have a MyList class which extends ObservableCollection<MyObject> where MyObject has the Boolean Property Enabled. Let's say I want the number of MyObject objects that have the ...
0
votes
3answers
146 views

Use of malloc in C

Is it necessary to call free function every time we use malloc in C. I am asking this because I have seen many times that it is not called . Thank you
0
votes
1answer
222 views

static or non-static, that is the question? [duplicate]

For comparisons / evaluations of objects of the same class(and other purposes), is it better to define a static or a non-static (is this called "dynamic" by chance?) function? Sample code: class ...
1
vote
2answers
179 views

What is the concept behind writing a cancel operation in c++? [closed]

I'm attempting to write a cancel operation for a software download application. This application will first transfer the software to the device and then install the software on it. (These are givens ...
0
votes
3answers
132 views

Writing a method to 'transform' an immutable object: how should I approach this?

(While this question has to do with a concrete coding dilemma, it's mostly about what's the best way to design a function.) I'm writing a method that should take two Color objects, and gradually ...
4
votes
2answers
360 views

Why are there no function pointers in Java? [closed]

Lately I started studying about different interesting concepts that exist in languages other than Java. Since the only language I've ever programmed with is Java, a lot of these concepts are very new ...
1
vote
3answers
114 views

Reason to treat internal class variables and functions inside a class as “separate” entities

Note: there is a similar question that addresses my subject: Better style for member variables? ... but that question does not address "dealing with large legacy code base and comprehension of ...
1
vote
1answer
82 views

Use Generic of Specific Function names for similar objects

Is there a school of thought on putting focus on using generic names for functions, opposed to naming functions based on the things they do? Example Say we have a Bill / Price Sheet object, that has ...
5
votes
6answers
566 views

Are closures considered impure functional style?

I have a few related questions on closures: Are closures considered impure in functional programming? It seems one can generally avoid closures by passing values directly to a function. Therefore ...
0
votes
1answer
148 views

design patterns in Javascript - toggle functionality [duplicate]

My goal is to add some functionality to the bxslider script - in this case, I want to be able to enable/disable a keyboard event handler AFTER a slider is instantiated. I guess this code is self ...
2
votes
1answer
87 views

How to simplify unit testing with higher order functions?

this is not a question on how to write unit tests this is not a question on what to test this is a question on reducing the typing needed overall for a personal testing framework I know you can ...
2
votes
4answers
105 views

To make a class function or not?

I'm wondering about functions used like this: object1->objectMethod1(arg1, arg2); if objectMethod1: - does not change the object state - does not use the object's state at all - is used by the ...
2
votes
3answers
157 views

Using past participle for non-altering functions [closed]

I often have naming problems when I write classes and functions. Generally speaking, I try to follow the following set of rules if I have to deal with a function whose name is a verb: If the ...
1
vote
3answers
230 views

Does designing a method that changes the argument's (if was object) values a good practice?

The premise is using a language (e.g. C#, javascript) which passes an object by reference into a method. Assume there is an object "Player" which has a property "Level" and there is a method takes ...
4
votes
1answer
227 views

Passing a Scala function to a Java 8 method

The following Scala code works and can be passed to a Java method expecting a function. Is there a cleaner way to do this? Here's my first pass: val plusOne = new ...
0
votes
3answers
106 views

Long vs short scripts? Big vs small scripts? [closed]

As a programmer, I have always wondered whether it is preferable to write (a) short modular functions that are each stored in their own script (i.e., file) or (b) long comprehensive scripts that ...
2
votes
6answers
783 views

An alternative to an array of functions?

I'm programming an app (php) which requires a very long list of similar yet different functions, which are being called by a set of keys: $functions = [ "do this" => function() { // ...
2
votes
2answers
316 views

Why function returning by Address can not be a Lvalue?

Why it is not possible to make it LValue if a function return by address (while possible in case of reference)? int* returnByAdress() { int x =20; return &x; } int& ...
0
votes
2answers
1k views

When to use functions vs methods?

So I know that methods are more OOP than functions. I was wondering if someone could show me an example of a function and a method and explain the differences between methods and functions to me? ...
1
vote
3answers
1k views

If statements vs switch cases? in a JavaScript game and if to use a function [duplicate]

I am developing a game in JavaScript where you start with a user input, stored in the variable "controller". The options for the user consists of start to start the game or about to learn about the ...
5
votes
2answers
128 views

Should more than one function be used when they do similar, but not identical things?

For example, if I wanted to generate some HTML based on some input. Which is the preferred way out of: <?php function generate_html($type, $input){ switch($type){ case 'paragraph': ...
6
votes
6answers
511 views

Why do many languages not support named parameters? [closed]

I was just thinking how much easier it would be to read code if, when calling a function, you could write: doFunction(param1=something, param2=somethingElse); I can't think of any drawbacks and it ...
1
vote
1answer
120 views

Helper functions - well defined inputs or well defined outputs?

Problem In Codeigniter (PHP framework), I am trying to create a helper function that will inject an ActiveRecord (for MySQL) command into a chain of existing ActiveRecord calls. The reasoning for ...
1
vote
2answers
219 views

Using Functions for Never-Repeated Code [duplicate]

What are some best practices for using functions to break up large blocks of code into discrete chunks of logic when those functions are only ever going to be used once within the lifetime of a ...
9
votes
5answers
2k views

Multiple arguments in function call vs single array

I have a function that takes in a set of parameters, then applies to them as conditions to an SQL query. However, while I favored a single argument array containing the conditions themselves: ...
32
votes
6answers
2k views

Why is “tight coupling between functions and data” bad?

I found this quote in "The Joy of Clojure" on p. 32, but someone said the same thing to me over dinner last week and I've heard it other places as well: [A] downside to object-oriented programming ...
3
votes
3answers
431 views

Good Procedure or Function Design

This is in reference to the question posted here. As I would judge it, the question there should be closed simply because it seems to ask why the Borland developers made one thing a function and ...
8
votes
2answers
812 views

What is an example of a continuation not implemented as a procedure?

An interesting discussion about the distinction between callbacks and continuations over on SO has prompted this question. By definition, a continuation is an abstract representation of the logic ...
3
votes
2answers
429 views

Should I put utility methods inside a class?

I have been working on a library which contains a large set of functions. For the sake of simplicity, I am going to use just one set as an example. I am not sure which is the better way, in terms of ...
27
votes
4answers
1k views

How do programming languages define functions?

How do programming languages define and save functions/methods? I am creating an interpreted programming language in Ruby, and I am trying to figure out how to implement function declaration. My ...
17
votes
11answers
1k views

Why are we supposed to use short functions to sectionalize our code? [duplicate]

I've seen an increasing trend in the programming world saying that it is good practice to separate code blocks into their own functions. Obviously, if that code block is reusable, you should do that. ...
0
votes
1answer
76 views

Good practice to use namespace or prefix to indicate what file function is from? [duplicate]

For example, I have the function generate_salt() in encryption.file but the person may not know where generate_salt() is from. Using a namespace like encryption::generate_salt(), or ...
2
votes
3answers
125 views

Non-trivial get function, should it be named differently

For example I have this code. class Command { var $responseFactory; var $service; /** * Returns the class name of the responseFactory if it is set * or the default class name ...
-1
votes
5answers
635 views

Named output parameters vs return values

Which code is better: // C++ void handle_message(...some input parameters..., bool& wasHandled) void set_some_value(int newValue, int* oldValue = nullptr) // C# void handle_message(...some ...
44
votes
4answers
2k views

How do functional languages handle random numbers?

What I mean about that is that in nearly every tutorial I've read about functional languages, is that one of the great things about functions, is that if you call a function with the same parameters ...
5
votes
2answers
2k views

Reason for return statement in recursive function call

I just had a doubt in my mind. The following subroutine(to search an element, in a list, for example) has a return statement at the end: list *search_list(list *l, item_type x) { if (l == NULL) ...
2
votes
4answers
317 views

How do you decide what code to put into a function?

I started out with a script that was a few hundred lines. Later, I realized I wanted another script that would require much of the same code. I decided to wrap certain areas of the original script ...
1
vote
6answers
387 views

Naming functions that retrieve a value

I have this personal rule to start all function/method names with a verb. My verb of choice for functions or methods that get a value based on some data structure or object is get. I'm wondering if ...
0
votes
1answer
85 views

Show all definitions in Scheme?

I want to see all user-made definitions in a Scheme REPL, both loaded from files and entered at the REPL. Is there any way to "dump all definitions"? E.g. if there is: (define (plusone x) (+ 1 ...
0
votes
1answer
290 views

PHP function types

I am trying to find a way of classifying different types of PHP functions. For example that fopen, fwrite, fclose and so on are all part of IO, and the MySQL functions and MySQLi class is all for ...
15
votes
2answers
1k views

Boolean Method Naming Affirmative vs Negative

Should boolean methods always take the affirmative form, even when they will only ever be used in the negative form? Say I wanted to check whether an entity exists before creating one, my argument is ...
1
vote
1answer
160 views

Documenting Function That Takes Random Parameters?

What's the best approach to creating documentation (displaying the function prototype if you will) for functions that take a variety of different forms in terms of parameters. Let's say there are 10 ...