ethereum / solidity

Solidity, the Smart Contract Programming Language
https://soliditylang.org
GNU General Public License v3.0
22.67k stars 5.62k forks source link

Saner treatment of auto in coding style. #15077

Open ekpyron opened 1 month ago

ekpyron commented 1 month ago

Just that we have something to argue about :-).

clonker commented 1 month ago

I am all for the use of auto, as it removes unwanted implicit conversions and (IMO) helps readability if the code is well-written in the first place. :-D

Bad:

auto result = computeSomeStuff(x, y, z);
auto result2 = computeSomeStuff(x, z, y);

here it's not clear what types result and result2 are; also they can be drastically different types to begin with.

Potentially bad:

auto result = Eigen::MatrixXd::Random(3, 3).inverse();

this would return some weird intermediate proxy thing but not actually (yet) compute the inverse. This can be desired but may also lead to unexpected behavior. The actual result is obtained by writing MatrixXd result = .... Although I've never encountered such behavior outside of Eigen (or other linalg libraries) where you'd have lazy computation of individual elements of a tensor.

Bad, but vice versa:

std::function<void(int)> f = []() {return 6;};

implicit conversion with performance implications if f is evaluated frequently.

Of course there are classical examples like iterator types derived from some map or so, which are super unwieldy. I guess there is no discussion that here auto is always preferable.

In the end I feel like it's hard to find a general rule like "always use auto" or "never use auto" as it's depending on context and generally depending on what you're working with. But personally, I would turn the statement around and say something to the effect of: use explicit types sparingly and deliberately if they can be replaced by auto.

github-actions[bot] commented 1 month ago

This pull request is stale because it has been open for 14 days with no activity. It will be closed in 7 days unless the stale label is removed.

github-actions[bot] commented 3 weeks ago

This pull request was closed due to a lack of activity for 7 days after it was stale.

github-actions[bot] commented 1 week ago

This pull request is stale because it has been open for 14 days with no activity. It will be closed in 7 days unless the stale label is removed.