Sunday, August 14, 2005

Codesmith: Code Generation v. Refactoring

[This was originally posted at http://timstall.dotnetdevelopersjournal.com/codesmith_code_generation_v_refactoring.htm]

Looking into CodeSmith, a good code generation tool, I started analyzing the differences between code generation and refactoring. Both help reduce redundancy. They are complimentary, with each concept having a different purpose:

  • Code Generation - Given input values and a template, automatically generate redundant code that varies only by the input values. This deals with how you create code.
  • Refactoring - Keep the functionality of your code the same, but improve the code itself via eliminating redundancy, improving clarity, etc... This deals with the final code, regardless of how that code was created.

Whenever you find yourself making repetitive code, you should at least check if these techniques can bail you out. Common examples may be (depending on your project) the data-access layer, the structure of business entities, documentation, unit tests, etc... Note that code generation and refactoring aren't opposites - but rather complementary. You can create a refactored class that still has some repetitiveness, and then code-generate it. For example, a fully refactored business entity may still have many properties that all follow the same format. For example, say your class has 10 such properties below. Given only the DataType and PropertyName, a code-generation template could crank this out:

public Double MyValue
{
    get
    {
        return _dblMyValue;
    }
    set
    {
        _dblMyValue = value;
    }
}

There are certainly places where repetitive code can be refactored such that there is no longer any redundancy, and therefore nothing to code-generate. A good code-generation tool is no excuse for not refactoring properly. However there are many places where refactoring alone is insufficient:

  1. Design time: Code generation is done at design-time, and thus offers a performance benefit over refactoring (executed at run time). Say you needed a class to access the properties of your business entities. You could use reflection at design time, or (assuming you know the entities) you could use code-generation to create the necessary code before hand.
  2. More functionality: Certain things can't effectively be refactored. If you needed to refactor a class that could handle many different Data Types, you'd need to use boxing./unboxing to handle the type-conversion. For example, pre-.Net 2.0 (which has generics) most collections, like HashTable or ArrayList, are not strongly typed. This requires boxing and unboxing which has obvious performance problems. .Net 1.1 does not provide a way to refactor the handling of various data types without the performance loss of boxing/unboxing. However you could use CodeSmith's strongly typed collection templates to automatically generate your own collections that don't require boxing. This is essentially still refactored because it provides additional functionality that the "refactored" version didn't meet - type safety.
  3. Beyond source code: A good code generation tool, like CodeSmith, can create any text file, not just object-oriented source code. For example, suppose you wanted to document your database schema. CodeSmith provides pre-packaged templates that do this. I am not a aware of a way that "refactoring" would solve this problem.
  4. Always automatic: Refactoring is great for object-oriented code with supporting unit tests. But it is difficult for many things outside of source code like documents or html pages. Short of other tools, these require a more manual approach. Code generation remains automatic

In conclusion, both refactoring and code generation are good things, but they are different and complimentary things.

2 comments:

  1. Rau câu món ăn được nhiều người ưa thích dễ dàng làm tại nhà. Cùng bếp 247 tìm hiểu cách làm rau câu dẻo đơn giản tại nhà. Với các bước cơ bản chỉ cần mất vài phút bạn có ngay món rau câu mát lạnh cho riêng mình. Ngoài ra rau câu được sang tạo làm với nhiều hương vị và máu sắc khác nhau như cách làm thạch rau câu 3d có thể tạo nhiều hình dáng đẹp mắt trong suốt phản trong bánh rau câu như bông hoa, chữ, tên….được sử dụng thay thế cho bánh sinh nhật. Cũng gần hơn là cách làm thạch rau câu xoài giúp bạn có món rau câu ngọt cộng với vị chua thanh của trái xoài. Giúp bạn có hương vị rau câu vô cùng đặc biệt. Nếu bạn yêu thích hương vị café thì không nên bỏ qua món rau câu café cách làm thì cũng khá đơn giản có thể tham khảo tại cach lam thach rau cau cafe tại nhà mà chúng tôi blog nấu ăn đã giới thiệu cho các bạn lần trước. Nhanh tay tìm hiểu và thực hiện ngay cho mình món rau câu yêu thích không nên bỏ lỡ có ngay hũ rau câu mát lạnh trong tủ lạnh nhà bạn. Ngoài ra bạn có thể tham khảo thêm cach lam thach rau cau trai dua đơn giản tại nhà chỉ trong 30 phút thực hiện.

    ReplyDelete
  2. Tronng văn phòng công ty với mỗi phòng ban đều có những chức năng công việc khác nhau cho nên việc lựa chọn mua bàn làm việc bằng gỗ cũng hòa toàn khác nhau tùy vào mục đích sử dụng của văn phòng. Cho nên việc lựa chọn mua sản phẩm nội thất văn phòng như bàn, ghế, tủ… đều phải có sự thống nhất và dựa vào không gian văn phòng làm việc mà lựa chọn được mẫu sản phẩm cho phù hợp nhất. Hiện nay trên thị trường nội thất có rất nhiều sản phẩm bàn văn phòng giá rẻ khác nhau nhiều giá, mẫu mã và chức năng.
    Bàn làm việc văn phòng cho nhân viên thì phụ thuộc vào diện tích khá nhiều. Những chiếc bàn làm việc đơn có hộc sẽ được sử dụng cho những căn phòng rộng để giúp mỗi người có một khoảng làm việc riêng và cũng để văn phòng không bị trống trải. Tuy nhiên, ở những thiết kế nội thất văn phòng hiện đại đề cao tính gắn kết nên lại chọn những kiểu bàn làm việc văn phòng giá rẻlớn dạng đôi để tập trung từng nhóm nhân viên lại với nhau. Vách ngăn vẫn sẽ giúp mỗi người có những khoảng không gian riêng cho mình. Cho nên để có một văn phòng đẹp hiện đại các nhà quản lý cần phải chọn mua được sản phẩm nội thất văn phòng giá rẻ tphcm sao cho hài hòa với văn phòng cũng như là không gian làm việc tốt nhất. Chính vì thế các nhà thầu thiết kế cũng nên cân nhắc về việc tư vấn cho khách hàng của mình chọn được mẫu sản phẩm nội thất sử dụng tốt nhất.

    ReplyDelete