Skip to content

gh-1031 refactor C Style Guide #1692

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 7, 2021
Merged

gh-1031 refactor C Style Guide #1692

merged 8 commits into from
Apr 7, 2021

Conversation

Onvember
Copy link
Contributor

@Onvember Onvember commented Dec 14, 2020

The current page about C style is quite old and confusing so I've tried to make it better.

Tarantool style features will be rendered like this:
image

!!!NOTE FOR REVIEWERS!!!
The "c_style_guide.rst" and "c_style_guide_new.rst" files are identical. I added a "c_style_guide_new.rst" just to make sure that git comparisons with the old version would not get in the way during the review. I will remove extra files after review. Feel free to comment on any of these files.

fixes #1031

@NickVolynkin NickVolynkin linked an issue Feb 12, 2021 that may be closed by this pull request
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Tabs are 8 characters, and thus indentations are
also 8 characters. There are heretic movements that try to make indentations
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is worth mentioning that we use 8-width tabs, not 8 whitespaces.

if (condition) do_this;
do_something_everytime;

Don't put multiple assignments on a single line either. Kernel coding style
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not Kernel coding style anymore. We must purge all mentioning of the kernel from the doc.

Lots of people think that typedefs ``help readability``. Not so. They are
useful only for:

#. totally opaque objects (where the typedef is actively used to **hide**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally should start with a capital letter?

might be an ``unsigned int`` and under other configurations might be
``unsigned long``, then by all means go ahead and use a typedef.

#. when you use sparse to literally create a **new** type for
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

brain to become accustomed to the standard types like ``uint32_t``,
some people object to their use anyway.

Therefore, the Linux-specific ``u8/u16/u32/u64`` types and their
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use these types. They can only be found in old SQL code, but we are getting rid of them.

a parameter is known to be a compiletime constant, and as a result of this
constantness you *know* the compiler will be able to optimize most of your
function away at compile time. For a good example of this later case, see
the kmalloc() inline function.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kmalloc() is a kernel function, we should not mention it.

than an indication of whether the computation succeeded, are not subject to
this rule. Generally they indicate failure by returning some out-of-range
result. Typical examples would be functions that return pointers; they use
NULL or the ERR_PTR mechanism to report failure.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have ERR_PTR, only NULL.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Chapter 14: Inline assembly
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tbh, I would drop chapter this entirely. We don't write in assembly except extremely rare exceptions which I can't even remember what was the last one.

Prefer to compile out entire functions, rather than portions of functions or
portions of expressions. Rather than putting an ``#ifdef`` in an expression,
factor out part or all of the expression into a separate helper function and
apply the conditional to that function.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be "the conditional" -> "the condition"?

going unused, do not compile it and use #if for this.
(However, if a function or variable *always* goes unused, delete it.)

Within code, where possible, use the IS_ENABLED macro to convert a Kconfig
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have IS_ENABLED nor Kconfig. I would propose to drop the text from here to line 841.

@NickVolynkin NickVolynkin self-requested a review April 6, 2021 09:02
@Onvember Onvember merged commit f973348 into 1.10 Apr 7, 2021
@Onvember Onvember deleted the onvember/gh-1031 branch April 7, 2021 11:16
Onvember added a commit that referenced this pull request Apr 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[3pt] Cleanup Tarantool C Style Guide
3 participants