GetDunne Wiki

Notes from the desk of Shane Dunne, software development consultant

User Tools

Site Tools


Modernizing the VanillaJuce code base

I was very pleased to receive the following feedback on VanillaJuce directly from JUCE author Jules Storer himself. I have added a bit of formatting to improve legibility:

Great contribution!

But… if you’re posting your code for beginners to learn from, you really need to get up to speed with modern C++ practices, because all the old-fashioned stuff in here sets a really bad example to any newcomers who don’t know that there are better ways to do things.

I can see that you’re a proficient programmer, and I’m guessing you’ve written a lot of high-quality C in the past, but modern C++ is a very different beast and you need to lose all that old baggage!

So quick code review:

  • ditch all your new/delete/sprintf/strcmp and other C functions.
  • Convert all your for loops into range-based fors and you’ll cut out a ton of boilerplate. Use member variables for subcomponents rather than pointers.
  • Use juce::String or std::string, not raw char arrays (!)
  • “typedef enum” is C, not C++! An “enum class” has vastly more type-safety, and enforces a syntax that makes the old-fashioned ‘k’ prefix redundant.
  • NEVER use a #define for a constant!! Use constexpr!
  • Pass references if possible, not pointers!
  • Use inline member variable initialisation and you’ll save another hundred lines of code.
  • If you’re passing an argument that’s a string, you can just use a string literal e.g. foo (“xyz”), you don’t need to wrap it in foo (String (“xyz”)) (God knows why we see sooo much code that’s written that way)

Hope that helps!

(Also, the list above is probably something that could be copy-pasted as a code review for hundreds of other projects that people have shared on here over the years!)

Jules's instinct is spot-on. I started out as a C programmer, and although I have used C++ since about 1991, I have not always kept up with how the language has changed. This is partly because I've had to code for many different platforms, including embedded systems and microcontrollers, where the latest language features are often missing.

It occurred to me that Jules's “quick code review” could form the basis of a nice little article, so instead of simply working through the list and updating the code, I've decided to document the process here, for the benefit of other programmers who, like me, may not be familiar with the latest C++ features and practices.

I'll start by reorganizing Jules's list into categories, and write something about each.

Outdated C syntax and library functions

Pointers and references

Newer C++ features and constructs

The following two items remain open at this point, pending further guidance from more experienced JUCE developers. I reviewed all my for loops, and didn't see a case where using C++11 range-based constructs would yield improvement, and I'm not certain yet how inline member-variable initialization actually applies to my code (unless Jules was referring to the kinds of changes I already made to my GUI classes). Comments welcome on the JUCE Forum!

modernizing_the_vanillajuce_code_base.txt · Last modified: 2017/09/01 19:49 by shane