This is just code cleanup, no behavior is changing. It deduplicates getbalance code and makes it comprehsible so it is not doing bizarre things like subtracting negative fees (https://github.com/bitcoin/bitcoin/pull/9167).
For each wallet transaction, the balance is updated as follows:
0+ CAmount debit = wtx.GetDebit(filter);
1+ const bool outgoing = debit > 0;
2+ for (const CTxOut& out : wtx.tx->vout) {
3+ if (outgoing && IsChange(out)) {
4+ debit -= out.nValue;
5+ } else if (IsMine(out) & filter && depth >= minDepth && (!account || *account == GetAccountName(out.scriptPubKey))) {
6+ balance += out.nValue;
7+ }
8+ }
9+
10+ if (outgoing && (!account || *account == wtx.strFromAccount)) {
11+ balance -= debit;
12+ }