Almost Always Auto

For the past four years I’ve been developing mostly in C# at work and gotten used to some of the more generally accepted code styles in the .NET world or at least within my project. One of those styles is the usage of var basically everywhere, which has really grown on me and it’s also why I prefer the Almost Always Auto style (named by Herb Sutter) these days when writing C++.

Someone commented on my last post asking why I used auto so often and while I provided a brief response as comment, I thought to also share these points as dedicated post here and maybe it can lead to some interesting discussions.

Code to Solve Problems

When writing C++ or other “low-level” languages, one can quickly forget that the main goal of writing code is to solve problems. The path to get there may require a lot of technicalities, including the correct usage of types, classes, etc. but that shouldn’t be the main focus of writing code, nor should it distract from getting to the solution.

From my personal experience, reading (well written) code becomes easier, when we don’t have to care as much about the types used and can rely on the compiler to know the details. Additionally, if the code becomes hard to understand when types are replaced by auto, then it might also indicate, that the code itself may be lacking clarity in general.

Side note: In many cases the readability of code can be improved noticeably with only two changes:

  1. Picking good variable names which describe the intention much more than the type or kind
  2. Extracting independent code pieces into their own functions — this also applies to complex logic expressions

Otherwise most Clean Code practices can be very valuable for writing readable and maintainable code.

Ease of Mind

The use of auto doesn’t convert C++ into a dynamically typed language, as some seem to kind of portray it, instead it frees the programmers mind from having to track every single type, especially when it’s quite obvious, what you’ll get or when you don’t really care and just want to run known or generic operations on a type.

If you call a factory method, the method itself will domain-wise already tell you, what you’ll get in return. Having to specify the exact return type can be a waste of time.

How often did you have to look up the exact return type of a function, just so you can actually start using the function, even though you knew what operations you could perform on the returned value. For example you know GetCount() returns a number that you want to add something to, but does it return an int or an unsigned int or a std::size_t?

Maybe it’s a bit exaggerated, but not having to constantly track down the correct type, makes writing code faster in a sense and more fun for me personally.

Refactoring

Most code bases I’ve worked with in C++ weren’t large enough to really benefit from any simplification of refactoring. Plus we’ve seen a lot of useful refactoring tools from ReSharper++ or direct integrations in Visual Studio, that makes refactoring fairly easy these days.

If you don’t have such tools available, not having to find and replace every uses of class, can make a renaming a lot easier. Or if you introduce a class hierarchy, you don’t have to update all the usages, but can simply change the factory method or constructor call.

Speaking of factory methods, if you construct all your types like auto a = A{} it becomes very easy to replace the explicit constructor call with a factory method, like auto a = CreatePrefabA(), thus enabling a progression in code writing.

Closing Thoughts

I’ve had a lot of discussions over the years on this topic, rarely have I found someone who’s tried the style (either with C++ auto or C# var) for a longer period of time and switched back. Most arguments I’ve heard come from people who are skeptical about it and list various issues, yet never actually tried applying it. From my experience most or all listed arguments don’t apply in real world scenarios and the fear of not knowing the exact type at a glance evaporates.

If you have been using this style for a while and have some arguments/experiences against it, let me know, always interested in learning more.

Leave a Comment

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.