A website devoted to technical training for the First Tech Challenge
How to enable Android Studio to give you hints on what functions to use.
In the general sense, "code completion" means tools that the programming environment (Android Studio) provides that helps you either type code faster, ensures that code is completed correctly, or both. One of the FTC Ninja's favorite components of this within Android Studio is the ability for it to see what functions are available for a given object. When first introducing students to java programming and the FIRST API in general, the FTC Ninja always teaches them that if they don't know how to do something, but know that they need to interact with a specific object to do it, that they type the name of the object following by a period. If code completion has been enabled within Android Studio, this will then populate a list of all of the available functions for that object. In many cases this is sufficient for the students to look through the list and find something that sounds correct. Couple this with showing the documentation automatically and typically it is a great first pass tool for finding an answer. The image below shows an example of code completion being use on a DcMotor object called leftDrive. By simply typing leftDrive and a period, a list has been generated for all of the functions that are available. In this case, maybe you were looking for the encoder position of that motor. Well, the third function in the list, "getCurrentPosition()" sure sounds like what we're looking for!
Before we go ahead and show how to enable code completion, let's cover a few more details on exactly what it shows when it pops up and how to use it.
Once the code completion menu has appeared, use the up arrow ↑ and the down arrow ↓ or use your mouse on the scroll bar to scroll through the options. Once a function is selected that you would like to use and complete your code with, press either the Tab key or the Enter key to fill in the text.
The image below shows a single line from the code completion menu. The first two items that are shown are a little "M" and a green unlocked lock. These symbols have meanings relevant to the function or parameter. For the most part, it's not typically required to know what the differences are, but if you are curious you can look up the full list here. For the function shown below, it means that it is a public (unlocked) method. Next, the list shows the name of the function, in this case the "setPower" function. Then, within the parentheses, it lists any parameters that need to be passed to the function. As you see below, the setPower has a single parameter called power, and it is of type double. Finally, off to the right, it shows what is returned by the function. In this case nothing (void) is returned.
There are other small hints associated with the auto documentation on functions that are quite helpful. Consider the list of the three options shown below. The first, "setPower" is in standard text. The second, "getCurrentPosition" is in bold. Finally, the last function, "setPowerFloat" is crossed out. These three indicators also imply meaning. In Java, classes can inherit traits from other classes and then build upon them. This allows you to create a more specific or more powerful version of something and reuse all of the other code. The difference in bold vs not bold means that this function was either inherited (not bold) or is a direct function within the selected class (bold). For this specific example, the "setPower" function is defined within the DcMotorSimple class. The "getCurrentPosition" function is defined within the DcMotor class (which is what leftDrive is). The functionality for setPower isn't within this base DcMotor class however, it is simply inherited and reused from DcMotorSimple. Finally, it's worth pointing out the last function that is crossed out. A crossed out function means that it is deprecated. That is a fancy way of saying "Hey... please don't use this." There's a good chance that it might have issues, or will be removed in a future update, so stay away from those!
In order to configure Android Studio to activate auto code completion, perform the following steps within Android Studio:
FTC Ninja