Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Compiler Enhancement Suggestion

Tags:

c#

.net

Imagine someone coding the following:

string s = "SomeString";
s.ToUpper();

We all know that in the example above, the call to the “ToUpper()” method is meaningless because the returned string is not handled at all. But yet, many people make that mistake and spend time trying to troubleshoot what the problem is by asking themselves “Why aren’t the characters on my ‘s’ variable capitalized”????

So wouldn’t it be great if there was an attribute that could be applied to the “ToUpper()” method that would yield a compiler error if the return object is not handled? Something like the following:

[MustHandleReturnValueAttribute]
public string ToUpper()
{
…
}

If order for this code to compile correctly the user would have to handle the return value like this:

string s = "SomeString";
string uppers = s.ToUpper();

I think this would make it crystal clear that you must handle the return value otherwise there is no point on calling that function.

In the case of the string example this may not be a big deal but I can think of other more valid reasons why this would come in handy.

What do you guys think?

Thanks.

like image 859
Rene Avatar asked Mar 09 '09 22:03

Rene


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


3 Answers

Does one call a method for its side-effects, for its return value, or for both? "Pure" functions (which have no effects and only serve to compute a return value) would be good to annotate as such, both to eliminate the type of error you describe, as well as to enable some potential optimizations/analyses. Maybe in another 5 years we'll see this happen.

(Note that the F# compiler will warn any time you implicitly ignore a return value. The 'ignore' function can be used when you want to explicitly ignore it.)

like image 112
Brian Avatar answered Oct 17 '22 03:10

Brian


If you have Resharper it will highlight things like this for you. Cant recommend resharper highly enough, it has lots of useful IDE additions especially around refactoring.

http://www.jetbrains.com/resharper/

like image 6
Dean Avatar answered Oct 17 '22 04:10

Dean


I am not sure that I like this. I have called many a method that returns a value that I choose not to capture. Adding some type of default (the compiler generates a warning when a return value is not handled) just seems wrong to me.

I do agree that something along the suggested lines might help out new programmers but adding an attribute at this point in the game will only affect a very small number of methods relative the the large existing body. That same junior programmer will never get their head around the issue when most of their unhandled return values are not flagged by the compiler.

Might have been nice way back when but the horses are out of the barn.

like image 2
andleer Avatar answered Oct 17 '22 03:10

andleer