Function is a block of code which performs a specific task.
-1
votes
2answers
61 views
How to minimize the code duplication? [on hold]
I have the following code which I wrote, but there is so much of code duplication. I would like to minimize it but I am unable to come up with the ideas. Could anyone help me out with this?
myFunc(r: ...
1
vote
2answers
66 views
JavaScript function that acts on same element every time
Let's say I have a HTML/JS project which involves interaction with a canvas. Resizing, drawing, retrieving the current state, etc.
Obviously I should separate these into several functions. However, I'...
182
votes
16answers
36k views
My boss asks me to stop writing small functions and do everything in the same loop
I have read a book called Clean Code by Robert C. Martin. In this book I've seen many methods to clean up code like writing small functions, choosing names carefully, etc. It seems by far the most ...
0
votes
1answer
84 views
What native JavaScript functions/methods/properties/operators can return null? undefined? [closed]
I'm writing some code that needs to deal with many potential JavaScript values including so-called non-values . I'm realizing it would be very helpful to know if there are some general rules about ...
4
votes
2answers
154 views
How should I name functions that return values in Python?
I'm confused about choosing names for my functions in Python. Sometimes Python built-in functions are imperative such as: print function and string method find. Sometimes they aren't such as: len its ...
0
votes
1answer
85 views
When is using a lot of parameters considered using too many parameters?
I do a lot of programming in my class. It's just my first semester, and most of my stuff is review. I've taken to using a verifyInput function to ensure that user inputs match certain criteria. Since ...
6
votes
4answers
291 views
Techniques for minimising number of function arguments
In Clean Code, it is written that "the ideal number of arguments for a function is zero". The reasons why are explained and make sense. What I'm after is techniques to refactor methods with 4 or more ...
17
votes
5answers
4k views
Function returning true/false vs. void when succeeding and throwing an exception when failing
I'm building an API, a function that uploads a file. This function will return nothing/void if the file was uploaded correctly and throws an exception when there was some problem.
Why an exception ...
-3
votes
1answer
96 views
Python recursion in functions [closed]
I am just learning about recursive functions. This was an example given in a textbook where t is a turtle. I was wondering how the code works, but more specifically, how the code runs past the first ...
0
votes
2answers
77 views
How to name variables that clash with functions?
As an example, in Python I have a lowest_prime_factor function which does it exactly what it says it does. Then, in another function, I need to call this function and store it. In another language ...
0
votes
2answers
166 views
How to 'read' arrow functions in ES6?
I've been trying to learn some of the new features of ES6, and while most of it makes sense, I'm having trouble grasping the arrow function. I'm not asking so much why it exists as I am how to read it....
1
vote
1answer
97 views
How do you organize your hoisted functions?
In a module (file) I organize several functions related to a specific domain task. I usually put the function linearly from top to bottom, then compose a functional call on the bottom. Doing so I can ...
-1
votes
2answers
176 views
One Word for Functions, Variables, and Objects
Is there a single word that can describe JavaScript functions, variables, and objects? I was thinking "entities", but that word is usually used to describe the mapping between objects and a database. ...
3
votes
2answers
296 views
Can I add to a built-in function?
I want be to be able to re-write the code of an existing PHP function in an abstract manner.
Here is an example:
scandir() scans a directory for files and folders, and returns the relative paths '.' ...
5
votes
1answer
309 views
How to move from OOP object composition to FP function composition in C#
I have been working for a few weeks on a new web project and I am realizing that all I am doing is basically calculations and transformations on data, and that most of my classes do not contain any ...
16
votes
6answers
4k views
A language based on limiting amount of arguments passed to functions
The idea is inspired by the fact operators such as +, -,%, etc. can be seen as functions with either one or two arguments passed, and no side-effects. Assuming I, or someone else, writes a language ...
0
votes
1answer
189 views
Is it possible to run a program in C language without libraries?
I was working on a C program and i just noted that i can execute my program even if i didn't declared the required libraries, this is the code:
#include <stdio.h>
#include <string.h>
int ...
1
vote
2answers
174 views
How can you enforce a “do you really want to do this” confirmation in a publically exposed API?
Is there a commonly (or rarely) used pattern for a "confirming you want to do this" message when calling a function from a library?
I have a API that exposes some operations that are potentially ...
40
votes
15answers
8k views
Is it inadvisable to make a function that essentially renames a built-in function?
I get confused over min and max functions, in certain contexts.
In one context, when you're using the functions to take the greater or lesser of two values, there is no issue. For example,
//how ...
0
votes
2answers
136 views
Is it bad practice to verify arguments passed to a function? [duplicate]
I find myself writing a lot of functions whereby the first few lines "verify" the arguments, that is, they typecast, check falseness, range, etc. before doing any work. The main reasoning for this is ...
0
votes
1answer
89 views
Fundamental difference between a static class and namespaced functions
I have been using "static" classes as a method to group functions with relating purposes under a common name that provides readability and maintainability to the code at the cost of performance and ...
3
votes
2answers
305 views
In C++, why shouldn't all function parameters be references?
I am currently learning C++ from the C++ Primer 5th edition. The book's chapter on functions states that only large objects (large being relative as standard library strings count, but "primitive ...
4
votes
2answers
161 views
Calling different library functions based on parameter
I am writing a rather large class where the user can specify at runtime whether she wants to use algorithm A or B of an third party library e.
Basically there is only one function call different in ...
0
votes
1answer
129 views
How to call an unknown member function through an instance of a related class?
I have two classes. The first, called Game_Events, controls the objects and manages the general tasks. The second, called Button, is for one of those object instances.
The example below is wrong but ...
0
votes
3answers
110 views
Design for a function to skip a step if called multiple times
Terrible title, but this is the situation I find myself in often and have not found a good design to make it nice. Lets say I'm working with Javascript and I have an object I am working on that needs ...
3
votes
4answers
245 views
When should a function take a pointer for a collection to fill vs returning a pointer with a filled collection?
In C++ I frequently see these two signatures used seemingly interchangeably:
void fill_array(Array<Type>* array_to_fill);
Array<Type>* filled_array();
I imagine there is a subtle ...
-3
votes
1answer
125 views
Returning a variable or a return code from a function
Let assume that we have a main() function in which we call a createDirectory() function.
In Python for instance, the code would be:
def main():
# Do some stuff
createDirectory(myPath)
# ...
2
votes
2answers
60 views
Cleanly generating several 0-airty JavaScript functions with slightly different bodies
Say I have a bunch of JavaScript functions similar to:
message = [“000”, “111”, “222”, “333”];
function F0(){
alert (message[0]);
}
function F1(){
alert (message[1]);
}
function F2(){
...
-2
votes
1answer
64 views
Multiple arguments for last parameter
I have function that has three parameters, called key, value and attribute.
Parameter attribute may be empty or have within two arguments. Code of function below is shortened (deprived of arguments ...
3
votes
3answers
507 views
Differences between “first-class function” mechanisms
Some languages (Javascript, Python) have the notion that a function is an object:
//Javascript
var fn = console.log;
This means that functions can be treated like any other object (first-class ...
4
votes
1answer
153 views
Is there a programming language which requires argument qualifiers (reference/value) to be specified at the call point?
For quite a long time now, I have been using a calling convention from C++ google style guide, which boils down to the following: "[for a function] arguments are values or const references while ...
7
votes
3answers
1k views
Is a function getting a value from another function considered pure?
I'm trying to figure out a way to handle default variable values when making functions without side effects and have ended up with the following:
function getDefaultSeparator() {
return ':';
}
...
24
votes
2answers
1k views
Why is passing large anonymous functions as arguments to other functions so widely accepted in JavaScript?
I have an opinion (which I am sure will be shared by some) that passing anonymous functions which contain more than a few lines of code, as arguments to other functions affects readability and self-...
1
vote
3answers
322 views
Is it considered good practice to always have methods return a value?
Sorry for the terrible title but hopefully these snippets will give you the gist.
Method 1:
class Person:
def __init__(self, name):
self.name = name
def set_name(self, new_name):
...
1
vote
4answers
144 views
How to name functions that use conditionals in refactoring [closed]
Consider this bit of code :
private Norf foo(Baz baz) {
// ...
// Logic on baz
// ...
if (baz.color == Baz.BLUE) {
// Do this thing
}
// ...
// More logic
// ...
2
votes
1answer
151 views
Enumerating the primitive recursive functions
How can I enumerate (by expression tree size, for example) all of the primitive recursive functions that map natural numbers to natural numbers in a traditional programming language like C?
For ...
1
vote
1answer
225 views
Why use tuples as function parameters in languages that support currying?
In languages that support currying, I can't think of many cases where using a tuple as function input parameters would be better than breaking the tuple apart into multiple parameters, which then ...
-1
votes
2answers
69 views
“Separate Query from Modifier” and return values [closed]
I've heard it said that you should keep query and modifier functions separate from one another, and I agree with the sentiment. When trying to apply it, I often run into two issues/questions:
What, ...
11
votes
5answers
1k views
Swift functions vs computed properties
Say I have have a class Event as follows:
class Event {
private var attendees: [Person] = []
// Case 1
//*******
// Should I use a func…
func countOfAttendees() -> Int {
...
0
votes
1answer
191 views
How to document **kwargs in python? [closed]
I have a function which has a large number of arguments.
I want to have the names of the arguments available in the help() function, but I want the results as a dict.
At the moment, I have the ...
2
votes
2answers
361 views
Java - Using a Function variable to set the toString() method's return value
Lately I've started adding this to certain classes in an in-house API:
public class MyClass {
// I usually do this with classes I expect to
// be printed out or tested a lot (particularly
// ...
1
vote
1answer
115 views
Reducing the arity of functions [closed]
I have discovered that reducing the arity of functions in my code to zero or one improves their non-functional characteristics significantly, such as testability, maintainability and their ...
0
votes
1answer
188 views
Function or class design in library API for efficient object reuse
I am currently building a library in C++11, where I spent a lot of time trying to design a good interface. After some small redesigns along the way, I've ended up with a design that I am happy with. ...
5
votes
3answers
738 views
Functions whose parameters are implicitly supplied
Callback functions are able to accept some parameters, but they are not explicitly declared when calling the function.
How does this actually work? What is going on that allows us to pass a function ...
5
votes
2answers
311 views
What is the difference between currying and partial function application in practice
I understand the difference between partial function application and a curried function (f(X x Y x Z) -> N vs f(X -> (Y -> (Z -> N)))), but I do not see what the consequence of this ...
3
votes
1answer
434 views
What does comma signify in a function definition?
From PHP manual:
array array_change_key_case ( array $array [, int $case = CASE_LOWER ] )
What does comma signify in a function definition?
1
vote
1answer
241 views
Function name in parentheses after fuction call in Python
I ran into these lines of code in the QPYTHON Android app. They are part of a sample that uses the Bottle module to create a simple Web server that seems to work fine.
app = Bottle()
app.route('/', ...
2
votes
2answers
379 views
What's wrong about extending a class with prototype methods?
I was at a bar last night with a few of my colleagues. They said that it's a bad idea to extend the functionality of basic JavaScript Objects with a prototype method.
For example, let's say you ...
0
votes
2answers
72 views
problems compiling a function with a trait Add in Rust [closed]
I'm trying to write a generic function summ in rust - but to no avail. Could someone please elucidate the problem?
fn summ<T:Add>(a:T,b:T)->T {
a+b
}
0
votes
1answer
225 views
Const and non-const methods, and possible mutable data member?
I'm working on a design and implementation change due to a bug. The bug fix requires us to modify a class object in a const method. I think the two options are:
Have the non-const method cast this to ...