nhibernate-core icon indicating copy to clipboard operation
nhibernate-core copied to clipboard

Question: why "while" and not "if"?

Open ZmorzynskiK opened this issue 2 years ago • 4 comments

Hi,

I'm porting legacy .NET Framework 4.8 app to new .NET 6 and updating from custom MultipleHiLoPerTableGenerator to Enhanced.TableGenerator and I'm evaluating options and params.

I just stumbled upon those 2 lines: https://github.com/nhibernate/nhibernate-core/blob/ab5eb9d96649332f6237f2ac8673e639a7c2093e/src/NHibernate/Id/Enhanced/OptimizerFactory.cs#L377-L378

I known that this is probably VERY rare case, but wouldn't it be better to just have:

if(_value < 1) _value = 1;

Is it some kind of leftover after copying some code from another place, or intended loop?

ZmorzynskiK avatar Feb 05 '23 10:02 ZmorzynskiK

Yes, you are right it's

some kind of leftover

Your suggestion looks correct.

bahusoid avatar Feb 06 '23 10:02 bahusoid

It seems that PoolLoOptimizer is not entirely ported. The "pool" part is not. See how it was intended: https://github.com/hibernate/hibernate-orm/blob/f2842732f89281d53df79fcee5409fc681444e80/hibernate-core/src/main/java/org/hibernate/id/enhanced/PooledLoOptimizer.java#L65-L67

hazzik avatar Feb 08 '23 00:02 hazzik

Yeah, but it also looks like the increment() method in Java version is just doing some sanity check and then value++ - so I guess it's safe to be just changed to value = 1 pattern, right?

ZmorzynskiK avatar Feb 08 '23 08:02 ZmorzynskiK

I did a significant part of the porting of this code, back in November 2011. At that time Hibernate did not call it generationState, it was just called value, but it was an object typed as the interface IntegralDataTypeHolder, which looks like a way to generically work on integer, short, long or whatever. That interface did not provide a setter, and replacing the object is not an alternative. That should explain why they put in a loop using increment.

When I ported I either just missed the simplification that could be done, or maybe at the time I thought to keep the code structure similar line-by-line when possible.

Hibernate state of Nov 2011: https://github.com/hibernate/hibernate-orm/blob/129c0f13482b99e2705f0e234b6bc0572a95c271/hibernate-core/src/main/java/org/hibernate/id/enhanced/OptimizerFactory.java#L487 https://github.com/hibernate/hibernate-orm/blob/129c0f13482b99e2705f0e234b6bc0572a95c271/hibernate-core/src/main/java/org/hibernate/id/IntegralDataTypeHolder.java

oskarb avatar Feb 08 '23 09:02 oskarb