<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"><html><head><meta name="qrichtext" content="1" /><style type="text/css">p, li { white-space: pre-wrap; }</style></head><body style=" font-family:'Sans Serif'; font-size:10pt; font-weight:400; font-style:normal;">In compiz++ I've made few little changes that do not follow the our previous coding style:<br>
- allow variable definition in "for" loops:<br>
for (int i = 0; i < foo; i++)<br>
In the "C" version of compiz we've tried to follow the C89 rules, but this is not necessary anymore and should improve readability.<br>
- define variables not at the top of a function:<br>
For references we have to define a variable at the point where it gets initialised. I would like the keep the "define the variables at the top of a function" style, but we should allow "in body" variable definition, at places where it improves readability of the code.<br>
- indent "case" statements inside of "switch" statements:<br>
in the "C" version of compiz we aligned the case statements at the same level like its switch statement:<br>
switch (foo)<br>
{<br>
case foo1:<br>
break;<br>
case foo2<br>
break;<br>
}<br>
<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><br></p>I think that indenting the "case" statement is a better style here:<br>
switch (foo)<br>
{<br>
case foo1:<br>
break;<br>
case foo2:<br>
break;<br>
}<br>
<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><br></p>Another style rule that I would like to add to our coding style is to provide a consistent system to mark differences between function local variables, function parameters, class member variables and global variables.<br>
I have already two possible solutions here:<br>
Solution 1:<br>
postfix member and global variables with a "_"<br>
prefix function parameter with a "_"<br>
<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><br></p>Solution 2:<br>
prefix member variable with "m_"<br>
prefix global variables with "g_"<br>
<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><br></p>For my diploma thesis I'm currently working on a C++ tool that already uses the first rule, and I've realized that it really improves readability of the code, because it's pretty clear where a variable is coming from. I would really like to get the opinions and solutions of other developers here.<br>
<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><br></p>Dennis<br>
<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><br></p>Am Dienstag 03 Februar 2009 16:46:27 schrieb Kristian Lyngstol:<br>
> I've been trying to come up with some good coding standards, but I've<br>
> realized that most of us already know most of this. So instead of trying to<br>
> define every aspect of how we code Compiz-code, I'll try to outline some of<br>
> the finer points. For a more complete list, you can review any of the many<br>
> coding-standard documents out there (For Linux, FreeBSD or any other<br>
> project).<br>
><br>
> Part of this draft is designed to document the already undocumented code,<br>
> which means some additional work for developers working on existing code.<br>
> I hope the degree of work expected is acceptable.<br>
><br>
> Note that this is a very rough draft.<br>
><br>
> 1. Maintainability<br>
><br>
> Your code will be used by thousands of users, and it will be used even if<br>
> you no longer find yourself working on Compiz. Design your code with this<br>
> in mind. Remember that if you can get 90% of the functionality with 10% of<br>
> the work, that's a good thing.<br>
><br>
> Split your code into logical pieces. Ideally, functions span 1-3 screens<br>
> on a 80x25 terminal. Complex functions should strive for a smaller size,<br>
> while simple functions (like initialization of large structures) could be<br>
> quite long. If your function is one big if-elseif-elseif-...else-<br>
> statement, each block should be tiny or split out into separate functions.<br>
> Performance-wise, there's nothing stopping you from using an<br>
> inline-function to achieve the same performance with increased readability.<br>
><br>
> To reduce complexity, clear and well defined interfaces are needed. The<br>
> fewer interfaces we need, the better. This goes for both core<->plugin<br>
> interaction, and interaction between different parts of core or a plugin.<br>
><br>
> If you can't avoid complexity, attempt to separate it from the rest of the<br>
> code as much as possible, and make sure it's well-documented in-line in<br>
> your code. Any special exceptions you have to make should be documented in<br>
> the code. Which brings us to...<br>
><br>
> 2. Document your code<br>
><br>
> Note that from now on, it is your responsibility to document code you work<br>
> on, even if you aren't the original author. Commits that lack the necessary<br>
> documentation is likely to be revoked. There are exceptions to this, but<br>
> read on.<br>
><br>
> This does not mean writing on the wiki, writing a seperate text file or<br>
> publishing a book. It means that you stick a little comment in each file<br>
> under the license explaining very briefly what the plugin does, and if<br>
> there are any special tricks used. And it means that if you work on any<br>
> non-trivial function, make sure you stick a comment on top of it explaining<br>
> what it does and how to use it, if it isn't evident.<br>
><br>
> If in doubt, make a comment.<br>
><br>
> There's a formula for when to do this, which is loosely based on<br>
> complexity, importance and how exposed your function is. This basically<br>
> means that ANY core function that is not declared static should be<br>
> documented. For core functions, you should also mention where you want this<br>
> function to be used, even if it might already be obvious from header-files.<br>
> This is specially important with functions that are only safe or valid in<br>
> certain contexts.<br>
><br>
> As for exceptions: If you are only tweaking an existing function, for<br>
> instance to adjust it to an API change, it is not required to comment. If<br>
> you are in doubt about the code functionality, and don't wish to verify<br>
> with others, writing a FIXME: with a brief question/comment so somebody<br>
> else can pick it up, is acceptable.<br>
><br>
> 3. Be consistent<br>
><br>
> This applies to both within your own code, but also with regards to the<br>
> rest of the project. Use the same grammatical coding style (found on the<br>
> wiki), similar naming-conventions and so on.<br>
><br>
> 4. Don't supply useless code or options<br>
><br>
> Our job as developers is to create mechanism, not policy. Don't use your<br>
> plugin or core-code to change the world. Don't supply 5 different options<br>
> when you can supply one that allows for the same level of flexibility.<br>
> Think about Compiz' position in the bigger picture, not just the world you<br>
> live in.<br>
><br>
> If you really really really want to change the world, at the very least, do<br>
> it as a warning, not an error!<br>
><br>
> 5. Expect the unexpected<br>
><br>
> Check return values. Check input-values for non-static code. It has a<br>
> negative development cost; Any time used now will be made up for down the<br>
> road.<br>
><br>
> -----------------------------------------<br>
><br>
> That's it so far. I really hope someone can rip this apart, since I ended<br>
> up trashing my more detailed ideas and threw this together in a hurry. At<br>
> least it should get the discussion started.<br>
><br>
> Specially since this is written with C, and not C++ in mind.<br>
><br>
> - Kristian<br>
<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><br></p><p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:0;"><br></p></body></html>