@@ -24,6 +24,70 @@ weather-formulas is a TypeScript library that provides atmospheric and meteorolo
2424
2525## Coding Guidelines
2626
27+ ### Code Reuse and Avoiding Duplication
28+ ** CRITICAL** : Always reuse existing formulas, functions, and utilities from the library instead of reimplementing them.
29+
30+ Before writing any new calculation or utility function:
31+ 1 . ** Search the codebase** for existing implementations in:
32+ - ` src/formulas/ ` - Core calculations (temperature, pressure, humidity, altitude, air density)
33+ - ` src/indices/ ` - Weather indices (heat index, humidex, UTCI, PET)
34+ - ` src/phenomena/ ` - Weather phenomena (fog, clouds, snow, wind)
35+ - ` src/constants.ts ` - Physical constants and valuation sets
36+ - ` src/common.ts ` - Shared utility types and interfaces
37+
38+ 2 . ** Common reusable functions** already available:
39+ - ** Temperature conversions** : ` kelvinToCelcius() ` , ` celciusToKelvin() ` , ` kelvinToFahrenheit() ` , ` fahrenheitToKelvin() ` , ` celciusToFahrenheit() ` , ` fahrenheitToCelcius() `
40+ - ** Humidity calculations** : ` saturationVaporPressure() ` , ` vaporPressure() ` , ` actualVaporPressure() ` , ` relativeHumidity() ` , ` absoluteHumidity() ` , ` mixingRatio() ` , ` specificHumidity() ` , ` dewPointDepression() ` , ` liftingCondensationLevel() `
41+ - ** Temperature metrics** : ` dewPointMagnusFormula() ` , ` dewPointArdenBuckEquation() ` , ` potentialTemperature() ` , ` virtualTemperature() ` , ` equivalentTemperature() ` , ` wetBulbTemperature() ` , ` windChillIndex() ` , ` australianApparentTemperature() `
42+ - ** Pressure calculations** : Functions in ` src/formulas/pressure.ts ` and ` src/formulas/altitude.ts `
43+ - ** Conversion utilities** : ` meterPerSecondToKilometerPerHour() ` , ` roundToTwoDecimals() `
44+ - ** Constants** : Use predefined constants from ` constants.ts ` like ` CELSIUS_TO_KELVIN ` , ` STANDARD_ATMOSPHERIC_CONSTANTS ` , ` DEW_POINT_VALUATIONS ` , etc.
45+
46+ 3 . ** How to discover existing functions** :
47+ - Use file search to find keywords related to what you need (e.g., "vapor", "pressure", "temperature")
48+ - Check the module index files to see exported functions
49+ - Look at similar formulas to see what they import and reuse
50+ - Review test files to understand how existing functions are used
51+
52+ 4 . ** Benefits of reusing existing code** :
53+ - Maintains consistency across the library
54+ - Reduces bugs and inconsistencies
55+ - Leverages tested and validated implementations
56+ - Keeps the codebase maintainable and DRY (Don't Repeat Yourself)
57+ - Ensures consistent units and conventions
58+
59+ ** Example of proper code reuse** :
60+ ``` typescript
61+ // ❌ BAD - Reimplementing temperature conversion
62+ export function myNewFormula(tempK : number ): number {
63+ const tempC = tempK - 273.15 ; // Don't do this!
64+ // ... calculations
65+ }
66+
67+ // ✅ GOOD - Reusing existing function
68+ import { kelvinToCelcius , celciusToKelvin } from ' ./temperature' ;
69+
70+ export function myNewFormula(tempK : number ): number {
71+ const tempC = kelvinToCelcius (tempK );
72+ // ... calculations
73+ return celciusToKelvin (result );
74+ }
75+
76+ // ❌ BAD - Reimplementing saturation vapor pressure
77+ export function badHumidityCalc(temperature : number ): number {
78+ const svp = 611.2 * Math .exp ((17.62 * (temperature - 273.15 )) / (243.12 + (temperature - 273.15 )));
79+ // Don't reimplement existing formulas!
80+ }
81+
82+ // ✅ GOOD - Reusing existing saturation vapor pressure
83+ import { saturationVaporPressure } from ' ./humidity' ;
84+
85+ export function goodHumidityCalc(temperature : number ): number {
86+ const svp = saturationVaporPressure (temperature );
87+ // ... rest of calculation
88+ }
89+ ```
90+
2791### Units and Measurements
2892** CRITICAL** : All temperature values MUST be in Kelvin (K)
2993- Input parameters: Kelvin
@@ -265,11 +329,17 @@ Keep dependencies minimal and well-justified.
265329### New Formula Checklist
266330When adding a new formula:
267331
332+ 0 . ** Check for Existing Code**
333+ - [ ] Search the codebase for similar formulas or components
334+ - [ ] Identify existing functions that can be reused (conversions, calculations, constants)
335+ - [ ] Review ` src/formulas/ ` , ` src/constants.ts ` , and related modules
336+
2683371 . ** Source Code**
269338 - [ ] Add function to appropriate module in ` src/ `
270339 - [ ] Include complete JSDoc documentation
271340 - [ ] Export function and any related types/constants
272341 - [ ] Use TypeScript strict typing
342+ - [ ] ** Reuse existing library functions** wherever possible
273343
2743442 . ** Tests**
275345 - [ ] Create or update test file in ` tests/ `
@@ -290,12 +360,14 @@ When adding a new formula:
290360
291361## Common Mistakes to Avoid
292362
293- 1 . ** Temperature Units** : Never mix Celsius/Fahrenheit with Kelvin without explicit conversion
294- 2 . ** Floating Point** : Use ` toBeCloseTo() ` in tests, not exact equality
295- 3 . ** Magic Numbers** : Define constants in ` constants.ts ` or as named variables
296- 4 . ** Missing Tests** : All exported functions must have tests
297- 5 . ** Documentation** : Always include JSDoc comments with parameter units
298- 6 . ** Type Safety** : Don't use ` any ` - use proper types or ` unknown ` with validation
363+ 1 . ** Code Duplication** : Never reimplement existing formulas or functions - always search the codebase first and reuse existing implementations
364+ 2 . ** Temperature Units** : Never mix Celsius/Fahrenheit with Kelvin without explicit conversion
365+ 3 . ** Temperature Conversions** : Always use the conversion functions from ` temperature.ts ` - never write manual conversion formulas
366+ 4 . ** Floating Point** : Use ` toBeCloseTo() ` in tests, not exact equality
367+ 5 . ** Magic Numbers** : Define constants in ` constants.ts ` or as named variables, or use existing constants if they already exist
368+ 6 . ** Missing Tests** : All exported functions must have tests
369+ 7 . ** Documentation** : Always include JSDoc comments with parameter units
370+ 8 . ** Type Safety** : Don't use ` any ` - use proper types or ` unknown ` with validation
299371
300372## Performance Considerations
301373
0 commit comments