Skip to content

STL: Avoid declaring multiple variables on a single line #522

Description

In the STL, we conventionally avoid declaring multiple variables on a single line. That is:

int cats, dogs; // we avoid this style

int meow; // we prefer this style
int woof;

Our reasons for this convention are:

  1. Being aware of the variables in a class or function is important, because humans have difficulty reasoning about program state (especially when it's modifiable). Consistently declaring one variable per line makes it clear when variables are being introduced, and whether they're initialized. (It also makes it "feel costly" to declare many variables, which is subtle encouragement to avoid introducing unnecessary state.)
  2. C++'s rules for multi-variable declarations are surprising to newer programmers. (In particular, the rules when pointers are involved.) Avoiding such declarations avoids the need to learn/teach/read that bit of complexity.

We're pretty consistent about following this convention, but there are a few files that should be changed. I observe the following occurrences (there may be more):

const _Ty *_First, *_Last, *_Other;

_Iosarray *_Ptr1, *_Ptr2;

_Iosarray *_Ptr1, *_Ptr2;

_Fnarray *_Pfa1, *_Pfa2;

_Elem _Ch, *_Dest;

_ULARGE_INTEGER _Available, _Capacity, _Free;

char *se, sign;

char *se, sign;

STL/stl/src/xstoul.cpp

Lines 35 to 36 in fffbd8f

const char *sc, *sd;
const char *s1, *s2;

unsigned long x, y;

STL/stl/src/xstoull.cpp

Lines 27 to 29 in fffbd8f

const char *sc, *sd;
const char *s1, *s2;
char dig, sign;

unsigned long long x, y;

short errx, xexp;

short errx, xexp;

int j, k;

  • These occurrences are weird-looking, but should be treated similarly:

STL/stl/inc/iostream

Lines 21 to 29 in fffbd8f

__PURE_APPDOMAIN_GLOBAL extern istream cin, *_Ptr_cin;
__PURE_APPDOMAIN_GLOBAL extern ostream cout, *_Ptr_cout;
__PURE_APPDOMAIN_GLOBAL extern ostream cerr, *_Ptr_cerr;
__PURE_APPDOMAIN_GLOBAL extern ostream clog, *_Ptr_clog;
__PURE_APPDOMAIN_GLOBAL extern wistream wcin, *_Ptr_wcin;
__PURE_APPDOMAIN_GLOBAL extern wostream wcout, *_Ptr_wcout;
__PURE_APPDOMAIN_GLOBAL extern wostream wcerr, *_Ptr_wcerr;
__PURE_APPDOMAIN_GLOBAL extern wostream wclog, *_Ptr_wclog;

STL/stl/inc/iostream

Lines 33 to 41 in fffbd8f

__PURE_APPDOMAIN_GLOBAL extern _CRTDATA2_IMPORT istream cin, *_Ptr_cin;
__PURE_APPDOMAIN_GLOBAL extern _CRTDATA2_IMPORT ostream cout, *_Ptr_cout;
__PURE_APPDOMAIN_GLOBAL extern _CRTDATA2_IMPORT ostream cerr, *_Ptr_cerr;
__PURE_APPDOMAIN_GLOBAL extern _CRTDATA2_IMPORT ostream clog, *_Ptr_clog;
__PURE_APPDOMAIN_GLOBAL extern _CRTDATA2_IMPORT wistream wcin, *_Ptr_wcin;
__PURE_APPDOMAIN_GLOBAL extern _CRTDATA2_IMPORT wostream wcout, *_Ptr_wcout;
__PURE_APPDOMAIN_GLOBAL extern _CRTDATA2_IMPORT wostream wcerr, *_Ptr_wcerr;
__PURE_APPDOMAIN_GLOBAL extern _CRTDATA2_IMPORT wostream wclog, *_Ptr_wclog;

extern _CRTIMP2_PURE_IMPORT /* const */ _Dconst _Denorm, _Hugeval, _Inf, _Nan, _Snan;

extern _CRTIMP2_PURE_IMPORT /* const */ _Dconst _FDenorm, _FInf, _FNan, _FSnan;

extern _CRTIMP2_PURE_IMPORT /* const */ _Dconst _LDenorm, _LInf, _LNan, _LSnan;

  • Finally, the occurrences in xcharconv_ryu.h should not be changed at this time (as this code is kept in sync with the upstream Ryu project):

uint32_t __vr, __vp, __vm;

uint64_t __vr, __vp, __vm;

I found all of these occurrences by using VSCode to regex-search for (\w+)(, \*?\w+)+; excluding files in tests/*.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementSomething can be improvedfixedSomething works now, yay!good first issueGood for newcomers

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions