HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Nested IF code in GUI Controls

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
codenestedguicontrols

Problem

I'm writing GUI controls and there are many places where there are many nested ifs checking for some result.

function TMyObject.GetCursor: TCursor;
begin
  if CanDragX then
  begin
    if CanDragY then
      Result := crSizeAll
    else
      Result := crSizeWE;
  end
  else if CanDragY then
    Result := crSizeNS
  else
  if CanClick then
    Result := crHandPoint
  else
    Result := crArrow;
end;


How would you format/rewrite this code?

Solution

I would do this:

function TMyObject.GetCursor: TCursor;
begin
  if CanDragX and CanDragY then
    Result := crSizeAll
  else if CanDragX then
    Result := crSizeWE
  else if CanDragY then
    Result := crSizeNS
  else
    if CanClick then
      Result := crHandPoint
    else
      Result := crArrow;
end;


But really it is a matter of style and what is most readable to you (and the person who will maintain it).

Code Snippets

function TMyObject.GetCursor: TCursor;
begin
  if CanDragX and CanDragY then
    Result := crSizeAll
  else if CanDragX then
    Result := crSizeWE
  else if CanDragY then
    Result := crSizeNS
  else
    if CanClick then
      Result := crHandPoint
    else
      Result := crArrow;
end;

Context

StackExchange Code Review Q#3954, answer score: 5

Revisions (0)

No revisions yet.