Go to Post We fail to present a coherent picture of what we are if we aren't vigilant about our self-image. - Alan Anderson [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #4   Spotlight this post!  
Unread 02-07-2016, 00:25
AMendenhall AMendenhall is offline
Registered User
FRC #3925
 
Join Date: Sep 2015
Location: Ventura
Posts: 22
AMendenhall is an unknown quantity at this point
Re: Rewrote Robot Code: Opinions/Thoughts?

In reference to euhlmann's statement about your declaration of drive motors:
Quote:
Just make sure you define your arrays as "VictorSP[] driveMotors" for example, and not "Object[] driveMotors". That makes it a bit easier to read.
Not only does defining the array with type VictorSP make your code easy to read, it can prevent errors from happening in future.

Imagine this scenario:
Code:
Object driveMotor;
...
driveMotor = new VictorSP(1);
...
driveMotor.set(0.5);
Now you'll get a compiler error on the line "driveMotor.set(0.5);" This is because the compiler (and your code) doesn't know that driveMotor references a VictorSP. Sure, you know that since you assigned it to one in the previous line, but driveMotor was <i>declared</i> at the beginning as an object. You'd have to cast to a VictorSP, but that could lead to runtime errors because driveMotor could reference a USBCamera or a Date or a Potato.

The point is, when you declare a new variable, its type is all the rest of your code has to go by. Thus, you should be as specific as possible when declaring its type (by this I mean the lowest in the tree. In Java, every class extends Object. If a class has subclasses, unless for some reason the type isn't supposed to be known, you should declare it as one of those subclasses).

Amended:
Code:
VictorSP driveMotor;
...
driveMotor = new VictorSP(1);
...
//No Errors!
driveMotor.set(0.5);
Reply With Quote
 


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 09:37.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi