Hot answers tagged

212

Depends on that one line. If the line is readable and concise by itself, the function may not be needed. Simplistic example: void printNewLine() { System.out.println(); } OTOH, if the function gives a good name to a line of code containing e.g. a complex, hard to read expression, it is perfectly justified (to me). Contrived example (broken into multiple ...


118

As others have already mentioned: don't create a function with a name that is similar to that of a builtin, standard-library or generally widely used function but change its behavior. It is possible to get used to a naming convention even if it doesn't make much sense to you at first sight but it will be impossible to reason about the functioning of your ...


115

If you make a function like that where minimize(4, 10) returns 10, then I'd say that is inadvisable because your fellow programmers may strangle you. (Okay, maybe they will not literally strangle you to death, but seriously... Don't do that.)


99

Is it because there is no formal input argument? It is because the output depends on something that is not an input, namely the current time. Why is the actual time of day not treated as the "input to the function" Because you didn't pass it as a parameter. If you did pass it in as a parameter, the function would become an identity function on dates,...


86

In typography this is generally handled by using a different rendering, whether or not it's the start of a sentence, to indicate that what's hitting the eye is not just a word in the sentence but a special entity. Your fread implementation is broken. fread needs to return how many bytes were read. Depending on how formal a document is, it can adopt the ...


82

I could overload the constructor so that order [of the parameters] doesn't matter... But is that a good idea? No. Having different constructor overloads will have the opposite effect of what you are intending. The programmer coming after you expects different overloads to have different behavior, and will ask: "What sort of different behavior is being ...


71

You can't create a pure function called random that will give a different result every time it is called. In fact, you can't even "call" pure functions. You apply them. So you aren't missing anything, but this doesn't mean that random numbers are off-limits in functional programming. Allow me to demonstrate, I'll use Haskell syntax throughout. Coming from ...


68

Yes, splitting long functions is normal. This is a way of doing things that's encouraged by Robert C. Martin in his book Clean Code. Particularly, you should be choosing very descriptive names for your functions, as a form of self-documenting code.


61

It's called the identity function and is sometimes abbreviated as id in category theory and functional programming languages.


57

Yes, this can be used to satisfy best practices. For instance, it is better to have a clearly-named function do some work, even if it is only one line long, than to have that line of code within a larger function and need a one-line comment explaining what it does. Also, neighbouring lines of code should perform tasks at the same abstraction level. A ...


57

It's because it's important for humans to recognize that functions are not just "another named entity". Sometimes it makes sense to manipulate them as such, but they are still able to be recognized at a glance. It doesn't really matter what the computer thinks about the syntax, as an incomprehensible blob of characters is fine for a machine to interpret, ...


56

Your question is sort of like asking what's the good of the number zero if whenever you add it to something you get the same value back. An identity function is like the zero for functions. Kind of useless by itself, but occasionally useful as part of an expression using higher-order functions, where you can take a function as a parameter or return it as a ...


51

If there is an absolute requirement to start each sentence with a cap, then simply replace fread with "The fread function" wherever it starts a sentence.


50

Inner functions are not an anti-pattern, they are a feature. If it doesn't make sense to move the inner functions outside, then by all means, don't. On the other hand, it would be a good idea to move them outside so you can unit test them easier. (I don't know if any framework lets you test inner functions.) When you have a function with 250+ lines, and ...


46

Using a dict let's you translate the key into a callable. The key doesn't need to be hardcoded though, as in your example. Usually, this is a form of caller dispatch, where you use the value of a variable to connect to a function. Say a network process sends you command codes, a dispatch mapping lets you translate the command codes easily into executable ...


45

I've never seen a guideline, but in my experience a function that takes more than three or four parameters smells like one of two problems: The function is doing too much. It should be split into several smaller functions, each which have a smaller parameter set. There is another object hiding in there. You may need to create another object or data ...


44

First, the "self-calling functions" aren't actually self-calling. I know that's what people in the Javascript community call them, but it's really misleading; the functions never reference themselves, and in fact a lot of the time there's no way to call that particular function more than once. If they were actually self-calling functions, then recursive ...


44

I think the reason is that most popular languages either come from or were influenced by the C family of languages as opposed to functional languages and their root, the lambda calculus. And in these languages, functions are not just another value: In C++, C# and Java, you can overload functions: you can have two functions with the same name, but ...


40

C++ can have non-method functions just fine, if they do not belong to a class don't put them in a class, just put them at global or other namespace scope namespace special_math_functions //optional { int math_function1(int arg) { //definition } }


39

Should boolean methods always take the affirmative form, even when they will only ever be used in the negative form? Making rules about such things seems a little much -- I wouldn't want to see a guideline in a coding standards document that says thou shalt not use negative names for boolean properties. But as a matter of personal style, I think trying ...


38

There are lots of languages which already work this way, e.g. Haskell. In Haskell, every function takes exactly one argument and returns exactly one value. It is always possible to replace a function that takes n arguments with a function that takes n-1 arguments and returns a function that takes the ultimate argument. Applying this recursively, it is ...


37

As people pointed, this improves readability. A person reading process_url() may see more clearly what is the general process to deal with URLs just by reading a few method names. The problem is that other people may think those functions are used by some other piece of the code, and if some of them need to be changed they may choose to preserve those ...


37

A design constraint of the C language was that it was supposed to be compiled by a single-pass compiler, which makes it suitable for very memory-constrained systems. Therefore, the compiler knows at any point only about stuff that was mentioned before. The compiler can't skip forward in the source to find a function declaration and then go back to compile a ...


37

Robert C. Martin in his book "Clean Code" recommends heavily the use of functions with 0, 1 or 2 parameters at maximum, so at least there is one experienced book author who thinks code becomes cleaner by using this style (however, he is surely not the ultimative authority here, and his opinions are debatable). Where Bob Martin is IMHO correct is that ...


34

I think its useful sometimes for hiding implementation. function sayHello() { window.alert("Hello"); } And this gives you the flexibility to change it later function sayHello() { console.log("Hello"); }


32

I think that in many cases such function is good style, but you may consider local boolean variable as alternative in cases when you don't need use this condition somewhere in other places e.g.: bool someConditionSatisfied = [complex expression]; This will give hint to code reader and save from introducing new function.


31

Instead of number of lines, the criteria I would use is that each function should do only one thing and does it well.


31

I always try to give my variables and functions great names. If I can't think of a great name, I'll settle for a good name. If I can't come up with a good name, I'll use an okay name. I have never, in 15 years of professional programming, been unable to come up with a decent name.



Only top voted, non community-wiki answers of a minimum length are eligible