# Avoid 'auto' in tutorials

**URL:** <https://forum.juce.com/t/avoid-auto-in-tutorials/37749>\
**Category:** General JUCE discussion\
**Created:** [February 24, 2020, 9:57pm UTC](https://forum.juce.com/t/avoid-auto-in-tutorials/37749 "2020-02-24T21:57:11Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jos](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jos/32/2096_2.png) [@jos](https://forum.juce.com/u/jos)\
**Post date:** [February 24, 2020, 9:57pm UTC](https://forum.juce.com/t/avoid-auto-in-tutorials/37749/1 "2020-02-24T21:57:11Z")

</div>

I would like to advocate _not_ using ‘auto’ in tutorial code examples. While convenient for the programmer, the learner would like to know the type. It gives a better idea of what the returned value contains, and facilitates class-reference lookup and such.

---

<div class="post-metadata">

**Author:** ![Mrugalla](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mrugalla/32/13092_2.png) [@Mrugalla](https://forum.juce.com/u/Mrugalla)\
**Post date:** [February 24, 2020, 11:22pm UTC](https://forum.juce.com/t/avoid-auto-in-tutorials/37749/2 "2020-02-24T23:22:05Z")

</div>

i think so too. auto is cool and all, but it just makes everything so unreadable. sometimes when i hover over some of these variables it doesn’t even show me what it is. also some older juce tutorials should be updated to follow the current juce standard. i always have to ask around because some weird stuff doesn’t work. doesn’t anyone (who has more knowledge than me) feel responsible for them or so?

---

<div class="post-metadata">

**Author:** ![rMidi](https://avatars.discourse-cdn.com/v4/letter/r/ee59a6/32.png) [@rMidi](https://forum.juce.com/u/rMidi)\
**Post date:** [February 25, 2020, 1:06am UTC](https://forum.juce.com/t/avoid-auto-in-tutorials/37749/3 "2020-02-25T01:06:12Z")

</div>

Convenient for ‘the programmer’ yes.  
Convenient for another programmer definitely not! It is also a maintenance nightmare.

I would think such a thing would be frowned upon though by typical JUCE developers.

---

<div class="post-metadata">

**Author:** ![Toddler-Boy](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/toddler-boy/32/2007_2.png) [@Toddler-Boy](https://forum.juce.com/u/Toddler-Boy)\
**Post date:** [February 25, 2020, 1:26am UTC](https://forum.juce.com/t/avoid-auto-in-tutorials/37749/4 "2020-02-25T01:26:34Z")

</div>

Sorry, but I disagree. Even tutorials should use best-practices whenever possible, as not to teach beginners the “wrong” stuff.

auto is a very good thing. In 90% of the cases you shouldn’t even need to know the return value, as you just pass it on to another function etc.

In the few cases you actually need to know, modern IDEs can always tell you.

There is a reason why even VERY experienced developers preach the “Almost always auto” mantra:

> **[GotW #94 Solution: AAA Style (Almost Always Auto)](https://herbsutter.com/2013/08/12/gotw-94-solution-aaa-style-almost-always-auto/)**
>
> Toward correct-by-default, efficient-by-default, and pitfall-free-by-default variable declarations, using “AAA style”… where “triple-A” is both a mnemonic and an evaluation …

---

<div class="post-metadata">

**Author:** ![Mrugalla](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mrugalla/32/13092_2.png) [@Mrugalla](https://forum.juce.com/u/Mrugalla)\
**Post date:** [February 25, 2020, 7:01am UTC](https://forum.juce.com/t/avoid-auto-in-tutorials/37749/5 "2020-02-25T07:01:18Z")

</div>

for learning purposes it would be cool to directly see all return types. at least for all tutorials that are marked as “beginner”. then in the advanced stuff juce could be like: “ok now that you know how things work around here, check out how convenient auto is!”

---

<div class="post-metadata">

**Author:** ![eyalamir](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/eyalamir/32/2978_2.png) [@eyalamir](https://forum.juce.com/u/eyalamir)\
**Post date:** [February 25, 2020, 7:33am UTC](https://forum.juce.com/t/avoid-auto-in-tutorials/37749/6 "2020-02-25T07:33:01Z")

</div>

I’m also pro auto - to me it makes the code much more readable and makes reader focus more on variable names and semantics (by reference or by value) which I care more about than actual types - unless dealing with math and numbers.

P.S. if your IDE can’t show you the return type on hover you might want to switch to a good one. 🙂

---

<div class="post-metadata">

**Author:** ![eyalamir](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/eyalamir/32/2978_2.png) [@eyalamir](https://forum.juce.com/u/eyalamir)\
**Post date:** [February 25, 2020, 7:47am UTC](https://forum.juce.com/t/avoid-auto-in-tutorials/37749/7 "2020-02-25T07:47:14Z")

</div>

Also, it’s important to mention that auto can’t exist without something you can ‘hover’ over and ‘go to definition’ on right next to it, and that expression should be what we focus on in terms of clarity.

For example in the Processing Audio Input tutorial:

> auto\* outBuffer = bufferToFill.[buffer](https://docs.juce.com/master/structAudioSourceChannelInfo.html#a2e5c55518a161daf6d36502e1db9f148) → [getWritePointer](https://docs.juce.com/master/classAudioBuffer.html#a909904fa6e89008f43c83d5375c160dc) (channel, bufferToFill.[startSample](https://docs.juce.com/master/structAudioSourceChannelInfo.html#a42e88ccc05d4893015e2415785390259));

Even if I was told in code this is a float\* and not auto\*. Does a float\* reveal all I need to know about this code? Probably not… I’ll need to check what getWritePointer() does, otherwise I will not be able to use this pointer correctly.

---

<div class="post-metadata">

**Author:** ![Toddler-Boy](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/toddler-boy/32/2007_2.png) [@Toddler-Boy](https://forum.juce.com/u/Toddler-Boy)\
**Post date:** [February 25, 2020, 7:56am UTC](https://forum.juce.com/t/avoid-auto-in-tutorials/37749/8 "2020-02-25T07:56:18Z")

</div>

Even the beginners tutorials are aimed at Juce beginners, not C++ beginners. I think being proficient in C++ is a requirement to even start with Juce.

Juce is way too complex and some of its concepts too hard to understand for C++ beginners.

If auto is too much for you, then Juce will be way too hard for you too. Learn to walk before you run.

---

<div class="post-metadata">

**Author:** ![gavinburke](https://avatars.discourse-cdn.com/v4/letter/g/e19adc/32.png) [@gavinburke](https://forum.juce.com/u/gavinburke)\
**Post date:** [February 25, 2020, 8:59am UTC](https://forum.juce.com/t/avoid-auto-in-tutorials/37749/9 "2020-02-25T08:59:10Z")

</div>

For me personally, it is just an annoyance and the effort involved in switching to auto consistently is not worth any gains I can perceive. Worst case you end up with some mixed use, which is what we have in JUCE now.

Also agree with @rMidi, it makes the code harder to scan and see what is happening fast.

I’d prefer is people would just learn RUST and stop messing around with C++ and shoe horning in these features. It all comes off a bit like re-arranging the deck chairs on the Titanic.

---

<div class="post-metadata">

**Author:** ![cesare](https://avatars.discourse-cdn.com/v4/letter/c/dfb087/32.png) [@cesare](https://forum.juce.com/u/cesare)\
**Post date:** [February 25, 2020, 9:06am UTC](https://forum.juce.com/t/avoid-auto-in-tutorials/37749/10 "2020-02-25T09:06:28Z")

</div>

I think the OP has a totally valid point, as often tutorial code (like code reviews) are not done in a sensible IDE, so you can’t lean on the IDE for hover over help with types.

I’m pretty certain thought that if the type is unclear, probably the best think you could do would be to name the variable and methods more descriptively to make the type ‘pop’ a bit better.

C++ isn’t proscriptive about such things, and there are arguments for and against various aspects of the language. If there was a clear winner, we’d all be doing it (and there are examples of this, such as the adoption of smart pointers) but for auto the patterns are still being found as we’re only 9 years into that particular experiment.

---

<div class="post-metadata">

**Author:** ![jos](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jos/32/2096_2.png) [@jos](https://forum.juce.com/u/jos)\
**Post date:** [February 25, 2020, 9:22am UTC](https://forum.juce.com/t/avoid-auto-in-tutorials/37749/11 "2020-02-25T09:22:32Z")

</div>

I like the idea of giving the class name in the variable name, e.g.,

`auto complexBagClass = something.getComplexBagClass();`

One can also specify units and other aspects of type in the name, e.g.,

`auto integerSamplingRateInHz = somethingReturningThat();`

In the worst case, a comment can be used:

` auto bag = something.getBag(); // ComplexBagClass`

It’s not reasonable to expect an IDE to be present, as tutorials are often read in HTML format, or PDF, etc., such as on a tablet. It’s learning time, not programming time. Also, not everyone adapts code in an IDE. Some of us use powerful old-school text editors.

---

<div class="post-metadata">

**Author:** ![eyalamir](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/eyalamir/32/2978_2.png) [@eyalamir](https://forum.juce.com/u/eyalamir)\
**Post date:** [February 25, 2020, 9:38am UTC](https://forum.juce.com/t/avoid-auto-in-tutorials/37749/12 "2020-02-25T09:38:32Z")

</div>

Usually, the function should usually be clear enough about the type name, but a good variable name would also help.

Not to mention - usually even without an IDE you’re going to have to look at the getComplexBagClass() anyway - is it a plain getter? Is it returning a reference counted object like Image or ValueTree does? do I need to worry because it’s a copy and not a reference?

If it’s a plain float or int, sure - the type name is valuable because you know all about it. But a custom Type is not something that’s obvious anyway, you’re gonna have to look at what it is, even if you’re using text search+replace and not an IDE…

The type name itself here wouldn’t provide much info you won’t have to look at anyway.

---

<div class="post-metadata">

**Author:** ![asimilon](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/asimilon/32/4235_2.png) [@asimilon](https://forum.juce.com/u/asimilon)\
**Post date:** [February 25, 2020, 9:41am UTC](https://forum.juce.com/t/avoid-auto-in-tutorials/37749/13 "2020-02-25T09:41:31Z")

</div>

Just last night I just came across a perfect example of where `auto` makes _a lot_ of sense:

Updating my plugins to use double precision `processBlock`: all the code in my later plugins that used `auto` were easy to update, just refactored my original float `processBlock` as a template and create two versions of `processBlock` that call the templated original appropriately. With the very first plugin I created (where I was just getting back into C++ programming, having not really touched it since 1995) I had all manner of compiler errors where I had to change `float*` assignments into `auto*` assignments etc. etc. So it took about 10x longer to change that one than the others.

---

<div class="post-metadata">

**Author:** ![jos](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jos/32/2096_2.png) [@jos](https://forum.juce.com/u/jos)\
**Post date:** [February 25, 2020, 9:42am UTC](https://forum.juce.com/t/avoid-auto-in-tutorials/37749/14 "2020-02-25T09:42:31Z")

</div>

The main use for the type name is the ability to look it up on the spot (not in an IDE, but in a browser, say). I actually use the suffix P for pointers and R for references, so my variable names tell me what I need to know along that dimension as well.

---

<div class="post-metadata">

**Author:** ![eyalamir](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/eyalamir/32/2978_2.png) [@eyalamir](https://forum.juce.com/u/eyalamir)\
**Post date:** [February 25, 2020, 9:57am UTC](https://forum.juce.com/t/avoid-auto-in-tutorials/37749/15 "2020-02-25T09:57:54Z")

</div>

Yeah, I hear what you’re saying.  
You can still look it up though. The right side of auto has (by definition) to be text searchable. And the result of that is really what you want - if it’s a getter for a custom type you can search for that one too, so nothing is ‘hidden’ from you by using that.

In fact - auto forces that since it can’t exist without the right side representing a valid expression.

So to me, even if it forces one more search on IDE-less people, it makes you focus on what the variable does and semantics, which as a reader I care more about usually, since I probably don’t know the types anyway. If you already know the types you probably know what the getters return…

---

<div class="post-metadata">

**Author:** ![mathieudemange](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mathieudemange/32/13333_2.png) [@mathieudemange](https://forum.juce.com/u/mathieudemange)\
**Post date:** [February 25, 2020, 12:23pm UTC](https://forum.juce.com/t/avoid-auto-in-tutorials/37749/16 "2020-02-25T12:23:36Z")

</div>

Maybe the tutorials could have extra comments that explain the involved types behind `auto`?

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [February 25, 2020, 12:36pm UTC](https://forum.juce.com/t/avoid-auto-in-tutorials/37749/17 "2020-02-25T12:36:59Z")

</div>

> [@jos](#):
>
> I actually use the suffix P for pointers and R for references, so my variable names tell me what I need to know along that dimension as well.

This is a very common practice, and I think it is actually hazardous. During development, the type is often changed in a refactoring. So later one decides, that reference should be able to become nullptr, let me change it to a pointer, just to see if it works… oh it works, let me check that in and move on. The type has changed, but the name hasn’t. If one was to rely on the name, they are doomed. In a matter of seconds you left a trapdoor for future programmers or even yourself.

Even worse, if you convey if something is static in the name (or you rely in the absence of a hint that it is not)

The only rule to follow is: descriptive variable names. The second rule: the wider the scope, the more descriptive the variable name.

For tutorials I agree, adding comments for cases that are not obvious makes sense. But I didn’t find the juce tutorials lacking comments.  
But then, I am not a beginner…

TL;DR: show best practices in tutorials and use auto.

---

<div class="post-metadata">

**Author:** ![alatar](https://avatars.discourse-cdn.com/v4/letter/a/34f0e0/32.png) [@alatar](https://forum.juce.com/u/alatar)\
**Post date:** [February 25, 2020, 2:05pm UTC](https://forum.juce.com/t/avoid-auto-in-tutorials/37749/18 "2020-02-25T14:05:16Z")

</div>

I agree with the OP. I am not a fan of “auto”. Because it makes the code harder to read for me. And in Xcode, you cannot even hover over an “auto”, to see, what the type is.

---

<div class="post-metadata">

**Author:** ![asimilon](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/asimilon/32/4235_2.png) [@asimilon](https://forum.juce.com/u/asimilon)\
**Post date:** [February 25, 2020, 3:17pm UTC](https://forum.juce.com/t/avoid-auto-in-tutorials/37749/19 "2020-02-25T15:17:42Z")

</div>

> [@Alatar](#):
>
> you cannot even hover over an “auto”, to see, what the type is

Why would you care? As soon as you try to actually use it then it will show you the type:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/f/fa2eb863d4539d4dd6d564f9f4c5313bf72a06bf.png)

---

<div class="post-metadata">

**Author:** ![johngalt91](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@johngalt91](https://forum.juce.com/u/johngalt91)\
**Post date:** [February 25, 2020, 4:18pm UTC](https://forum.juce.com/t/avoid-auto-in-tutorials/37749/20 "2020-02-25T16:18:58Z")

</div>

> And in Xcode, you cannot even hover over an “auto”, to see, what the type is.

I’ve been using XCode all this time without the autocomplete/suggestions working properly, always moving around to find how X thing is named. The other day I tried VSCode and man that’s night and day, giving all the info needed even the comments made on that variable. I can’t imagine how many time I’ve lost wondering on files and lines of code in Xcode.

[Next page](https://forum.juce.com/t/avoid-auto-in-tutorials/37749.md?page=2)
