In most programming languages, this is the practice of placing the else keyword between two curly braces in order to save vertical space. According to a quick web search, this term originates from the Perl community, although it can apply to many programming languages (C, C++, C#, and Java, to name a few).

The use or disuse of cuddled elses is usually codified in a style guide. For example, where I work, cuddled elses are the standard, so the following is proper code.

if (condition) {
    code segment A;
} elsif (another-condition) {
    code segment B;
} else {
    code segment C;
}

By contrast, some Perl programmers prefer the semi-cuddled else or uncuddled else method. (ariels says that Larry Wall prefers this method, according to the perlstyle man page.) By the way, "semi-cuddled else" is a term I've heard at work, but the only matches that Google finds for the term are in anime erotic fan-fiction, so I don't think that it's a widely-accepted term. The same example above with semi-cuddled elses looks like:

if (condition) {
    code segment A;
}
elsif (another-condition) {
    code segment B;
}
else {
    code segment C;
}

This allows lines to be cut and pasted easily without chopping a line halfway. However, it takes up more vertical space. The other extreme of the spectrum is Allman style as it is called in the Jargon file (topic indent style) which puts braces on their own lines:

if (condition) 
{
    code segment A;
}
elsif (another-condition)
{
    code segment B;
}
else
{
    code segment C;
}

Personally, I like cuddled elses, but nobody will ever settle on one indenting style. Choose one style and stick with it.


Sources: the Jargon file, perltidy man page, perlstyle Perldoc page, internal style guides.

Log in or registerto write something here or to contact authors.