Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should validation classes be static classes?

Tags:

java

static

I m working on a system which get thousands of requests per second, and essentially one of the tasks we are trying to avoid is to create unnecessary/extra objects.

We need to validate incoming request for 6 request items per se.

I m thinking of creating a class per each item validation.

However, I m trying to justify if i should use static validation classes vs object with instances that contains HttpRequest as the instance field.

should i use static classes or objects? what would you do?

Essentially, what I m doing is injecting List<IValidator> validators to request handler and iterating over it. but not sure if i should have an instance vs static classes.

like image 556
DarthVader Avatar asked Aug 28 '12 18:08

DarthVader


People also ask

When should a class be static?

Use a static class as a unit of organization for methods not associated with particular objects. Also, a static class can make your implementation simpler and faster because you do not have to create an object in order to call its methods.

Should a class validate itself?

It depends on the context. Sometimes, your object is absolutely valid internally, but in the context its values aren't acceptable. If you have simple Data Transfer Object then it probably shouldn't validate itself. If you have a class of Domain Model then it should do some validation.

Why should a class be static?

The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created. Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class except Object.

Should objects validate themselves?

Are Value Objects responsible for self-validation? No. But they are responsible for keeping their state consistent so sanity checking data on construction can be considered a good practice.


2 Answers

Have you actually measured the impact creating new Validator instances has on memory versus re-using static methods? The cost of using a short-lived object is very, very small. You should measure what the difference between the two approaches is and if there is no measurable difference, use the one where the code is cleaner and easier to understand.

In cases like this it always makes sense to measure the difference instead of just assuming one versus the other is better.

like image 118
matt b Avatar answered Oct 02 '22 16:10

matt b


In multithreaded environments, using static classes / methods always open concurrency pitfalls. Since the creation and collection of short lived objects is cheap, it is often better to create short lived objects than running into cuncurrency issues and extra synchronization, which is expensive.

Struts switched from static request handlers to instance-based request handlers for similar reasons.

like image 35
Sylar Avatar answered Oct 02 '22 16:10

Sylar