4 ms·
Maybe if you gave your variables more meaningful names, like "meaningOfLife" instead of just "result", it would be more useful.
by moron4hire 5y ago
Maybe if you gave your variables more meaningful names, like "meaningOfLife" instead of just "result", it would be more useful.
- jmull 5y agoIn that code snippet, “answer” would really be the right name for the variable (because the method is called ComputeAnswer). Redundantly restating immediately apparent context isn’t a good style.
- moron4hire 5y agoI cannot disagree with you any harder. Redundantly naming variables is good style, as it gives you more room to rename functions slightly, split functions, introduce new variables and inject new operations in the middle. And frequently, the function name has nothing to do with the semantics of the returned value. "result := sort(someOtherResult)" doesn't tell me anything. "sortedItems := sort(items)" tells me a lot. result1 := getUserInput() result2 := validate(result1) result3 := parse(result2) result4 := getRouting(result3) or addressInsecureStr := getUserInput() addressStr := validate(ipAddressInsecureStr) address := parse(ipAddressStr) pathAddresses := getRouting(ipAddress) I know which I would choose
- jmull 5y agoYou switched the context from a unit test to something different. Here, there I'd use something like this (changing some method names, too, since we're fixing names): userInput := getUserInput() validatedInput := validateUserInput() ipAddress := parseIpAddress(validatedInput) pathAddresses := getIpPathRouteTo(ipAddress) I'm making some assumptions about what these methods do and return. E.g., if validate/validateUserInput does something more specific than general user input validation then it should have a more specific name and the variable its result is assigned to would too.
- quietbritishjim 5y agoIn a function getMeaningOfLife(), a variable called result is perfectly meaningful. In fact I'd find it a better name than meaningOfLife, especially if you consistently result for all your functions. But yes you could rename the variable if you know it's going to be used in an assert like that.