Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Some programming languages have constructs that because of their frequent use are often mistaken for language features.

For example, many people think that self is a keyword in Python, but it's just the conventional name for the first argument to a method (you can use any variable name you want):

class Complex:
    def __init__(self, realpart, imagpart):
        self.r = realpart
        self.i = imagpart

Another example: when I was learning R, I thought the syntax for filtering a vector was kind of weird:

d[d %% 2 == 0] # even elements only

At first I thought d %% 2 == 0 was some sort of lambda that gets magically applied to every element of d, until I realized that this is a straightforward application of R's vectorized operations: d %% 2 == 0 returns a vector like [TRUE, FALSE, FALSE, ...], and then d just uses that list to filter values that are at positions marked as TRUE.

What are some other constructs that may first appear to be specially designed language features, but are just straightforward applications of the language's core syntax & semantics?

share|improve this question

5 Answers 5

up vote 9 down vote accepted

else if

for instance in C#.

If you learned BASIC (including Visual Basic) first, you'd be likely to think else if functions like a special keyword, because ElseIf is a keyword in BASIC. In BASIC the rule is that all multi-line If statements must end in End If, so we would have to nest If statements in the Else clause like this:

If foo = bar Then
    foo = foo + 1
    bar = bar + 1
Else 
    If foo > bar Then
        foo = foo - 1
        bar = bar + 1
    Else
        If foo < bar Then
            foo = foo + 1
            bar = bar - 1
        End If
    End If
End If

but by using the ElseIf keyword, we can eliminate the extra End Ifs, like this:

If foo = bar Then
    foo = foo + 1
    bar = bar + 1
ElseIf foo > bar Then
    foo = foo - 1
    bar = bar + 1
ElseIf foo < bar Then
    foo = foo + 1
    bar = bar - 1
End If

Now compare to C-style languages like C#. There, the curly braces { and } serve a similar role to BASIC's Then and End If. However, the rule for when braces are required differs from the rule for when End If is required. C# doesn't care if you have line-breaks in your else clause, it just cares if it can be considered a single statement. So you can write:

if (foo == bar)
{
    foo = foo + 1;
    bar = bar + 1;
}
else if (foo > bar)
{
    foo = foo - 1;
    bar = bar + 1;
}
else if (foo < bar)
{
    foo = foo + 1;
    bar = bar - 1;
}

without any extra braces, because the else of your second if is already a single statement, equivalent (up to meaningless line breaks) to:

if (foo < bar) { foo = foo + 1; bar = bar - 1; }

and likewise the else of your first if is a single statement, equivalent to:

if (foo > bar) { foo = foo - 1; bar = bar + 1; } else if (foo < bar) { foo = foo + 1; bar = bar - 1; }

In other words, else if isn't some special keyword (like it was in BASIC), it's just an else followed by an if.

share|improve this answer
    
Yep - I constantly typed elseif when I first started C. –  Michael K Nov 4 '10 at 12:13
    
I moved from Perl to PHP and had to break myself from saying elsif. That's not just a language keyword, it's spelling of that keyword that's weird and unique! –  Dan Ray Nov 4 '10 at 12:30

Defining constants as all upper case, e.g. in C:

#define SECONDS_IN_DAY   86400

The same convention (with different syntax) is used in many other languages (e.g. const in C++, define in PHP), but whether the identifier is uppercase is not enforced by either the preprocessor or compiler. CamelCase is commonly used for consts in C# -- again it is a convention, not something enforced by the compiler.

Ruby does assume a identifier beginning with an upper-case is a constant, and will issue a warning if it's value is changed.

share|improve this answer

in Delphi, classes almost always start with T. TObject, TStringList, TButton, etc. Its a carry-over from Turbo Pascal days. Pointers to objects start with P. PObject, etc. Its not required, but its so commonly adhered to that it wouldnt suprise me if some Delphi developers think its a language requirement.

share|improve this answer

NULL in C or C++ is often mistaken for something magical, but its' a preprocessor defined as 0 or (void *)0. When learning C or C++, you test pointers against NULL. Later on, you learn that you can just do if (ptr).

In other languages such as Objective-C or C#, nil/null is part of the language, even if it does essentially the same thing.

share|improve this answer
    
I had always thought that the preprocessor defined it as (void *)0 to discern it as a pointer. Now I'm going to go check. –  Jesse C. Slicer Nov 4 '10 at 17:26
    
creepy: #ifdef __cplusplus #define NULL 0 #else #define NULL ((void *)0) #endif –  Jesse C. Slicer Nov 4 '10 at 17:29
    
Sorry, yes, my typo there. –  JBRWilkinson Nov 5 '10 at 23:08
1  
You should add that this will be fixed in C++0x where nullptr is defined. –  Klaim Nov 6 '10 at 1:12

The standard C library.

After programming C for 10 years or so reading the sourcecode of software by Dan Bernstein made me realize that you can use C without using printf, malloc and all the other stuff we take for granted.

share|improve this answer
    
But what's the use of it if you can't output a thing? –  Christian Mann Nov 6 '10 at 7:33
1  
Directly call into the kernel. No need for printf if you can use the write(2) syscall. –  mdorseif Nov 6 '10 at 9:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.