Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8252581

HotSpot Style Guide should permit declarations in condition forms

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 16
    • hotspot

      The Style Guide currently says

        Use `bool` for boolean values. Do not use ints or pointers as
        (implicit) booleans with `&&`, `||`, `if`, `while`. Instead, compare
        explicitly, i.e. `if (x != 0)` or `if (ptr != nullptr)`, etc. Do not
        use declarations in /condition/ forms, i.e. don’t use
        `if (T v = value) { ... }`.

      The last part, about condition forms, was added to make explicit a restriction that was already implicit. There have been complaints in code reviews about using that feature. Using that feature is clearly using an integer or pointer as a boolean, or using a boolean conversion operator (and without C++11 explicit conversions those are either implicit (nearly always a bad idea) or using the safe-bool idiom, which is a pain to write and looks really strange to someone not already familiar with it).

      There exist uses it for some iterators:

      for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thr = jtiwh.next(); ) {

      This is useful to limit the scope of the instance. We shouldn't discourage this usage; indeed, perhaps we should encourage it, as limiting the scope of a variable generally helps code understanding, and this is less intrusive (where applicable) then adding a block level.

      Note that C++17 extends the syntax for `if` and `switch`: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0305r1.html

            Unassigned Unassigned
            kbarrett Kim Barrett
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: