a=(b+c)
Because you know it's not necessary. But we're human and sometimes make mistakes when we're sure we didn't.That's also why we allow for some misunderstanding, (rather than (writing (using) (sentence trees))).
a=(b+c)
Because you know it's not necessary. But we're human and sometimes make mistakes when we're sure we didn't.That's also why we allow for some misunderstanding, (rather than (writing (using) (sentence trees))).
On a related note, I occasionally write expressions that both assign and test, e.g. if ( ( a = b ) == c ) and in those cases I parenthesize liberally and always write a comment that indicates yes, I intend to make an assignment in the middle of a test for equality.
while((buf = resultofstreamingoperation() != null) {
}
The ` != null ` is redundant here but you can imagine where it'd be required. if ((error = do_a_thing())) {
// some error happened, good thing we saved the error code
}
Given the standard "zero is success" paradigm, handling errors is very succinct. a = b
if ( a == c ) ...
The repetition is even worse if you replace 'a' or 'b' with a more complex expression: a[foo/bar] = b[baz/bat]
if (a[foo/bar] == c) ...
Oof indeed! Just rewrite it as: if ( ( a[foo/bar] = b[baz/bat] ) == c ) ... // Yes, assign and test!In a code review I would definitely reject this.
What I'm getting at is one person's "easy to parse" is another person's "difficult to parse", and there may be no objective answer which makes one any better than the other.
I would think to myself if I was writing one “have I made a mistake somewhere that means I have to use this?”
I don’t think it’s necessarily about “can I parse this”, it’s much more about will all the devs on the team be able to parse this.
To me, code is all about communication - to myself, but also to an audience I haven’t met with varying skill, knowledge and context levels.
I’ve been bitten too many times with code that only one or two people can work on.
I'm not sure where you get that idea. Repetition anywhere is usually a code smell.
> You write code once
And then you update it when you add new features or the requirements change or you fix bugs, etc. Having to change two symbols is more error prone than changing one. And having to parse more code is harder than parsing less code.
The assign-and-test pattern is common in several languages (e.g. C), and adding a comment that explains the logic should remove all doubt as to what is happening and why, so I see it as a win/win.
In any case, there is a trade-off between terseness and legibility, and while I usually favor more verbose code, I tend to draw the line at needless repetition. But that's my personal preference, and everybody draws that line in different places.
I'd rather parse three straightforward copies with minor differences than one chunk of dry spaghetti. And in assign & test case, splitting them makes debugging much easier.
key1 = foo/bar
key2 = baz/bat
a[key1] = b[key2]
if (a[key1] == c)
I'm sure a coder who understands the context can come up with better names than key1 and key2.edit: or better yet
key2 = baz/bat
normal_var = b[key2]
if (normal_var == c)
...
key1 = foo/bar
a[key1] = normal_varYou're also splitting what is intended to be essentially an atomic operation into multiple steps, which can be good if you want to analyze and tweak them in the future, but it's now no longer clear where the process begins and ends in relation to the code that comes before and after: you have to add more comments, or split the whole thing out into its own function.
I'm not saying your code is bad or wrong, just that there are downsides to any solution (including mine), and ultimately everybody has to pick whichever has the fewest negatives for their particular project.