Sign in to follow this  
JohnnyCode

programming language feature idea

Recommended Posts

JohnnyCode    1046

Hi,

 

I got an idea about code language idea and it fascinates me, so I would be happy to share it and get critics since I think it is a reasonable feature.

C was a language that promissed runtime variables allocations, as opposed to Pascal. Leading noob programmers to pit of despair. but utilizing a rather fast and absolute control over RAM. Java and C# took some hidden managment of this practice to save programmer and production from ML bugs. Those two languages also eased function definition. And this is my idea -"Inline Function Definition".

Many times I was dived in a large function full of code and I decided I need a function, precisely for the scope of the function I was in, or the class I was in. For example 

public void Instructions()
{
.
.
.
string thelogiclyvarying="constblaconstblacnsta"+ id+"ocnstbla"+onto+"asfloaksf"+wouldlike+"constbla"; /* those variables on right side do not exist and exist and vary in cycle */
//so I would do
public string Compose(string id, string onto,string wouldlike)
{
return "constblaconstblacnsta"+ id+"ocnstbla"+onto+"asfloaksf"+wouldlike+"constbla"; 
}
while (sqlreader.Read())
{
m_sContent+=Compose(sqlreader["odID"],sqlreader["ontorequest"],sqlreader["wouldlike"]);
}
}

You could say I could just write the line- defintion of the function, to the cycle, but I am using this string in more places of the code, plus, if you got habit of doing it, you could clear the code much, plus the coding enviroment could hide the definitions or such.

 

So would you want this compiler feature?

Would it lead to mess or is it against code writing culture?

Share this post


Link to post
Share on other sites
Nypyren    12063
You mean like this?
var m_sContent = string.Join("\n", sqlreader.Select(x => Compose(x.odID, x.ontorequest, x.wouldLike)));
Or this?
var m_sContent = string.Join("\n", sqlreader.Select(x => string.Format("constblaconstblacnsta{0}ocnstbla{1}asfloaksf{2}constbla", x.odId, x.ontorequest, x.wouldLike)));
Or this?
Func<dynamic,string> compose = x => string.Format("{0} {1} {2}", x.odId, x.ontorequest, x.wouldLike));
var m_sContent = string.Join("\n", sqlreader.Select(compose));
Seriously. C# has got you covered. Edited by Nypyren

Share this post


Link to post
Share on other sites
Alpha_ProgDes    6921

You mean like this?
 

 

var m_sContent = string.Join("\n", sqlreader.Select(x => Compose(x.odID, x.ontorequest, x.wouldLike)));

//Or this?

var m_sContent = string.Join("\n", sqlreader.Select(x => string.Format("constblaconstblacnsta{0}ocnstbla{1}asfloaksf{2}constbla", 
               x.odId, x.ontorequest, x.wouldLike)));

//Or this?

Func<SQLElement,string> compose = x => string.Format("{0} {1} {2}", x.odId, x.ontorequest, x.wouldLike));
var m_sContent = string.Join("\n", sqlreader.Select(compose));
 

 




Seriously. C# has got you covered.

Edited by Alpha_ProgDes

Share this post


Link to post
Share on other sites
Cornstalks    7030
Yup, C# already has those, as others have mentioned. Nypyren has posted some decent idomatic solutions, but I'll give a more "direct" translation from your posted code to real C# code (assuming I don't make a mistake):
public void Instructions()
{
    string thelogiclyvarying="constblaconstblacnsta"+ id+"ocnstbla"+onto+"asfloaksf"+wouldlike+"constbla"; /* those variables on right side do not exist and exist and vary in cycle */
    // And all the magic happens in the next line...
   Func<string, string, string, string> compose = (id, onto, wouldlike) =>
   {
      return "constblaconstblacnsta"+ id+"ocnstbla"+onto+"asfloaksf"+wouldlike+"constbla"; 
   }

   while (sqlreader.Read())
   {
      m_sContent += compose(sqlreader["odID"],sqlreader["ontorequest"],sqlreader["wouldlike"]);
   }
}
Other languages have lambda functions too. C++, C#, every functional language, etc.

Share this post


Link to post
Share on other sites
Ravyne    14300

Lambda is what we get in C++11, and aside from the somewhat odd syntax it does everything you're asking. In Pre-C++11 days you could accomplish the same by nesting a functor class, although the generally-large amount of boiler-plate code needed to do so was such that it was often impractical--works exactly the same though, I once had to backport some early C++11 code to a compiler that didn't support lambda syntax, so I became quite familiar with that boilerplate unfortunately.

 

I'd like to have plain old local functions though, and lots of languages have them (D is the only C-like language with direct support). Its not even that they're all that useful--most of the places you'd use them you want a lambda anyway--its more a matter of orthogonality to me, and of giving programmers the means to manage scope and such as they see fit.

Share this post


Link to post
Share on other sites
cr88192    1570

Lambda is what we get in C++11, and aside from the somewhat odd syntax it does everything you're asking. In Pre-C++11 days you could accomplish the same by nesting a functor class, although the generally-large amount of boiler-plate code needed to do so was such that it was often impractical--works exactly the same though, I once had to backport some early C++11 code to a compiler that didn't support lambda syntax, so I became quite familiar with that boilerplate unfortunately.

 

I'd like to have plain old local functions though, and lots of languages have them (D is the only C-like language with direct support). Its not even that they're all that useful--most of the places you'd use them you want a lambda anyway--its more a matter of orthogonality to me, and of giving programmers the means to manage scope and such as they see fit.

 

C++11 lambda does, of course, have its slightly funky syntax and semantics though, though it has been done worse... (*cough* Java *cough*).

granted, I suspect the absence of GC may play into this some (as well as keeping it parsable).

 

as for lambda vs nested functions:

for a lot of languages where lambdas are supported, the compiler is smart enough at least to generally figure out where a cheaper (non-closure) nested-functions can be used instead.

 

or, if you mean the ability to declare nested functions like normal functions/methods (vs having to use a variable and store a lambda into it), then yes, this is a useful feature.

Share this post


Link to post
Share on other sites
JohnnyCode    1046

You mean like this?

var m_sContent = string.Join("\n", sqlreader.Select(x => Compose(x.odID, x.ontorequest, x.wouldLike)));
Or this?
var m_sContent = string.Join("\n", sqlreader.Select(x => string.Format("constblaconstblacnsta{0}ocnstbla{1}asfloaksf{2}constbla", x.odId, x.ontorequest, x.wouldLike)));
Or this?
Func<dynamic,string> compose = x => string.Format("{0} {1} {2}", x.odId, x.ontorequest, x.wouldLike));
var m_sContent = string.Join("\n", sqlreader.Select(compose));
Seriously. C# has got you covered.

Thanks, but it seems too unreadable to me, and my production leaders would not be happy about this habit, plus, I myself am not quite getting it, I think I am getting old :(. Also, the last example seems to be a rather instance of function that gets disposed out of the block, if I am not mistaken, I would rather have possibility to immediately define a function in the scope of the class, so it would not be:
 redefintiion able, instance utilized... and so on. Thanks, it seems that this practice is not very common upon programers, and it did not get maintained to level of effective and reasonable usage .

Share this post


Link to post
Share on other sites
tstrimp    1798

Thanks, but it seems too unreadable to me, and my production leaders would not be happy about this habit, plus, I myself am not quite getting it, I think I am getting old sad.png. Also, the last example seems to be a rather instance of function that gets disposed out of the block, if I am not mistaken, I would rather have possibility to immediately define a function in the scope of the class, so it would not be:

 redefintiion able, instance utilized... and so on. Thanks, it seems that this practice is not very common upon programers, and it did not get maintained to level of effective and reasonable usage .

 

It's actually quite common with modern C# programmers. I use lambda's quite frequently. 

Share this post


Link to post
Share on other sites
Ravyne    14300

lambdas like those are increadibly common already, and gaining steam. I would also suggest that any 'production leaders' affraid of a little new syntactic sugar every now and then ought to go back amongst the dinosours and take their stone-age coding practices with them. Lambdas are not just modern fluff, or an odd way of defining functions, they enable one to write the kind of code we always meant to but never did before because it was too verbose.

 

Anyway, what's the advantage your hypothetical but non-existant approach holds over the new-to-you but here-today one?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this