|
As soon as I switch to a multi-line statement inside any conditional I put in the brackets. I've had too many bugs over the years because the brackets weren't there.
|
|
|
|
|
I'll do a single line if-statement if the code inside the braces is a single line, function call, etc. If I were reviewing another's code, either way would be suitable.
|
|
|
|
|
This is why we use a code formatter at our place, to avoid the drama and debates on the code reviews! Everyone's code looks equally terrible.
|
|
|
|
|
If you can keep your head while those about you are losing theirs, perhaps you don't understand the situation.
|
|
|
|
|
|
Agree. Similarly, we use Resharper. I just accept whatever it does and move on.
|
|
|
|
|
Quote: I stood my ground, (and won out), not because I didn't want to spend 2 minutes changing the code, but because I value properly documented standards. Personally, I would have spent the 2 minutes changing the code (actually I wouldn't have done it that way but that's a different argument) ..
.. AND a further 2 minutes updating the standards (or at least 2 minutes making life h3ll for the owner of those standards until they updated them )
|
|
|
|
|
Since both formats are used within the code base the person who bounced it was just wrong. Code reviews should be about functionality not form.
(I would have done it the 4 line way personally but no way would I have rejected the review on style.)
if (condition)
{
action
}
But then, I'm old.
|
|
|
|
|
MarkTJohnson wrote: But then, I'm old.
Me too! Which is probably why I get landed with working with archaic languages.
MarkTJohnson wrote: Since both formats are used within the code base the person who bounced it was just wrong. Code reviews should be about functionality not form.
Yep. If this had been about something not working, (or if it was important enough to be documented as a standard), I would have changed it in a jif!
|
|
|
|
|
5teveH wrote: I would have changed it in a jif!
I do like peanut butter[^], but I'm a little unsure what to think of this.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
Absent a team style document, definitely stand your ground.
And certainly ignore weenies who try to quote from other teams' style documents. "But Microsoft says.." "We don't work for Microsoft, you weenie!"
I'm a whitespace supremicist as most of you know, so I nearly always use as much vertical space as I deem reasonable.
There are times, though, when I would use a series of one-liners. Usually if the tests and actions are very similar -- to show how they are similar.
if ( TestX ) { DoX() } ;
if ( TestY ) { DoY() } ;
if ( TestZ ) { DoX() } ;
...
I feel that in this situation, this style leads to more readable/understandable code and that copy/paste errors like above may be more easily spotted.
Does the language you're using not allow:
{do something} if {condition}
VAX BASIC V3.9-000
Ready
10 LET X = 42
20 PRINT "yes" IF X = 42
runnh
yes
Ready
modified 12-Aug-20 11:47am.
|
|
|
|
|
I'm with you on everything. Particularly like the VAX BASIC - not a million miles away from what I'm working with.
|
|
|
|
|
I think you may have won the battle, but lost the larger point of it all.
What you accomplished was to make yourself difficult to deal with. I don't think that's what you intended.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
I didn't just make myself difficult to deal with. I've been like that for about 40 years!
Hopefully, anyone who knows me, knows that as well as being challenging and pedantic, I will always accept when I'm wrong; never take myself too seriously; and never get personal. Also, I generally give those above me a hard time, rather than my peers. Yep, my chances of promotion are zero - but that matches my ambition. I am way too old to be worrying about career and more than happy to come in and do the day-to-day grunt work.
|
|
|
|
|
Oh, well in that case, carry on
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
It's if (...), not if {...}
I'll consider a single line if, if it is simple; and at the top of a method. Otherwise no. e.g.
if ( parm1 == null ) { return; }
And brackets around code: always. Even one-liners.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
modified 12-Aug-20 11:16am.
|
|
|
|
|
Yup, anything that effectively comes down to an error exit could be a one-liner: typically a simple return or a throw.
Anything else should be surrounded by {}, no matter the format. (that's what format tools are for). It's just not worth risking an incorrect semantic only because someone was too lazy to foresee that someone might change your one-liner into a two-liner. It's not like we're printing that code and need to save paper!
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
(If you got another message ignore it ... CP is posting my responses to the wrong msg).
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
I think the point of the code review is to encourage developers to talk and agree on standards in their work environment.
Don't discuss it with us, discuss it with the other developer, add more reviewers.
Think of the consequences of your decision for future problems and future if statements
|
|
|
|
|
iskSYS wrote: I think the point of the code review is to encourage developers to talk and agree on standards in their work environment.
Surely the main reason for a Code Review is to help prevent developers from breaking the production system. It's one of the key steps, (along with SIT and UAT), in the SDLC which are intended to reduce the risk of change. i.e. check for coding errors/bugs.
Second would be, where possible, (and this isn't always the case), to check that the changes deliver the functionality that was requested. And don't break existing functionality.
Next, (in my opinion), would be ensuring adherence to formal coding standards. Here, I'm talking more about structural, than syntactical stuff. You may have a standard program structure that should be followed; or standard functions that should be used - e.g. we have an email validation function - so we don't want 20 developers doing their own ('better') version.
Finally, check 'adherence' to informal standards. This would be where coding style and readability sit. And this is a subjective thing - so much harder to manage.
|
|
|
|
|
I beg to differ, the unit tests, the integration tests, and system tests (automated and manual) are responsible to check whether or not the production systems are broken.
Visually inspecting the code for bugs i would argue is not the best solution for that.
In addition, some companies use static inspection tools such as code sonar.
That said, of course inspecting the code doesn't hurt, but I think the main reason is to check whether code meets the code standards, architectural standards, design patterns etc.
In some companies, the informal standards are usually solved with a standardized formatter rules.
To reach this point however, developers need to define these rules, and thus my original argument, use code Review to create a set of rules for the formatter.
|
|
|
|
|
One liner is fine, but I always add the brackets since getting stung by a badly formed #define or macro (can't recall which). It had a ; in it, so only the first statement in the do something was executed
|
|
|
|
|
Not everything has to be documented.
An oral agreement is legally valid, so you could've taken the opportunity to once and for all solve this matter (until someone forgets it or a new member joins the team) without documenting anything.
Of course it would be nice to write it down for future reference, and if you already have such a document you should certainly add it, but my experience is that no one ever reads it anyway (unless they want to prove someone else wrong).
Style-wise I agree with your coworker.
|
|
|
|
|
Assuming this is at work.
Have a discussion and vote on the syntax you all want to use and document it as a standard in your workplace only.
These debates happen all the time and the only way to move forward is to all agree on a single way. The code is easy to understand when it's all int he same format, There's less time thinking about what option to take, and no one will reformat files to their own preferred way.
Just make it clear that there is no "right" or "wrong" way of doing this. All accepted syntax by the IDE is correct, but being aligned is more important. and when voting make sure to include an option for "no preference".
Make sure you document these decisions in a "[Company name] Best practices".
Eventually the debates will stop and you will have some awesome best practices for new starters to just pick up
|
|
|
|
|
i don't know about documented coding standards, but i frequently use one liner if , for , if-else and even for-if .
for me, when working in a lower level language, they represent some alternative to higher level constructs.
one liner if and for it that scenario, at least for me, are not equivalent to branching and looping. the are equivalent to something that in JavaScript would represent Array.map(), Array.every(), Array.some()...
for (condition) if (condition) expression;
in C if-else is not well suited for a one liner, because if and else are separated by a semicolon.
but in Pascal, they go swell.
if (condition) then expression1 else expression2;
on the other hand, whenever i use if as a logical condition for branching (in old books represented as rhombus with arrows going left or right), i always create a new block at a new line.
|
|
|
|