redundancy in GUI callback functions

35 views
Skip to first unread message

Travis Hinkelman

unread,
May 25, 2019, 10:57:16 PM5/25/19
to Racket Users
Hi All,

I'm working through the tasks in 7guis to learn about GUI programming in Racket. My code for the temperature converter task works as expected but there is a lot of redundancy in my update-fahrenheit and update-celsius callback functions. I would appreciate feedback on how to make this code less redundant.

Thanks,

Travis

Sorawee Porncharoenwase

unread,
May 25, 2019, 11:19:23 PM5/25/19
to Travis Hinkelman, Racket Users

Just abstract the common part out? E.g.,

(define (update-text-field read-field write-field converter)
  (define text (send read-field get-value))
  (cond
    [(string=? text "")
     (send read-field set-field-background (make-object color% 255 255 255 1))
     (send write-field set-value "")]
    [(number? (string->number text))
     (send read-field set-field-background (make-object color% 255 255 255 1))
     (send write-field set-value (converter (string->number text)))]
    [else
     (send read-field set-field-background (make-object color% 255 0 0 1))
     (send write-field set-value "")]))

(define (update-fahrenheit control event)
  (update-text-field text-celsius text-fahrenheit convert-c))

(define (update-celsius control event)
  (update-text-field text-fahrenheit text-celsius convert-f))

Note that your original code has a bug: you always use convert-c in both directions. Obviously, in update-celsius you want to use convert-f.

There’s also another subtle bug that I didn’t fix. Suppose you type an invalid character (like “a”) to the left text field. This will make the field become red. Now, switch to the right field and type “32”. The left field will output “0” while being red, though it probably should now be white.


--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/8f97260c-62be-4a6f-9459-244a88632ef9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Travis Hinkelman

unread,
May 26, 2019, 10:02:03 AM5/26/19
to Racket Users
Ah, well, that was more straightforward than I expected. I was lazy with my Saturday-night learning session and didn't push very hard to solve it on my own. Thank you for taking the time to help. I also appreciate that you caught those bugs and improved the code style. Thanks again.


On Saturday, May 25, 2019 at 8:19:23 PM UTC-7, Sorawee Porncharoenwase wrote:

Just abstract the common part out? E.g.,

(define (update-text-field read-field write-field converter)
  (define text (send read-field get-value))
  (cond
    [(string=? text "")
     (send read-field set-field-background (make-object color% 255 255 255 1))
     (send write-field set-value "")]
    [(number? (string->number text))
     (send read-field set-field-background (make-object color% 255 255 255 1))
     (send write-field set-value (converter (string->number text)))]
    [else
     (send read-field set-field-background (make-object color% 255 0 0 1))
     (send write-field set-value "")]))

(define (update-fahrenheit control event)
  (update-text-field text-celsius text-fahrenheit convert-c))

(define (update-celsius control event)
  (update-text-field text-fahrenheit text-celsius convert-f))

Note that your original code has a bug: you always use convert-c in both directions. Obviously, in update-celsius you want to use convert-f.

There’s also another subtle bug that I didn’t fix. Suppose you type an invalid character (like “a”) to the left text field. This will make the field become red. Now, switch to the right field and type “32”. The left field will output “0” while being red, though it probably should now be white.


On Sat, May 25, 2019 at 7:57 PM Travis Hinkelman <travis....@gmail.com> wrote:
Hi All,

I'm working through the tasks in 7guis to learn about GUI programming in Racket. My code for the temperature converter task works as expected but there is a lot of redundancy in my update-fahrenheit and update-celsius callback functions. I would appreciate feedback on how to make this code less redundant.

Thanks,

Travis

--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages