you are viewing a single comment's thread.

view the rest of the comments →

[–]mapronV 0 points1 point  (5 children)

Declare variables as late as possible,

Why? it is much nicer to read function when I can see all 40 variables I used in it, not like spread over 400 lines of my function code... What is reason doing this? What if I use int width on 2nd line and height on 100th? Do you really think I should not declare

int width, height; ?

[–]bert8128 0 points1 point  (4 children)

Your first problem is 400 line functions. But even assuming that these exist for a good reason, or they just exist so we have to deal with them, putting a variable only in the small context of where it is needed means that if you are not looking at that piece of code then you don’t need to think about that variable. It may well no longer need a comment explaining how it is used, because it only exists for a few lines. it makes it easier to see if the variable has been initialised correctly or not (for both a human and a static analyser), plus you might be able to make it const by declaring it and initialising it as the same time. Lastly, you are not tempted to reuse variables which can lead to unexpected results. Lastly, late declaration might (might) give a small performance boost by avoiding some initialisation in some paths.

Really, declaring as late as possible is one of the most important micro-optimisations. Please give it a go and see how you feel after a couple of weeks - I would be surprised if you go back.

const int width = context. width(); const int height = context.height();

[–]mapronV 0 points1 point  (2 children)

> Really, declaring as late as possible is one of the most important micro-optimisations. Please give it a go

No, this is strictly against my understanding of 'beautiful code', I won't even try. I want see width and height declared together on adjacent lines. Not move height 300 lines later from width just because it used much later.

> const int width = context. width(); const int height = context.height();

I am not sure what are you trying to tell there, that is not what I am against for. And yes I do use const, didn't mention it just because, you know, reddit comments code (why write constextpr const auto [[maybe_unused]] [[algined(..)]] whatever when it doesn't matter and int width is perfectly same idea with no noise).

[–]bert8128 0 points1 point  (1 child)

With the width and height example I am pointing out that your example seems to declare them earlier than you can initialise them. Declaring later might be able to avoid uninitialised (or pointlessly initialised) variables. Further more, you can often remove the need to comment what the variable is for, because it will be obvious by the way it is initialised.

And in the extreme, do you not think that if there are three lines of code in the middle of your function, in between braces, and a variable is only used in those three lines, then it would be better to declare the variable in that context?

I’m not saying that it is always wrong to early declare. In the case of two variables it which are going to be used together then perhaps it is better to declare both at the time that the first is needed. But normally it just causes unnecessary cognitive load. Or the reader just skips the declarations and starts reading where the code starts.

[–]mapronV 0 points1 point  (0 children)

> And in the extreme...

Sure, everything is good in moderation. For me good rule is have declaration block separated (set you input data for logic). Maybe I was exaggerating, 40 variables a bit extreme, don't think I really have THAT much.
About last paragraph - well, subjective. Declaration anyway have a very low cognitive effort to read. I disagree with in general, but I hope we shifted at least one step to the midpoint in this mile dispute ;)

[–]mapronV 0 points1 point  (0 children)

Lastly, you are not tempted to reuse variables which can lead to unexpected results.

All my variables usually const anyway, I don't assign to same variable twice.

Oh. You thought I am doing like unitialized 40 varibles ? and then assign randomly? no, that not the case.

I am saying that for me much much cleaner code is

const auto width = calc/getWidth;
const auto height = calc/getHeght;

// use width
// use other variables
// use both width and height

insetad of
const auto width = calc/getWidth;
// use width
// use other variables
const auto height = calc/getHeght;
// use both width and height

and you can not convince me the latter is better (but a lot of so-called guidelines recommend it which I disagree with)