The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex
If you have a static member function, it could usually be a free function. The usual reaction is then that the coder has opted for a static member function only because of the myth that "everything must be in an object".
ReplyDeleteThat's why people discourage them.
And, because it's not a very convincing argument, those people pointed to unit testing instead. Not sure what they'll do now.
Static methods aren't bad in themselves. Sometimes certain operations don't make sense to require a particular object to do. For example, a function like square root would make more sense being static.
ReplyDeleteMath::sqrRoot(5);
rather than having to instantiate a Math 'object' and then call the function.
$math = new Math();
$result = $math->sqrRoot(5);
More difficult to test is a reason but not the only one. Static methods provide global access, and global access is bad.
ReplyDeleteOf course, you'll find that there's another global access to objects and that's creating one with 'new'. Objects have to be created somewhere so we can't eliminate that (though minimizing it is a good idea). Static methods as global access to a class is bad, unless it is there to replace 'new' through higher level programming:
$user = new User();
$user->setPointsTo(100);
// vs
$user = User::with100StartingPoints();
In this case I created code that is more readable while not misusing global access (the 'new' needed to be called anyway).
Edit: Let me give you an example in the way static methods 'can' be the death of testability (note how in the example above you don't even need to mock the static method but can easily test the new and static method produces the same result). Let's use your logger example:
class Logger {
public static function log($text) { // etc }
}
class AccessControl {
public function isAllowed(User $user, $action) {
// do stuff, determine if $allowed
if (!$allowed) {
Logger::log('user X was not allowed to do Y');
}
// do stuff
}
}
There is no way we can test this method cleanly because of the global call to Logger::log. It will depend on the correct working of the Logger class.