The scope of my question is solely ASP.NET, as the answer may be different for Java and any other C based language.
How much overhead is involved when using the keyword "this" within a class to dereference a property? It seems that I've seen certain sources try to discourage the use of "this" for dereferencing, but generaly I've just ignored them until now.
I think it is just a style issue. As stated in the answer to this question, the compiler injects 'this' into implicit uses of this. I would use it for clarity.
StyleCop recommended using this for all instance field reference.
And when compared to access the said field through a property, shouldn't it actually be faster (albeit unnoticable) by bypassing abstraction?
No overhead.
Related
I was presented with this argument when fixing a bug related to implicit casting and speaking to the developer who told me:
If you use Closure, then you do not need absolute equality. Any value would be typed, therefor you don't need to worry about implicit casts.
My response was, that's dicey. You're making a lot of assumptions; Closure does not alter JavaScript, it's primarily a labyrinthine super layer (aside: probably a moot one, now that we have TypeScript).
Anyway, if one of these implicit things does slip by because the annotations don't resolve perfectly for some reason, you can end up with a tough bug (which is what happened; it got assigned to me because I guess the other dev didn't think it could be the problem).
I got responses of, "well if that dev had properly typed that object and this wasn't just an object and..."
Or...you could just protect against this sort of thing easily by using three equal signs instead of two. Use an assertion or console log to check the condition if necessary. Don't just leave it hanging out there.
Anyway what do you think; if you're using Closure, should you still observe the general best practice of using absolute equality in your JS code?
I know this leads to a wider conversation as well (e.g. Java 8's "optional" being "totally useless"), curious in the context of Closure though.
Question is a bit vague, code example would have helped. But yes, closure does not necessarily type every object and variable. I always use === unless there is a specific reason not to.
JSONPath seems to be a popular syntax to get XPath-like searching inside JSON data. And it has been repeatedly asked whether JSONPath supports navigation to a parent (see here and here).
My question is whether there is a good reason why it has not been suggested from the start, even though it is explicitly mentioned as unsupported. Is there some syntactic reason from JavaScript? Or is there some general workaround that I am missing?
This specification was written up on a blog; AFAIK, it is not part of any ongoing committee standardization.
However, to meet the need for parent accessors (and other features), at least one implementation, JSONPath-plus which is a superset of the original spec, allows for accessing parents through a number of means (see the README docs).
Disclaimer: I am involved in working on this implementation.
What is the difference between "Controls.Add(xyz)" and "this.Controls.Add(xyz)" in ASP.NET (C#)?
How/When does it matter if I am adding the same control onto a webpage but via two (different) aforementioned methods?
When one should be preferred over the other?
There is no difference; this refers to the current instance of the class you are in. Without specifying this, you will get the closest method within the current scope (which is this anyway).
In your case, this represents the current instance of the class. Hence, unless you are dealing with extension methods, you should be good to use either way as it is a matter of semantics.
What is the difference between "Controls.Add(xyz)" and "this.Controls.Add(xyz)" in ASP.NET (C#)?
None.
How/When does it matter if I am adding the same control onto a webpage but via two (different) aforementioned methods?
It does not matter.
When one should be preferred over the other?
Using this is usually preferred as it is more explicit and helps code readability. But it is a matter of preference.
Reference:
this (C# reference)
In this case, there is little difference.
Using this makes your code a bit more explicit that it refers to a member of the class.
Use whatever you and your team agreed on.
The difference is merely the text "this.". The two different statements perform the same tasks.
One should be preferred over the other based only on coding standards for your organization. (I like it best without the "this.")
The this pointer is explicitly pointing out the current instance.
No difference. It's a redundant qualifier but as Oded mentioned it's for code readability.
Generally speaking, creating a fluid API is something that makes all programmers happy; Both for the creators who write the interface, and the consumers who program against it. Looking beyond conventions, why is it that we prefix all our getters with the word "get". Omitting it usually results in a more fluid, easy to read set of instructions, which ultimately leads to happiness (however small or passive). Consider this very simple example. (pseudo code)
Conventional:
person = new Person("Joey")
person.getName().toLower().print()
Alternative:
person = new Person("Joey")
person.name().toLower().print()
Of course this only applies to languages where getters/setters are the norm, but is not directed at any specific language. Were these conventions developed around technical limitations (disambiguation), or simply through the pursuit of a more explicit, intentional feeling type of interface, or perhaps this is just a case of trickle a down norm. What are your thoughts? And how would simple changes to these conventions impact your happiness / daily attitudes towards your craft (however minimal).
Thanks.
Because, in languages without Properties, name() is a function. Without some more information though, it's not necessarily specific about what it's doing (or what it's going to return).
Functions/Methods are also supposed to be Verbs because they are performing some action. name() obviously doesn't fit the bill because it tells you nothing about what action it is performing.
getName() lets you know without a doubt that the method is going to return a name.
In languages with Properties, the fact that something is a Property expresses the same meaning as having get or set attached to it. It merely makes things look a little neater.
The best answer I have ever heard for using the get/set prefixes is as such:
If you didn't use them, both the accessor and mutator (getter and setter) would have the same name; thus, they would be overloaded. Generally, you should only overload a method when each implementation of the method performs a similar function (but with different inputs).
In this case, you would have two methods with the same name that peformed very different functions, and that could be confusing to users of the API.
I always appreciate consistent get/set prefixing when working with a new API and its documentation. The automatic grouping of getters and setters when all functions are listed in their alphabetical order greatly helps to distinguish between simple data access and advanced functinality.
The same is true when using intellisense/auto completion within the IDE.
What about the case where a property is named after an verb?
object.action()
Does this get the type of action to be performed, or execute the action... Adding get/set/do removes the ambiguity which is always a good thing...
object.getAction()
object.setAction(action)
object.doAction()
In school we were taught to use get to distinguish methods from data structures. I never understood why the parens wouldn't be a tipoff. I'm of the personal opinion that overuse of get/set methods can be a horrendous time waster, and it's a phase I see a lot of object oriented programmers go through soon after they start.
I may not write much Objective-C, but since I learned it I've really come to love it's conventions. The very thing you are asking about is addressed by the language.
Here's a Smalltalk answer which I like most. One has to know a few rules about Smalltalk BTW.
fields are only accessible in the they are defined.If you dont write "accessors" you won't be able to do anything with them.
The convention there is having a Variable (let's anme it instVar1.
then you write a function instVar1 which just returns instVar1 and instVar: which sets
the value.
I like this convention much more than anything else. If you see a : somewhere you can bet it's some "setter" in one or the other way.
Custom.
Plus, in C++, if you return a reference, that provides potential information leakage into the class itself.
As the title says really. Does the virtual keyword cause a performance hit?
First off, in Actionscript you don't need to explicitly mark a method to be overridden as virtual as you do in C#, the compiler will do this for you. This is why the keyword doesn't show up in any docs because it is irrelevant from the developers perspective.
To answer your question though, which is whether overriding a method makes it slower, the answer is no as long as your class is sealed, meaning you don't use the 'dynamic' keyword when defining the class.
The reason why is that when constructing a sealed class, you're going to have explicit markers to every method for that object, when overriding a method you get a marker directly to the new function, the class does not have to look it up at runtime like in a dynamic class.
All methods are virtual in AS3 right now. But my guess is 'virtual' will become enforced in a future version of AS poss. via some compiler option setting.