Skip to main content
96 votes
Accepted

In C#, why are variables declared inside a try block limited in scope?

What if your code was: try { MethodThatMightThrow(); var firstVariable = 1; } catch {} try { var secondVariable = firstVariable; } catch {} Now you'd be trying to use an undeclared ...
Ben Aaronson's user avatar
  • 6,953
67 votes

In C#, why are variables declared inside a try block limited in scope?

I know that this has been well answered by Ben, but I wanted to address the consistency POV that was conveniently pushed aside. Assuming that try/catch blocks didn't affect scope, then you would end ...
Peter M's user avatar
  • 2,039
47 votes

In OOP, isn't the 'protected' keyword required?

It depends on what you mean by "required". Access modifiers are not a necessity. You could replace every access modifier with public and most applications will work just like they did when you used ...
Flater's user avatar
  • 50.1k
43 votes

Why do most mainstream languages not support "x < y < z" syntax for 3-way Boolean comparisons?

Why is x < y < z not commonly available in programming languages? In this answer I conclude that although this construct is trivial to implement in a language's grammar and creates value for ...
Aaron Hall's user avatar
  • 5,945
40 votes
Accepted

Why do most mainstream languages not support "x < y < z" syntax for 3-way Boolean comparisons?

These are binary operators, which when chained, normally and naturally produce an abstract syntax tree like: When evaluated (which you do from the leaves up), this produces a boolean result from x &...
Karl Bielefeldt's user avatar
39 votes

Programming language where every function call/block is done in a separate thread?

every function call/new block (if clauses, loops etc) will work in a separate thread. Read a lot more about continuations and continuation-passing style (and their relation to threads or coroutines) ...
Basile Starynkevitch's user avatar
38 votes

In OOP, isn't the 'protected' keyword required?

No, it's not required: Bjarne Stroustrup, explained how he naively added protected to C++ release 1.2, thinking to provide a useful feature to class developers, just to conclude only 5 years later ...
Christophe's user avatar
  • 77.3k
37 votes

Programming language where every function call/block is done in a separate thread?

You may be interested in reading about the research into data parallel Haskell. If you search around on youtube, Simon Peyton Jones has given some interesting talks related to the subject as well. ...
Karl Bielefeldt's user avatar
29 votes
Accepted

Can Just-In-Time compilation be considered a secure feature?

JIT compilation is risky because of the W^X violation: at runtime, it is possible to generate new code, similar to an eval() in dynamic languages. But being able to dynamically generate executable ...
amon's user avatar
  • 134k
23 votes

In C#, why are variables declared inside a try block limited in scope?

Consistency-sake aside, wouldn't it make sense for us to be able to wrap our code with error handling without the need to refactor? To answer this, it's necessary to look at more than just a variable'...
Eliah Kagan's user avatar
20 votes
Accepted

Why is having a NotImplementedException a good thing?

It allows the code to compile for your method stub (regardless of the method's return type), while you get around to putting in an implementation. It also reminds you to put in the implementation, ...
Robert Harvey's user avatar
16 votes
Accepted

Logically, is there a reason why ++i++ can not be a valid expression?

You are misinterpreting GCC's error message: <source>:5:8: error: lvalue required as increment operand ++i++; ^ Note where the error message is pointing: the postfix++ part. ...
Nicol Bolas's user avatar
  • 11.9k
15 votes

Programming language where every function call/block is done in a separate thread?

This is exactly what Erlang does. It handles rejoining the threads mostly by using queues. It`s a brilliant concept but a bit difficult to wrap your head around initially if your background is more ...
GenericJam's user avatar
15 votes
Accepted

Is having a generator language facility such as `yield` a good idea?

Caveats first - C# is the language I know best, and while it has a yield that seems to be very similar to other languages' yield, there may be subtle differences I am unaware of. I am concerned ...
Telastyn's user avatar
  • 109k
14 votes

Why do most mainstream languages not support "x < y < z" syntax for 3-way Boolean comparisons?

Computer languages try to define the smallest possible units and let you combine them. The smallest possible unit would be something like x < y which gives a boolean result. You may ask for a ...
gnasher729's user avatar
  • 45.4k
14 votes

Why does HTML collapse whitespace?

In HTML, line breaks are inserted at rendering time in order to make the line length fit the width of the screen. This is in contrast to both plain text formats and layout formats like PDF where line ...
JacquesB's user avatar
  • 59.6k
13 votes

Is having a generator language facility such as `yield` a good idea?

Is having a generator language facility such as yield a good idea? I'd like to answer this from a Python perspective with an emphatic yes, it's a great idea. I'll start by addressing some questions ...
Joel Harmon's user avatar
  • 1,083
12 votes

Which programming languages support operators as first class citizens?

In some languages, operators are not very special. Instead, they are merely functions with names made up of symbols, and often don't use the normal function call syntax. Lisp: Operators are just ...
amon's user avatar
  • 134k
12 votes

Do any programming languages use types as values? Would there be any point?

Do any programming languages use types as values? Most languages that even have types (your question doesn't make sense for languages like ECMAScript, Python, Ruby, Smalltalk, Magpie, etc. which don'...
Jörg W Mittag's user avatar
11 votes

Why do most mainstream languages not support "x < y < z" syntax for 3-way Boolean comparisons?

In many programming languages, x < y is a binary expression that accepts two operands and evaluates to a single boolean result. Therefore, if chaining multiple expressions, true < z and false &...
Robert Harvey's user avatar
10 votes

Why don't empty iterables in python raise Exceptions when you try to iterate over them

This is odd to me. Why should an empty collection be treated any differently? Forcing the programmer to check if the collection is empty before doing things to it would be a widespread, problematic ...
Telastyn's user avatar
  • 109k
10 votes
Accepted

What do you need Covariant Return Types for?

Firstly, it is worth noting that the article is very likely wrong. There is currently a long list of features officially earmarked for C# 8.0, but covariant return types is not one of them. That ...
David Arno's user avatar
  • 39.3k
10 votes
Accepted

API across multiple languages. Use idiomatic code for each or support the same API structure

Conform to the language and not to the project API. The reason is simple. No matter how important a service is, how much mindshare it occupies in your consciousness or that of its maintainers... it ...
Kilian Foth's user avatar
9 votes

Why design a modern language without an exception-handling mechanism?

In my opinion, exceptions are an essential tool for detecting code errors at run time. Both in tests and in production. Make their messages verbose enough so in combination with a stack trace you can ...
yeoman's user avatar
  • 383
9 votes

Why were variable length arrays made optional in C 2011?

I've heard legends varying from "it should be optional as some small compilers should be able to be C11-compliant without VLAs" to "it was a mistake on the first place". I've never got one true and ...
Bernardo Sulzbach's user avatar
9 votes

Was Java the first programming language to support inner classes?

The first language with nested classes was Beta, a successor to Simula. They are carried even further in Beta's successor, gBeta. A modern language that carries them quite far is Newspeak. Scala's ...
Jörg W Mittag's user avatar
9 votes

In OOP, isn't the 'protected' keyword required?

Python is also a language that strongly adheres to the object oriented programming approach. It uses the classical approach of classes and objects. The thing to remember however is that any "word" is ...
paul23's user avatar
  • 1,091
8 votes

Why don't languages that have big integer support have unsigned versions?

The original reason to have unsigned integers in a language in the first place is to extend the numeric range of a fixed-size type upwards at the cost of limiting it downwards. Beneficial effects such ...
Sebastian Redl's user avatar
8 votes

Why do most mainstream languages not support "x < y < z" syntax for 3-way Boolean comparisons?

Most mainstream languages are (at least partially) object-oriented. Fundamentally, the underlying principle of OO is that objects send messages to other objects (or themselves), and the receiver of ...
Jörg W Mittag's user avatar
8 votes
Accepted

Is it important that a language is coherent to itself?

Let's be honest - nobody* is going to use your language. You're not going to get it done, or it's not going to work, or it's going to be too slow, or it's not going to be useful, or you're not going ...
Telastyn's user avatar
  • 109k

Only top scored, non community-wiki answers of a minimum length are eligible