109

I currently work at a place where a standard code format is enforced and the code is automatically formatted when saving the file, just like you are about to do. As a new member of the company I found that the common formatting rules gave me a warm and fuzzy feeling that "these guys know what they are doing", so I couldn't be happier. ;) As a related side ...


37

Every professional software developer will prefer to adopt a (good) standard rather than get into evangelical wars over style, for the very reasons you have outlined. Many software developers wage evangelical wars...... Depending on your position within the team and the team dynamics, you may decide that winning the war is not possible. In this case, it ...


34

Strict code formatting rules are useful when several developers work on the same code using a version control system. Merging can be a pain if different developers have different formatting rules as the same code would look different for the merging tool. Eclipse (or any good IDE for that matter) has code formatting rules that can be customized in the ...


33

Make them as small as possible, but no smaller. The limiting factor here has nothing to do with the work being done. It has to do with if you can think of a good name. A decent vocabulary is a powerful thing. Give me a choice between a flat 100 line monstrosity and a 10 line function full of functions named procedureA(), procedureB(), and I'll take the ...


30

Yes, that is good to have one code format styles for all developers. Design the code style formats and import that to all developers eclipse. This will help when we are merging code to 'Version control' system.


24

I've found the autoformatter very useful. Instead of constantly taking microdecisions on how the code should be formatted -- something which is error-prone and causes "cognitive friction" -- you can set up formatting rules and let Eclipse format the code for you (ideally automatically using "Save actions"). Of course, this requires that you have a code base ...


17

You should use the first approach because it scales to larger programs. The mentality of "I need to see how everything affects everything" is harmful to software development. You want functions/objects decoupled, so that you can change one thing without breaking another distant/unrelated function. Even if you use a compact coding style, you will eventually ...


17

The quote from the same page, same paragraph, a few sentences before: Making the code look pretty is worth something, but it's worth less than showing the code's structure. If one technique shows the structure better and another looks better, use the one that shows the structure better. [...] In practice, prioritizing logical representation usually doesn'...


16

What are you trying to gain, how anal retentive are you going to be in enforcing it (and in the level of detail your "rules" will be set out), are you going to try to enforce it on code written in different languages, are you going to try to enforce it retroactively on existing code? a common look and feel to code can indeed help make code more readable, ...


15

What you are referring to is a ternary operator. In my opinion it can increase readability for simplistic conditional checks like the one in your question. It's short and provides a clear and obvious pair of return values. Nesting them hurts readability. While not quite a duplicate question the "hurts" is clearly demonstrated in THIS question. Code from ...


15

Can a function be too short? Not really.... functions can be as short as you want, even as small as one line. Decent compilers will inline the code if it makes sense to do so, so don't worry that it will add overhead. Should a simple logical operation be encapsulated in a method? As for a single line null check-- that might make sense if the null check ...


12

Yes it is. Eclipse lets you define your own exportable code style which you can share among all your developers. If you don't want to setup your own formatting style, you may tell your developers to use Eclipse's default formatting. Even if you use the default one, I would advise you to export it and put it on a shared network drive for all developers to ...


12

It's all about the indents It's not really about the brackets, it's about indenting and vertical alignment. Not indented You can put brackets on the same line when indentation is not in play. Example in lambda expression: list.ForEach( a => { a.Init(); a.DoSomething(); a.Complete(); }); Example when declaring an empty interface, like this one ...


11

Yes, consistency is a good idea, for reasons others have mentioned. I just wanted to add a couple of points which haven't been used elsewhere: Oracle has published a set of conventions for Java, which are the de facto standard. Using these should help you avoid arguments about which style to follow. A lot of public libraries and open source projects tend ...


10

/********************************************************************* * People who do indent that way also tend to write pretty comments. * * It looks so very pretty, so very professional. Except it's not. * *********************************************************************/ Suppose a variable needs to be renamed for some reason. That simple change ...


10

In a compiled language, any superfluous whitespaces, comments or other elements without syntactical meaning do not survive the tokenization step of the compilation, so it doesn't make a difference at all for the resulting binary (at least for the executable parts - some compilers might embed original sourcecode in the generated binary for debugging purposes, ...


9

The Android style guide says Limit Line Length Each line of text in your code should be at most 100 characters long. There has been lots of discussion about this rule and the decision remains that 100 characters is the maximum. Exception: if a comment line contains an example command or a literal URL longer than 100 characters, that line ...


9

I was on a team that used the Checkstyle plugin. Rather than use the out-of-the-box features, we formed a small committee of interested developers. We debated over what seemed missing, what seemed excessive, and hammered things out. We all learned something in the process, and strengthened those developer muscles. (Examples of our decisions: 72 characters ...


6

If you're going to stick to the same IDE and incorporate some formating tools, it's a good idea because you're not requiring too much effort. Keep the rules simple with a focus on readability and not anal retentiveness. That should be the measuring stick. Although a consistently formated ball of mud is better than just a ball of mud, your time would be ...


6

I strongly recommend that humans enforce code formatting, and that minor infractions are graciously overlooked or touched-up. Reasons for this are, briefly, The machine gets it wrong at the worst possible time, and usually when you are using a new language feature. How will it handle closures in Java, etc? Jalopy has trouble with enums with member functions....


6

In C you can't do object->method(value), so it's idiomatic to put the "object"—the thing whose state you're changing—as the first parameter, and prefix the function name with the "class." The C standard library makes a lot more sense when you think about it that way. For example, strcpy(dest, src) reads as dest->cpy(src), where dest is a ...


6

Take a look at YAML. It is a data serialization language that is designed to be human readable. Here's a sample from the link: --- !clarkevans.com/^invoice invoice: 34843 date : 2001-01-23 bill-to: &id001 given : Chris family : Dumars address: lines: | 458 Walkman Dr. Suite #292 city : Royal Oak ...


6

One reason I can think of is that putting the closing brackets on individual lines makes it easier to (un)comment the whole block using // without breaking the syntax.


6

CandiedOrange's answer is closest to the real point. You should not be counting lines to decide if a chunk of code should be encapsulated into a function. Instead, what you want to do is think of your API(s) as a language(s). Now you may (typically will) have multiple layers of APIs corresponding to multiple levels of abstraction. The ideal is to have any ...


6

Why ? In C, C++, Python, Java, C#, Groowy, and Swift (historical order of appearance), and certainly in many other languages as well, the usual logical and-operator (mostly &&) works with short-circuits. In some languages such as ADA (i.e. and), the short-circuit form requires an extra effort (i.e. and then, which gives a little look like your ...


6

What would be the alternative? No linter at all, and thus no style fixes at all - except for the ones found during code reviews. But obviously - the latter have to be applied manually as well. So this ultimately depends on how much value you see in those suggested style fixes the tool provides, and if you think applying those fixes (manually) is worth the ...


Only top voted, non community-wiki answers of a minimum length are eligible