Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function return type style

I'm learning c++0x, at least the parts supported by the Visual C++ Express 2010 Beta. This is a question about style rather than how it works. Perhaps it's too early for style and good practice to have evolved yet for a standard that isn't even released yet...

In c++0x you can define the return type of a method using -> type at the end of the function instead of putting the type at the start. I believe this change in syntax is required due to lambdas and some use cases of the new decltype keyword, but you can use it anywhere as far as I know.

// Old style 
int add1(int a, int b)
{
 return a + b;
}

// New style return type
auto add2(int a, int b) -> int
{
 return a + b;
}

My question really then, is given that some functions will need to be defined in the new way is it considered good style to define all functions in this way for consistency? Or should I stick to only using it when necessary?

like image 816
jcoder Avatar asked Mar 15 '10 08:03

jcoder


People also ask

What is the return type of a function?

The result of a function is called its return value and the data type of the return value is called the return type. Every function declaration and definition must specify a return type, whether or not it actually returns a value.

Can a function return different types?

Actually, it's not very uncommon at all to return different types even in a statically typed language. That's why we have union types, for example. In fact, methods in Java almost always return one of four types: some kind of object or null or an exception or they never return at all.

What does () => void mean TypeScript?

The syntax (a: string) => void means “a function with one parameter, named a , of type string, that doesn't have a return value”. Just like with function declarations, if a parameter type isn't specified, it's implicitly any . Note that the parameter name is required.

What is function return type in JavaScript?

The return statement is used to return a particular value from the function to the function caller. The function will stop executing when the return statement is called. The return statement should be the last statement in a function because the code after the return statement will be unreachable.


2 Answers

Do not be style-consistent just for being consistent. Code should be readable, i.e. understandable, that's the only real measure. Adding clutter to 95% of the methods to be consistent with the other 5%, well, that just does not sound right to me.

like image 103
gimpf Avatar answered Sep 21 '22 18:09

gimpf


There is a huge codebase that uses the 'old'/current rules. I would bet that is going to be so for a long time. The problem of consistency is two-fold: who are you going to be consistent with, the few code that will require the new syntax or all existing code?

I will keep with the old syntax when the new one is not required for a bit, but then again, only time will tell what becomes the common usage.

Also note that the new syntax is still a little weird: you declare the return type as auto and then define what auto means at the end of the signature declaration... It does not feel natural (even if you do not compare it with your own experience)

like image 41
David Rodríguez - dribeas Avatar answered Sep 18 '22 18:09

David Rodríguez - dribeas