ctodx

This Blog

News

Favorite Posts

Archives

August 2010 - Posts

  • DevExpress Newsletter 32: Message from the CTO

    This time, I start a two-part message. The first part is about writing small single-task methods:

    The Composed Method pattern

    One of the things I've adopted over the years in my own programming is the use of small methods -- that is, small as in few lines of code. It's made much easier by the use of a good Extract Method refactoring tool. In my case, that's CodeRush, and it's become second nature to apply Extract Method as I'm writing the code since there no cognitive friction in using it.

    I was delighted to learn recently that this practice is known as the Composed Method pattern. It was first devised and promoted by Kent Beck in Smalltalk. In essence, there are three rules to the pattern:

       1. Divide your programs into methods that only perform one identifiable task.
       2. Keep all of the operations in a method at the same level of abstraction.
       3. This will result in programs with many small methods, each a few lines long.

    Why do this?

    Firstly: it's really easy to write unit tests for simple methods. The more testing you have the more confident you'll be that your application is good and will work both before and after maintenance.

    Secondly: it's easier to read a small self-contained method and understand what it does. It's also easier to verify the code in your head, to deskcheck it if you like, increasing your confidence in the validity of the code.

    Thirdly: in writing small methods, it's easier to give them understandable names. Once you have those, you make the code easier to read and avoid the need for comments -- and we all know that comments get stale and therefore meaningless.

    Fourthly: in writing these small methods, we discover what's known as emergent design. The smaller the methods, the more likely we're going to spot opportunities for creating new classes that have distinct behavior and state. The design of the class model will emerge from our incremental coding.

    That last point is extremely important for writing code in a TDD or BDD manner: let the design emerge from our exploratory coding. It's also very important if you're given a hunk of legacy code to maintain. Refactoring it to the Composed Method pattern and using emergent design lets you improve that legacy code and make maintenance easier.

    All in all, I'd recommend you look at the Composed Method pattern and start writing small methods.

    (You can see the video here.)

    The other thing I didn’t mention is that, in .NET, small methods are more likely to be inlined by the JITter at run-time, and in some other languages you can provide hints to the compiler that a method can be inlined if necessary. So the excuse that “calling lots of small methods increases run time” is not necessarily true.

    Which leads to another point I’ve made before: we should be writing code in such a way that other people can read and understand it, not for the compiler. After all, the compiler will correctly compile all kinds of badly-written code without complaint (I’m reminded of one app I had to maintain in my early days as a programmer: the original developer had used flower names for all his variables), but it’s the human programmer that has to read, understand, and debug it.

    Part two will be available in a fortnight’s time in the next newsletter. SLAP!

  • New gallery control in XtraBars Suite (coming in v2010 vol 2)

    For the next version of XtraBars, we’ve created a standalone version of the gallery from our ribbon control. This post shows you a quick preview of this control; it will be available in v2010 vol.2 later this year.

    The new GalleryControl provides a way to show a list of items, categorized, if necessary, into groups. Here’s the ‘default’ look:

    Default view for gallery control

    Of course, you are not limited just to the standard image. You can add a caption and a description to each image:

    Gallery control showing captions

    But wait! There is more. For example:

    1. The Gallery control provides all the same options that the original Ribbon galley has. (See the help for the Ribbon Gallery for details.)

    2. The Gallery control also supports group filtering. (See here for information on group filtering.)

    3. The gallery implements automatic item checking. With this feature, clicking an item automatically checks the item. Not only that, but the gallery provides several “check modes”:

    • Multiple items can be checked simultaneously
    • Multiple items can be checked, but only within a single group
    • Only a single item can be checked throughout the whole gallery
    • A single item can be checked in each group

    4. By default, when the end-user enlarges the gallery, it adds additional columns to fill the newly created free space. When they reduce the gallery’s width, it removes the columns that can no longer be shown. It's possible to disable this feature by setting the GalleryControl.Gallery.AutoFitColumns option to false. In this mode, the number of columns is specified by the gallery's ColumnCount property.

    5. The default orientation of gallery items is vertical, as shown above. The gallery also supports a horizontal orientation. If you do set the the orientation to horizontal, you will also change the orientation of the gallery group headers, the layout of the items  and the orientation of the scroll bar. As you can see in the following image, the GalleryControl's orientation is horizontal and its height has been reduced so that the control displays only one row of items (click to see the full size image):

    Gallery displayed horizontally

    6. Instead of just text, you can display custom controls within the group headers. You can see this best in the new PhotoViewer demo, where the group headers show images, multiple labels, and buttons (clink to enlarge):

    Gallery showing custom group headers

    7. We’ve also implemented smooth scrolling of items, as you can see in this video:

    There are also a bunch of minor features, such as:

    • specifying the background image or background color for the gallery
    • customizing the background image layout
    • the ability to customize the horizontal alignment of the gallery's content
    • providing multiple ways to specify how images are stretched within a gallery item
    • the custom drawing of gallery items

    Of course, there’s a demo that demonstrates a real-world usage of the gallery control, a PhotoViewer application:

    Gallery control demo

    If you select one or more images from the gallery (use the Shift and/or Ctrl key as usual), and click View, the chosen images will be displayed in a special view, together with a horizontal gallery of selected images:

    PhotoViewer demo application showing horizontal gallery 

    As I said above: this control is not available yet, but will be provided as part of DXperience v2010 vol.2.

LIVE CHAT

Chat is one of the many ways you can contact members of the DevExpress Team.
We are available Monday-Friday between 7:30am and 4:30pm Pacific Time.

If you need additional product information, write to us at info@devexpress.com or call us at +1 (818) 844-3383

FOLLOW US

DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, along with high-performance HTML JS Mobile Frameworks for developers targeting iOS, Android and Windows Phone. Whether using WPF, ASP.NET, WinForms, HTML5 or Windows 10, DevExpress tools help you build and deliver your best in the shortest time possible.

Copyright © 1998-2017 Developer Express Inc.
All trademarks or registered trademarks are property of their respective owners