Page 1 of 1

Code blocks must be short

PostPosted: 21 Feb 2013, 15:45
by Ursego
Don't allow code blocks to overgrow.

A code block is a fragment, placed between opening and closing operators. These operators are (in different languages):

1. Special code block delimiters: braces ({ and }) in C-like languages and, for example, BEGIN/END in PL/SQL.
2. Code branching operators (like IF ... ELSE ... END IF).
3. Looping operators (FOR ... NEXT, LOOP ... END LOOP, DO ... WHILE etc.)

It's a great idea to keep the opening and closing operators on one screen. If you see that it's impossible then think about extracting the block's code into a new function.

That solution will also decrease the indenting: for example, the fragment

PB:
Code: Select all
if [condition] then
   [very long code fragment with its own indents]
end if

C#:
Code: Select all
if ([condition])
{
   [very long code fragment with its own indents]
}

will be looking in the new function as

PB:
Code: Select all
if not [condition] then return
[very long code fragment with its own indents]


C#:
Code: Select all
if (![condition]) return;
[very long code fragment with its own indents]

But that is already the subject of the topic "Code after validations".