## [Prog] Separating mechanism from policy (microscopical view) - function implementation.

Essay/Software 2011.05.17 14:13

Separating mechanism from policy is very important issue of SW design.
This is also true for microscopic area - implementing function.
Here is one of simplest example - function that calculates area of rectangle.

```int rect_area(int left, int top, int right, int bottom) {
return (right - left) * (bottom - top);
}```

Simple, isn't it?
But, this function doesn't have any exception/error handling.

```int rect_area(int left, int top, int right, int bottom) {
if (left >= right)
left = right;
if (top >= bottom)
top = bottom;
return (right - left) * (bottom - top);
}```

It's seems good.
Soon after, another function that calculates area of rectangle is needed.
But, this function should return error value if input rectangle is invalid.
I think quick solution is

```int rect_area2(int left, int top, int right, int bottom) {
if (left > right || top > bottom)
return -1;
return (right - left) * (bottom - top);
}```

But, in this solution, code for calculating area of rectangle is duplicated.
At above example, this is just one-line code. So, duplicating is not a big deal.
But, it's not good in terms of code structure.
Why did this happen?
Calculating rectangle area is 'Mechanism'.
But, exception/error handling is 'Policy' at this example.
So, above example should be implemented like below.

```static inline int _rect_area(int left, int top, int right, int bottom) {
return (right - left) * (bottom - top);
}

int rect_area(int left, int top, int right, int bottom) {
if (left >= right)
left = right;
if (top >= bottom)
top = bottom;
return _rect_area(left, top, right, bottom);
}
int rect_area2(int left, int top, int right, int bottom) {
if (left > right || top > bottom)
return -1;
return _rect_area(left, top, right, bottom);
}```

Can you know the difference?
'_rect_area' is implementation of pure 'Mechanism'.
And policy is implemented at each interface function.
Even for simple function, developer should consider CONCEPT of separating Mechanism from Policy.

#### 'Essay > Software' 카테고리의 다른 글

 [Essay] SW 엔지니어 역량 측정 - 02  (0) 2011.06.01 2011.05.31 2011.05.17 2011.04.22 2011.03.25 2011.03.02
tags : function

## [Win32] Function Calling Convention

Domain/Win32 2008.12.03 14:45

VC[[ blog 이사 과정에서 정확한 posting날짜가 분실됨. 년도와 분기 정도는 맞지 않을까? ]]

__stdcall vs. __cdecl

refer below page..

http://unixwiz.net/techtips/win32-callconv.html

#### 'Domain > Win32' 카테고리의 다른 글

 windowsXP 에서 모든 동영상이 흑백으로만 재생될때...  (0) 2011.04.14 2008.12.03 2007.06.27 2007.06.25 2007.06.23 2007.06.20