jsoncpp icon indicating copy to clipboard operation
jsoncpp copied to clipboard

Refactor with datadiode.

Open cdunn2001 opened this issue 11 years ago • 4 comments

@datadiode says (in #103):

You may want to consider this fairly invasive changeset (which certainly goes way beyond the border case addressed in this ticket).

cdunn2001 avatar Jan 12 '15 16:01 cdunn2001

Lots of good stuff there, including the bug-fix in normalizeEOL and Python3 support. I want to work with you more closely, but you've got to learn to commit tiny changes. This is a practice I try to install in junior devs at Amazon too. There are many, many small points to discuss.

E.g.

  • Default arguments often add complexity without value. The setof<> template could be setof10, and we could easily provide 10 values on every call.
  • setof might be simpler (and compile faster) and more transparent as a macro. I don't love C macros, but templates are a band-aid on C++, not an elegant type-system. (This could be a very long discussion.)
  • You have removed helpful debugging information in some places.
  • "Prefer composition over derivation." I don't see why StyledWriterMethods should be a base-class -- except as an intermediate step in a refactor.
  • If you learn a functional programming language, your C++ coding style would change dramatically, in my opinion for the better. Specifically, I commend the advice (and erstwhile requirement) of the Google C++ Style Guide to eschew non-const references. (Also see talk by Titus at CppCon.)
  • You've copied a bunch of code into a switch statement. I prefer to keep the code within case statements brief, unless you're doing magic with gotos/labels for efficiency. inline is usually enough. I see that you want to use surrogates later in the method, but that can be done other ways. Your way would be debatably acceptable as original code, but as a change you have to respect the code-reviewer. You've made the diffs hard to see.
  • In general, if you are moving a block of code, then move it as a commit. Modify the moved code in a separate commit. It makes both discussion and partial reversion much simpler.

In Git, I suggest committing early-and-often. But it's never too late. Could you split these into smaller changes?

  • git rebase -i and change your commits from pick to edit. Then, for each one,
  • git reset HEAD~1
  • git add -p/git commit repeatedly.

Sub-dividing commits is a good skill to have anyway.

cdunn2001 avatar Jan 12 '15 16:01 cdunn2001

On Mon, Jan 12, 2015 at 11:56 AM, Christopher Dunn <[email protected]

wrote:

Lots of good stuff there, including the bug-fix in normalizeEOL and Python3 support. I want to work with you more closely, but you've got to learn to commit tiny changes. This is a practice I try to install in junior devs at Amazon too. There are many, many small points to discuss.

E.g.

  • Default arguments often add complexity without value. The setof<> template could be setof10, and we could easily provide 10 values on every call.
  • setof might be simpler (and compile faster) and more transparent as a macro. I don't love C macros, but templates are a band-aid on C++, not an elegant type-system. (This could be a very long discussion.)

All we need from setof<...> is its static contains(char) function. It's used in one place. We have better ways to do this than templates or macros.

Instead of

typedef setof<char, '.', 'e', 'E', '+', '-'> whitelist; ... whitelist::contains(c)

We could have:

inline bool in_whitelist(char c) { switch (c) { case '.': case 'e': case 'E': case '+': case '-': return true; default: return false; } } ... in_whitelist(c)

And I think that's just as fast once the optimizer gets involved.

  • You have removed helpful debugging information in some places.
  • "Prefer composition over derivation." I don't see why StyledWriterMethods should be a base-class -- except as an intermediate step in a refactor.
  • If you learn a functional programming language, your C++ coding style would change dramatically, in my opinion for the better. Specifically, I commend the advice (and erstwhile requirement) of the Google C++ Style Guide to eschew non-const references http://stackoverflow.com/questions/26441220/googles-style-guide-about-input-output-parameters-as-pointers. (Also see talk by Titus at CppCon.)
  • You've copied a bunch of code into a switch statement. I prefer to keep the code within case statements brief, unless you're doing magic with gotos/labels for efficiency. inline is usually enough. I see that you want to use surrogates later in the method, but that can be done other ways. Your way would be debatably acceptable as original code, but as a change you have to respect the code-reviewer. You've made the diffs hard to see.
  • In general, if you are moving a block of code, then move it as a commit. Modify the moved code in a separate commit. It makes both discussion and partial reversion much simpler.

In Git, I suggest committing early-and-often. But it's never too late. Could you split these into smaller changes?

  • git rebase -i and change your commits from pick to edit. Then, for each one,
  • git reset HEAD~1
  • git add -p/git commit repeatedly.

Sub-dividing commits is a good skill to have anyway.

— Reply to this email directly or view it on GitHub https://github.com/open-source-parsers/jsoncpp/issues/112#issuecomment-69602949 .

ǝnɥɐuop ʎllıq

BillyGoto avatar Jan 12 '15 17:01 BillyGoto

I recognize that we disagree quite fundamentally on C++ coding practices, and I'm afraid I'm pretty much uneducatable.

But since you seem to particularly dislike the setof<> template, it seems worth looking at this changeset, which replaces the whitelist::contains() thing with somewhat stricter float parsing logic.

About StyledWriterMethods: You may have missed that the central point of this class is to virtualize the write(const char*) method.

datadiode avatar Jan 12 '15 20:01 datadiode

setof is not a big deal. The problem is the large changeset.

By itself, that smaller change looks fine if rebased to master.

cdunn2001 avatar Jan 13 '15 17:01 cdunn2001