Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Type inference is optimized by the compiler afaik.
At least in Kotlin and Swift it is. I would like for anyone with more technical knowledge of the inner workings of c# to correct me if i am wrong. -
@AleCx04 in C# it is mostly syntax sugar. The compiler will translate this:
var x = 5;
to:
int x = 5; -
I'm confused...
There is "int" and a lot of other basic types that can hold a 4.
And why does the variable need the type prefix? ๐คจ -
@sunfishcc Too clean & straightforward, I guess?
@PonySlaystation Because it is a var and you don't 'know' it's type.. -
Seems like all commenters so far forgot how to read after the first paragraph of the rant
-
I'm actually thinking of rewriting this..creating a class with Type as a string and Description of what the Value is storing... Just so that in a few years people can rant about me here ๐๐๐๐๐
-
@sladuled in Visual Studio, you can hover over the 'var' keyword and a tooltip will show up with the type info ๐
-
@electrineer Yeah, I guess I should have used the read this in fake(,) stupid and highly sarcastic voice... xD
-
@MrCSharp (: I know..
been using it for my lazy way with writing linq to check if I got it right.. var x = someCrapImTryingToGroup().ToDict... then if I got what I expected I change var to correct type.. -
@sladuled it sounds like you're slowing yourself down a lot while coding.. why are you replacing var with the actual type?
-
@sladuled it has always bewildered me how hard is it for people to recognize sarcasm online
-
@MrCSharp probably because it's easier to read and less prone to mistakes. Var has its places, though.
-
Voxera113886yVar is not a type but an instruction to the compiler to infer the type. It will still be a real type, you just don’t have to specify it.
For a simple int like this it does not make much difference but if you have
Dictionary<string, MyGenericClass<MyOption>> d = Factory.GetData();
Consumer.PostData(d);
You could instead write it as
var d= Factory.GetData();
Consumer.PostData();
Which is much easier to read and intellisense in all common editors will show you the real type on hover.
Also, if GetData and PostData change types you don’t have to do anything, as long as they match the compiler will do the right thing.
With larger code bases and especially in Mvc and razor with dynamics it is a must. -
@MrCSharp Why wouldn't I? ๐ค
It's not like replacing var takes me 1000 times more time than figuring out what data structure I need..
Plus you see the code and know right away what type it will be. I'm a visual type, I see something I rember it, I don't want to go hover over whenever I wanna know the type of something new I find in code... Plus what if you're opening it in notepad? Good luck with that.. -
@Voxera Not saying it doesn't have good use cases, but this one (from my rant) is not one of them..
-
7Raiden8756y@sunfishcc the name is too long, I could almost understand it. Try
int _ = 4;
What's wrong with:
int numberOfDecimals = 4;
Or even
var numberOfDecimals = 4;
But defo not using var with the type specifier in the name ๐ -
@MrCSharp i know how that works. My post was going mostly at being better to use var since type inference is optimized by the compiler.
It is actually optimized or is it mere syntax sugar? -
@AleCx04 what do you mean by optimized? I don‘t see anything that could be optimized. It‘s what it is: Type inference.
-
Rocket3G2006y@Lensflare it's type inference at compiler time, less efficient (and even more error prone) would be type inference at runtime ๐
-
@Rocket3G afaik, there is no such thing as type inference at run time. At runtime it is called dynamic typing. Essentially the var in javascript.
-
Voxera113886y@sladuled It not really necessary in that case but many tools also offer help in replacing full class names with var.
I was similarly skeptical to begin with and did not see it as a good thing but after a few months I realized it reduced clutter and got me to focus on more important things.
With intelligence and such I very rarely need to know the actual type.
So I can offload to the editor and compiler to keep track of that. -
Voxera113886y@AleCx04 I do not think the code is more optimized but later when you refactor things it can mean you do not need to change variable declarations, like if you create a child class to the original and return that.
Existing code will of cause work due to inheritance BUT if one of the other methods the object is sent to need the new time ... with a full type name you would need to update it everywhere but with var it just keep working.
And for an in, sure you cannot create a child, BUT you could create a class with an implicit cast to int and return that and old methods requiring an int will still work and upgraded methods that need the new would also work without any changes in many files.
This is something that I often have use for as the codebase is over a decade old and we do a lot of refactoring. -
theuser47816yAt work, we are obligated to use "var" everywhere, but I highly disagree.
This is fine because you know the type from declaration:
var list = new List<string>();
Also this:
var x = y.getString();
This is not fine:
var x = y.Get(); -
@LensFlare and @Voxera lets ask our local systems developer(and asshole extraordinaire xoxo) @FrodoSwaggins <---- could you shed some light in the subject of type inference at compile time. Does it provide any level of optimization? Are we all high?
-
@FrodoSwaggins aww come on man that's why i included the xoxo! Don't take it the wrong way!
-
@FrodoSwaggins it seems then that we are left to do our own research u____u fair enough
-
RealKC9586yI don't see a problem with using var in that place since the type seems rather obvious from what's after the equals IMO.
What I have a grip with is Hungarian notation for variables(i.e. embedding type info in variable names) -
No integer type in C#? Of course there is. Var is used extensively and it is absolutely strongly typed. Using common sense, with C# knowledge of its primitive types, you should be able to infer primitive types yourself.
For example:
var s = "Is this a string?";
var i = 1; // is this an int?
var d = 1.0m; // m suffix makes it a decimal
var b = true; // is this a boolean?
Etc.
Use some common sense.
There are some guidelines regarding use of var:
- don't use it if the type is not obvious. E.g.:
var x = repository.GetProfiles(); // wtf is returned??
Instead:
IEnumerable<Profile> x = repository.GetProfiles();
Also, this is acceptable:
var x = Int32.Parse(y); // y is some string representation of an int.
var x = new Customer(); // right hand side of assignment makes var's type clear.
In some cases we don't care about the actual type:
var query = from c in Customers where c.City == "Amsterdam" orderby c.LastName select c;
Here, does it matter what query is? No. -
@RealKC oh yeah totally agree..please, not hungarian notation, no type abbreviations in variable names.
-
Yeah, starting to regret I didn't put <sarcasm> tags in the rant.. sigh..
Or that I wrote it in the first place..wanted to blow off steam after finding yet another part of the code that just doesn't fit in with the rest, but ended up getting lectured about common sense (& now I wonder who lacks it?) and starting a war with debate on compiler optimisation.. o.O -
musician6656y@sunfishcc dp as a variablename is whats wrong with it. Dont use abbreviations unless its something established, but even then. We have autocomplete these days so lenght isnt an issue anymore
-
Voxera113886y@RealKC that would depend on if you use the original hungarian notation or the more popular misconception?
Hungarian notation was invented at Microsoft but it was not for differentiating between string and int and such.
It was used for contextual type, is this width variable using pixels or centimeters, pxParagraphWidth vs cmParagraphWidth.
Similar to F#’s use of tuples (34,”cm”) vs (400,”px).
The was used for excel or word to keep apart the actual document area and printing area so they would not be directly assigned without applying the current scaling offset.
Yeah.. it's a shame C# doesn't have a type to hold this (4) and then you have to resort to using var.. I am so disappointed, I've heard C# is crap, but this.. THIS!! It doesn't even have a type for normal integers!! FFS!!! I thought it was better than this!!
Oh, wait.. it's not C# who is 'weird'!! It's my super duper cool ex coworker who made a mess of the simple code again... I admit, this is not such a huge deal.. BUT... It doesn't end here.. o.O
rant
wtf is wrong with people?!