Questions tagged [functions]

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

Filter by
Sorted by
Tagged with
-3
votes
0answers
6 views

Approach performance wise to send data to server side

Good Approach to send data to server using query string or some other way in AngularJS?? $scope.editParameters = function(configurationId,objectType) { if(objectType == 'Project Request') { ...
2
votes
1answer
55 views

how to represent functions and global variables in UML?

I'm going to translate into UML a C++ project I have been doing for a few days and which I estimate convenient to handle as a model for a way better management. The problem is the following: I tried ...
0
votes
1answer
80 views

How to maintain SRP with a chain of async functions?

For code maintainability I understand that it is generally better to have code structured like this: void abc() { a(); b(); c(); d(); } Instead of like: void abcd() { a(); bcd(...
0
votes
2answers
188 views

Anti-pattern in which code blocks are indirectly used as parameters

I was recently trying to explain a particular anti-pattern to some novice programmers and found that it was hard to express without an overly-detailed example. I'm sure it has a name and that someone ...
1
vote
2answers
116 views

Could multiple dynamic dispatch methods be bound to classes like traditional single dynamic dispatch methods?

C++ only supports single dynamic dispatch methods. Indeed, the following C++ program: #include <iostream> struct Shape { virtual void overlap(Shape* y) { std::cout << "Shape, Shape&...
2
votes
3answers
474 views

What do you call a function that returns a function?

Let's say, for example, that I have a function makeFoo that makes another function foo: function makeFoo(string) { return () => string } const foo = makeFoo('bar'); I know that makeFoo is a ...
8
votes
9answers
2k views

Returning true/nothing instead of true/false?

Is it OK to have a function that returns true/nothing instead of true/false? My examples use JavaScript, but I'm wondering about the general case, not attached to one specific language. The subjects ...
1
vote
3answers
118 views

Good etiquette for 2 optional arguments that can't both be used

I'll demonstrate with an example of the normal distribution in Python. def norm_pdf(x, mu=0, v=1, p=1): """Returns un-normalized probability density of normal distribution at x. mu: mean v: ...
-3
votes
1answer
39 views

Is there a good set of heuristics around how/when to use optional parameters

using a python function signature as an example: def this_function_has_an_optional_parameter(x, y = 42): ... I'm wondering if there is an existing set of guidelines specifically for this
0
votes
3answers
142 views

Should a function that returns something be named as an accessor (getSomething)?

I understand accessors have to do with OOP. For other languages such as C, imagine you have a function that given 2 numbers returns their sum. Should you name it getSum() or sum()?
1
vote
3answers
132 views

Can a function returning void be called a procedure? [duplicate]

I have read about function vs procedure function and procedure both are subroutines but function returns a value and procedure doesn't. Can a function returning void be called a procedure? Give ...
1
vote
2answers
54 views

Logic App next number

I have a need to generate a next number for each file type my Logic Apps is generating. My Logic App is doing some translations between an WMS and a customer's ERP. The issue is the customer is ...
1
vote
3answers
230 views

Are there any programs or IDEs that calculate Big-O notation on functions? Is this something that is possible to program into an IDE?

Is there anything that can calculate the Big-O notation of a function? While learning about Big-O, it is implied that it is pretty trivial to do in your head once you know the basic rules, however if ...
-2
votes
2answers
116 views

Python function name convention for “convert foo to bar”, e.g., foo_to_bar, foo2bar

I have a function that converts args given by argparse to a filename: def args2filename(args, prefix): filename = prefix kwargs = vars(args) for key, value in sorted(kwargs.items()): ...
5
votes
3answers
1k views

What does it mean for a method or a function to do one thing? [duplicate]

What does it mean when you say that a method or a function should do only one thing? Can it do a few things as long as it has a cohesive name? Should we avoid routines with the word "and" in them? ...
7
votes
4answers
700 views

Are “in” parameter modifiers considered a code smell?

As of C# 7.2+, you can add a parameter modifier in before the parameter type to define a parameter as const, essentially. It is like the ref or out keywords, except that in arguments cannot be ...
4
votes
4answers
1k views

Is a call stack required for robust computer architecture?

I am not too familiar with the computer architecture terminology yet so please bear with me. I seem to understand that von Neumann architectures are more robust ("universal Turing machines") as ...
2
votes
3answers
146 views

Declaring Multiple Variables of the Same Type within a Function's Parameters

We've started looking at functions, and our homework requires us to include some simple ones in our programming. As we've left it a little late in the semester to explore functions, we're well-used to ...
-1
votes
2answers
104 views

How to decide if a global variable is used inside or outside a function in Python?

In Python variables that are created outside of a function are known as global variables. However to create a global variable inside a function (a local variable), you can use the global keyword. My ...
0
votes
8answers
2k views

Is there a use case for boolean parameters? [closed]

Most programmers (including me) believe that methods with a boolean flag parameter should be refactored into two methods without the flag parameter. Are there any use cases for a boolean parameter ...
4
votes
6answers
2k views

Making recursion optional: Bad practice?

Let's say I have a function like this: /* Mode is a bool that determines whether the function uses it's recursive mode or not */ public static int func(int n, boolean mode) { if (!mode) ...
-1
votes
1answer
119 views

Why is it called a functional component?

A quick google search shows that the question I have has not been asked directly, and so I ask it here. For what its worth, I am learning react, and I come from a C++ background, where as far as I ...
4
votes
1answer
208 views

What's the benefit of avoiding partial functions in Haskell?

AFAIK in Haskell it is heavily recommended to avoid partial functions; and if these seem unavoidable (eg head) then return a Maybe. At least, so the Haskell wiki says 1 2 What's the use of the ...
0
votes
3answers
151 views

Idempotent functions as a feature in general

If I want a function to be idempotent, is there any framework, language or extension which can allow me to just mark the function as idempotent and the rest will be taken care of behind the scenes ...
7
votes
2answers
325 views

Why is it necessary to “import” a library first before usage?

My question regards the use of import statements. In most programming languages I've come across (e.g. Python, Scala, Go), it is necessary to first import a library before you can use its functions. ...
2
votes
4answers
275 views

Should one-line functions be avoided in jQuery/JavaScript?

So, I had a coworker complain about how I made the following code: ... var foo = getKendoDropdown(window.foo); var bar = getKendoDropdown(window.foo); var sna = getKendoDropdown(window.sna); var fu = ...
1
vote
3answers
129 views

What considerations should I mind when designing methods or functions that take in a lot of parameters?

What considerations should I mind when designing methods or functions that take in a lot of parameters? A lot meaning over 4 but less than 10. Example, I am debating whether to pass in an array like ...
1
vote
1answer
101 views

Python - Paradigm to compute different formulas with the same function

I have different equations to compute the same quantity. For example, val = my_func(a, b, c) but also val = my_func(x, y), where my_func represents the quantity I would like to compute: it could be ...
1
vote
4answers
503 views

Should validation be inside or outside function?

Which one is better? A if ( complexDataCheck(...) ) { doJob(...) } vs B doJob() func doJob(...) { if not(complextDataCheck(...)) return error; ... } In A, the function does one and only ...
2
votes
3answers
269 views

Practical way to indicate an error from a function that returns a stuct instance?

Suppose I have a function that returns an instance of a struct with the following declaration: struct data API(void); Suppose also that there is a chance for the function to fail with an error ...
-4
votes
4answers
624 views

Creating one function for multiple purposes vs multiple functions for one purpose each [closed]

I have one function that is used to compute distances of an object in 3 different ways. Is one of the following two methods considered better practice: Creating 3 different functions, one each for ...
0
votes
1answer
131 views

What are the pro or cons between a if statement and two different functions [duplicate]

I was wondering what are the pro and cons between having one function with a if statement dictating 2 sections of code or two discreet functions that are called on separately. In terms of pro and ...
2
votes
5answers
288 views

Is it a good practice to use method names that flow properly when spoken? [closed]

For example, suppose if I had a Character class and an inventory, before taking out a weapon to use it, it's probably a good idea to check if that weapon exists first, so I might have a method called ...
1
vote
1answer
190 views

How function parameters are compiled in JavaScript

I am trying to understand how functions -- which could have dozens of parameters that could themselves be functions or complex objects (I'm thinking JavaScript) -- get passed the arguments when ...
100
votes
6answers
20k views

What is a term for a function that when called repeatedly, has the same effect as calling once?

(Assuming a single-threaded environment) A function that fulfills this criterion is: bool MyClass::is_initialized = false; void MyClass::lazy_initialize() { if (!is_initialized) { ...
1
vote
1answer
137 views

Signature for a Rust method that modifies object but might also drop it?

I'w writing a program in Rust that basically pushes data through a pipeline of steps that transforms it in different ways. The data is represented by an Entry, and I am designing a Step trait for the ...
0
votes
2answers
2k views

Creating a list of functions in java [closed]

I have a list of functions which need to be tested against a list of inputs to measure their relative performance. I have already create a test function like below: public static String testFunction(...
51
votes
9answers
11k views

Should you guard against unexpected values from external APIs?

Lets say you are coding a function that takes input from an external API MyAPI. That external API MyAPI has a contract that states it will return a string or a number. Is it recommended to guard ...
1
vote
1answer
115 views

Difference between failed and canceled task

In case we hav functions (f_1,....,f_n) which runs in this order somewhere in the future. I would like to define a relationship between those functions such that if f_i failed due to expection then ...
-1
votes
1answer
77 views

Interest of having two functions doing the exact same thing [closed]

ls and objects functions are both from the base package and are exactly the same as discussed here. I've seen other examples of duplicated functions (in the tidyverse and base if I remember correctly)....
-1
votes
1answer
124 views

Best practices on unit tests for consecutive functions

Let's say we have function A and function B which perform consecutive operations on some data, with B never receiving the data before A processes it. Function A makes exhaustive checks on potential ...
1
vote
1answer
107 views

Pure functions with a connection to a system

What's a good place to put pure functions that have connections to a system? public class Core { System system; } public class System { SubSystem subSystem; // subSystem.Multiply(a, b); }...
4
votes
2answers
283 views

Function that returns non parameter functions

I found a function in c# like this: private Dictionary<string, Func<string>> ObtenerExtraCfgCampo(MsgDefCamp camp) { var extra = new Dictionary<string, Func<string>&...
7
votes
4answers
1k views

Is it a good practice to end a function's name with a preposition?

I have seen examples of methods whose name ended with a preposition applying to the first (and most often only) parameters. But other examples don't use this kind of naming convention. Which do you ...
1
vote
0answers
192 views

What is a Function that Creates Functions Called?

Let's say I have the following function (in JavaScript, for sake of example): function foo() { var bar = function () {return 'lol';} return bar; } This function returns other functions. I had ...
2
votes
3answers
2k views

Is inlining almost all of my C++ application's methods a good or bad idea?

I am used to D coding and Python where the class definition also contains each method definiton. So it is my preferred style of coding. I abhor having to switch to the .cpp file and use that long ...
109
votes
15answers
16k views

When to optimize for memory vs performance speed for a method?

I recently interviewed at Amazon. During a coding session, the interviewer asked why I declared a variable in a method. I explained my process and he challenged me to solve the same problem with fewer ...
2
votes
3answers
277 views

Does SRP feed tight coupling?

I designed a function which, afterwards, I noticed it is clearly doing 2 very different tasks in the same time. So now I am thinking to split it into 2 different functions, each one of them doing ...
2
votes
1answer
818 views

Naming conventions for pure functions

Are there 'conventions' / best practices on naming pure functions? For example: adding numbers: add or sum? calculating the square root: calcSqrt or sqrt? reversing a list: reverse or reversed? ...
1
vote
1answer
65 views

Using framework (WordPress) functions in layered architecture

I'm building an app with the usual layers like presentation, application, infrastructure. Right now, I have been using WordPress functions in both my controllers and repositories. I figured that ...

1
2 3 4 5 6