Let's say I'm writing a class that tracks a single stock ticker. In this simplified example, the StockTracker class holds a string that tells me the trend direction and a variable window
that holds x amount of historical prices.
Now I want to expose this class so whenever a new stock price comes in, I can call a function to update the stats and trend for this stock. So I make a public function that calls two private functions, each having one responsibility each
class StockTracker(val window: List<Double>, val trendDirection: String) {
private fun updateWindow(xxx) {
// Long business logic goes here
}
private fun updateTrend(xxx) {
// Long business logic goes here
}
fun ingestNewRate(newRate) {
updateWindow(newRate)
updateTrend(newRate)
}
}
In this scenario:
- Am I following the single responsibility principle? Technically the function
ingestNewRate
is doing two things: updating stats and updating the trend. Is this fine? - When unit testing this, would I have to test the 2 private functions?
- Should I be exposing
updateWindow
andupdateTrend
in new classes so that they can be a publically available function that can be tested? - Is there anything else I should be looking out for in this example that is potentially bad practice?