Reputation: 4324
I want to know what is the best way to deal with the code below:
public void hideKeyboard() {
homePage.getDriver().navigate().back();
}
public void navigatetoPreviousPage() {
homePage.getDriver().navigate().back();
}
Basically, both these codes do the same thing where it presses the android back button, but I may use the back button to hide a keyboard or the back button to move back a page.
For readability, it makes sense to have a method stating hide keyboard like after typing a field. For other scenarios, it will make sense to have a method stating I am moving back a page.
Is it ok to implement it like the above or is there a better way to handle this?
Upvotes: 1
Views: 206
Reputation: 206776
It's OK to implement it like this for now. What you mention yourself is important:
For readability it makes sense to have a method stating hide keyboard like after typing a field. For other scenarios it will make sense to have a method stating I am moving back a page.
To have high-quality, maintainable code, it's important that your code expresses the intent that the programmer has with it - that will make it much easier to understand and work with for other programmers, or even for yourself in the future. So, in places in the code where you need to hide the keyboard, it's much better if you call your hideKeyboard()
method, because it's clear and easy to understand that at that point you need to hide the keyboard; and vice versa for navigating to the previous page.
It might turn out that later, you find a better way to hide the keyboard that works in a different way than just pressing the back button. If that's the case then the only thing you'd have to do is modify the implementation of your hideKeyboard()
method.
If you really want to avoid code duplication for now, you can just call navigatetoPreviousPage()
inside your hideKeyboard()
method:
public void hideKeyboard() {
navigatetoPreviousPage();
}
public void navigatetoPreviousPage() {
homePage.getDriver().navigate().back();
}
Upvotes: 2