Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [functions]

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

1
vote
1answer
89 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
266 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>&...
8
votes
4answers
602 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
143 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
386 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 ...
106
votes
14answers
14k 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
155 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
184 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
48 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 ...
15
votes
2answers
3k views

Is converting a C++ method to a C function with a pointer argument an acceptable pattern?

I use C++ on ESP-32. When registering a timer I have to do this: timer_args.callback = reinterpret_cast<esp_timer_cb_t>(&SoundMixer::soundCallback); timer_args.arg = this; Here the timer ...
0
votes
2answers
191 views

How to Implement a `function` with `return` Without Using the `function` keyword

Along the lines of How to Simulate Control-Flow without using Control-Flow Primitives, I am wondering how to simulate return from a function. Given an example setup like this: console.log(a(10)) ...
2
votes
6answers
158 views

Main function's subfunctions should terminate main function

So if I have a big function I break it to smaller ones to increase readability. If I have parts in my big function where I want it to return. I want my subfunctions to have the ability to make the ...
3
votes
3answers
319 views

Changing large number of if-elif-else statements to use underlying structure

I have a function that looks something like this: function_name(step, ... , typ): if typ == 'some type of calc method': if step == 1: do_me_at_step_1(...) elif step ...
-1
votes
2answers
86 views

How to choose the most suitable solution for a problem given some choices?

To ilustrate my main concern let's start by considering a "trivial" typical problem, data filtering & parsing coming from a process and dumping the information onto something {gui console, file, ...
-1
votes
1answer
121 views

Combining the arguments of a function using a single structure

What are the consequences (pros and cons) of using a single structure as the argument, instead of a list of parameters. Some of the advantages I see are: create multiple default parameters. Need not ...
3
votes
3answers
596 views

What is an example for a one-way hash function?

I am doing a bit of research on hash functions. I understand the concept that it is an equation that is easy to do one way (you take the number 00011010 for example and do reasonably simple math with ...
0
votes
1answer
45 views

Implicit conversion for function subtyping

Languages with coercions (i.e. implicit type conversions) allows expressing things like that (irrespective of any syntax): f : int32 → int32 = λx.x+x a : int16 = 42 b : int64 = f(a) Where type ...
0
votes
1answer
83 views

Generating combinations without getting stuck in recursive calls

This question is about figuring the design of a recursive function that changes the state of a group of elements by processing one of them at a time, with the goal of reaching a desired state. The ...
3
votes
2answers
139 views

Avoiding variables/functions only referenced once

I've heard some developers say that if a variable is only referenced one place, to just replace the reference to it with the value assigned to it. I've also heard developers say if a function is only ...
-2
votes
1answer
88 views

For use once only functions extracted from a longer function, should the name be xxx1(),xxx2,… or relate to there task?

According to Is it OK to split long functions and methods into smaller ones even though they won't be called by anything else?, I can split long functions into smaller functions even if they are ...
16
votes
4answers
1k views

Unable to solve mystery of functions in Javascript

I am trying to understand behind the curtain scenes of Javascript and kind of stuck in understanding the creation of built in objects, specially Object and Function and the relation between them. ...
9
votes
3answers
426 views

Is there a reason to not modify values of parameters passed by value?

Are there objective, supportable software-engineering arguments for or against modifying the values of by-value parameters in the body of a function? A recurring spat (mostly in good fun) on my team ...
8
votes
5answers
639 views

Long method refactoring: leaving as is vs separating into methods vs using local functions

Suppose I have long method like this: public void SomeLongMethod() { // Some task #1 ... // Some task #2 ... } This method doesn't have any repetitive parts that should be moved to ...
2
votes
1answer
40 views

Aliasing repeated function calls

I'm working on a JavaScript user script to assist my team in making assembling support tickets on a web interface more efficient. In doing so, I'm finding myself making a lot of calls to DOM API to ...
5
votes
4answers
265 views

Return an indicator if the process is succeed or not

I have member function that looks like: Point find_the_special_point(Image img); However, this function may fail to find the point (for example the img does not contain a special point at all). ...
0
votes
0answers
74 views

Why is overloading called overloading? [duplicate]

I have problems with the term "method overloading". I know that overloading is the ability to create multiple methods of the same name with different implementations. My problem is however, that the ...
6
votes
1answer
813 views

Why use `const foo = () => {}` instead of `function foo() {}`

For example, in this Redux video, the instructor always uses syntax like const counter = (state=0, action) => { ... function body here } where I would just use the "traditional" function ...
2
votes
2answers
374 views

Polymorphism based on number of arguments?

I've heard that changing a function's behaviour based on argument type is called ad hoc polymorphism: program Adhoc; function Add(x, y : Integer) : Integer; begin Add := x + y end; function Add(...
2
votes
0answers
117 views

Is there an accepted Coding Style for multiple ES6 Arrow Functions?

I'm starting to use ES6 arrow functions more, but haven't found a coding style that I like, especially when chaining them together. e.g., Eric Elliott gives this code: mix = (...fns) => x => ...
-5
votes
2answers
481 views

Looking For Information about coloring/highlighting output text in python

one of the first modules I’m working on in a python program I’m putting together is a journal. I’m getting comfortable using the print function, but for this project it would be great if i Could ...
0
votes
1answer
73 views

Is there a normal way to program a CLI script that overrides default parameters in python

If I've got a script that uses a config file (or defaults to a generic config file) that calls another module which needs the config file passed to it as a parameter should I always File 1 (the ...
4
votes
4answers
444 views

Is SRP an ambiguous principle? [closed]

I've been aware of SOLID for many years now and I've always though about "OLID" was a good set of design principles to follow... problem is I've always found difficulties with the "S", I've always ...
1
vote
2answers
335 views

Function that throws exceptions extending IllegalArgumentException

I have a try/catch block which looks like this : try { geoms.add(convertLineToGeom(ln)); } catch(IllegalArgumentException e) { System.out.println("ligne n°" + counter + " : " + e.getMessage())...
42
votes
6answers
13k views

Why do many functions that return structures in C, actually return pointers to structures?

What is the advantage of returning a pointer to a structure as opposed to returning the whole structure in the return statement of the function? I am talking about functions like fopen and other low ...
87
votes
12answers
15k views

When do function call costs still matter in modern compilers?

I am a religious person and make efforts not to commit sins. That is why I tend to write small (smaller than that, to reword Robert C. Martin) functions to comply with the several commandments ordered ...
0
votes
3answers
241 views

Should a function which takes dimensions/coordinates take them as multiple parameters, or one object? [duplicate]

I'm mainly thinking of x,y coordinates but I guess 3D coordinates and higher also apply. Let's say I'm writing a function to return the center of a rectangle given its width and height. Which is the ...
4
votes
2answers
236 views

Design of multi-test function to validate a string

Assume that I wish to perform an action on a string (e.g., print the string) if, and only if, the string first passes several tests. The tests are diverse (and may even be complex functions themselves)...
0
votes
1answer
51 views

If I'm explicitly using WCHAR strings in my Windows application, should I use the -W functions rather than the TCHAR #defines?

Basically, TCHAR and the associated macros (_T/TEXT) are only really necessary if you're targeting both ANSI and Unicode, and, like 99.9% of developers out there, the possibility that someone might be ...
1
vote
2answers
314 views

Assigning dictionary values to variables before using them as function arguments

I find myself often having a function, for example authenticate: authenticate(user, token): # do authentication and a dictionary created by reading a configuration file, like this: conf['...
4
votes
9answers
2k views

How small should functions be?

I'm new at writing professional code (the bulk of my experience is with personal projects) so excuse me if this is trivial. When I write code I find myself being a little inconsistent with how much ...
0
votes
3answers
87 views

easy way of preventing multiple users of working on the same task

Our client has a software, where tickets come in. Multiple employees are responsible for answering these tickets (via phone), in a short amount of time (call back within X minutes). What would be the ...
11
votes
2answers
169 views

Solving the problems that come with the dyadic function assertEquals(expected, actual)

After years of cowboy coding, I decided to pick up a book on how to write good quality code. I'm reading Clean Code by Robert Cecil Martin. In chapter 3 (functions) there is a section on dyadic ...
0
votes
4answers
230 views

Alternative to property on function in Python?

I have a collection of Python objects, lets call them fruits. Some of these fruits might be faulty. To check for these faults, I have a number of functions that performs different checks, and returns ...
1
vote
3answers
399 views

What should I return from a function?

I have a function in C language which takes 3 matrices as arguments; It performs matrix multiplication of first 2 matrices and stores the result in the 3rd one. My question is what should I return ...
-5
votes
3answers
161 views

Why solve a problem with a class and not a function?

I am a new to in programming, especially to C++. At first I learned how I make functions (it was amazing). But now that I've learned about classes (and objects) I am so confused... when I should use ...
60
votes
10answers
10k views

Is it bad practice to use a C++ compiler just for function overloading?

So I am working on a software design using C for a certain processor. The tool-kit includes the ability to compile C as well as C++. For what I am doing, there is no dynamic memory allocation ...
3
votes
6answers
2k views

Splitting up a very large function/program into smaller functions. Effective?

I know function names can be very expressive. And therefore it can be tempting split up a program into particular functions and call them from a large "oversee-able" functions file. However is this ...
3
votes
2answers
1k views

How to refactor functions?

Let's say I want to have a function to load a project's data from both a json and a file path, the first idea which comes to my mind would be having a couple of functions such as: def ...
12
votes
6answers
1k views

What is the term used to describe a function/method that modifies the object it's called on?

Sorry for the generic question. I have searched all over and found so many threads similar to this, however not one that answers my specific question - perhaps because the term I'm looking for doesn't ...
1
vote
1answer
171 views

Should callbacks be called with named or positional arguments?

The question is asked in the context of Python, but it is also relevant for any languages with named parameters support. If some entity in my code (e.g. a pubsub implementation) or even a simple ...