Limit variable scope as much as possible in order to:
- improve code readability,
- reduce complexity, and/or
- avoid unnecessary initialisations.
Limit variable scope as much as possible in order to:
utACK 5e6c94531046135d406ea755851fd614a76fb543
854 | @@ -855,7 +855,7 @@ bool CBlockPolicyEstimator::Read(CAutoFile& filein) 855 | try { 856 | LOCK(cs_feeEstimator); 857 | int nVersionRequired, nVersionThatWrote; 858 | - unsigned int nFileBestSeenHeight, nFileHistoricalFirst, nFileHistoricalBest; 859 | + unsigned int nFileBestSeenHeight;
move down to the before L865, to move it closer to its first use.
47 | @@ -48,12 +48,12 @@ int TrafficGraphWidget::getGraphRangeMins() const
48 | void TrafficGraphWidget::paintPath(QPainterPath &path, QQueue<float> &samples)
49 | {
50 | int h = height() - YMARGIN * 2, w = width() - XMARGIN * 2;
h, w, x could also be moved into the if clause
103 | int nHeight = 0;
104 |
105 | { // Don't keep cs_main locked
106 | LOCK(cs_main);
107 | - nHeightStart = chainActive.Height();
108 | + int nHeightStart = chainActive.Height();
is this var even needed? could do nHeight = chainActive.Height(); and use nHeight instead of nHeightStart on the line after.
262 | @@ -263,11 +263,11 @@ bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& 263 | set_error(serror, SCRIPT_ERR_UNKNOWN_ERROR); 264 | if (script.size() > MAX_SCRIPT_SIZE) 265 | return set_error(serror, SCRIPT_ERR_SCRIPT_SIZE); 266 | - int nOpCount = 0; 267 | bool fRequireMinimal = (flags & SCRIPT_VERIFY_MINIMALDATA) != 0;
why did you move only nOpCount but not this or some of the other vars above?
Yes, there are more vars that could be moved into the tighter try scope. I'll take all interpreter.cpp changes in a separate PR since they'll need a more thorough review. I've now removed the nOpCount move from this PR.
@benma Feedback items addressed! Thanks for the review.
46 | @@ -47,13 +47,14 @@ int TrafficGraphWidget::getGraphRangeMins() const 47 | 48 | void TrafficGraphWidget::paintPath(QPainterPath &path, QQueue<float> &samples) 49 | { 50 | - int h = height() - YMARGIN * 2, w = width() - XMARGIN * 2; 51 | - int sampleCount = samples.size(), x = XMARGIN + w, y; 52 | + int sampleCount = samples.size(); 53 | if(sampleCount > 0) { 54 | + int w = width() - XMARGIN * 2; 55 | + int x = XMARGIN + w, h = height() - YMARGIN * 2;
a super-nit, but I preferred w and h on the same line as before, because of the symmetry.
utACK 4adc8389af964e84c1f97bc28707904341bbbc74
@benma Good point. w and h now on the same line again :-)
46 | @@ -47,13 +47,14 @@ int TrafficGraphWidget::getGraphRangeMins() const 47 | 48 | void TrafficGraphWidget::paintPath(QPainterPath &path, QQueue<float> &samples) 49 | { 50 | - int h = height() - YMARGIN * 2, w = width() - XMARGIN * 2; 51 | - int sampleCount = samples.size(), x = XMARGIN + w, y; 52 | + int sampleCount = samples.size(); 53 | if(sampleCount > 0) { 54 | + int h = height() - YMARGIN * 2, w = width() - XMARGIN * 2; 55 | + int x = XMARGIN + w;
Sorry for another nit, but why not inline this x (YMARGIN + h is also inlined)? Then you can declare int x on line 56 (or inline those two as well).
Currently, there something like shadowing going on, where x is not modified inside the loop, as they are independent.
@benma I'm not sure I follow. The value of x assigned in the loop is used outside of the loop, so declaring it on line 56 wouldn't maintain the same logic as before, would it? Please post a gist with your suggested formulation :-)
You are correct, sir! Apologies :)
@benma Apology accepted! 😄
utACK 90593ed92cfd5d49900a6fb6c2c10a482ab9fdbb
utACK 90593ed