Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One line functions in C?

What do you think about one line functions? Is it bad? One advantage I can think of is that it makes the code more comprehensive (if you choose a good name for it). For example:

void addint(Set *S, int n)
{
     (*S)[n/CHAR_SIZE] |= (unsigned char) pow(2, (CHAR_SIZE - 1) - (n % CHAR_SIZE));
}

One disadvantage I can think of is that it slows the code (pushing parameters to stack, jumping to a function, popping the parameters, doing the operation, jumping back to the code - and only for one line?)

is it better to put such lines in functions or to just put them in the code? Even if we use them only once?

BTW, I haven't found any question about that, so forgive me if such question had been asked before.

like image 620
Ori Popowski Avatar asked Apr 24 '09 09:04

Ori Popowski


People also ask

What is a one line function?

by Chris. 5/5 - (1 vote) A lambda function allows you to define a function in a single line. It starts with the keyword lambda , followed by a comma-separated list of zero or more arguments, followed by the colon and the return expression.

What's an inline function in C?

An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory. This eliminates call-linkage overhead and can expose significant optimization opportunities.

How do you write a one line function?

Here's the most Pythonic way to write a function in a single line of code: f2 = lambda x: str(x * 3) + '! ' You create a lambda function and assign the new function object to the variable f2 .

What is inline function example?

Example. In the following class declaration, the Account constructor is an inline function. The member functions GetBalance , Deposit , and Withdraw aren't specified as inline but can be implemented as inline functions. In the class declaration, the functions were declared without the inline keyword.


1 Answers

Don't be scared of 1-line functions!

A lot of programmers seem to have a mental block about 1-line functions, you shouldn't.

If it makes the code clearer and cleaner, extract the line into a function.

Performance probably won't be affected.

Any decent compiler made in the last decade (and perhaps further) will automatically inline a simple 1-line function. Also, 1-line of C can easily correspond to many lines of machine code. You shouldn't assume that even in the theoretical case where you incur the full overhead of a function call that this overhead is significant compared to your "one little line". Let alone significant to the overall performance of your application.

Abstraction Leads to Better Design. (Even for single lines of code)

Functions are the primary building blocks of abstract, componentized code, they should not be neglected. If encapsulating a single line of code behind a function call makes the code more readable, do it. Even in the case where the function is called once. If you find it important to comment one particular line of code, that's a good code smell that it might be helpful to move the code into a well-named function.

Sure, that code may be 1-line today, but how many different ways of performing the same function are there? Encapsulating code inside a function can make it easier to see all the design options available to you. Maybe your 1-line of code expands into a call to a webservice, maybe it becomes a database query, maybe it becomes configurable (using the strategy pattern, for example), maybe you want to switch to caching the value computed by your 1-line. All of these options are easier to implement and more readily thought of when you've extracted your 1-line of code into its own function.

Maybe Your 1-Line Should Be More Lines.

If you have a big block of code it can be tempting to cram a lot of functionality onto a single line, just to save on screen real estate. When you migrate this code to a function, you reduce these pressures, which might make you more inclined to expand your complex 1-liner into more straightforward code taking up several lines (which would likely improve its readability and maintainability).

like image 164
Wedge Avatar answered Sep 27 '22 20:09

Wedge