Are static methods good for scalability?

后端 未结 8 511
故里飘歌
故里飘歌 2021-02-04 19:03

Does static methods and class are good for scalability ? I think so static class/method improves scalability of application and instance methods doesn\'t scales much. So is it g

相关标签:
8条回答
  • 2021-02-04 19:15

    Which form of scalability do you mean? The scalability, that the code is maintainable and extensible in big and small projects? Then using only static-methods hurt. Do you mean performance? If instance-methods are slower (what I don't believe in this generality), then this doesn't mean they don't scale. If they need twice the time as static-methods, they need also twice much time if you call them all 10000 times. Choosing the right algorithms and data-representation decide much more about the scalability of the performance.

    At last, if you think static-methods are the way to go, you should not use an object-oriented language like Java. Try C or Pascal or classic Basic-dialects.

    0 讨论(0)
  • 2021-02-04 19:17

    I think you're barking up the wrong tree:

    In the real world, scalability (or the lack thereof) typically springs from appropriateness of one's algorithms and the efficiency of the operations made against data stores (think: good SQL queries).

    Things like whether a method is static or not (or the % of methods which are static) generally have nothing to do with the scalability of a system. You can certainly have a super scalable system which uses no static methods, or super scalable system which uses them exclusively.

    0 讨论(0)
  • 2021-02-04 19:21

    No. I think you may be assuming that each instance has its own copy of the method definition, taking up that amount of space for each instance, which is not the case.

    Editing to add:

    In case you wonder how an instance method can be actually shared between the instances: It is because each call to an instance method implicity passes a reference to the instance object to the method. This is generally referred to as the "implicit this" In other words - when you define or call an instance method with two parameters like myMethod(a, b), you can think of it as actually being myMethod(this, a, b), and Java takes care of the this parameter for you, without your having to explicitly define or pass it.

    (This, by the way, is handled differently in Python, where you have to explicitly put the object reference in as the first parameter of an instance method definition, though not in the call.)

    For an explanation of what goes on at the Java bytecode level here's a link: http://www.artima.com/underthehood/invocationP.html (See stuff around: "The objectref is the implicit this pointer that is passed to any instance method.")

    0 讨论(0)
  • 2021-02-04 19:30

    No static methods don't intrinsically scale better. Infact the programming style (imperative or object oriented) does not really make any difference to scaling whatsoever. There are two major aspects of scaling and what to do to improve scale depends on which we mean:

    1 Scaling by number of requests a second handled

    This type of scaling is normally about adding more computers to a cluster to improve overall throughput of the system. Increasing scaling is often about initially reducing the amount of shared resources used through the use of caches and then later making the data access split into shards.

    2 Data Scaling

    This is when the system gets more and more data over time and operations that access the data (search, filtering etc) get slower as the algorithms are more complex than O(1). In this case the normal strategy is to increase the number of read and write points and use parallel algorithms such as Map/Reduce.

    But neither of these aspects has anything to do with whether you use static methods or not, just whether multiple requests work on large sets of data or single sources of data.

    0 讨论(0)
  • 2021-02-04 19:31

    Does static methods and class are good for scalability?

    One has little to do with the other.

    I think so static class/method improves scalability of application and instance methods doesn't scales much.

    Wrong. Why do you think so?

    So is it good programming practice to write static method where ever it is possible?

    No. In fact, it is very bad practice because it abandons the maintainability advantages of object-oriented programming.

    0 讨论(0)
  • 2021-02-04 19:32

    Voted down. The OP clearly doesn't quite understand OO. An instance method doesn't take up any extra space when an instance object is created. Static methods aren't going to save you anything unless you're also avoiding creating any instances, in which case you're going so far afield from what an OO language was built for that it's sort of pointless discussion.

    0 讨论(0)
提交回复
热议问题