908 lines
78 KiB
HTML
908 lines
78 KiB
HTML
<!doctype html>
|
||
<html class="no-js">
|
||
<head><meta charset="utf-8"/>
|
||
<meta name="viewport" content="width=device-width,initial-scale=1"/>
|
||
<meta name="color-scheme" content="light dark"><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
|
||
<link rel="index" title="Index" href="../../genindex.html" /><link rel="search" title="Search" href="../../search.html" /><link rel="next" title="Managing Memory" href="memory-management.html" /><link rel="prev" title="Programming Guidelines" href="../programming.html" />
|
||
|
||
<meta name="generator" content="sphinx-4.3.0, furo 2022.06.21"/>
|
||
<title>C Coding Style - GNOME Developer Documentation</title>
|
||
<link rel="stylesheet" type="text/css" href="../../_static/pygments.css" />
|
||
<link rel="stylesheet" type="text/css" href="../../_static/styles/furo.css?digest=40978830699223671f4072448e654b5958f38b89" />
|
||
<link rel="stylesheet" type="text/css" href="../../_static/tabs.css" />
|
||
<link rel="stylesheet" type="text/css" href="../../_static/styles/furo-extensions.css?digest=30d1aed668e5c3a91c3e3bf6a60b675221979f0e" />
|
||
<link rel="stylesheet" type="text/css" href="../../_static/gnome.css" />
|
||
|
||
|
||
|
||
|
||
<style>
|
||
body {
|
||
--color-code-background: #f8f8f8;
|
||
--color-code-foreground: black;
|
||
--color-brand-primary: #4a86cf;
|
||
--color-brand-content: #4a86cf;
|
||
|
||
}
|
||
@media not print {
|
||
body[data-theme="dark"] {
|
||
--color-code-background: #202020;
|
||
--color-code-foreground: #d0d0d0;
|
||
|
||
}
|
||
@media (prefers-color-scheme: dark) {
|
||
body:not([data-theme="light"]) {
|
||
--color-code-background: #202020;
|
||
--color-code-foreground: #d0d0d0;
|
||
|
||
}
|
||
}
|
||
}
|
||
</style></head>
|
||
<body>
|
||
|
||
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
|
||
<symbol id="svg-toc" viewBox="0 0 24 24">
|
||
<title>Contents</title>
|
||
<svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 1024 1024">
|
||
<path d="M408 442h480c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8H408c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8zm-8 204c0 4.4 3.6 8 8 8h480c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8H408c-4.4 0-8 3.6-8 8v56zm504-486H120c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h784c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zm0 632H120c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h784c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zM115.4 518.9L271.7 642c5.8 4.6 14.4.5 14.4-6.9V388.9c0-7.4-8.5-11.5-14.4-6.9L115.4 505.1a8.74 8.74 0 0 0 0 13.8z"/>
|
||
</svg>
|
||
</symbol>
|
||
<symbol id="svg-menu" viewBox="0 0 24 24">
|
||
<title>Menu</title>
|
||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
|
||
stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather-menu">
|
||
<line x1="3" y1="12" x2="21" y2="12"></line>
|
||
<line x1="3" y1="6" x2="21" y2="6"></line>
|
||
<line x1="3" y1="18" x2="21" y2="18"></line>
|
||
</svg>
|
||
</symbol>
|
||
<symbol id="svg-arrow-right" viewBox="0 0 24 24">
|
||
<title>Expand</title>
|
||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
|
||
stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather-chevron-right">
|
||
<polyline points="9 18 15 12 9 6"></polyline>
|
||
</svg>
|
||
</symbol>
|
||
<symbol id="svg-sun" viewBox="0 0 24 24">
|
||
<title>Light mode</title>
|
||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
|
||
stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" class="feather-sun">
|
||
<circle cx="12" cy="12" r="5"></circle>
|
||
<line x1="12" y1="1" x2="12" y2="3"></line>
|
||
<line x1="12" y1="21" x2="12" y2="23"></line>
|
||
<line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line>
|
||
<line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line>
|
||
<line x1="1" y1="12" x2="3" y2="12"></line>
|
||
<line x1="21" y1="12" x2="23" y2="12"></line>
|
||
<line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line>
|
||
<line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line>
|
||
</svg>
|
||
</symbol>
|
||
<symbol id="svg-moon" viewBox="0 0 24 24">
|
||
<title>Dark mode</title>
|
||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
|
||
stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" class="icon-tabler-moon">
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none" />
|
||
<path d="M12 3c.132 0 .263 0 .393 0a7.5 7.5 0 0 0 7.92 12.446a9 9 0 1 1 -8.313 -12.454z" />
|
||
</svg>
|
||
</symbol>
|
||
<symbol id="svg-sun-half" viewBox="0 0 24 24">
|
||
<title>Auto light/dark mode</title>
|
||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
|
||
stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" class="icon-tabler-shadow">
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
|
||
<circle cx="12" cy="12" r="9" />
|
||
<path d="M13 12h5" />
|
||
<path d="M13 15h4" />
|
||
<path d="M13 18h1" />
|
||
<path d="M13 9h4" />
|
||
<path d="M13 6h1" />
|
||
</svg>
|
||
</symbol>
|
||
</svg>
|
||
|
||
<input type="checkbox" class="sidebar-toggle" name="__navigation" id="__navigation">
|
||
<input type="checkbox" class="sidebar-toggle" name="__toc" id="__toc">
|
||
<label class="overlay sidebar-overlay" for="__navigation"></label>
|
||
<label class="overlay toc-overlay" for="__toc"></label>
|
||
|
||
|
||
|
||
<div class="page">
|
||
<header class="mobile-header">
|
||
<div class="header-left">
|
||
<label class="nav-overlay-icon" for="__navigation">
|
||
<i class="icon"><svg><use href="#svg-menu"></use></svg></i>
|
||
</label>
|
||
</div>
|
||
<div class="header-center">
|
||
<a href="../../index.html"><div class="brand">GNOME Developer Documentation</div></a>
|
||
</div>
|
||
<div class="header-right">
|
||
<label class="toc-overlay-icon toc-header-icon" for="__toc">
|
||
<i class="icon"><svg><use href="#svg-toc"></use></svg></i>
|
||
</label>
|
||
</div>
|
||
</header>
|
||
<aside class="sidebar-drawer">
|
||
<div class="sidebar-container">
|
||
|
||
<div class="sidebar-sticky"><a class="sidebar-brand" href="../../index.html">
|
||
|
||
|
||
<span class="sidebar-brand-text">GNOME Developer Documentation</span>
|
||
|
||
</a><form class="sidebar-search-container" method="get" action="../../search.html" role="search">
|
||
<input class="sidebar-search" placeholder=Search name="q" aria-label="Search">
|
||
<input type="hidden" name="check_keywords" value="yes">
|
||
<input type="hidden" name="area" value="default">
|
||
</form>
|
||
<div id="searchbox"></div><div class="sidebar-scroll"><div class="sidebar-tree">
|
||
<p class="caption" role="heading"><span class="caption-text">Contents</span></p>
|
||
<ul class="current">
|
||
<li class="toctree-l1 has-children"><a class="reference internal" href="../../introduction.html">Platform Introduction</a><input class="toctree-checkbox" id="toctree-checkbox-1" name="toctree-checkbox-1" role="switch" type="checkbox"/><label for="toctree-checkbox-1"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
|
||
<li class="toctree-l2 has-children"><a class="reference internal" href="../../introduction/components.html">Platform Components</a><input class="toctree-checkbox" id="toctree-checkbox-2" name="toctree-checkbox-2" role="switch" type="checkbox"/><label for="toctree-checkbox-2"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
|
||
<li class="toctree-l3"><a class="reference internal" href="../../introduction/overview/libraries.html">Libraries</a></li>
|
||
<li class="toctree-l3"><a class="reference internal" href="../../introduction/overview/services.html">Services</a></li>
|
||
</ul>
|
||
</li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../introduction/languages.html">Programming Languages</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../introduction/builder.html">GNOME Builder</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../introduction/flatpak.html">Flatpak</a></li>
|
||
</ul>
|
||
</li>
|
||
<li class="toctree-l1 current has-children"><a class="reference internal" href="../../guidelines.html">Guidelines</a><input checked="" class="toctree-checkbox" id="toctree-checkbox-3" name="toctree-checkbox-3" role="switch" type="checkbox"/><label for="toctree-checkbox-3"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul class="current">
|
||
<li class="toctree-l2 current has-children"><a class="reference internal" href="../programming.html">Programming Guidelines</a><input checked="" class="toctree-checkbox" id="toctree-checkbox-4" name="toctree-checkbox-4" role="switch" type="checkbox"/><label for="toctree-checkbox-4"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul class="current">
|
||
<li class="toctree-l3 current current-page"><a class="current reference internal" href="#">C Coding Style</a></li>
|
||
<li class="toctree-l3"><a class="reference internal" href="memory-management.html">Managing Memory</a></li>
|
||
<li class="toctree-l3"><a class="reference internal" href="writing-good-code.html">The Importance of Writing Good Code</a></li>
|
||
<li class="toctree-l3"><a class="reference internal" href="optimizing.html">Optimizing GNOME Applications</a></li>
|
||
<li class="toctree-l3"><a class="reference internal" href="namespacing.html">Namespacing</a></li>
|
||
<li class="toctree-l3"><a class="reference internal" href="introspection.html">Introspection</a></li>
|
||
</ul>
|
||
</li>
|
||
<li class="toctree-l2 has-children"><a class="reference internal" href="../accessibility.html">Accessibility</a><input class="toctree-checkbox" id="toctree-checkbox-5" name="toctree-checkbox-5" role="switch" type="checkbox"/><label for="toctree-checkbox-5"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
|
||
<li class="toctree-l3"><a class="reference internal" href="../accessibility/coding-guidelines.html">Coding Guidelines for Supporting Accessibility</a></li>
|
||
<li class="toctree-l3"><a class="reference internal" href="../accessibility/custom-widgets.html">Making Custom Components Accessible</a></li>
|
||
</ul>
|
||
</li>
|
||
<li class="toctree-l2 has-children"><a class="reference internal" href="../localization.html">Localization</a><input class="toctree-checkbox" id="toctree-checkbox-6" name="toctree-checkbox-6" role="switch" type="checkbox"/><label for="toctree-checkbox-6"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
|
||
<li class="toctree-l3"><a class="reference internal" href="../localization/practices.html">Best Practices for Localization</a></li>
|
||
</ul>
|
||
</li>
|
||
<li class="toctree-l2 has-children"><a class="reference internal" href="../maintainer.html">Maintainer Guidelines</a><input class="toctree-checkbox" id="toctree-checkbox-7" name="toctree-checkbox-7" role="switch" type="checkbox"/><label for="toctree-checkbox-7"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
|
||
<li class="toctree-l3"><a class="reference internal" href="../maintainer/api-stability.html">API Stability</a></li>
|
||
<li class="toctree-l3"><a class="reference internal" href="../maintainer/parallel-installability.html">Parallel Installability</a></li>
|
||
<li class="toctree-l3"><a class="reference internal" href="../maintainer/integrating.html">Integrating with GNOME</a></li>
|
||
</ul>
|
||
</li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../devel-docs.html">Developer Documentation Style Guidelines</a></li>
|
||
</ul>
|
||
</li>
|
||
<li class="toctree-l1 has-children"><a class="reference internal" href="../../tutorials.html">Tutorials</a><input class="toctree-checkbox" id="toctree-checkbox-8" name="toctree-checkbox-8" role="switch" type="checkbox"/><label for="toctree-checkbox-8"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
|
||
<li class="toctree-l2 has-children"><a class="reference internal" href="../../tutorials/beginners.html">Beginners Tutorials</a><input class="toctree-checkbox" id="toctree-checkbox-9" name="toctree-checkbox-9" role="switch" type="checkbox"/><label for="toctree-checkbox-9"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
|
||
<li class="toctree-l3 has-children"><a class="reference internal" href="../../tutorials/beginners/getting_started.html">Getting Started</a><input class="toctree-checkbox" id="toctree-checkbox-10" name="toctree-checkbox-10" role="switch" type="checkbox"/><label for="toctree-checkbox-10"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
|
||
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/getting_started/content_view.html">Adding A Content View</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/getting_started/opening_files.html">Loading Content From A File</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/getting_started/cursor_position.html">Showing The Cursor Position</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/getting_started/saving_files.html">Saving The Content To A File</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/getting_started/saving_state.html">Saving The Application State</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/getting_started/adding_toasts.html">Notifying The User With Toasts</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/getting_started/dark_mode.html">Forcing The Dark Color Scheme</a></li>
|
||
</ul>
|
||
</li>
|
||
<li class="toctree-l3 has-children"><a class="reference internal" href="../../tutorials/beginners/components.html">UI components</a><input class="toctree-checkbox" id="toctree-checkbox-11" name="toctree-checkbox-11" role="switch" type="checkbox"/><label for="toctree-checkbox-11"><div class="visually-hidden">Toggle child pages in navigation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
|
||
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/window.html">Windows</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/image.html">Images</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/label.html">Labels</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/button.html">Buttons</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/box.html">Boxes</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/stack.html">Stacks</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/leaflet.html">Leaflets</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/toggle.html">Toggles</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/check_box.html">Check Boxes</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/radio_button.html">Radio Buttons</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/spin_button.html">Spin Buttons</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/link_button.html">Link Buttons</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/switch.html">Switches</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/menu_button.html">Menu Buttons</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/entry.html">Entries</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/password_entry.html">Password Entries</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/message_dialog.html">Messages</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/file_dialog.html">File Dialogs</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/spinner.html">Spinners</a></li>
|
||
<li class="toctree-l4"><a class="reference internal" href="../../tutorials/beginners/components/level_bar.html">Level Bars</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
</li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/application-id.html">Application ID</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/application.html">Using GtkApplication</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/save-state.html">Saving and Loading Window State</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/notifications.html">Using Notifications</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/themed-icons.html">Themed Icons</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/deprecations.html">Dealing With Deprecations</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/actions.html">Actions</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/menus.html">Menus</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/search-provider.html">Writing a Search Provider</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/pre-and-post-conditions.html">Pre- and Post-Conditions</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/main-contexts.html">Main Contexts</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/lists.html">Using GLib Lists</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/threading.html">Threading</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/asynchronous-programming.html">Asynchronous Programming</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/drag-and-drop.html">Drag and Drop</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/widget-templates.html">Widget Templates</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
</div>
|
||
</div>
|
||
|
||
</div>
|
||
|
||
</div>
|
||
</aside>
|
||
<main class="main">
|
||
<div class="content">
|
||
<article role="main">
|
||
<label class="toc-overlay-icon toc-content-icon" for="__toc">
|
||
<i class="icon"><svg><use href="#svg-toc"></use></svg></i>
|
||
</label>
|
||
<section id="c-coding-style">
|
||
<h1>C Coding Style<a class="headerlink" href="#c-coding-style" title="Permalink to this headline">#</a></h1>
|
||
<p>This document presents the preferred coding style for C programs in GNOME.</p>
|
||
<p>While coding style is very much a matter of taste, in GNOME we favor a coding
|
||
style that promotes consistency, readability, and maintainability.</p>
|
||
<p>We present examples of good coding style as well as examples of bad style that
|
||
is not acceptable in GNOME. Please try to submit patches that conform to GNOME’s
|
||
coding style; this indicates that you have done your homework to respect the
|
||
project’s goal of long-term maintainability. Patches with GNOME’s coding style
|
||
will also be easier to review!</p>
|
||
<div class="admonition important">
|
||
<p class="admonition-title">Important</p>
|
||
<p>This document is intended for C code. Unlike C, other programming languages
|
||
have their own official coding style recommendations; we encourage you to
|
||
follow them wherever applicable.</p>
|
||
</div>
|
||
<p>These guidelines are heavily inspired by the GTK coding style document; the
|
||
Linux kernel coding style; and the GNU coding standards. These are slight
|
||
variations of each other, with particular modifications for each project’s
|
||
particular needs and culture, and GNOME’s version is no different.</p>
|
||
<section id="the-single-most-important-rule">
|
||
<h2>The single most important rule<a class="headerlink" href="#the-single-most-important-rule" title="Permalink to this headline">#</a></h2>
|
||
<p>The single most important rule when writing code is this: <strong>check the surrounding
|
||
code and try to imitate it</strong>.</p>
|
||
<p>As a maintainer it is dismaying to receive a patch that is obviously in a
|
||
different coding style to the surrounding code. This is disrespectful, like
|
||
someone tromping into a spotlessly-clean house with muddy shoes.</p>
|
||
<p>So, whatever this document recommends, if there is already written code and you
|
||
are contributing to it, keep its current style consistent even if it is not your
|
||
favorite style.</p>
|
||
<p>Most importantly, do not make your first contribution to a project a change in
|
||
the coding style to suit your taste. That is <em>incredibly</em> disrespectful.</p>
|
||
</section>
|
||
<section id="line-width">
|
||
<h2>Line width<a class="headerlink" href="#line-width" title="Permalink to this headline">#</a></h2>
|
||
<p>Try to use lines of code between 80 and 120 characters long. This amount of text
|
||
is easy to fit in most monitors with a decent font size. Lines longer than that
|
||
become hard to read, and they mean that you should probably restructure your
|
||
code. If you have too many levels of indentation, it means that you should fix
|
||
your code anyway.</p>
|
||
</section>
|
||
<section id="indentation">
|
||
<h2>Indentation<a class="headerlink" href="#indentation" title="Permalink to this headline">#</a></h2>
|
||
<p>In general there are two preferred indentation styles for code in GNOME:</p>
|
||
<ol class="arabic simple">
|
||
<li><p>Linux Kernel style. Tabs with a length of 8 characters are used for the
|
||
indentation, with K&R brace placement:</p></li>
|
||
</ol>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">num_elements</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">foo</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">foo</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">42</span><span class="p">;</span><span class="w"></span>
|
||
|
||
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">foo</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">35</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">printf</span><span class="w"> </span><span class="p">(</span><span class="s">"Foo!"</span><span class="p">);</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">foo</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">--</span><span class="p">;</span><span class="w"></span>
|
||
<span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">printf</span><span class="w"> </span><span class="p">(</span><span class="s">"Bar!"</span><span class="p">);</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">foo</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">++</span><span class="p">;</span><span class="w"></span>
|
||
<span class="w"> </span><span class="p">}</span><span class="w"></span>
|
||
<span class="p">}</span><span class="w"></span>
|
||
</pre></div>
|
||
</div>
|
||
<ol class="arabic simple" start="2">
|
||
<li><p>GNU style. Each new level is indented by 2 spaces, braces go on a line by
|
||
themselves, and they are indented as well:</p></li>
|
||
</ol>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">num_elements</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"></span>
|
||
<span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">foo</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">foo</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">42</span><span class="p">;</span><span class="w"></span>
|
||
|
||
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">foo</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">35</span><span class="p">)</span><span class="w"></span>
|
||
<span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">printf</span><span class="w"> </span><span class="p">(</span><span class="s">"Foo!"</span><span class="p">);</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">foo</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">--</span><span class="p">;</span><span class="w"></span>
|
||
<span class="w"> </span><span class="p">}</span><span class="w"></span>
|
||
<span class="w"> </span><span class="k">else</span><span class="w"></span>
|
||
<span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">printf</span><span class="w"> </span><span class="p">(</span><span class="s">"Bar!"</span><span class="p">);</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">foo</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">++</span><span class="p">;</span><span class="w"></span>
|
||
<span class="w"> </span><span class="p">}</span><span class="w"></span>
|
||
<span class="w"> </span><span class="p">}</span><span class="w"></span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Both styles have their pros and cons. The most important things is to <em>be
|
||
consistent with the surrounding code</em>. For example, the GTK library, which is
|
||
GNOME’s widget toolkit, is written with the GNU style. Nautilus, GNOME’s file
|
||
manager, is written in Linux kernel style. Both styles are perfectly readable
|
||
and consistent when you get used to them.</p>
|
||
<p>Your first feeling when having to study or work on a piece of code that doesn’t
|
||
have your preferred indentation style may be, how shall we put it,
|
||
gut-wrenching. You should resist your inclination to reindent everything, or to
|
||
use an inconsistent style for your patch. Remember the first rule: be consistent
|
||
and respectful of that code’s customs, and your patches will have a much higher
|
||
chance of being accepted without a lot of arguing about the right indentation
|
||
style.</p>
|
||
<section id="tab-characters">
|
||
<h3>Tab characters<a class="headerlink" href="#tab-characters" title="Permalink to this headline">#</a></h3>
|
||
<p><em>Do not ever change the size of tabs in your editor</em>; leave them as 8 spaces.
|
||
Changing the size of tabs means that code that you didn’t write yourself will be
|
||
perpetually misaligned.</p>
|
||
<p>Instead, set the indentation size as appropriate for the code you are editing.
|
||
When writing in something other than Linux kernel style, you may even want to
|
||
tell your editor to automatically convert all tabs to 8 spaces, so that there is
|
||
no ambiguity about the intended amount of space.</p>
|
||
</section>
|
||
</section>
|
||
<section id="braces">
|
||
<h2>Braces<a class="headerlink" href="#braces" title="Permalink to this headline">#</a></h2>
|
||
<p>Curly braces should not be used for single statement blocks:</p>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/* valid */</span><span class="w"></span>
|
||
<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">condition</span><span class="p">)</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">single_statement</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
|
||
<span class="k">else</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">another_single_statement</span><span class="w"> </span><span class="p">(</span><span class="n">arg1</span><span class="p">);</span><span class="w"></span>
|
||
</pre></div>
|
||
</div>
|
||
<p>The “no block for single statements” rule has only four exceptions:</p>
|
||
<ol class="arabic simple">
|
||
<li><p>In GNU style, if either side of an <code class="docutils literal notranslate"><span class="pre">if</span></code>…<code class="docutils literal notranslate"><span class="pre">else</span></code> statement has braces, both
|
||
sides should, to match up indentation:</p></li>
|
||
</ol>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/* valid */</span><span class="w"></span>
|
||
<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">condition</span><span class="p">)</span><span class="w"></span>
|
||
<span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">foo</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">bar</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
|
||
<span class="w"> </span><span class="p">}</span><span class="w"></span>
|
||
<span class="k">else</span><span class="w"></span>
|
||
<span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">baz</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
|
||
<span class="w"> </span><span class="p">}</span><span class="w"></span>
|
||
</pre></div>
|
||
</div>
|
||
<ol class="arabic simple" start="2">
|
||
<li><p>If the single statement covers multiple lines, e.g. for functions with many
|
||
arguments, and it is followed by <code class="docutils literal notranslate"><span class="pre">else</span></code> or <code class="docutils literal notranslate"><span class="pre">else</span> <span class="pre">if</span></code>:</p></li>
|
||
</ol>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/* valid Linux kernel style */</span><span class="w"></span>
|
||
<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">condition</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">a_single_statement_with_many_arguments</span><span class="w"> </span><span class="p">(</span><span class="n">some_lengthy_argument</span><span class="p">,</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">another_lengthy_argument</span><span class="p">,</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">and_another_one</span><span class="p">,</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">plus_one</span><span class="p">);</span><span class="w"></span>
|
||
<span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">another_single_statement</span><span class="w"> </span><span class="p">(</span><span class="n">arg1</span><span class="p">,</span><span class="w"> </span><span class="n">arg2</span><span class="p">);</span><span class="w"></span>
|
||
|
||
<span class="cm">/* valid GNU style */</span><span class="w"></span>
|
||
<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">condition</span><span class="p">)</span><span class="w"></span>
|
||
<span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">a_single_statement_with_many_arguments</span><span class="w"> </span><span class="p">(</span><span class="n">some_lengthy_argument</span><span class="p">,</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">another_lengthy_argument</span><span class="p">,</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">and_another_one</span><span class="p">,</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">plus_one</span><span class="p">);</span><span class="w"></span>
|
||
<span class="w"> </span><span class="p">}</span><span class="w"></span>
|
||
<span class="k">else</span><span class="w"></span>
|
||
<span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">another_single_statement</span><span class="w"> </span><span class="p">(</span><span class="n">arg1</span><span class="p">,</span><span class="w"> </span><span class="n">arg2</span><span class="p">);</span><span class="w"></span>
|
||
<span class="w"> </span><span class="p">}</span><span class="w"></span>
|
||
</pre></div>
|
||
</div>
|
||
<ol class="arabic simple" start="3">
|
||
<li><p>If the condition is composed of many lines:</p></li>
|
||
</ol>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/* valid Linux kernel style */</span><span class="w"></span>
|
||
<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">condition1</span><span class="w"> </span><span class="o">||</span><span class="w"></span>
|
||
<span class="w"> </span><span class="p">(</span><span class="n">condition2</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="n">condition3</span><span class="p">)</span><span class="w"> </span><span class="o">||</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">condition4</span><span class="w"> </span><span class="o">||</span><span class="w"></span>
|
||
<span class="w"> </span><span class="p">(</span><span class="n">condition5</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="p">(</span><span class="n">condition6</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">condition7</span><span class="p">)))</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">a_single_statement</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
|
||
<span class="p">}</span><span class="w"></span>
|
||
|
||
<span class="cm">/* valid GNU style */</span><span class="w"></span>
|
||
<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">condition1</span><span class="w"> </span><span class="o">||</span><span class="w"></span>
|
||
<span class="w"> </span><span class="p">(</span><span class="n">condition2</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="n">condition3</span><span class="p">)</span><span class="w"> </span><span class="o">||</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">condition4</span><span class="w"> </span><span class="o">||</span><span class="w"></span>
|
||
<span class="w"> </span><span class="p">(</span><span class="n">condition5</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="p">(</span><span class="n">condition6</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">condition7</span><span class="p">)))</span><span class="w"></span>
|
||
<span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">a_single_statement</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
|
||
<span class="w"> </span><span class="p">}</span><span class="w"></span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>Such long conditions are usually hard to understand. A good practice is to
|
||
set the condition to a boolean variable, with a good name for that variable.
|
||
Another way is to move the long condition to a function.</p>
|
||
</div>
|
||
<ol class="arabic simple" start="4">
|
||
<li><p>Nested <code class="docutils literal notranslate"><span class="pre">if</span></code>, in which case the block should be placed on the outermost <code class="docutils literal notranslate"><span class="pre">if</span></code>:</p></li>
|
||
</ol>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/* valid Linux kernel style */</span><span class="w"></span>
|
||
<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">condition</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">another_condition</span><span class="p">)</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">single_statement</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
|
||
<span class="w"> </span><span class="k">else</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">another_single_statement</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
|
||
<span class="p">}</span><span class="w"></span>
|
||
|
||
<span class="cm">/* valid GNU style */</span><span class="w"></span>
|
||
<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">condition</span><span class="p">)</span><span class="w"></span>
|
||
<span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">another_condition</span><span class="p">)</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">single_statement</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
|
||
<span class="w"> </span><span class="k">else</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">another_single_statement</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
|
||
<span class="w"> </span><span class="p">}</span><span class="w"></span>
|
||
</pre></div>
|
||
</div>
|
||
<p>In general, new blocks should be placed on a new indentation level, like this:</p>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="kt">int</span><span class="w"> </span><span class="n">retval</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"></span>
|
||
|
||
<span class="n">statement_1</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
|
||
<span class="n">statement_2</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
|
||
|
||
<span class="p">{</span><span class="w"></span>
|
||
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">var1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">42</span><span class="p">;</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">gboolean</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">FALSE</span><span class="p">;</span><span class="w"></span>
|
||
|
||
<span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">statement_3</span><span class="w"> </span><span class="p">(</span><span class="n">var1</span><span class="p">);</span><span class="w"></span>
|
||
|
||
<span class="w"> </span><span class="n">retval</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="mi">-1</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"></span>
|
||
<span class="p">}</span><span class="w"></span>
|
||
</pre></div>
|
||
</div>
|
||
<p>While curly braces for function definitions should rest on a new line they
|
||
should not add an indentation level:</p>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/* valid Linux kernel style*/</span><span class="w"></span>
|
||
<span class="k">static</span><span class="w"> </span><span class="kt">void</span><span class="w"></span>
|
||
<span class="nf">my_function</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">argument</span><span class="p">)</span><span class="w"></span>
|
||
<span class="p">{</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">do_my_things</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
|
||
<span class="p">}</span><span class="w"></span>
|
||
|
||
<span class="cm">/* valid GNU style*/</span><span class="w"></span>
|
||
<span class="k">static</span><span class="w"> </span><span class="kt">void</span><span class="w"></span>
|
||
<span class="nf">my_function</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">argument</span><span class="p">)</span><span class="w"></span>
|
||
<span class="p">{</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">do_my_things</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
|
||
<span class="p">}</span><span class="w"></span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="conditions">
|
||
<h2>Conditions<a class="headerlink" href="#conditions" title="Permalink to this headline">#</a></h2>
|
||
<p>Do not check boolean values for equality. By using implicit comparisons, the
|
||
resulting code can be read more like conversational English. Another rationale
|
||
is that a ‘true’ value may not be necessarily equal to whatever the <code class="docutils literal notranslate"><span class="pre">TRUE</span></code>
|
||
macro uses. For example:</p>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">found</span><span class="p">)</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">do_foo</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
|
||
|
||
<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="n">found</span><span class="p">)</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">do_bar</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
|
||
</pre></div>
|
||
</div>
|
||
<p>The C language uses the value <code class="docutils literal notranslate"><span class="pre">0</span></code> for many purposes. As a numeric value, the
|
||
end of a string, a null pointer and the <code class="docutils literal notranslate"><span class="pre">FALSE</span></code> boolean. To make the code
|
||
clearer, you should write code that highlights the specific way <code class="docutils literal notranslate"><span class="pre">0</span></code> is used.
|
||
So when reading a comparison, it is possible to know the variable type. For
|
||
boolean variables, an implicit comparison is appropriate because it’s already a
|
||
logical expression. Other variable types are not logical expressions by
|
||
themselves, so an explicit comparison is better:</p>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">some_pointer</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">do_blah</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
|
||
|
||
<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">number</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">do_foo</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
|
||
|
||
<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">str</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nb">NULL</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="o">*</span><span class="n">str</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="sc">'\0'</span><span class="p">)</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">do_bar</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="functions">
|
||
<h2>Functions<a class="headerlink" href="#functions" title="Permalink to this headline">#</a></h2>
|
||
<p>Functions should be declared by placing the returned value on a separate line from the function name:</p>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="kt">void</span><span class="w"></span>
|
||
<span class="nf">my_function</span><span class="w"> </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="w"></span>
|
||
<span class="p">{</span><span class="w"></span>
|
||
<span class="w"> </span><span class="c1">// ...</span>
|
||
<span class="p">}</span><span class="w"></span>
|
||
</pre></div>
|
||
</div>
|
||
<p>The argument list must be broken into a new line for each argument, with the
|
||
argument names right aligned, taking into account pointers:</p>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="kt">void</span><span class="w"></span>
|
||
<span class="nf">my_function</span><span class="w"> </span><span class="p">(</span><span class="n">some_type_t</span><span class="w"> </span><span class="n">type</span><span class="p">,</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">another_type_t</span><span class="w"> </span><span class="o">*</span><span class="n">a_pointer</span><span class="p">,</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">double_ptr_t</span><span class="w"> </span><span class="o">**</span><span class="n">double_pointer</span><span class="p">,</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">final_type_t</span><span class="w"> </span><span class="n">another_type</span><span class="p">)</span><span class="w"></span>
|
||
<span class="p">{</span><span class="w"></span>
|
||
<span class="w"> </span><span class="c1">// ...</span>
|
||
<span class="p">}</span><span class="w"></span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="admonition tip">
|
||
<p class="admonition-title">Tip</p>
|
||
<p>If you use Emacs, you can use <code class="docutils literal notranslate"><span class="pre">M-x</span> <span class="pre">align</span></code> to do this kind of alignment
|
||
automatically. Just put the point and mark around the function’s prototype,
|
||
and invoke that command.</p>
|
||
</div>
|
||
<p>The alignment also holds when invoking a function without breaking the line
|
||
length limit:</p>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">align_function_arguments</span><span class="w"> </span><span class="p">(</span><span class="n">first_argument</span><span class="p">,</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">second_argument</span><span class="p">,</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">third_argument</span><span class="p">);</span><span class="w"></span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="whitespace">
|
||
<h2>Whitespace<a class="headerlink" href="#whitespace" title="Permalink to this headline">#</a></h2>
|
||
<p>Always put a space before an opening parenthesis but never after:</p>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">condition</span><span class="p">)</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">do_my_things</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
|
||
|
||
<span class="k">switch</span><span class="w"> </span><span class="p">(</span><span class="n">condition</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||
<span class="p">}</span><span class="w"></span>
|
||
</pre></div>
|
||
</div>
|
||
<p>When declaring a structure type use newlines to separate logical sections of the
|
||
structure:</p>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">struct</span><span class="w"> </span><span class="nc">_GtkWrapBoxPrivate</span><span class="w"></span>
|
||
<span class="p">{</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">GtkOrientation</span><span class="w"> </span><span class="n">orientation</span><span class="p">;</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">GtkWrapAllocationMode</span><span class="w"> </span><span class="n">mode</span><span class="p">;</span><span class="w"></span>
|
||
|
||
<span class="w"> </span><span class="n">GtkWrapBoxSpreading</span><span class="w"> </span><span class="n">horizontal_spreading</span><span class="p">;</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">GtkWrapBoxSpreading</span><span class="w"> </span><span class="n">vertical_spreading</span><span class="p">;</span><span class="w"></span>
|
||
|
||
<span class="w"> </span><span class="n">guint16</span><span class="w"> </span><span class="n">spacing</span><span class="p">[</span><span class="mi">2</span><span class="p">];</span><span class="w"></span>
|
||
|
||
<span class="w"> </span><span class="n">guint16</span><span class="w"> </span><span class="n">minimum_line_children</span><span class="p">;</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">guint16</span><span class="w"> </span><span class="n">natural_line_children</span><span class="p">;</span><span class="w"></span>
|
||
|
||
<span class="w"> </span><span class="n">GList</span><span class="w"> </span><span class="o">*</span><span class="n">children</span><span class="p">;</span><span class="w"></span>
|
||
<span class="p">};</span><span class="w"></span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Do <strong>not</strong> eliminate whitespace and newlines just because something would fit on
|
||
a single line:</p>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/* invalid */</span><span class="w"></span>
|
||
<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">condition</span><span class="p">)</span><span class="w"> </span><span class="n">foo</span><span class="w"> </span><span class="p">();</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="n">bar</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Do eliminate trailing whitespace on any line, preferably as a separate patch or
|
||
commit. Never use empty lines at the beginning or at the end of a file.</p>
|
||
</section>
|
||
<section id="the-switch-statement">
|
||
<h2>The <code class="docutils literal notranslate"><span class="pre">switch</span></code> statement<a class="headerlink" href="#the-switch-statement" title="Permalink to this headline">#</a></h2>
|
||
<p>A <code class="docutils literal notranslate"><span class="pre">switch</span></code> should open a block on a new indentation level, and each case
|
||
should start on the same indentation level as the curly braces, with the case
|
||
block on a new indentation level:</p>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/* valid Linux kernel style */</span><span class="w"></span>
|
||
<span class="k">switch</span><span class="w"> </span><span class="p">(</span><span class="n">condition</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||
<span class="k">case</span><span class="w"> </span><span class="no">FOO</span><span class="p">:</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">do_foo</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
|
||
<span class="w"> </span><span class="k">break</span><span class="p">;</span><span class="w"></span>
|
||
|
||
<span class="k">case</span><span class="w"> </span><span class="no">BAR</span><span class="p">:</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">do_bar</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
|
||
<span class="w"> </span><span class="k">break</span><span class="p">;</span><span class="w"></span>
|
||
<span class="p">}</span><span class="w"></span>
|
||
|
||
<span class="cm">/* valid GNU style */</span><span class="w"></span>
|
||
<span class="k">switch</span><span class="w"> </span><span class="p">(</span><span class="n">condition</span><span class="p">)</span><span class="w"></span>
|
||
<span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||
<span class="w"> </span><span class="k">case</span><span class="w"> </span><span class="no">FOO</span><span class="p">:</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">do_foo</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
|
||
<span class="w"> </span><span class="k">break</span><span class="p">;</span><span class="w"></span>
|
||
|
||
<span class="w"> </span><span class="k">case</span><span class="w"> </span><span class="no">BAR</span><span class="p">:</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">do_bar</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
|
||
<span class="w"> </span><span class="k">break</span><span class="p">;</span><span class="w"></span>
|
||
|
||
<span class="w"> </span><span class="k">default</span><span class="o">:</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">do_default</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
|
||
<span class="w"> </span><span class="p">}</span><span class="w"></span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="admonition tip">
|
||
<p class="admonition-title">Tip</p>
|
||
<p>It is preferable, though not mandatory, to separate the various cases with a
|
||
newline.</p>
|
||
</div>
|
||
<div class="admonition tip">
|
||
<p class="admonition-title">Tip</p>
|
||
<p>The <code class="docutils literal notranslate"><span class="pre">break</span></code> statement for the <code class="docutils literal notranslate"><span class="pre">default</span></code> case is not mandatory.</p>
|
||
</div>
|
||
<p>If switching over an enumerated type, a <code class="docutils literal notranslate"><span class="pre">case</span></code> statement must exist for every
|
||
member of the enumerated type. For members you do not want to handle, alias
|
||
their case statements to <code class="docutils literal notranslate"><span class="pre">default</span></code>:</p>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">switch</span><span class="w"> </span><span class="p">(</span><span class="n">enumerated_condition</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||
<span class="k">case</span><span class="w"> </span><span class="no">HANDLED_1</span><span class="p">:</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">do_foo</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
|
||
<span class="w"> </span><span class="k">break</span><span class="p">;</span><span class="w"></span>
|
||
|
||
<span class="k">case</span><span class="w"> </span><span class="no">HANDLED_2</span><span class="p">:</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">do_bar</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
|
||
<span class="w"> </span><span class="k">break</span><span class="p">;</span><span class="w"></span>
|
||
|
||
<span class="k">case</span><span class="w"> </span><span class="no">IGNORED_1</span><span class="p">:</span><span class="w"></span>
|
||
<span class="k">case</span><span class="w"> </span><span class="no">IGNORED_2</span><span class="p">:</span><span class="w"></span>
|
||
<span class="k">default</span><span class="o">:</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">do_default</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
|
||
<span class="p">}</span><span class="w"></span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="admonition tip">
|
||
<p class="admonition-title">Tip</p>
|
||
<p>If most members of the enumerated type should not be handled, consider using an
|
||
<code class="docutils literal notranslate"><span class="pre">if</span></code> … <code class="docutils literal notranslate"><span class="pre">else</span> <span class="pre">if</span></code> statement instead of a <code class="docutils literal notranslate"><span class="pre">switch</span></code>.</p>
|
||
</div>
|
||
<p>If a <code class="docutils literal notranslate"><span class="pre">case</span></code> block needs to declare new variables, the same rules as the inner
|
||
blocks apply (see above); the <code class="docutils literal notranslate"><span class="pre">break</span></code> statement should be placed outside of
|
||
the inner block:</p>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">switch</span><span class="w"> </span><span class="p">(</span><span class="n">condition</span><span class="p">)</span><span class="w"></span>
|
||
<span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||
<span class="w"> </span><span class="k">case</span><span class="w"> </span><span class="no">FOO</span><span class="p">:</span><span class="w"></span>
|
||
<span class="w"> </span><span class="p">{</span><span class="w"></span>
|
||
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">foo</span><span class="p">;</span><span class="w"></span>
|
||
|
||
<span class="w"> </span><span class="n">foo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">do_foo</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
|
||
<span class="w"> </span><span class="p">}</span><span class="w"></span>
|
||
<span class="w"> </span><span class="k">break</span><span class="p">;</span><span class="w"></span>
|
||
|
||
<span class="w"> </span><span class="c1">// ...</span>
|
||
<span class="w"> </span><span class="p">}</span><span class="w"></span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="header-files">
|
||
<h2>Header files<a class="headerlink" href="#header-files" title="Permalink to this headline">#</a></h2>
|
||
<p>The only major rule for headers is that the function definitions should be
|
||
vertically aligned in three columns:</p>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">return_type</span><span class="w"> </span><span class="nf">function_name</span><span class="w"> </span><span class="p">(</span><span class="n">type</span><span class="w"> </span><span class="n">argument</span><span class="p">,</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">type</span><span class="w"> </span><span class="n">argument</span><span class="p">,</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">type</span><span class="w"> </span><span class="n">argument</span><span class="p">);</span><span class="w"></span>
|
||
</pre></div>
|
||
</div>
|
||
<p>The maximum width of each column is given by the longest element in the column:</p>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="kt">void</span><span class="w"> </span><span class="nf">gtk_type_set_property</span><span class="w"> </span><span class="p">(</span><span class="n">GtkType</span><span class="w"> </span><span class="o">*</span><span class="n">type</span><span class="p">,</span><span class="w"></span>
|
||
<span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">value</span><span class="p">,</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">GError</span><span class="w"> </span><span class="o">**</span><span class="n">error</span><span class="p">);</span><span class="w"></span>
|
||
<span class="k">const</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="nf">gtk_type_get_property</span><span class="w"> </span><span class="p">(</span><span class="n">GtkType</span><span class="w"> </span><span class="o">*</span><span class="n">type</span><span class="p">);</span><span class="w"></span>
|
||
</pre></div>
|
||
</div>
|
||
<p>It is also possible to align the columns to the next tab, to avoid having to
|
||
reformat headers every time you add a new function:</p>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="kt">void</span><span class="w"> </span><span class="nf">gtk_type_set_prop</span><span class="w"> </span><span class="p">(</span><span class="n">GtkType</span><span class="w"> </span><span class="o">*</span><span class="n">type</span><span class="p">,</span><span class="w"></span>
|
||
<span class="w"> </span><span class="kt">float</span><span class="w"> </span><span class="n">value</span><span class="p">);</span><span class="w"></span>
|
||
<span class="kt">float</span><span class="w"> </span><span class="nf">gtk_type_get_prop</span><span class="w"> </span><span class="p">(</span><span class="n">GtkType</span><span class="w"> </span><span class="o">*</span><span class="n">type</span><span class="p">);</span><span class="w"></span>
|
||
<span class="kt">int</span><span class="w"> </span><span class="nf">gtk_type_update_foobar</span><span class="w"> </span><span class="p">(</span><span class="n">GtkType</span><span class="w"> </span><span class="o">*</span><span class="n">type</span><span class="p">);</span><span class="w"></span>
|
||
</pre></div>
|
||
</div>
|
||
<p>If you are creating a public library, try to export a single public header file
|
||
that in turn includes all the smaller header files into it. This is so that
|
||
public headers are never included directly; rather a single include is used in
|
||
applications. For example, GTK uses the following in its header files that
|
||
should not be included directly by applications:</p>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="c1">// The __GTK_H_INSIDE__ symbol is defined in the gtk.h header</span>
|
||
<span class="c1">// The GTK_COMPILATION symbol is defined only when compiling</span>
|
||
<span class="c1">// GTK itself</span>
|
||
<span class="cp">#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)</span>
|
||
<span class="cp">#error "Only <gtk/gtk.h> can be included directly."</span>
|
||
<span class="cp">#endif</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>For libraries, all headers should have inclusion guards (for internal usage) and
|
||
C++ guards. These provide the extern “C” magic that C++ requires to include
|
||
plain C headers:</p>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cp">#pragma once</span>
|
||
|
||
<span class="cp">#include</span><span class="w"> </span><span class="cpf"><gtk/gtk.h></span><span class="cp"></span>
|
||
|
||
<span class="n">G_BEGIN_DECLS</span><span class="w"></span>
|
||
|
||
<span class="c1">// ...</span>
|
||
|
||
<span class="n">G_END_DECLS</span><span class="w"></span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="admonition tip">
|
||
<p class="admonition-title">Tip</p>
|
||
<p>Instead of the <code class="docutils literal notranslate"><span class="pre">once</span></code> pragma you can use an explicit symbol-based
|
||
inclusion guard: <code class="docutils literal notranslate"><span class="pre">#ifndef</span> <span class="pre">FILE_H</span> <span class="pre">#define</span> <span class="pre">FILE_H</span> <span class="pre">...</span> <span class="pre">#endif</span></code>.</p>
|
||
</div>
|
||
</section>
|
||
<section id="gobject-classes">
|
||
<h2>GObject classes<a class="headerlink" href="#gobject-classes" title="Permalink to this headline">#</a></h2>
|
||
<p>GObject class definitions and implementations require some additional coding
|
||
style notices, and should always be correctly namespaced.</p>
|
||
<p>Type declarations should be placed at the beginning of the file:</p>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">typedef</span><span class="w"> </span><span class="k">struct</span><span class="w"> </span><span class="nc">_GtkBoxedStruct</span><span class="w"> </span><span class="n">GtkBoxedStruct</span><span class="p">;</span><span class="w"></span>
|
||
<span class="k">typedef</span><span class="w"> </span><span class="k">struct</span><span class="w"> </span><span class="nc">_GtkMoreBoxedStruct</span><span class="w"> </span><span class="n">GtkMoreBoxedStruct</span><span class="p">;</span><span class="w"></span>
|
||
</pre></div>
|
||
</div>
|
||
<p>This includes enumeration types:</p>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">typedef</span><span class="w"> </span><span class="k">enum</span><span class="w"></span>
|
||
<span class="p">{</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT</span><span class="p">,</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH</span><span class="w"></span>
|
||
<span class="p">}</span><span class="w"> </span><span class="n">GtkSizeRequestMode</span><span class="p">;</span><span class="w"></span>
|
||
</pre></div>
|
||
</div>
|
||
<p>And callback types:</p>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">typedef</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="p">(</span><span class="o">*</span><span class="w"> </span><span class="n">GtkCallback</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="n">GtkWidget</span><span class="w"> </span><span class="o">*</span><span class="n">widget</span><span class="p">,</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">gpointer</span><span class="w"> </span><span class="n">user_data</span><span class="p">);</span><span class="w"></span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Instance structures should be declared using the <code class="docutils literal notranslate"><span class="pre">G_DECLARE_FINAL_TYPE()</span></code> or
|
||
<code class="docutils literal notranslate"><span class="pre">G_DECLARE_DERIVABLE_TYPE()</span></code> macros:</p>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cp">#define GTK_TYPE_FOO (gtk_foo_get_type ())</span>
|
||
<span class="n">G_DECLARE_FINAL_TYPE</span><span class="w"> </span><span class="p">(</span><span class="n">GtkFoo</span><span class="p">,</span><span class="w"> </span><span class="n">gtk_foo</span><span class="p">,</span><span class="w"> </span><span class="n">GTK</span><span class="p">,</span><span class="w"> </span><span class="n">FOO</span><span class="p">,</span><span class="w"> </span><span class="n">GtkWidget</span><span class="p">)</span><span class="w"></span>
|
||
</pre></div>
|
||
</div>
|
||
<p>For final types, private data can be stored in the object struct, which should
|
||
be defined in the C file:</p>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">struct</span><span class="w"> </span><span class="nc">_GtkFoo</span><span class="w"></span>
|
||
<span class="p">{</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">GObject</span><span class="w"> </span><span class="n">parent_instance</span><span class="p">;</span><span class="w"></span>
|
||
|
||
<span class="w"> </span><span class="n">guint</span><span class="w"> </span><span class="n">private_data</span><span class="p">;</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">gpointer</span><span class="w"> </span><span class="n">more_private_data</span><span class="p">;</span><span class="w"></span>
|
||
<span class="p">};</span><span class="w"></span>
|
||
</pre></div>
|
||
</div>
|
||
<p>For derivable types, private data must be stored in a private struct in the C
|
||
file, configured using <code class="docutils literal notranslate"><span class="pre">G_DEFINE_TYPE_WITH_PRIVATE()</span></code> and accessed using the
|
||
generated <code class="docutils literal notranslate"><span class="pre">_get_instance_private()</span></code> function:</p>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cp">#define GTK_TYPE_FOO gtk_foo_get_type()</span>
|
||
<span class="n">G_DECLARE_DERIVABLE_TYPE</span><span class="w"> </span><span class="p">(</span><span class="n">GtkFoo</span><span class="p">,</span><span class="w"> </span><span class="n">gtk_foo</span><span class="p">,</span><span class="w"> </span><span class="n">GTK</span><span class="p">,</span><span class="w"> </span><span class="n">FOO</span><span class="p">,</span><span class="w"> </span><span class="n">GtkWidget</span><span class="p">)</span><span class="w"></span>
|
||
|
||
<span class="k">struct</span><span class="w"> </span><span class="nc">_GtkFooClass</span><span class="w"></span>
|
||
<span class="p">{</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">GtkWidgetClass</span><span class="w"> </span><span class="n">parent_class</span><span class="p">;</span><span class="w"></span>
|
||
|
||
<span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="p">(</span><span class="o">*</span><span class="w"> </span><span class="n">handle_frob</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="n">GtkFrobber</span><span class="w"> </span><span class="o">*</span><span class="n">frobber</span><span class="p">,</span><span class="w"></span>
|
||
<span class="w"> </span><span class="n">guint</span><span class="w"> </span><span class="n">n_frobs</span><span class="p">);</span><span class="w"></span>
|
||
|
||
<span class="w"> </span><span class="c1">// Padding, for ABI compatible expansion of the class</span>
|
||
<span class="w"> </span><span class="n">gpointer</span><span class="w"> </span><span class="n">padding</span><span class="p">[</span><span class="mi">12</span><span class="p">];</span><span class="w"></span>
|
||
<span class="p">};</span><span class="w"></span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Always use the <code class="docutils literal notranslate"><span class="pre">G_DEFINE_TYPE()</span></code>, <code class="docutils literal notranslate"><span class="pre">G_DEFINE_TYPE_WITH_PRIVATE()</span></code>, and
|
||
<code class="docutils literal notranslate"><span class="pre">G_DEFINE_TYPE_WITH_CODE()</span></code> macros, or their abstract variants
|
||
<code class="docutils literal notranslate"><span class="pre">G_DEFINE_ABSTRACT_TYPE()</span></code>, <code class="docutils literal notranslate"><span class="pre">G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE()</span></code>, and
|
||
<code class="docutils literal notranslate"><span class="pre">G_DEFINE_ABSTRACT_TYPE_WITH_CODE()</span></code>; also, use the similar macros for
|
||
defining interfaces and boxed types.</p>
|
||
<p>Interfaces should be declared using the <code class="docutils literal notranslate"><span class="pre">G_DECLARE_INTERFACE()</span></code> macro:</p>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cp">#define GTK_TYPE_FOOABLE gtk_fooable_get_type()</span>
|
||
<span class="n">G_DECLARE_INTERFACE</span><span class="w"> </span><span class="p">(</span><span class="n">GtkFooable</span><span class="p">,</span><span class="w"> </span><span class="n">gtk_fooable</span><span class="p">,</span><span class="w"> </span><span class="n">GTK</span><span class="p">,</span><span class="w"> </span><span class="n">FOOABLE</span><span class="p">,</span><span class="w"> </span><span class="n">GObject</span><span class="p">)</span><span class="w"></span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="memory-allocation">
|
||
<h2>Memory allocation<a class="headerlink" href="#memory-allocation" title="Permalink to this headline">#</a></h2>
|
||
<p>When dynamically allocating data on the heap use <code class="docutils literal notranslate"><span class="pre">g_new()</span></code>.</p>
|
||
<p>Public structure types should always be returned after being zero-ed, either
|
||
explicitly for each member, or by using <code class="docutils literal notranslate"><span class="pre">g_new0()</span></code>.</p>
|
||
</section>
|
||
<section id="macros">
|
||
<h2>Macros<a class="headerlink" href="#macros" title="Permalink to this headline">#</a></h2>
|
||
<p>Try to avoid private macros unless strictly necessary. Remember to <code class="docutils literal notranslate"><span class="pre">#undef</span></code>
|
||
them at the end of a block or a series of functions needing them.</p>
|
||
<p>Inline functions are usually preferable to private macros.</p>
|
||
<p>Public macros should not be used unless they evaluate to a constant.</p>
|
||
</section>
|
||
<section id="public-api">
|
||
<h2>Public API<a class="headerlink" href="#public-api" title="Permalink to this headline">#</a></h2>
|
||
<p>Avoid exporting variables as public API, since this is cumbersome on some
|
||
platforms. It is always preferable to add getters and setters instead. Also,
|
||
beware global variables in general.</p>
|
||
<p>To avoid exposing private API in the shared library, it is recommended to
|
||
default to a <code class="docutils literal notranslate"><span class="pre">hidden</span></code> symbol visibility, and explicitly annotate public
|
||
symbols in the header file.</p>
|
||
<p>Non-exported functions that are only needed in one source file should be
|
||
declared <code class="docutils literal notranslate"><span class="pre">static</span></code> to that file.</p>
|
||
</section>
|
||
</section>
|
||
|
||
</article>
|
||
<footer>
|
||
|
||
<div class="related-pages">
|
||
<a class="next-page" href="memory-management.html">
|
||
<div class="page-info">
|
||
<div class="context">
|
||
<span>Next</span>
|
||
</div>
|
||
<div class="title">Managing Memory</div>
|
||
</div>
|
||
<svg><use href="#svg-arrow-right"></use></svg>
|
||
</a>
|
||
<a class="prev-page" href="../programming.html">
|
||
<svg><use href="#svg-arrow-right"></use></svg>
|
||
<div class="page-info">
|
||
<div class="context">
|
||
<span>Previous</span>
|
||
</div>
|
||
|
||
<div class="title">Programming Guidelines</div>
|
||
|
||
</div>
|
||
</a>
|
||
</div>
|
||
|
||
</footer>
|
||
|
||
</div>
|
||
<aside class="toc-drawer">
|
||
|
||
|
||
<div class="toc-sticky toc-scroll">
|
||
<div class="toc-title-container">
|
||
<span class="toc-title">
|
||
Contents
|
||
</span>
|
||
</div>
|
||
<div class="toc-tree-container">
|
||
<div class="toc-tree">
|
||
<ul>
|
||
<li><a class="reference internal" href="#">C Coding Style</a><ul>
|
||
<li><a class="reference internal" href="#the-single-most-important-rule">The single most important rule</a></li>
|
||
<li><a class="reference internal" href="#line-width">Line width</a></li>
|
||
<li><a class="reference internal" href="#indentation">Indentation</a><ul>
|
||
<li><a class="reference internal" href="#tab-characters">Tab characters</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#braces">Braces</a></li>
|
||
<li><a class="reference internal" href="#conditions">Conditions</a></li>
|
||
<li><a class="reference internal" href="#functions">Functions</a></li>
|
||
<li><a class="reference internal" href="#whitespace">Whitespace</a></li>
|
||
<li><a class="reference internal" href="#the-switch-statement">The <code class="docutils literal notranslate"><span class="pre">switch</span></code> statement</a></li>
|
||
<li><a class="reference internal" href="#header-files">Header files</a></li>
|
||
<li><a class="reference internal" href="#gobject-classes">GObject classes</a></li>
|
||
<li><a class="reference internal" href="#memory-allocation">Memory allocation</a></li>
|
||
<li><a class="reference internal" href="#macros">Macros</a></li>
|
||
<li><a class="reference internal" href="#public-api">Public API</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
|
||
</aside>
|
||
</main>
|
||
</div><script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
|
||
<script src="../../_static/jquery.js"></script>
|
||
<script src="../../_static/underscore.js"></script>
|
||
<script src="../../_static/doctools.js"></script>
|
||
<script src="../../_static/scripts/furo.js"></script>
|
||
</body>
|
||
</html> |