What "advanced" features of programming languages do you use?
-
I avoid using
var
anddynamic
in C# because they are normally misused to resemble Dim in VB or shortcut strong typing because the developer is too lazy to think about maintenance..."I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
I'll toss the ViewBag into that bucket too.
-
I avoid using
var
anddynamic
in C# because they are normally misused to resemble Dim in VB or shortcut strong typing because the developer is too lazy to think about maintenance..."I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
OriginalGriff wrote:
I avoid using
var
anddynamic
in C#I agree. But I find it interesting how many experienced developers love using var. I feel like I must be missing something.
Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.
-
I use something called a "debugger". I always knew this was quite a maverick thing to do, but looking at the QA section I didn't realise just how maverick!
You have to wonder how any of these people manage to do anything useful. :sigh: I was sent this this morning: Cyanide & Happiness: Millenials[^] (possibly NSFW, very little of C&A is) :laugh:
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
-
OriginalGriff wrote:
I avoid using
var
anddynamic
in C#I agree. But I find it interesting how many experienced developers love using var. I feel like I must be missing something.
Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.
ZurdoDev wrote:
I agree. But I find it interesting how many experienced developers love using var. I feel like I must be missing something.
I used to hate it but got used to it now.
-
You have to wonder how any of these people manage to do anything useful. :sigh: I was sent this this morning: Cyanide & Happiness: Millenials[^] (possibly NSFW, very little of C&A is) :laugh:
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
-
I've been digging into a javascript based application lately and tripped across some code that made extensive use of the arity (which is deprecated now), currying capabilities and anonymous functions. Took me a lonnnnnng time to understand what the intent and operation of the code was. So, out of curiosity, I'm wondering what "advanced" features of people's favorite languages they use on a regular basis, how they use them, and why? Or, which ones do you seldom/never use because.... E.g: ....I like using lambdas in C++ because .... (I can't think of a good example because I avoid them!) ....I avoid lambdas in C++ because they are often used where a simple function would be clearer and more readable (truth in advertising).
Depends on what you think is advanced ;) I use lambdas in C# all the time, but they've been around for over 10 years and now also turn up in other languages like JavaScript and, apparently, C++. I use named tuples in C#, they were introduced two or three years ago, I think. I've used pattern matching too. A comment above this one mentions
var
anddynamic
, I usevar
quite often anddynamic
when I need it. Basically, if it's in the language, why shouldn't I use it if I have need for it? Personally, I don't find lambdas difficult to read at all. In fact, a simple lambda can much better convey what you're doing than a function and be better maintainable. For example:myCollection.Where(x => x.IsActive).ToList();
// vs.
myCollection.Where(IsActive).ToList();
// Elsewhere in the code.
private bool IsActive(Something x)
{
return x.IsActive;
}If the specs change, for example
x.IsActive && x.Status == Status.Done
, the first is an easy fix, the second would becomeIsActiveAndStateIsDone
or some such, which gets harder to read every time. In the case when the lambda is anExpression
, like with Entity Framework, a function can't even be parsed and you have to use a lambda.Best, Sander sanderrossel.com Migrating Applications to the Cloud with Azure arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly
-
Depends on what you think is advanced ;) I use lambdas in C# all the time, but they've been around for over 10 years and now also turn up in other languages like JavaScript and, apparently, C++. I use named tuples in C#, they were introduced two or three years ago, I think. I've used pattern matching too. A comment above this one mentions
var
anddynamic
, I usevar
quite often anddynamic
when I need it. Basically, if it's in the language, why shouldn't I use it if I have need for it? Personally, I don't find lambdas difficult to read at all. In fact, a simple lambda can much better convey what you're doing than a function and be better maintainable. For example:myCollection.Where(x => x.IsActive).ToList();
// vs.
myCollection.Where(IsActive).ToList();
// Elsewhere in the code.
private bool IsActive(Something x)
{
return x.IsActive;
}If the specs change, for example
x.IsActive && x.Status == Status.Done
, the first is an easy fix, the second would becomeIsActiveAndStateIsDone
or some such, which gets harder to read every time. In the case when the lambda is anExpression
, like with Entity Framework, a function can't even be parsed and you have to use a lambda.Best, Sander sanderrossel.com Migrating Applications to the Cloud with Azure arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly
-
I'm always fighting reading a lambda. How you would express "x => x.IsActive" in words? Maybe this can help me :-O
It does not solve my Problem, but it answers my question
How would you express a function in words? I'd say "x => x.IsActive" translates to "element is active". So in case of a Where you'd get "where element is active" and in case of an OrderBy you'd get "order by element is active". If you're looking for a more literal reading I'd say "x arrow x dot active", which is a lot easier than describing another function in detail. I believe they're even called "arrow functions" in JavaScript.
Best, Sander sanderrossel.com Migrating Applications to the Cloud with Azure arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly
-
How would you express a function in words? I'd say "x => x.IsActive" translates to "element is active". So in case of a Where you'd get "where element is active" and in case of an OrderBy you'd get "order by element is active". If you're looking for a more literal reading I'd say "x arrow x dot active", which is a lot easier than describing another function in detail. I believe they're even called "arrow functions" in JavaScript.
Best, Sander sanderrossel.com Migrating Applications to the Cloud with Azure arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly
-
OriginalGriff wrote:
I avoid using
var
anddynamic
in C#I agree. But I find it interesting how many experienced developers love using var. I feel like I must be missing something.
Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.
ZurdoDev wrote:
I agree. But I find it interesting how many experienced developers love using var. I feel like I must be missing something.
If you have a framework of Interfaces then var makes far more sense. It's very much related to Dependency Injection and flexibility. Of course, var is abused since any dev can just allow the compiler to set the type for all vars now. But, this is also the way newer (dynamic) languages go about things (Kotlin, Swift).
-
I've been digging into a javascript based application lately and tripped across some code that made extensive use of the arity (which is deprecated now), currying capabilities and anonymous functions. Took me a lonnnnnng time to understand what the intent and operation of the code was. So, out of curiosity, I'm wondering what "advanced" features of people's favorite languages they use on a regular basis, how they use them, and why? Or, which ones do you seldom/never use because.... E.g: ....I like using lambdas in C++ because .... (I can't think of a good example because I avoid them!) ....I avoid lambdas in C++ because they are often used where a simple function would be clearer and more readable (truth in advertising).
I am not sure it classifies as advanced but I have used unsafe code with casts and pointer arithmetic in C# a lot and am now switching to Span, ref structs and SIMD instructions
-
Thank you for this. I think my confusion comes because I try to read it like mathematical functions e.g. "f: x->x^3"
It does not solve my Problem, but it answers my question
Try reading it as a trimmed down function (which now also allow for an arrow instead of curly braces).
// If you'd write an actual function on a single line:
myCollection.Where(bool IsActive(MyObject x) { return x.IsActive; });// Replace the {} with =>, the ; can then also be removed:
myCollection.Where(bool IsActive(MyObject x) => return x.IsActive);// The return statement isn't necessary:
myCollection.Where(bool IsActive(MyObject x) => x.IsActive);// Neither is a function name:
myCollection.Where(bool (MyObject x) => x.IsActive);// Types can usually be inferred:
myCollection.Where((x) => x.IsActive);// Remove redundant parenthesis:
myCollection.Where(x => x.IsActive);It is pretty close to the mathematical notation. Unfortunately, it doesn't always work that well.
// Example with multiple arguments, here you need parenthesis again:
myCollection.Where((x, y) => x.IsActive && y > 10);// Sometimes the type can't be inferred:
myCollection.Where((MyObject x, int y) => x.IsActive && y > 10);// You can (always) use full argument names if you like, although a single letter is common:
myCollection.Where((collectionItem, index) => collectionItem.IsActive && index > 10);Hope this explains it even further.
Best, Sander sanderrossel.com Migrating Applications to the Cloud with Azure arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly
-
OriginalGriff wrote:
I avoid using
var
anddynamic
in C#I agree. But I find it interesting how many experienced developers love using var. I feel like I must be missing something.
Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.
I like var, it removes a lot of unnecessary noise
-
I like var, it removes a lot of unnecessary noise
RugbyLeague wrote:
it removes a lot of unnecessary noise
Like being able to look at the declaration of a variable and know what type of object it is? ;)
Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.
-
RugbyLeague wrote:
it removes a lot of unnecessary noise
Like being able to look at the declaration of a variable and know what type of object it is? ;)
Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.
That's what intellisense is for.
-
I've been digging into a javascript based application lately and tripped across some code that made extensive use of the arity (which is deprecated now), currying capabilities and anonymous functions. Took me a lonnnnnng time to understand what the intent and operation of the code was. So, out of curiosity, I'm wondering what "advanced" features of people's favorite languages they use on a regular basis, how they use them, and why? Or, which ones do you seldom/never use because.... E.g: ....I like using lambdas in C++ because .... (I can't think of a good example because I avoid them!) ....I avoid lambdas in C++ because they are often used where a simple function would be clearer and more readable (truth in advertising).
-
That's what intellisense is for.
Hover over each item each time you want to know what is? If you think that's easier and worth it, I hope you never work on my team. :-D
Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.
-
I avoid using
var
anddynamic
in C# because they are normally misused to resemble Dim in VB or shortcut strong typing because the developer is too lazy to think about maintenance..."I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
I only use var in cases where the type is either given elsewhere on the line of declaration
var frobables = new List();
var frobables2 = GetFrobables(/*params*/);or is an intermediate value whose exact type is both nasty looking and whose explicit declaration doesn't add much value.
var temp = db.tableName
.Where(x => /*filter*/)
.Select(x => new
{
x.Property1,
x.Property2,
x.Property3,
x.Property4
});I only use
dynamic
in one off code, eg single shot tools or data importers; like the fluffy languages it resembles, for anything that needs to be maintained the long term costs exceed the short term savings.Did you ever see history portrayed as an old man with a wise brow and pulseless heart, weighing all things in the balance of reason? Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful? --Zachris Topelius Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies. -- Sarah Hoyt
-
rjmoses wrote:
I had forgotten some languages don't come with a debugger.
... and those languages are hopelessly buggered at a result. :laugh:
Did you ever see history portrayed as an old man with a wise brow and pulseless heart, weighing all things in the balance of reason? Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful? --Zachris Topelius Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies. -- Sarah Hoyt
-
I only use var in cases where the type is either given elsewhere on the line of declaration
var frobables = new List();
var frobables2 = GetFrobables(/*params*/);or is an intermediate value whose exact type is both nasty looking and whose explicit declaration doesn't add much value.
var temp = db.tableName
.Where(x => /*filter*/)
.Select(x => new
{
x.Property1,
x.Property2,
x.Property3,
x.Property4
});I only use
dynamic
in one off code, eg single shot tools or data importers; like the fluffy languages it resembles, for anything that needs to be maintained the long term costs exceed the short term savings.Did you ever see history portrayed as an old man with a wise brow and pulseless heart, weighing all things in the balance of reason? Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful? --Zachris Topelius Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies. -- Sarah Hoyt
And you get far too many who just go "sod it, I can't be assed":
var x = 6;
foreach (var x in y.GetAll())
...There is a good blog post on it here: The Use and Abuse of the C# “var” Keyword[^]
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!