I have read tutorials all over the web with different kinds of tutorials specified on game (however, this turns out to be pretty general).
Are there any reasons to why many developers name their variables like:
mContext
For me it is default to just name it "context" or something similar.
Are there any reasons why the "m" are before? (I know that this is a matter of style, but I'm just curious what it stands for)
To those of us who disapprove of cluttering up our variable names with such characters, they're known as "warts". In my opinion, with today's IDEs it is better to leave the warts off, because we can readily distinguish between local variables and member variables without their help.
The m will be to signify that the object is a member variable of the class in question. It's a common use of Hungarian Notation to prefix the name with clues to the variable's purpose or type.
Many programmers like to prefix their variables with lowercase letters that represent the object type that variable represents. For example:
var strMyString = new String();
Related
I am deploying my tf model to android, and I just want to ask what are considered as 'const op' in tf? Also, if your going to save you variables into a checkpoint and be saved into an optimized file that will be used in an android app, should you explicitly name all of them? Or does tf automatically assigns a name for all the variables?
What is 'Const Op' in Tensorflow?
This is an op that manages constants created with tf.constant (or similar function in another language). Basically, it's just a constant tensor.
Or does tf automatically assigns a name for all the variables?
Tensorflow automatically gives names to the variables, but you should better give the names to those variables that you care about to find them easily later on. Automatic names can be sometimes cryptic and misleading. For instance, gru_def/rnn/gru_cell/gates/kernel is one of the variables inside a GRU cell and most of the scopes are defined with the library, not in your code.
Or, you might see Placeholder_1 and think that this is the first placeholder in your model, but it would be wrong, because the true first placeholder is Placeholder, while Placeholder_1 is the second one (in fact, that was a real bug).
I am wondering if you should use strings.xml instead of global constants. I learned that global variables should be avoided but then again strings.xml are probably not ment to be used like this?
Are there any advantages / disadvantages using one or the other?
I am pretty sure that hardcoded strings like the following is not a good way.
putExtra("extraKey", extra);
With strings.xml or Constants you have a spellcheck and autocompletion.
A typical line with R.string could look like this.
intent.putExtra(getString(R.string.first_player_for_intent), firstPlayer);
in comparison to the
intent.putExtra(MyClass.first_player_for_intent), firstPlayer);
If you should use constants, in what class should they be located?
I wouldn't use res/strings.xml to store constants. You might want to access their value even though you don't have a context. Also, your keys don't need to be localized. Regarding the place where you should store it, it is up to you, and imo, it is just a matter of taste. I usually avoid having a dedicated class just for constants, but I declare it where it imo belongs. For instance, if you have a class called Player, I would put all the constants Player related there.
strings.xml is there for a purpose and primary purpose of it is to support localization.
you should not be overloading this system with constants which are not relevant for localization.
As Blackbelt correctly said you may need to access your constants even without context so that's another reason.
why Google calls variables with the prefix "m" for example:
private int mSectionResourceId;
private int mTextResourceId;
I see it in all examples. But i not understand why they do it?
And now i have some example where it practic very good. If a called variabels without prefix i need write
public SimpleSectionedRecyclerViewAdapter(Context context, int sectionResourceId, int textResourceId,
RecyclerView.Adapter baseAdapter) {
this.sectionResourceId = sectionResourceId;
this.textResourceId = textResourceId;
but if i use prefix i can write
public SimpleSectionedRecyclerViewAdapter(Context context, int sectionResourceId, int textResourceId,
RecyclerView.Adapter baseAdapter) {
mSectionResourceId = sectionResourceId;
mTextResourceId = textResourceId;
I think it more readable. Who can explain to me the pros and cons of a prefix?
The variables starting with m are telling you they are variables in the scope of your class. Member of the class.
Link to Android Code Style Guide
The m just stands for 'Member'. It is simply declared that your Variable is a Class-Member.
It is more readable Code, because you know where Class Members got declared, so you can find it pretty fast. You don't need to write this, even if you don't prefix your Variables with an m.
In your Example, this only makes it more readable when there is no prefix-m. Another developer knows that it is a instance variable (member variable) and so declared on top or bottom of the class.
It is a prefix for class member variables. It's just a naming convention.
Mostly sure, taken from Hungarian Notation where similar prefix: m_ stands for exactly the same).
Referring to pros & cons:
Pros:
it allows to type fewer chars during programming,
programmers that are used to use Hungarian Notation may found it easier to follow the code.
Cons:
as the code changes very often, it is easy to forget about changing prefixes every time, when variable changes it's purpose (especially during prototyping),
it makes the code starts to smell bad,
Generally, it is some kind of reinventing the wheel. Java has this keyword that should be more than enough for accessing proper variable. If it's not, the code requires refactoring, maybe because of naming glitches or using too wide variable scopes.
Personally, I do not recommend to use Hungarian Notation (even the part of Android Code Style). We have great IDEs that increases the readability of the code.
There is an exception. The code, where Hungarian Notation (or more general, specific code style) was already been used. It is a matter of consistency.
The m is just a member variable. A class member if you will. Useable with constructors like WebView M WebView then later on you would use something like mWebView.loadurl("example.com"); it's just a placeholder for the variable you created. You don't have to add the member class variable as an m but it's more organized if you do
Ive been searching the internet for a while now but cant find a good page for good habit examples in android programming. For examples im interested in things like how to name classes or xml files (what case letters, where to use _) and also in file things like naming variables and fields or edittexts, prefixes and everything like that.
If someone could help me with a link i would be very grateful!1
Android developers has its answer for this.
If you need anything elaborated I'll update my answer from your comments :-)
For Classes use Java naming convention:
First letter capitalized, no "_" in the class names but start with capitalized letter for each new word ei:
MyActivity or MySettingsActivity
For xml object naming you have some other limitations as in, first letter cannot be capitalized, therefor I suggest your either use the general java naming convention for methods (First letter decapitalized, and then new words capitalized like: buttonQuit or quitButton), if it should be button first or last is up to you, but stick with 1 style. don't name 1 item: nameTextView (or nameTxtVw) and then something else buttonSubmit (Have the indicators in the same orders).
For xml files, use lower case separated with underscore "_".
Why a lot java files are called Activity in the end. It is to describe in the name that they inherit from from the superclass "Activity". It is a principle that came became big with Android, which uses it a lot, examples if these classes are inherited:
(super class = end of their heirs name)
AsyncTask = Task
Service = Service
Activity = Activity
Handler = Handler
I could go on :)
For xml files, belonging to activities I personally like to call them the name before Activity, so MainActivity's xml layout would be main.xml
If you are looking for some coding guidelines, have a look at Code Style Guidelines for Contributors
If you want to enforce this rules without remembering all of them, and you are using eclipse then you can use formatting rules provided by Android team: android-formatting.xml. Just import it into eclipse using Preferences->Java->Code Style->Formatter, click Import.
You should learn Java code conventions, and so read this:
java style
It's good to practice always name classes upper camel style (e.g CustomerService)
Class names should be noun,
For naming variables you should use lower Camel Style (e.g myVariable)
Xml file shoudl be lower case and words should be separated by under score
please read the above links!
[Edited] this part added after the first comment!
I don't know any good reference for good naming convention and things like this but those are things that you can find out be practicing! for example:
"activity" prefix may be not useful in case if all your layouts are for activities. but if there is bunch of other style (e.g dialog layouts) it could help to put "activity" prefix.
another example: I prefer to put "Activity" prefix for all classes that extends Activity, because eclipse shows classes base on name, and this could help to somehow a better management (but it's against Java naming convention! because you should suffix your class name with super class name)
For naming Id: You could prefix the resource with the name of what it is (e.g titleStatus), it doesn't really matters! but you for better managing your Ids and not losing in lots of name, always follow the same rule, if your name some element in your status like "statusTitle" the other element should be "statusDescription" but not "descriptionStatus"!
I am porting an app from Android Java to iPhone.
In Android I used Lists/ArrayLists alot.
On iPhone I plan to use NSMutableArray.
Is there any way to define or even indicate the type of objects in an NSMutableArray.
I know one can put any type of object there, but I would like to make it more visible and transparent.
Many thanks
It's not clear exactly what you're asking.
If you just want to make it clear to the reader what sorts of object of are in the array, just name it appropriately (you can't enforce it at the language level):
NSMutableArray *arrayOfMyClasses;
If, on the other hand, you want to find out the type of an object that you're reading back from the array then you can get the underlying class using:
[obj class]
Or easily compare to other class types:
if ([obj isKindOfClass:[MyClass class]) { ... }
Tim
I assume you are looking for template pattern in Objective C. Unfortunately, it is not available in Objective C (at least directly).
You might find this question of StackOverflow.com interesting
You can only indicate a type.
for(id obj in _assets) {
NSString *className = NSStringFromClass([obj class]);
NSLog(#"%#", className);
}
Arrays are ordered collections of any sort of object. For example, the
objects contained by the array in Figure 1 can be any combination of
cat and dog objects, and if the array is mutable you can add more dog
objects. The collection does not have to be homogeneous.
Collections Programming Topics - Arrays: Ordered Collections