Tips & Tricks for C++ environment

TL;DR:
I’m new to the C++ programming environment and am looking for some good tips, tricks and code repositories.

Hello,
so my team (7700) is a new team, and we participated in the 2019 competition. Since we didn’t have much time to get started, I programmed our robot in LabVIEW. After experiencing the pain that is LabVIEW (I feel that it is a good language, but not the right one for this task), I decided that I wanted to switch over to a script based language. I chose C++, as I feel it is the best since it’s basically an industry standard, and there is also (probably) the most support for it.

Since we are a new team and haven’t used C++ before, I was wondering if I could get any tips & tricks on using C++. I am looking for good code repositories and a few things that you have learned over the years that you think you should mention to any newbies. (Do note: I am not new to script-based languages, as I have used others in the past. Though, I will be training people who have never coded before.) I will be reading the WPILbi, so anything that is mentioned in there doesn’t need to be said there, I’m just looking for stuff that you wouldn’t find there.

Thanks for your time
~solo Team 7700 | Head Programmer | Head Electrician | Head Pneumatics

(I’m also new to chiefdelphi, so feel free to yell at me in the comments if I did something wrong.)

Oh boy.
I got even less help here, then on reddit.
I… didn’t expect that…

Hey,
So I don’t really post around here, but seeing that you didn’t get any other responses and clearly would have liked to, I do feel like maybe I should offer what little I can. My team uses c++ and we have for a long time, but I only learned it through robotics so I don’t know the language like a lot of other people (I assume), and while I don’t want to speak for everyone I don’t really think there are a lot of good “tips,” assuming you understand the basic structure of .h and .cpps and pointers and stuff. Your best bet for programming discussion is likely the FRC Discord, where there are plenty of people happy to help (myself included, though I generally don’t due to general lack of experience :P) Anyway, if you have a specific problem, your best bet is likely to reach out there, but as far as general tips I’m afraid I don’t have too much :stuck_out_tongue: If you’re not aware what forward declaration is, you may want to look it up as its something we use fairly frequently, although I’m not sure I could explain it myself. We use a command-based system, which works well for us though I’m not sure how common it is nor how it compares to the others (another good question for the community on discord). I wish I could offer more but to be completely honest my understanding of c++ is pretty much limited to what makes the robot go. I’m sorry for the wall of text here but I felt it was better than the crickets, and if you take only one thing away from this, it should probably be to visit the discord, as you’re much more likely to get quick, reliable c++ help there (that’s what I do when I’m lost :P). If you do decide to go command-based, I’d always be happy to help if you pm me directly.
Discord Link: https://discord.gg/frc
Personal Discord: Storm#5632
Best of luck! Sorry I can’t offer more, and it’s possible you already know most of this, but I figured I’d respond just in case.

1 Like

Yeah, I’m less looking for direct help, and more some stuff like github repositories that you guys have found useful over the year, and the like. I don’t think that “tips & tricks” was the right name. I’m also not at all familiar with C++, though I have used java in the past. (The reason I don’t want to switch to it, is because it’s not as efficient, and less of an industry standard.)

But thanks anyways for that! (also, I’m already in the discord.)

Hey fellow rookie! I’m using Java because our lead faculty mentor teaches AP Computer Science A which teaches Java. I’ve found so far that most teams use Java, though at some point I would like to switch to C++ as it carries over to actual use more. That being said, Java has been pretty good to use so if you don’t have any experience in C++ I would highly suggest you use Java.

Anyhow, I would check out team 971’s code as I believe they use C++. Here’s a tar.xz file of their 2018 code: 2018_frc971_software_20181031.tar.xz (48.4 MB)

For any help, feel free to msg me. I’m not super good at C++ in particular but I can give you general programming advice. :slight_smile:

1 Like

The lines are kind of fuzzy on exactly what a “scripting language” is, but I haven’t a seen definition where C++ is primarily considered a scripting language.

Before picking a language you should figure out what your team is looking for in a programming language. Are you actually concerned that you’ll write quality code in Java but will end up being limited by the language? Do you have any metrics to support this? 254 has managed to win a few championships primarily using Java.

If your goal is to prepare students for the embedded software industry, I would agree that C++ is more of an industry standard. However, if you’re looking at the software industry as a whole, then Java is going to be more widely applicable.

Despite C++ being my favorite language, our team has gone with Java simply because it’s more approachable for students who are brand new to programming both conceptually and syntactically. I have also found that there is as much, if not more, support for Java in FRC. The overlap with AP Computer Science A is also a big plus.

I don’t want to start language wars, I just want to make sure you have all the information before you make a final decision for your team.

1678 has some good C++ out there: https://github.com/frc1678/robot-code-public

3 Likes

Thanks for this information! One of the main reasons I picked C++ was because I thought it was used more by the FRC community, rather than java. On the other hand, my other reason for choosing C++ was that it is much more used by large scale companies, and knowing C++ is probably worth more than knowing Java. The last reasons I had for choosing C++, was that it has more code libraries (outside of FRC) and that it is much better optimized than Java.

Also, students (excluding myself) might also be a factor for me to think of, so thanks for bringing it up. Since I have some experience in other languages, I though that C++ wouldn’t be that hard to pick up, it was a little confusing at first, but I’m starting to understand it a bit more. That being said, I need to train someone else to be able to do this too, as I am currently the only programmer (we don’t even have any mentors.)
It would be nice if you could tell me how some of your students went about picking up Java, and maybe someone else could compare stories on how hard it was for new students to pick up C++, that way I could maybe get a bit of a sense of how difficult it would be.

Also, by scripting l meant a language that required me to physically write out the code, rather than using blocks (like in the case of LabVIEW) (Probably not the best thing to call it, but I don’t know how else I would call it.)

Thanks for your time
~solo

Thanks for that! I’ll be sure to check it out.
Though, I was under the impression that more teams used C++, rather than java. And about experience: I don’t have any experience in C++, but after reading through some documentation, I’m starting to get a hang of it. My one concern, on the other hand, is that it may be hard for other students to learn, discouraging them from joining programming.

Thanks for your time
~solo

1 Like

In my experience, C++ in industry is considered more of a niche “experts language” used mostly for performance sensitive applications, embedded systems, high speed stock trading, etc. You’re right that many large tech companies, Google for instance, use C++ extensively, but Java is still more widely used and is more transferable to other languages (i.e. C#). That being said, you won’t have trouble finding a job if you know either of them well.

Our logic for language choice went a bit like this; “What language will get the most kids actually programming a robot in the shortest amount of time possible w/o limiting our capabilities?”. I’ve found that the hardest part for students new to programming is actually getting the syntax right so that they can make the code compile and do something. Java is the clear winner in that category. Will we regret not using C++ when/if our robot gets so complex that we’re pushing the limits of the Java environment? Maybe. But for the foreseeable future, we’re happy with our choice.

Here’s some examples of the more complex C++ syntax:

  • . vs -> accessors
  • &'s vs *'s
  • :: for statics
  • .c and .h files

Also, by scripting l meant a language that required me to physically write out the code, rather than using blocks (like in the case of LabVIEW) (Probably not the best thing to call it, but I don’t know how else I would call it.)

Ah I see what you meant, “text-based” would be the term you’re looking for.

2 Likes

Thank you for this. I think that if anything, the syntax should be my main deciding factor, as I probably don’t need that most well optimized code for the types of things that we are going to be doing. And since I can probably pick up any language, I’m going to need to look at what language others will be able to learn the fastest. (Though, faster would be nice. I have heard that java is notorious for being slow for certain tasks, though I don’t expect to be sorting huge data sets.)

In that case, I may end up using C# or Java, though probably Java as it is most used.

And thank you. “Text based” was what I was looking for.

Thanks for your time
~solo

I mentor a team that uses C++. However, I have programmed in Java since Java 1.1 and have seen the language evolve.

Last year I surveyed all teams at our local regional about the language the team used. An equal number of teams used C++ and LabVIEW, but twice as many teams programmed using Java as the combined number of C++ and LabVIEW teams.

C++ predates Java by a dozen or so years. Both adopted the general syntax of the C programming language (curly braces are used for code block), similar looping structures, similar conditional structures, etc).

The goal of the C++ designers was backward compatibility with C. This design constraint carried with it a fair amount of baggage, since C is very, er, flexible (as one C instructor of mine use to say, “You can do anything in C”).

In contrast, beyond adopting the C syntax style, Java made a cleaner break from C, wanting to avoid the things about C that made it so challenging and error-prone. For example, Java has no pointers. This can be a double-edged sword. On the plus side, eliminating pointers avoids a rich and fertile area for bug production. On the bad side, you cannot directly access memory when you need to, such as when needing to access hardware registers, often essential when performing embedded programming, device driver programming, programming operating system kernels and other advanced programming. Java created Java Native Interfaces (JNI) as an accommodation, which lets you interface Java to a language that supports memory access (like C or C++).

If you were to set out to master the C++ language, then it is without question more difficult to learn and program than Java. When Java was first conceived it was a very simple language. Perhaps too simple. However, its complexity has increased over the years and, although I sometimes wonder whether it too isn’t getting too complex, I think the Java designers have generally done a much better job staying true to its architectural principles.

Java generally gets a bad wrap for being slow. Java executes byte code, which you can think of as the machine language of a notional machine that doesn’t actually exist (some organizations have actually created hardware whose machine language is Java byte code, but I digress). Java needs a Java Virtual Machine (JVM) to translate the byte code to the native machine code of the computer on which it’s running. In its early days the argument that Java was slow was well-deserved. However, in its first decade Java made great strides in improving its performance. When a Java program first runs it is converting byte code to machine code as the program executes. However, for parts of the program that are run frequently the JVM will just go ahead and compile the byte code to machine code so that it can avoid the performance penalty of the byte code to machine code translation. This is called Just-In-Time compilation. That is one of the major reasons why a Java program speeds up after it’s been run once. However, some advanced programming (such as hard real-time systems) can’t tolerate this variability in execution time.

Another double-edged sword is Java garbage collection (GC). The garbage collector is responsible for freeing heap memory that’s no longer needed. Having Java manage memory is a HUGE benefit for the programmer and it can all but eliminate another bug genre: memory leaks. In contrast, in C++ the programmer is generally responsible for managing all heap memory used by the program. The downside for Java is that, like housecleaning, garbage collection takes some time. And execution must pause during garbage collection. Early Java used to wait as long as possible before running the GC, creating a pronounced pause in execution when the GC finally ran and likely contributing to the slow performance reputation. Sun/Oracle have tinkered with GC tuning over the years and Java is much better at staying on top of its GC duties without forcing a noticeable execution pause. GC is another Java feature that presents challenges for hard real-time system programming.

The good news about C++ is that, even though the language has many obscure features, a great many of them are ones that you probably shouldn’t be using anyway if you’re writing a new program rather than, say, maintaining a computer system from 1990 or writing kernel code. Having spend so much time programming Java, I find that the best approach to writing C++ code is to write it as close as possible to how I would write it in Java (e.g, don’t use multiple inheritance), adapting to the language as appropriate (e.g., use header files, create a copy constructor (or explicitly prohibit it, so the compiler doesn’t create one for me), create a copy assignment operator (or explicitly prohibit it; same reason)).

2 Likes

Note that not only is C# not an officially supported FRC language, RobotDotNet (the FRC C# library) is also unmaintained. I can safely say that no team is using C# for their robot code this year.

Thank you for this, but I already decided to change to the Java environment, as I will need to be teaching others how to program too, and they have never done so before. I feel it’ll be easier to teach them Java, than C++. Additionally, I know Java, and it’ll be easier to get off the ground with. The last reason is that Java is the more used platform, effectively having more support, since there’ll be more people to help me when thing invariably go wrong.

Also, here is the post I made about switching to Java

Thanks for your time
~solo

Hey, I have actually decided to choose the Java environment over C++ or C#. Though, I didn’t know that it wasn’t supported officially. I was actually told about the C# environment by a mentor, so I thought that it was officially supported. Oh well.

Also, here is the post I made about switching to Java

Thanks for your time
~solo