![Page 1: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/1.jpg)
How I Code and Why Tony Van Eerd, Research In Motion
May 17, 2012
![Page 2: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/2.jpg)
How do You Code and Why? Tony Van Eerd, Research In Motion
May 17, 2012
![Page 3: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/3.jpg)
Examples That Stick/Stuck Tony Van Eerd, Research In Motion
May 17, 2012
![Page 4: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/4.jpg)
BoostCon/C++Now (2013?)
C++ Solution Station (?)
![Page 5: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/5.jpg)
May 17, 2012
www.bobdevol.com
![Page 6: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/6.jpg)
Single Responsibility Principle
Open/Closed Principle
Liskov Substitution Principle
Interface Segregation Principle
Dependency Inversion Principle
![Page 7: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/7.jpg)
8 May 17, 2012
“Thanks”
P.S. github.com/blackberry/Boost
![Page 8: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/8.jpg)
May 17, 2012
{ // // reads a tga, writes out a tga with the image copied 4 times across and 4 times down (4x4) ie 16 times. // if (argc < 3 || argc > 5) { return -1; } char const * intga = argv[1]; char const * outtga = argv[2]; int replicateX = argc >= 4 ? atoi(argv[3]) : 4; int replicateY = argc >= 5 ? atoi(argv[4]) : replicateX; TGAFileReader in(intga); static const int pixelSize = 4; // bytes per pixel - ie 32bpp //static const int replicate = 4; // 4 x 4 int dstWidth = in.getWidth() * replicateX; int dstHeight = in.getHeight() * replicateY; // final height, not height of the dst buffer! // MUST do Bassamatic BEFORE Splunker bassamatic_init(); splunker_init(); char * dst = new char[dstWidth * in.getHeight() * pixelSize]; // buffer only needs to be sourceHeight high, and we will reuse 4 times char * dstStart = dst; int sourceLineByteLength = in.getWidth() * pixelSize; // read in image, replicating it across into 4 copies for (int y = 0; y < in.getHeight(); y++) { in.readLine(dst); // copy that line across 3 times, so we have it 4 times as wide for (int r = 1; r <= replicateX; r++) { std::memcpy(dst + r * sourceLineByteLength, dst, sourceLineByteLength); } dst += replicateX * sourceLineByteLength; } // now it is copied 4 times across, but still only 1x high if (in.isUpsideDown()) { TGAFileFormat::flip_vert(dstStart, dstWidth, in.getHeight()); } // now write out the 4x wide 4 times TGAFileWriter out(outtga, dstWidth, dstHeight); for (int z = 0; z < replicateY; z++) { out.writeLines(in.getHeight(), dstStart); }
![Page 9: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/9.jpg)
10 May 17, 2012
![Page 10: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/10.jpg)
May 17, 2012
{ // // reads a tga, writes out a tga with the image copied 4 times across and 4 times down (4x4) ie 16 times. // if (argc < 3 || argc > 5) { return -1; } char const * intga = argv[1]; char const * outtga = argv[2]; int replicateX = argc >= 4 ? atoi(argv[3]) : 4; int replicateY = argc >= 5 ? atoi(argv[4]) : replicateX; TGAFileReader in(intga); static const int pixelSize = 4; // bytes per pixel - ie 32bpp //static const int replicate = 4; // 4 x 4 int dstWidth = in.getWidth() * replicateX; int dstHeight = in.getHeight() * replicateY; // final height, not height of the dst buffer! // MUST do Bassamatic BEFORE Splunker bassamatic_init(); splunker_init(); char * dst = new char[dstWidth * in.getHeight() * pixelSize]; // buffer only needs to be sourceHeight high, and we will reuse 4 times char * dstStart = dst; int sourceLineByteLength = in.getWidth() * pixelSize; // read in image, replicating it across into 4 copies for (int y = 0; y < in.getHeight(); y++) { in.readLine(dst); // copy that line across 3 times, so we have it 4 times as wide for (int r = 1; r <= replicateX; r++) { std::memcpy(dst + r * sourceLineByteLength, dst, sourceLineByteLength); } dst += replicateX * sourceLineByteLength; } // now it is copied 4 times across, but still only 1x high if (in.isUpsideDown()) { TGAFileFormat::flip_vert(dstStart, dstWidth, in.getHeight()); } // now write out the 4x wide 4 times TGAFileWriter out(outtga, dstWidth, dstHeight); for (int z = 0; z < replicateY; z++) { out.writeLines(in.getHeight(), dstStart); }
![Page 11: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/11.jpg)
May 17, 2012
{ // // reads a tga, writes out a tga with the image copied 4 times across and 4 times down (4x4) ie 16 times. // if (argc < 3 || argc > 5) { return -1; } char const * intga = argv[1]; char const * outtga = argv[2]; int replicateX = argc >= 4 ? atoi(argv[3]) : 4; int replicateY = argc >= 5 ? atoi(argv[4]) : replicateX; TGAFileReader in(intga); static const int pixelSize = 4; // bytes per pixel - ie 32bpp //static const int replicate = 4; // 4 x 4 int dstWidth = in.getWidth() * replicateX; int dstHeight = in.getHeight() * replicateY; // final height, not height of the dst buffer!
// MUST do Bassamatic BEFORE Splunker bassamatic_init(); splunker_init(); char * dst = new char[dstWidth * in.getHeight() * pixelSize]; // buffer only needs to be sourceHeight high, and we will reuse 4 times char * dstStart = dst; int sourceLineByteLength = in.getWidth() * pixelSize; // read in image, replicating it across into 4 copies for (int y = 0; y < in.getHeight(); y++) { in.readLine(dst); // copy that line across 3 times, so we have it 4 times as wide for (int r = 1; r <= replicateX; r++) { std::memcpy(dst + r * sourceLineByteLength, dst, sourceLineByteLength); } dst += replicateX * sourceLineByteLength; } // now it is copied 4 times across, but still only 1x high if (in.isUpsideDown()) { TGAFileFormat::flip_vert(dstStart, dstWidth, in.getHeight()); } // now write out the 4x wide 4 times TGAFileWriter out(outtga, dstWidth, dstHeight); for (int z = 0; z < replicateY; z++)
![Page 12: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/12.jpg)
May 17, 2012
{ // // reads a tga, writes out a tga with the image copied 4 times across and 4 times down (4x4) ie 16 times. // if (argc < 3 || argc > 5) { return -1; } char const * intga = argv[1]; char const * outtga = argv[2]; int replicateX = argc >= 4 ? atoi(argv[3]) : 4; int replicateY = argc >= 5 ? atoi(argv[4]) : replicateX; TGAFileReader in(intga); static const int pixelSize = 4; // bytes per pixel - ie 32bpp //static const int replicate = 4; // 4 x 4 int dstWidth = in.getWidth() * replicateX; int dstHeight = in.getHeight() * replicateY; // final height, not height of the dst buffer!
// MUST do Bassamatic BEFORE Splunker // *otherwise* the splunker table… bassamatic_init(); splunker_init(); char * dst = new char[dstWidth * in.getHeight() * pixelSize]; // buffer only needs to be sourceHeight high, and we will reuse 4 times char * dstStart = dst; int sourceLineByteLength = in.getWidth() * pixelSize; // read in image, replicating it across into 4 copies for (int y = 0; y < in.getHeight(); y++) { in.readLine(dst); // copy that line across 3 times, so we have it 4 times as wide for (int r = 1; r <= replicateX; r++) { std::memcpy(dst + r * sourceLineByteLength, dst, sourceLineByteLength); } dst += replicateX * sourceLineByteLength; } // now it is copied 4 times across, but still only 1x high if (in.isUpsideDown()) { TGAFileFormat::flip_vert(dstStart, dstWidth, in.getHeight()); } // now write out the 4x wide 4 times TGAFileWriter out(outtga, dstWidth, dstHeight);
![Page 13: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/13.jpg)
My favourite comment word is
Otherwise.
Thus…
![Page 14: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/14.jpg)
May 17, 2012
case DOWN: ... break; case MOVE: // disable popup menu for this touch sequence, // *otherwise* if we got a HOVER later (user stopped moving for a while) // then we would bring up the Menu, // and the UX team says we don't want the popup menu to happen after a MOVE // (ie scroll then pause should not bring up the menu) _disablePopupMenu = true; ... break; case HOVER: if ( !_disablePopupMenu) { showPopupMenu(); } break; case UP: _disablePopupMenu = false; // reset ... break;
![Page 15: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/15.jpg)
May 17, 2012
case DOWN: ... break; case MOVE: // disable popup menu for this touch sequence, // *otherwise* if we got a HOVER later (user stopped moving for a while) // then we would bring up the Menu, // and the UX team says we don't want the popup menu to happen after a MOVE // (ie scroll then pause should not bring up the menu) _movedSinceDown = true; ... break; case HOVER: if ( !_movedSinceDown) { showPopupMenu(); } break; case UP: _movedSinceDown = false; // reset ... break;
![Page 16: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/16.jpg)
May 17, 2012
case DOWN: ... break; case MOVE: // disable popup menu for this touch sequence, // *otherwise* if we got a HOVER later (user stopped moving for a while) // then we would bring up the Menu, // and the UX team says we don't want the popup menu to happen after a MOVE // (ie scroll then pause should not bring up the menu) _disablePopupMenu = true; ... break; case HOVER: if ( !_disablePopupMenu) { showPopupMenu(); } break; case UP: _disablePopupMenu = false; // reset ... break;
![Page 17: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/17.jpg)
May 17, 2012
case DOWN: ... break; case MOVE: // disable popup menu for this touch sequence, // *otherwise* if we got a HOVER later (user stopped moving for a while) // then we would bring up the Menu, // and the UX team says we don't want the popup menu to happen after a MOVE // (ie scroll then pause should not bring up the menu) _movedSinceDown = true; ... break; case HOVER: if ( !_movedSinceDown) { showPopupMenu(); } break; case UP: _movedSinceDown = false; // reset ... break;
![Page 18: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/18.jpg)
May 17, 2012
case DOWN: ... break; case MOVE: // disable popup menu for this touch sequence, // *otherwise* if we got a HOVER later (user stopped moving for a while) // then we would bring up the Menu, // and the UX team says we don't want the popup menu to happen after a MOVE // (ie scroll then pause should not bring up the menu) _disablePopupMenu = true; ... break; case HOVER: if ( !_disablePopupMenu) { showPopupMenu(); } break; case UP: _disablePopupMenu = false; // reset ... break;
![Page 19: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/19.jpg)
May 17, 2012
case DOWN: ... break; case MOVE: // disable popup menu for this touch sequence, // *otherwise* if we got a HOVER later (user stopped moving for a while) // then we would bring up the Menu, // and the UX team says we don't want the popup menu to happen after a MOVE // (ie scroll then pause should not bring up the menu) _disablePopupMenu = true; ... break; case HOVER: if ( !_disablePopupMenu) { showPopupMenu(); } break; case UP: _disablePopupMenu = false; // reset ... break;
Think about other code that needs to
disable the popup menu.
Does it also set _disablePopupMenu?
or popupMenu.disable()?
who resets it?
![Page 20: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/20.jpg)
May 17, 2012
case DOWN: ... break; case MOVE: // disable popup menu for this touch sequence, // *otherwise* if we got a HOVER later (user stopped moving for a while) // then we would bring up the Menu, // and the UX team says we don't want the popup menu to happen after a MOVE // (ie scroll then pause should not bring up the menu) _movedSinceDown = true; ... break; case HOVER: if ( !_movedSinceDown) { showPopupMenu(); } break; case UP: _movedSinceDown = false; // reset ... break;
Alternatively, think about other code
that needs to set _movedSinceDown…
…Hopefully there is none!
![Page 21: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/21.jpg)
May 17, 2012
case DOWN: ... break; case MOVE: _movedSinceDown = true; ... break; case HOVER: // the UX team says we don't want the popup menu to happen after a MOVE // (ie scroll then pause should not bring up the menu) if ( !_movedSinceDown) { showPopupMenu(); } break; case UP: _movedSinceDown = false; // reset ... break;
![Page 22: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/22.jpg)
May 17, 2012
case DOWN: ... break; case MOVE: break; case HOVER: break; case DOWNHOVER: // or some better name showPopupMenu(); break; case UP: ... break;
![Page 23: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/23.jpg)
“Separation of Concerns”
Thus…
![Page 24: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/24.jpg)
May 17, 2012
if ( !_disablePopupMenu)
![Page 25: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/25.jpg)
Avoid Double Negatives
Thus…
![Page 26: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/26.jpg)
…
![Page 27: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/27.jpg)
How a Button invokes a „click‟ action:
virtual Button::invokeAction()
virtual Invokeable::invokeAction() // Button : private Invokeable {};
(*invokeAction)(theirdata) // C styles
_listener->invokeAction()
boost::function
“callable” // template<typename F> onClick(F f); // converts to function<> for you
os/framework_sendmessage(destId, buttonId, actionId, etc)
os/framework_postmessage(destId, buttonId, actionId, etc) //**
queue a boost::function to a threaded work queue //**
condvar //**
boost::signal<>, Qt signal, framework signal
member.invokeAction() where Button<T> has a T member.
Base::invokeAction() // template <typename Base> Button : Base {};
invokeAction()
![Page 28: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/28.jpg)
May 17, 2012
template <typename ActionFramework> class Button : ActionFramework // use CRTP? { ... void handleInput(...) { if (...decide to invoke...) { invokeAction(); } } };
![Page 29: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/29.jpg)
Separation of Concerns (?)
Inversion of Everything (?)
Top Down (?)
I don’t care / not my problem
Thus…
![Page 30: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/30.jpg)
May 17, 2012
UIFView
CurrentStyleView
UIFImageView
UIFHandleSizeChange
UIFHappyPixelsAutoAnytimeDevice
UIFViewManager
ImageInterface
DeviceUpdater
ViewSettings
SomeoneHandleSizeChange
SimpleImageView
UIFHappyPixels AnytimeDevice
HappyPixelsImage
HappyPixelsAutoDevice
HappyPixelsDevice
GetDCFromUIFView
ViewManager
HappyPixels
UIFHappyPixelsDevice
SomeoneGetMeTheDC
UIFHappyPixels HasaIsaHappyPixels
![Page 31: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/31.jpg)
Thus…
?!
![Page 32: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/32.jpg)
Speaking of Buttons…
![Page 33: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/33.jpg)
May 17, 2012
class CheckBox { public: bool isChecked() { ... } };
![Page 34: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/34.jpg)
CheckBox::isChecked()
you‟re doing it wrong.
Sean Parent (paraphrased)
![Page 35: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/35.jpg)
No raw loops
Speaking of Sean Parent…
![Page 36: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/36.jpg)
OH NO! Pointers!
How to go from Java to C++…
![Page 37: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/37.jpg)
OH NO! Pointers!
Oh, No Pointers.
How to go from Java to C++…
![Page 38: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/38.jpg)
OH NO! Pointers!
Oh, No Pointers.
Value Types.
How to go from Java to C++…
![Page 39: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/39.jpg)
Value Types /
“shared_ptr is as good as a global”
Speaking of Sean Parent…
![Page 40: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/40.jpg)
Speaking of Sean Parent…
Value Types /
“shared_ptr considered harmful”
![Page 41: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/41.jpg)
Speaking of „is‟…
![Page 42: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/42.jpg)
May 17, 2012
class LockFreeList { public: bool isEmpty() // or just empty() { ... } };
![Page 43: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/43.jpg)
May 17, 2012
{ if (!list.isEmpty()) { Foo foo = list.pop(); ... } };
![Page 44: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/44.jpg)
May 17, 2012
class LockFreeList { public: bool wasEmpty() { ... } };
![Page 45: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/45.jpg)
was not is
in threaded programming.
Thus…
![Page 46: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/46.jpg)
from not to.
Also…
![Page 47: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/47.jpg)
…
![Page 48: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/48.jpg)
M + N vs M x N
![Page 49: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/49.jpg)
M + N vs M x N is for Unit Tests
![Page 50: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/50.jpg)
Examples That Suck Tony Van Eerd, Research In Motion
May 17, 2012
![Page 51: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/51.jpg)
May 17, 2012
__try { // guarded code } __except ( expression ) { // exception handler code }
“Structured Exception Handling” (http://msdn.microsoft.com/en-us/library/s58ftw19%28v=vs.80%29.aspx)
MS Windows
![Page 52: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/52.jpg)
May 17, 2012
__try { // guarded code } __except ( expression ) { // exception handler code }
“Structured Exception Handling” (http://msdn.microsoft.com/en-us/library/s58ftw19%28v=vs.80%29.aspx)
OS_TRY { // guarded code } OS_CATCH() { // exception handler code }
MS Windows Portable
![Page 53: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/53.jpg)
May 17, 2012
class OSCatcher { static atomic<jmp_buf*> _current; jmp_buf _local, *_prev; bool _ok; OSCatcher() : _ok(true) { _prev = _current.exchange(&_local); if (setjmp(_local)) { // example! not thread safe _ok = false; _current = _prev; } } operator bool() { return _ok; } };
![Page 54: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/54.jpg)
May 17, 2012
class OSCatcher { static atomic<jmp_buf*> _current; jmp_buf _local, *_prev; bool _ok; OSCatcher() : _ok(true) { _prev = _current.exchange(&_local); if (setjmp(_local)) { // example! not thread safe _ok = false; _current = _prev; } } operator bool() { return _ok; } }; #define OS_TRY if (OSCatcher catcher) #define OS_CATCH() else
![Page 55: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/55.jpg)
May 17, 2012
class OSCatcher { static atomic<jmp_buf*> _current; jmp_buf _local, *_prev; bool _ok; OSCatcher() : _ok(true) { _prev = _current.exchange(&_local);; if (setjmp(_local)) { _ok = false; _current = _prev; } } operator bool() { return _ok; } }; #define OS_TRY if (OSCatcher catcher) #define OS_CATCH() else
int main() { signal(SIGINT, sigint_handler); ... } sigint_handler() { longjmp(*OSCatcher::_current, 1); }
![Page 56: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/56.jpg)
Can != Should.
Thus…
![Page 57: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/57.jpg)
May 17, 2012
int func() { static Once once; if (Once::Guard guard(once)) { // init… } ... }
![Page 58: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/58.jpg)
May 17, 2012
#define once static Once UNIQUE(once); \ if (Once::Guard guard(once)) int func() { once { // init… } ... }
![Page 59: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/59.jpg)
MACROS are evil
(Mostly unrelated actually…)
![Page 60: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/60.jpg)
Use Locks
As Always…
![Page 61: How I Code and Why - GitHub · How I Code and Why Tony Van Eerd, Research In Motion May 17, 2012 . How do You Code and Why? Tony Van Eerd, Research In Motion May 17, 2012 . Examples](https://reader033.vdocuments.us/reader033/viewer/2022050203/5f56ac98b308aa03f06290b9/html5/thumbnails/61.jpg)
Experiment
Thank you for participating.
Thus…