Tag Info

Hot answers tagged

192

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 ...


172

Testing code that does lots of things is difficult. Debugging code that does lots of things is difficult. The solution to both of these problems is to write code that doesn't do lots of things. Write each function so that it does one thing and only one thing. This makes them easy to test with a unit test (one doesn't need umpteen dozen unit tests). A ...


94

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 ...


85

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 ...


80

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 ...


60

Please pardon my memory if I have this incorrect... Javascript isn't my preferred implementation language. There are several reasons why one would want to have a no arg function wrap another function call. While the simple call to window.alert("Hello"); is something that you could imagine just instead calling directly instead of sayHello(). But what if ...


59

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 ...


58

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.


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 ...


56

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, ...


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 ...


50

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.


47

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 ...


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 ...


35

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 ...


34

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 ...


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.


32

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 } }


32

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 ...


31

Would I be correct in assuming that your "parse" function not only parses the code but also executes it at the same time? If you wanted to do it that way, instead of storing the contents of a function in your map, store the location of the function. But there's a better way. It takes a bit more effort up-front, but it yields much better results as ...


31

In theory, loose function-data coupling makes it easier to add more functions to work on the same data. The down side is it makes it more difficult to change the data structure itself, which is why in practice, well-designed functional code and well-designed OOP code have very similar levels of coupling. Take a directed acyclic graph (DAG) as an example ...


29

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 ...


28

Code organization is all about displaying enough information to convey a single idea. The sweet spot is getting your code pared down enough that a single idea can fit in a single unit of code. Your unit of code can be a function, a class, etc. These are merely tools of organization. As with any tool, it can be over used or used incorrectly. Having a one ...


27

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.


27

Sure. Imagine an external API that allows you to retrieve things from somewhere that you absolutely need, but you have to specify filter criteria and output formatters for every query, even if you want all the results and want to get them exactly as stored. Then you'd have to invent a trivial "accept everything" and a trivial "return unchanged" function to ...


26

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


21

Revised Answer OK, I see what you mean. I wouldn't call those "self-calling functions" -- the first thing that comes to mind is "immediately invoked functions". "Module" would also work, but I'm not sure how readily it would be understood. Rather than talking about "self-calling" and "non-self-calling" functions, I'd document the script by explaining ...


21

Virtual methods are commonly implemented via so-called virtual method tables (vtable for short), in which function pointers are stored. This adds indirection to the actual call (gotta fetch the address of the function to call from the vtable, then call it -- as opposed to just calling it right ahead). Of course, this takes some time and some more code. ...



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