Fields vs method arguments [closed]

I just started writing some new class and it occurred to me that I was adding a lot of method arguments that are not strictly needed. This is following a habit to avoid having state in classes that is specific to some method call, rather than being general configuration or dependencies of the class.

Doing so means that a lot of methods that could have no arguments end up with one, two or three.

I’d like to hear your opinions on what you think of this tradeoff, and how you go about deciding which approach to take in what situation?

Since code is often easier to understand than English when describing code, I created a little gist that has both variants in it: https://gist.github.com/JeroenDeDauw/6525656

2

Since the only externally visible method of your example is updateTable, I think it is fine to use fields instead of method parameters.

If this is part of a more generic class (e.g. TableTools), I would move the helper methods that need the state into a hidden inner class.

Pseudo-code example:

class TableTools {
    ...
    public void updateTable(currentTable, newTable) {
        TableUpdater u = new TableUpdater(schemaModifier, currentTable, newTable);
        u.removeRemovedFields();
        u.addAddedFields();
     }

     private class TableUpdater { ... }
}

That way, you avoid fields that are used by only one public method. In addition, the code is thread-safe in the sense that every call to updateTable uses its own copy of TableUpdater and, thus, of TableUpdater’s instance variables.

Using fields co-opts the ability to have multithreading available for the methods that use those fields.

Using fields like that is only slightly better than using globals from a reusability and maintainability standpoint, the key point here is that complex setups need careful and up-to-date documentation about which methods use and/or clobber which fields; something you don’t need to do when using arguments.

1

In layman’s words:

  • methods should have as few arguments as possible ( Martin’s Clean Code )
  • one of the features of objects is than they can (and should) have a state
  • non-static methods that don’t operate on the object’s state, i.e. receive everything as parameters, are not cohesive
  • non-cohesive methods might as well be made static and grouped in an utility class

Again, in my humble opinion non-cohesive methods belong to an utility class and not to a class with a domain name.

Don’t use fields in the current case! Two “threads” using the object at the same time will seriously confuse each other. They don’t have to be real, separate threads either (hence the quotes). If you set up the object for one table, then call a method that uses it for another table, then try to use the original set up, you have a problem. Stick with parameters for the moment.

What you want to do here is create a new updater class that is used in only one case. The original class could have a method to create an instance whenever needed. The new class would have fields. You have the best of both worlds. Sometimes it’s simpler just to stick with the parameters, but in your example you’re already getting to where a separate class would be better.

I think, that the choosing should be according to the real situation, as you see it. If an item belongs to an instance, it should be seen as its field. If the item is external to the instance, it should be passed as a method parameter.

We should be guided in this case not by the effectivity (the difference is insignificant anyway), or (God save!) easiness of typing, but by the understandability and naturalness of the code.

In my opinion if you are writing code that does something to something then it should take parameters that define what things it should do them to and its name should define as far as possible what it does to it.

If you are writing code that packages up an action to be performed on something then you should wrap the things it should be performed on in an object and pass them to your thing that does something.

This action then becomes a kind of meta-description of the calls to the methods of the first that you can then perhaps perform at a later date by queueing it up or even decide not to do it at all for some reason.

So your question translates into is it an Action or a Function? An Action could be postponed or cancelled and therefore should encapsulate that which it acts upon. A Function happens immediately so there is no need top preserve its parameters.

You can undo and Action but not a Function.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Fields vs method arguments [closed]

I just started writing some new class and it occurred to me that I was adding a lot of method arguments that are not strictly needed. This is following a habit to avoid having state in classes that is specific to some method call, rather than being general configuration or dependencies of the class.

Doing so means that a lot of methods that could have no arguments end up with one, two or three.

I’d like to hear your opinions on what you think of this tradeoff, and how you go about deciding which approach to take in what situation?

Since code is often easier to understand than English when describing code, I created a little gist that has both variants in it: https://gist.github.com/JeroenDeDauw/6525656

2

Since the only externally visible method of your example is updateTable, I think it is fine to use fields instead of method parameters.

If this is part of a more generic class (e.g. TableTools), I would move the helper methods that need the state into a hidden inner class.

Pseudo-code example:

class TableTools {
    ...
    public void updateTable(currentTable, newTable) {
        TableUpdater u = new TableUpdater(schemaModifier, currentTable, newTable);
        u.removeRemovedFields();
        u.addAddedFields();
     }

     private class TableUpdater { ... }
}

That way, you avoid fields that are used by only one public method. In addition, the code is thread-safe in the sense that every call to updateTable uses its own copy of TableUpdater and, thus, of TableUpdater’s instance variables.

Using fields co-opts the ability to have multithreading available for the methods that use those fields.

Using fields like that is only slightly better than using globals from a reusability and maintainability standpoint, the key point here is that complex setups need careful and up-to-date documentation about which methods use and/or clobber which fields; something you don’t need to do when using arguments.

1

In layman’s words:

  • methods should have as few arguments as possible ( Martin’s Clean Code )
  • one of the features of objects is than they can (and should) have a state
  • non-static methods that don’t operate on the object’s state, i.e. receive everything as parameters, are not cohesive
  • non-cohesive methods might as well be made static and grouped in an utility class

Again, in my humble opinion non-cohesive methods belong to an utility class and not to a class with a domain name.

Don’t use fields in the current case! Two “threads” using the object at the same time will seriously confuse each other. They don’t have to be real, separate threads either (hence the quotes). If you set up the object for one table, then call a method that uses it for another table, then try to use the original set up, you have a problem. Stick with parameters for the moment.

What you want to do here is create a new updater class that is used in only one case. The original class could have a method to create an instance whenever needed. The new class would have fields. You have the best of both worlds. Sometimes it’s simpler just to stick with the parameters, but in your example you’re already getting to where a separate class would be better.

I think, that the choosing should be according to the real situation, as you see it. If an item belongs to an instance, it should be seen as its field. If the item is external to the instance, it should be passed as a method parameter.

We should be guided in this case not by the effectivity (the difference is insignificant anyway), or (God save!) easiness of typing, but by the understandability and naturalness of the code.

In my opinion if you are writing code that does something to something then it should take parameters that define what things it should do them to and its name should define as far as possible what it does to it.

If you are writing code that packages up an action to be performed on something then you should wrap the things it should be performed on in an object and pass them to your thing that does something.

This action then becomes a kind of meta-description of the calls to the methods of the first that you can then perhaps perform at a later date by queueing it up or even decide not to do it at all for some reason.

So your question translates into is it an Action or a Function? An Action could be postponed or cancelled and therefore should encapsulate that which it acts upon. A Function happens immediately so there is no need top preserve its parameters.

You can undo and Action but not a Function.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Fields vs method arguments [closed]

I just started writing some new class and it occurred to me that I was adding a lot of method arguments that are not strictly needed. This is following a habit to avoid having state in classes that is specific to some method call, rather than being general configuration or dependencies of the class.

Doing so means that a lot of methods that could have no arguments end up with one, two or three.

I’d like to hear your opinions on what you think of this tradeoff, and how you go about deciding which approach to take in what situation?

Since code is often easier to understand than English when describing code, I created a little gist that has both variants in it: https://gist.github.com/JeroenDeDauw/6525656

2

Since the only externally visible method of your example is updateTable, I think it is fine to use fields instead of method parameters.

If this is part of a more generic class (e.g. TableTools), I would move the helper methods that need the state into a hidden inner class.

Pseudo-code example:

class TableTools {
    ...
    public void updateTable(currentTable, newTable) {
        TableUpdater u = new TableUpdater(schemaModifier, currentTable, newTable);
        u.removeRemovedFields();
        u.addAddedFields();
     }

     private class TableUpdater { ... }
}

That way, you avoid fields that are used by only one public method. In addition, the code is thread-safe in the sense that every call to updateTable uses its own copy of TableUpdater and, thus, of TableUpdater’s instance variables.

Using fields co-opts the ability to have multithreading available for the methods that use those fields.

Using fields like that is only slightly better than using globals from a reusability and maintainability standpoint, the key point here is that complex setups need careful and up-to-date documentation about which methods use and/or clobber which fields; something you don’t need to do when using arguments.

1

In layman’s words:

  • methods should have as few arguments as possible ( Martin’s Clean Code )
  • one of the features of objects is than they can (and should) have a state
  • non-static methods that don’t operate on the object’s state, i.e. receive everything as parameters, are not cohesive
  • non-cohesive methods might as well be made static and grouped in an utility class

Again, in my humble opinion non-cohesive methods belong to an utility class and not to a class with a domain name.

Don’t use fields in the current case! Two “threads” using the object at the same time will seriously confuse each other. They don’t have to be real, separate threads either (hence the quotes). If you set up the object for one table, then call a method that uses it for another table, then try to use the original set up, you have a problem. Stick with parameters for the moment.

What you want to do here is create a new updater class that is used in only one case. The original class could have a method to create an instance whenever needed. The new class would have fields. You have the best of both worlds. Sometimes it’s simpler just to stick with the parameters, but in your example you’re already getting to where a separate class would be better.

I think, that the choosing should be according to the real situation, as you see it. If an item belongs to an instance, it should be seen as its field. If the item is external to the instance, it should be passed as a method parameter.

We should be guided in this case not by the effectivity (the difference is insignificant anyway), or (God save!) easiness of typing, but by the understandability and naturalness of the code.

In my opinion if you are writing code that does something to something then it should take parameters that define what things it should do them to and its name should define as far as possible what it does to it.

If you are writing code that packages up an action to be performed on something then you should wrap the things it should be performed on in an object and pass them to your thing that does something.

This action then becomes a kind of meta-description of the calls to the methods of the first that you can then perhaps perform at a later date by queueing it up or even decide not to do it at all for some reason.

So your question translates into is it an Action or a Function? An Action could be postponed or cancelled and therefore should encapsulate that which it acts upon. A Function happens immediately so there is no need top preserve its parameters.

You can undo and Action but not a Function.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật