# Should I share classes on the server with the client?

I am fortunate being able to use the same programming language on both the server and client (javascript). I would therefore like to share my code and classes between server and client, like Half-Life (and possibly others) did.

To make things simple, suppose I have an MMO with a Player and an Enemy class, with a position, velocity, and health.

var Player = function() {
this.health = 100;
this.x = 0;
this.y = 0;
}

var Enemy = function() {
this.health = 100;
this.x = 0;
this.y = 0;
}


I know ideally these should inherit from each other (or use a CES system). But my question is as follows:

Since these classes will be used on the client and server (since both client and server needs to know about these entities) with some but not all shared logic, would it not be crazy to define two separate classes on both the server and client?

What are the advantages and disadvantages of sharing classes between server and client?

Are there any helpful websites with tips/tricks for this approach?

• I would have a base class shared by the client and server, containing all the data shared by both (posision, rotation etc) Then I would derive a player and an enemy class on both the client and server. The client might need some more info from the player class like the representation (2d sprite, 3d model etc), the server has nothing to do with these. I would then make an update method on the base class with a parameter of type 'base class'. server. This way you can update the player and enemy class from the data received from the server – Thomas Oct 4 '13 at 9:38
• You could also use preprocessor directives to omit code from the client. So you would only have a single class shared between the client & server but when building your game, you would enable or disable the preprocessor directive based on whether you're targeting the client or server. – user94720 Apr 8 '17 at 13:07