Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Having pairs of static and instanced methods that perform the same tasks?

On the first part, "static methods don't mutate", that's widely used in OOP. I haven't heard of it being expressed explicitly. But it is common sense: "If you change an object, why would the method be static if it could be an instance method?" So I completely agree with the "static methods don't mutate".

On the second part, "instance methods do [mutate]", that's actually not as widely used. It rather depends on whether you decide your design to apply immutability or mutability. Examples from the Java API: java.lang.String is immutable, java.util.Date is mutable (most likely by accident / bad design), java.lang.StringBuilder is mutable intentionally (that's its purpose). Mutability can lead to defensive cloning in order to protect the code from mutation bugs. Whether this really is a problem depends on a few things:

  • Is it an API others will use? You never know how they will use your code... IMO it's more important to protect API code from mutation bugs than normal code.
  • How good is the unit test coverage? Would your unit tests find all the mutation bugs that might sneak in? If you follow TDD properly (Uncle Bob's 3 Laws of TDD), and it's non-API code, mutation bugs are very unlikely to sneak in without being instantly discovered.
  • If you have code that has to protect itself against mutation bugs using defensive cloning, how often is that code called? If defensive clones are created frequently, it might be better to use immutable objects than mutable objects. Basically this is the call of the number of calls of read-only methods (that would eventually defensively clone) of associating classes vs. the number of calls of mutator methods on the class itself.

Personally, I prefer immutable objects, I'm a fan of final (if I could change Java, I would make final the default for all fields and variables, and introduce a keyword var to make them non-final), and I try to do functional programming in Java, although it is not a functional programming language, as much as possible. From my experience I know that I spend significantly less time debugging my code than others (actually I run the Java debugger maybe twice a year or so). I do not have enough empirical data and proper analysis for creating any kind of "causal relationship" between experience, immutability, functional programming and correctness, therefore I will only say I believe that immutability and functional programming help for correctness, and you will have to come up with your own judgement on this.

Concluding on the second part, "instance methods do [mutate]" is the widely used assumption in case the object is mutable anyway, otherwise instance methods would clone.

Categories : Java

Related to : Having pairs of static and instanced methods that perform the same tasks?
Is there any difference between normal and static local variables in static methods?
Yes both are different. For every call a will be created whereas b will be created only once and it is same for all the objects of type A. By same I mean, all the objects share a single memory of b.

Categories : C++
How to Mock Static Methods in Easymock
As mystarrocks mentioned in the comment, you can mock static methods using PowerMock, even you can test final class/method and private methods too! From the documentation: PowerMock is a framework that extend other mock libraries such as EasyMock with more powerful capabilities. PowerMock uses a custom classloader and bytecode manipulation to enable mocking of static methods, constructo

Categories : Android
magic methods - Can I use them to catch the inaccessible static property?
Don't think so. There's no magic method like __getStatic or __setStatic. As far as your question deals with the registry pattern, you can do this: Use the standard registry pattern: class Reg { private $register = array(); private static $instance = null; public static function getInstance() { if ( self::$instance === null) { self::$instance = new R

Categories : Misc
How to convert Sequence of Future tasks to Enumerator, that would consume latest complete tasks
You could use Enumerator.unfoldM. Something like: val listOfCalculations:List[Future[A]] = ... Enumerator.unfoldM(0) { i => if(i < listOfCalculations.length) listOfCalculations(i).map(a => Some((i + 1, a))) else Future.successful(None) } This is kind of like a fold. i keeps track of our position in the list; each time our mapper is called we get the next future from the list an

Categories : Scala
django: serve static assets *without* copying them (collect static)
Use python collectstatic --link From -l --link Create a symbolic link to each file instead of copying. This way they won't use up extra space, if the Heroku systems allow symbolic links.

Categories : Python
Recently Add
Redirecting the output directory of 'mvn package' or 'mvn compile' command
No suitable constructor found for ProductoExtranjero
java Composite design pattern(Directory &File)
Java JTree's ui refresh after removing node from parent
First REST Spring application
How to cancel Indexing of a Solr document using Update Request Processor
PowerMock - Mock a Singleton with a Private Constructor
Calling a Postgres stored function SQL error
Where to store Morphlines Java custom command class?
Generic repository using map
How can I scroll a ScrolledComposited in Eclipse SWT Design view?
2 Frames/layout in 1 Activity
Writing a switch differently
Next button opens another activity when its reaches the array limit
Is EclipseLink MOXy capable of applying JSR-303 Bean Validation when unmarshalling XML to object?
Why my jdk can't work,and before the java_home, there is a space that is not from me
How to add List of objects in a Map
How to make notepad++ function like regular notepad in cmd?
Cell renderer and the lost focus
how can I implement iterable for LinkedList>
Disable Androids image-crunch in eclipse (run as) builds
java 8 lambda != myMap.size() after merging myMap
Issue with Calendar calculation that spans 2 calendar years
JSF 2.0 Spring bean injection
Java Regex ReplaceAll with grouping
Getting any word and last word using sed
Clicking on link on JEditorPane throws IOException
printing out difference of two arrays
Spring Bean Alias in JavaConfig
Using Factory Method to Create Generics
© Copyright 2017 Publishing Limited. All rights reserved.