Post removed.
Post removed.
Post removed.
Last edited by TheRLG on Fri Dec 28, 2007 7:34 am, edited 2 times in total.
or, even better:
Code: Select all
irr::core::rect<irr::s32> EasyRect(irr::s32 X, irr::s32 Y, irr::s32 Width, irr::s32 Height)
{
return irr::core::rect<irr::s32>(X,Y,X+Width,Y+Height);
}-
Baal Cadar
- Posts: 377
- Joined: Fri Oct 28, 2005 10:28 am
- Contact:
-
Baal Cadar
- Posts: 377
- Joined: Fri Oct 28, 2005 10:28 am
- Contact:
Actually I proposed (and still do) to not create a seperate function at all for this task. Instead do what EasyRect does in-place. This little complexity doesn't warrant a dedicated function imho. I am all for functional decomposition, but this drives it too far.
Rationale: Source code has to be obvious. One should see on the first glance what is done. Question is, whether it is easier to read:
or
Without prior knowledge about what EasyRect does, the code above is easier to read. Else it is equal. So you win nothing and loose a bit in this aspect.
Performance is equal when the function is inlined, less else.
Other aspects don't play a role here, imho.
So at the end of the day EasyRect usage makes the code slightly worse.
In an effort to be a bit constructive here, I propose two changes. Make it inline and make it a template function.
Rationale: Source code has to be obvious. One should see on the first glance what is done. Question is, whether it is easier to read:
Code: Select all
guiEnv->addButton(rect<s32>(left, top, left + width, top + height));
Code: Select all
guiEnv->addButton(EasyRect(left, top, width, height));
Performance is equal when the function is inlined, less else.
Other aspects don't play a role here, imho.
So at the end of the day EasyRect usage makes the code slightly worse.
In an effort to be a bit constructive here, I propose two changes. Make it inline and make it a template function.
Code: Select all
template<typename T> inline irr::core::rect<T> EasyRect(T x, T y, T width, T height)
{
return irr::core::rect<T>(x, y, x + width, y + height);
}
-
xterminhate
- Posts: 206
- Joined: Thu Sep 01, 2005 9:26 pm
- Location: France
Avoid multiple copy, use constness/references :
Code: Select all
irr::core::rect<irr::s32> EasyRect( irr::s32 const & X, irr::s32 const & Y, irr::s32 const & Width, irr::s32 const & Height)
{
return irr::core::rect<irr::s32>(X,Y,X+Width,Y+Height);
}Return to Irrlicht after years... I'm lovin it.
It's hard to be a Man !
Si vis pacem para belum
It's hard to be a Man !
Si vis pacem para belum
-
Baal Cadar
- Posts: 377
- Joined: Fri Oct 28, 2005 10:28 am
- Contact:
RabidLockerGnome, both are correct, though your proposed order is more common and thus to be preferred.
Anyway, I'd not use const& here. There is no positive effect whatsoever. Whether pointers are put on the stack or their contents is irrelevant here, since s32 is the same size as a pointer anyway. You also don't safe something on execution. Const references are useful, when the argument type is bigger than a pointer. Const correctness is a nice thing and can be done on the value type arguments too. Though I'd ommit those for the function prototype for readability reasons. There it doesn't matter anyway, since the function has call-by-value semantics. You can use const in the actual implementation then if you want.
Anyway, I'd not use const& here. There is no positive effect whatsoever. Whether pointers are put on the stack or their contents is irrelevant here, since s32 is the same size as a pointer anyway. You also don't safe something on execution. Const references are useful, when the argument type is bigger than a pointer. Const correctness is a nice thing and can be done on the value type arguments too. Though I'd ommit those for the function prototype for readability reasons. There it doesn't matter anyway, since the function has call-by-value semantics. You can use const in the actual implementation then if you want.
That's really useful. From time to time I wish I had decided to make the rect work with a position and a dimension rather than two positions. But when I wrote the software renderer and the gui (which was the first thing I did, before even doing D3D or OpenGL), it was better that way. But that solution is a very nice workaround.
