|
Paul Darlington wrote: Another discussion on the very old topic is, in my opinion, a waste of time
But we're software developers and we love religious wars!
In reality, whatever people answer on this it would not be top of my list of maintainability bugbears.
Kevin
|
|
|
|
|
Every, every time!
In legacy code I inherit this is so often the cause of errors. Anyway, the programming standards demand it. OK, it was me who wrote it in there, but it makes sense.
If you want to save space, you can write
if (errcode == 1) { return iError; }
------------------<;,><-------------------
|
|
|
|
|
RedSonja wrote: In legacy code I inherit this is so often the cause of errors.
If that is really the case then it's because the programmer (original or the one who performs changes) is clueless and no coding standard will help it.
|
|
|
|
|
Awfully sweet of you to put it like that.
I do this for a living, it's not a hobby, so when I am modifying someone else's code we are in a hurry, the project leader is hovering and there is a progress meeting at 12 noon. If they didn't bother to code tidily it takes longer. If my version of VS tabs and indents differently from whatever they used then it is not clear what they were thinking. If they used braces is it clear at once.
Sometimes you can see from the tabs and comments what they intended but they forgot the braces and it is wrong. This is a common mistake made by our students.
------------------<;,><-------------------
|
|
|
|
|
That is, I always use begin/end.
That was not there, so i selected third by mistake.
Avinash S. Godse
|
|
|
|
|
I don't know if the question was edited but it says this at the end:
Replace {}'s with Begin/End or whatever is appropriate for your language du jour
|
|
|
|
|
For if only, while only and so on, I never wrap single line.
But, for else, if it comes after a braced if, I wrap it also.
So:
if (x)
DoSomething();
else
DoOtherSomething();
but
if (x)
{
DoSomething1();
DoSomething2();
}
else
{
DoSomething3();
}
|
|
|
|
|
for if only, while only and so on ...
... but, for else, if it comes after a braced if ...
What?
|
|
|
|
|
Look at the example... I think you will understand.
|
|
|
|
|
Agree with you, I use them when it makes the code more readable... sometimes they add clutter when used and not needed though...
|
|
|
|
|
I use
if(statement){
...
}
Like real programmers.
|
|
|
|
|
Ennis Ray Lynch, Jr. wrote: Like real programmers.
Then I must be an imposter.
--
** You don't hire a handyman to build a house, you hire a carpenter.
** Jack of all trades and master of none.
|
|
|
|
|
You're either wrapping or you're not. The question doesn't ask about formatting.
cheers,
Chris Maunder
The Code Project | Co-founder
Microsoft C++ MVP
|
|
|
|
|
Chris Maunder wrote: You're either wrapping or you're not.
will the real slim shady, please stand up.
"I do not know with what weapons World War 3 will be fought, but World War 4 will be fought with sticks and stones." Einstein
"Few things are harder to put up with than the annoyance of a good example." Mark Twain
|
|
|
|
|
That is what I do all the time.
John
|
|
|
|
|
In the words of Nicholas A. Solter and Scott J. Kleper from the book "Professional C++"...
"The difference is subtle, and the determination of which is better left to the reader, yet we can't move on from the issue without pointing out that if is not a function!"
|
|
|
|
|
bob16972 wrote: "The difference is subtle, and the determination of which is better left to the reader, yet we can't move on from the issue without pointing out that if is not a function!"
Yes, whitespace is your friend.
I can't stand this crap:
if(a!=3&&a!=5){
a=b+4*c;
}
Mine would look like:
if(a != 3 && a != 5)
{
a = b + (4*c);
}
I know the parentheses are not necessary, but it makes you not have to stop and wonder if it does what I intended it to do. I'm getting a bit OT here, but all these things are related to how much formatting can make the code's meaning jump off the screen, as opposed to being pried out with a pick-axe.
I used to trail if s with { inline, but I got over it.
He said, "Boy I'm just old and lonely,
But thank you for your concern,
Here's wishing you a Happy New Year."
I wished him one back in return.
|
|
|
|
|
Couldn't agree more!
|
|
|
|
|
Almost yes, but:
If whitespace is friend, why not to use space also after "if" word?
i.e.:
if (a != 3 && a != 5)
{
a = b + (4 * c);
}
|
|
|
|
|
petriiik wrote: If whitespace is friend, why not to use space also after "if" word?
That was something I accidentally didn't correct after the copy & paste. I agree 100%, I go so far as doing a global search and replace on "if(" --> "if (" if I find a file that was written that way.
He said, "Boy I'm just old and lonely,
But thank you for your concern,
Here's wishing you a Happy New Year."
I wished him one back in return.
|
|
|
|
|
I liked your first post and I too dislike not having white space after the if. However, I don't consider it the end of the world though.
Kevin
|
|
|
|
|
Our coding standard would require an additional step:
if (a != 3
&& a!= 5)
{
a = b + (4 * c);
}
We have a single evaluatable conditional per line
If you vote me down, my score will only get lower
|
|
|
|
|
Roger Allen wrote:
if (a != 3
&& a!= 5)
{
a = b + (4 * c);
}
Yeah, for longer statements, ifs are broken into multiple lines. It depends on the case. And for grouping higher precedence operations (i.e. "4*c")it "feels" clearer to me, though I still use parentheses when it isn't instantly clear.
I chose not to enforce coding standards across all our developers (within reason, some things need to be global, like tabs=3 spaces and there no tab characters). This way, I can *instantly* tell who wrote something when I look at a piece of code, or at least tell that it wasn't me It's quicker than using svn blame.
We're a small department though (3-4 developers max), larger companies probably couldn't get away with that.
He said, "Boy I'm just old and lonely,
But thank you for your concern,
Here's wishing you a Happy New Year."
I wished him one back in return.
|
|
|
|
|
gag (sticking finger down throat...)
toss in a pair of parens:
if ( (a != 3) && (a != 5) )
{
... do something useful
}
If the conditional is quite long, the perhaps break it across two lines,
but have you ever noticed that coding standards (nothing wrong with them) have
not evolved past 80 char terminals?
Charlie Gilley
<italic>You're going to tell me what I want to know, or I'm going to beat you to death in your own house.
"Where liberty dwells, there is my country." B. Franklin, 1783
“They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
|
|
|
|
|
Actually, I dislike the extra parentheses because in this case the condition without them is semantically unambiguous. The extra parentheses just add clutter and can actually decrease readability with more elaborate conditionals.
Kevin
|
|
|
|