Returning from methods
When pair programming there are obviously times when you have different opinions about how things should be done.
One of these is the way that we should return from methods. There seem to be two approaches when it comes to this:
Exit as quickly as possible
The goal with this approach is as the title suggests, to get out of the method at the earliest possible moment.
The Guard Block is the best example of this. It is generally used at the start of methods to stop further execution of the method if the parameters are invalid for example:
12345~ </td> | public void DoSomething(SomeObject someObject){ if (someObject == null) return; // Do some other stuff}~ </td> </tr> </tbody></table> When used in this way it is very similar to what would be called a pre condition in the world of Design by Contract. It can also be used in methods which return a result:
|
About the author
I'm currently working on short form content at ClickHouse. I publish short 5 minute videos showing how to solve data problems on YouTube @LearnDataWithMark. I previously worked on graph analytics at Neo4j, where I also co-authored the O'Reilly Graph Algorithms Book with Amy Hodler.