Poll: if () with single inner statement. What do you do? What do you do?

Started by
46 comments, last by Vectorian 12 years, 4 months ago


Not every one uses the same editor on a project especially the open source once where you run into emacs vs vi vs edit and the like.


Both Emacs and Vim, as far as I can remember, have this capability, as do most of the bare bones open source editors including Notepad++. It is seriously a basic feature missing only from things like MS Notepad, which is fine for simple and quick editing, but I must question the sanity of anyone who uses it as a serious development platform.

On the tabs vs. spaces debate, the *only* reason I prefer tabs is because I work with several people who like different indents. I keep my tab width at 2 or 3, one of my co-workers likes 5, and a guy we used to work with likes 8.[/quote]

I can accept this argument, and only reply with the idea I prefer consistency that comes with preventing the mixing of tabs and spaces that almost invariably occurs when tabs are allowed. But I am not one for imposing personal preference on team members.
Advertisement

I can accept this argument, and only reply with the idea I prefer consistency that comes with preventing the mixing of tabs and spaces that almost invariably occurs when tabs are allowed.

We don't have that problem, because our local coding standard requires all tab indentation. And given that our codebase includes a fair number of supporting python scripts, this is pretty strictly enforced - python just doesn't play nicely with mixed spaces/tabs.

Someone once misinterpreted Guido van Rossum's statement that he regretted allowing both tabs and spaces, to mean that he regretted allowing tabs altogether. And this leads to my biggest pet-peeve with the YAML standard: they used this interpretation to justify disallowing tabs altogether for indentation.

Taking that freedom away causes us much more trouble than we encounter in working around python's indentation foibles. Half the team are unhappy about being forced to use spaces to indent, and most of the team is unhappy with being forced to use exactly 4-spaces to indent, since we had to define a fixed width for the entire codebase...

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

As I said, I would not force my preference because not everyone uses the same language or tools. Since my background is C++/C#/Java, whitespace is decorative, and you can end up with some fun results with mixed tabs and spaces. I just think the tools that allow you to standardize on this help a team maintain consistency, if they so desire.
Good discussion! I typically don't put them in unless required and I've never been tripped up in the manner described, that I can ever recall.

Unfortunately the people who get tripped up when working with other's code that has no braces, are the ones who always put braces in themselves. You might think you're making it easier for yourself by putting them in every single time, but your really just building reliance on all code you have to deal with being exactly the same, setting yourself up for trouble when you have to step outside your happy place.

By the way, the discussion applies equally to lambas. "x => x.field" vs "x => { return x.field; }"

Well that's my 2c anyway.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
To me the argument boils down to one of two options, brevity or unambiguity. If a piece of code could be misinterpreted by someone else (or even yourself 6+ months later), saving a little bit of whitespace isn't worth the trouble of hunting down the bizarre new bug introduced.

It's much like courtesy. It may take more effort to be polite to someone, but if you need their help at a later date, you're more likely to get it if you weren't a dick.
[size="2"]Currently working on an open world survival RPG - For info check out my Development blog:[size="2"] ByteWrangler
In VS I edit with visible whitespace. As an example of objectively perfect code formatting you can view this directory which was a program for a university term paper I did before I graduated. From what I remember it's all tab-size agnostic following the C# style. This results in some very nice looking C++ code if I don't say so myself.


Unfortunately the people who get tripped up when working with other's code that has no braces, are the ones who always put braces in themselves. You might think you're making it easier for yourself by putting them in every single time, but your really just building reliance on all code you have to deal with being exactly the same, setting yourself up for trouble when you have to step outside your happy place.

You underestimate how OCD some people can get with code style. I've changed thousands of lines of code to fit into the style I use in C++/C#/PHP etc. You get really fast at it while working after a while. Visual Studio has powerful tools for renaming things in a whole codebase also that makes such changes trivial.
I tend to always add them. Why? It's easier to see what piece of code goes with what statement. I also don't have to add them when I want to add more code to an if branch later on.
Follow and support my game engine (still in very basic development)? Link

In VS I edit with visible whitespace. As an example of objectively perfect code formatting you can view this directory which was a program for a university term paper I did before I graduated. From what I remember it's all tab-size agnostic following the C# style. This results in some very nice looking C++ code if I don't say so myself.

[quote name='iMalc' timestamp='1321411073' post='4884396']
Unfortunately the people who get tripped up when working with other's code that has no braces, are the ones who always put braces in themselves. You might think you're making it easier for yourself by putting them in every single time, but your really just building reliance on all code you have to deal with being exactly the same, setting yourself up for trouble when you have to step outside your happy place.

You underestimate how OCD some people can get with code style. I've changed thousands of lines of code to fit into the style I use in C++/C#/PHP etc. You get really fast at it while working after a while. Visual Studio has powerful tools for renaming things in a whole codebase also that makes such changes trivial.
[/quote]

Except that all the cpp files in that directory have no new lines in them when viewing in a browser so I wouldn't call that perfect. There is no perfect code formatting, I think the best we can strive for is consistency in a codebase so that all the files have the same code formatting. If you start messing with this, just to justify your own formatting, you just end up tripping other people up.

A human scans when he sees so we are particularly sensitive to anything that is different from the normal, and this is why I think we should keep the same style throughout a repository.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

I only make use of non-brace-statements at the beginning of blocks for break-outs. For example
void operation(Thing *arg)
{
if (arg == NULL)
return;

// stuff
}


or
for (int i = 0; i < v.size(); ++i)
if (v.somevalue == something)
continue;

// stuff
}


In the middle of functions it can easily get confusing, but for checking arguments etc. I find it quite apt to save space.

Of course I always adapt to the coding style used by the project I'm working on, but the above is my personal preference.

This topic is closed to new replies.

Advertisement