How to remove redundant code that enables button. Or “if” statement

I got probably “micro optimization” problem.

I got “History number”, “Next Number”, “Reset” buttons, as well “label” for text.

Every time I click At “Next number” button I would like to show random number in the label, and add this number to history array where I keep track of the past number.

Every time I click at “History number” it would randomly show one of the random numbers from history array.

Every time I click at “Reset” button, it would just clear history array.

And you can see that, that clicking at the “History number makes only sense when the history array is NOT empty.

And I wonder how to do this, without redundant “if” statement at the “history number” callback.

Or

Deactivating “history number” button at the start of the program. Then activating it EVERY time the “next number” is clicked (which is uuugly). And deactivating this button again on “Reset” button.

This scheme on whiteboard demonstrate what I mean :

Scheme that demonstrate buttons label and concept

And I have JavaScript code where I implemented one of ugly solution (latter one, with deactivating and activating on appropriate callbacks).

PS: I stumbled upon this problem writing in Objective-C, but I though, that it would be stupid to publish repository with iOS Objective-C code, while not everybody have to have Xcode. So I done Javascript Example. So don’t restrict your answers to only on language.

PS2: I though about having two “next number” functions.

1) One that would have the line of code that enable “history number” button and

2) second without.

At the start the 1) function would be handler. And at the end of that 1) function, the handler would replace itself to the 2) function.

Also the “reset” handler would put again handler to the 1) function.

PS3: I know, this could be overkill. But I just several times struggled with this kind of situation. And I wonder if there are better solution.

Thank you very very much in advance.

5

Setting a value to true every time you press “Next number” is not really expensive. Definitely not expensive enough to warrant even more code.
Even though your event handler switching would work it would make the code unnecessarily complex.

It’s really matter of performance versus maintainability, two quality aspects that often conflict.

You could wrap your history array in an object and have it raise an event like “change”. To decouple the button activation from the ‘Next number’ handler.

So, anyways, what happens to the label when you click ‘Reset’ ? 🙂

3

From the point of view of performance, branching via an if statement is not much different from branching via a mutable event handler so I don’t see the difference.

However, I would prefer the explicit if statement most of the time. Event handler are hidden, implicit state and I think its easier to read the code if I set them at the start and then don’t change them anymore. (I would rather have the event handler dispatch to a mutable variable holding callback than mutating the handler itself)

// I enable this button again, EVERY TIME – it is ugly and inefficient

I think its exactly the opposite. Its the cleanest way to say that you want the button to be enabled, irrespective of whether it used to be enabled or not. And its most likely not inefficient, since you aren’t doing it inside a loop.

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

How to remove redundant code that enables button. Or “if” statement

I got probably “micro optimization” problem.

I got “History number”, “Next Number”, “Reset” buttons, as well “label” for text.

Every time I click At “Next number” button I would like to show random number in the label, and add this number to history array where I keep track of the past number.

Every time I click at “History number” it would randomly show one of the random numbers from history array.

Every time I click at “Reset” button, it would just clear history array.

And you can see that, that clicking at the “History number makes only sense when the history array is NOT empty.

And I wonder how to do this, without redundant “if” statement at the “history number” callback.

Or

Deactivating “history number” button at the start of the program. Then activating it EVERY time the “next number” is clicked (which is uuugly). And deactivating this button again on “Reset” button.

This scheme on whiteboard demonstrate what I mean :

Scheme that demonstrate buttons label and concept

And I have JavaScript code where I implemented one of ugly solution (latter one, with deactivating and activating on appropriate callbacks).

PS: I stumbled upon this problem writing in Objective-C, but I though, that it would be stupid to publish repository with iOS Objective-C code, while not everybody have to have Xcode. So I done Javascript Example. So don’t restrict your answers to only on language.

PS2: I though about having two “next number” functions.

1) One that would have the line of code that enable “history number” button and

2) second without.

At the start the 1) function would be handler. And at the end of that 1) function, the handler would replace itself to the 2) function.

Also the “reset” handler would put again handler to the 1) function.

PS3: I know, this could be overkill. But I just several times struggled with this kind of situation. And I wonder if there are better solution.

Thank you very very much in advance.

5

Setting a value to true every time you press “Next number” is not really expensive. Definitely not expensive enough to warrant even more code.
Even though your event handler switching would work it would make the code unnecessarily complex.

It’s really matter of performance versus maintainability, two quality aspects that often conflict.

You could wrap your history array in an object and have it raise an event like “change”. To decouple the button activation from the ‘Next number’ handler.

So, anyways, what happens to the label when you click ‘Reset’ ? 🙂

3

From the point of view of performance, branching via an if statement is not much different from branching via a mutable event handler so I don’t see the difference.

However, I would prefer the explicit if statement most of the time. Event handler are hidden, implicit state and I think its easier to read the code if I set them at the start and then don’t change them anymore. (I would rather have the event handler dispatch to a mutable variable holding callback than mutating the handler itself)

// I enable this button again, EVERY TIME – it is ugly and inefficient

I think its exactly the opposite. Its the cleanest way to say that you want the button to be enabled, irrespective of whether it used to be enabled or not. And its most likely not inefficient, since you aren’t doing it inside a loop.

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