# Poll: Preferred style for tutorial code listings

**URL:** <https://fortran-lang.discourse.group/t/poll-preferred-style-for-tutorial-code-listings/147>\
**Category:** Poll\
**Created:** [June 26, 2020, 8:42pm UTC](https://fortran-lang.discourse.group/t/poll-preferred-style-for-tutorial-code-listings/147 "2020-06-26T20:42:52Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![gnikit](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/gnikit/32/1213_2.png) [@gnikit](https://fortran-lang.discourse.group/u/gnikit)\
**Post date:** [August 24, 2022, 11:12pm UTC](https://fortran-lang.discourse.group/t/poll-preferred-style-for-tutorial-code-listings/147/22 "2022-08-24T23:12:15Z")

</div>

My personal preference is to always separate `end` from the rest of the statement, makes reading statements easier.

---

<div class="post-metadata">

**Author:** ![rgba](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/rgba/32/2115_2.png) [@rgba](https://fortran-lang.discourse.group/u/rgba)\
**Post date:** [August 25, 2022, 1:13am UTC](https://fortran-lang.discourse.group/t/poll-preferred-style-for-tutorial-code-listings/147/23 "2022-08-25T01:13:06Z")

</div>

I think for one time I completely agree (and giving the high degree of subjectivity of the matter I’m even a little worried about that 🙂 )

Really, this all resonates too well with what I see in the codebase I work what I struggled with while debugging and, even more specifically, how much my weird mind values the 3 space indent making the body of a do or if align with the iterator/condition.

---

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [August 25, 2022, 5:29am UTC](https://fortran-lang.discourse.group/t/poll-preferred-style-for-tutorial-code-listings/147/24 "2022-08-25T05:29:41Z")

</div>

As several people have already mentioned, the choice in these matters is rather subjective. The most important thing as far as I am concerned is that the style is consistently used. At the very least within one example. I have seen code, ugh, where the indentation varied from none (or even negative) to four spaces within the range of a single screenful. That plays havoc on my ability to comfortably read it.

---

<div class="post-metadata">

**Author:** ![Carltoffel](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/carltoffel/32/1680_2.png) [@Carltoffel](https://fortran-lang.discourse.group/u/Carltoffel)\
**Post date:** [August 25, 2022, 6:14am UTC](https://fortran-lang.discourse.group/t/poll-preferred-style-for-tutorial-code-listings/147/25 "2022-08-25T06:14:37Z")

</div>

While I agree that a four spaces indent could be too much for deep nesting, I still use four spaces because it encourages me to avoid deep nesting, which could be considered unclean.  
With two a two spaces indent I have problems to recognize which lines are aligned, if the nesting is too deep.

---

<div class="post-metadata">

**Author:** ![awvwgk](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/awvwgk/32/154_2.png) [@awvwgk](https://fortran-lang.discourse.group/u/awvwgk)\
**Post date:** [August 25, 2022, 6:48am UTC](https://fortran-lang.discourse.group/t/poll-preferred-style-for-tutorial-code-listings/147/26 "2022-08-25T06:48:44Z")

</div>

A possible but (un)popular choice between four and two space indentation is to use three, which at least for me brings the right balance between too deep indentation and being to cluttered. Possibly due to binary counting the three is usually not considered.

---

<div class="post-metadata">

**Author:** ![egio](https://avatars.discourse-cdn.com/v4/letter/e/dbc845/32.png) [@egio](https://fortran-lang.discourse.group/u/egio)\
**Post date:** [August 25, 2022, 7:21am UTC](https://fortran-lang.discourse.group/t/poll-preferred-style-for-tutorial-code-listings/147/27 "2022-08-25T07:21:38Z")

</div>

Three spaces are fine for fixed form.

---

<div class="post-metadata">

**Author:** ![Pap](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pap/32/1305_2.png) [@Pap](https://fortran-lang.discourse.group/u/Pap)\
**Post date:** [August 25, 2022, 7:49am UTC](https://fortran-lang.discourse.group/t/poll-preferred-style-for-tutorial-code-listings/147/28 "2022-08-25T07:49:55Z")

</div>

Nice poll, which I discovered just now. Many people don’t realize a good, constant programming style is essential.

1. snake\_case for variables, and with enough letters to tell what it is right away. There is nothing worse than trying to remember what that variable `xy` is, in a code you wrote yourself six months ago and you probably forgot things since then. Those of you old enough to have programming experience in 8-bit computers of the 80s (when only the first two letters were significant) know exactly what I’m talking about.
2. camelCase for procedures. PascalCase is better, but I save it for library procedures. I saw this convention somewhere years ago and adopted it. You can still read procedure names easily and they clearly distinguish themselves from snake\_case variables.
3. Two spaces for indentation is optimal. I usually see four spaces in other people’s programs, presumably because many editors/viewers default Tab space to four. Also, indenting the body of a procedure (as C programmers often do) serves no purpose other than wasting horizontal space.
4. End statements should have spaces for better readbility. Not only that but `end subroutine foo` is better than `end subroutine` (according to the F standard the latter is actually treated as a syntax error).

(1) and (2) above are interchangeable. Either use snake\_case for variables and camel/Pascal case for procedures or vice versa; both are good enough. Mixing them, however, is a masochistic act - you just pay additional time to figure out if `go_figure(x)` is a function call or a vector element.

A problem with capitalization is that Fortran doesn’t care about it and you can easily type camelcase instead of camelCase without realizing it for ages. There should be a compiler flag (perhaps not enabled by default) to treat those two as different. So far the only compiler I’ve seen that does that is the old SunStudio Fortran compiler, later on included in Oracle Developer Studio, and now a dormant/abandoned project.

Lastly, a joke I’ve seen somewhere years ago: _Real programmers never indent their code, never name their variables in a sane way, and never add comments. It was hard to write the code, it should be hard to understand it_. 😆

---

<div class="post-metadata">

**Author:** ![zoziha](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/zoziha/32/582_2.png) [@zoziha](https://fortran-lang.discourse.group/u/zoziha)\
**Post date:** [August 25, 2022, 8:51am UTC](https://fortran-lang.discourse.group/t/poll-preferred-style-for-tutorial-code-listings/147/29 "2022-08-25T08:51:19Z")

</div>

For Chinese programmers, comments are often in Chinese, and Chinese characters generally occupy 2 words wide (as well as Japanese/Korean), so I believe Chinese programmers will be more inclined to indent 4 spaces, or the priority is 4-space \> 2-space \> 3-space.

[Previous page](https://fortran-lang.discourse.group/t/poll-preferred-style-for-tutorial-code-listings/147.md?page=1)
