April 2018
Intermediate to advanced
300 pages
7h 41m
English
Always write methods to do one thing at a time. Let's suppose we have a method that reads the user ID from the database and then calls an API to retrieve the list of documents the user has uploaded. The best approach with this scenario is to have two separate methods, GetUserID and GetUserDocuments, to retrieve the user ID first and then the documents, respectively:
public int GetUserId(string userName)
{
//Get user ID from database by passing the username
}
public List<Document> GetUserDocuments(int userID)
{
//Get list of documents by calling some API
}
The benefit of this approach is that it reduces code repetition. In the future, if we wanted to change the logic of either method, we just have to change it in one ...