The above Specific examples described in are as follows. (Source code is C #) specification) Consider comment management on bulletin boards. The posted comment is displayed in the comment list, and each comment is edited, deleted, approved, remanded, detailed display, and each button is controlled under the following conditions.
Source code) * Since I want to see only conditional branching, button activation control is omitted.
private const string AuthorId = "Poster ID";
private const string FirstManagerId = "First administrator ID";
private const string SecondManagerId = "Second administrator ID";
/// <summary>
///Control the activity of the button.
/// </summary>
/// <param name="loginUserId">Login person ID</param>
/// <param name="firstApproverId">Primary approver ID(Null if not approved)</param>
/// <param name="secondApproverId">Secondary approver ID(Null if not approved)</param>
private void SetButtonControls(string loginUserId, string firstApproverId, string secondApproverId)
{
//Each button is inactive and initialized.
if (loginUserId == AuthorId && firstApproverId == null && secondApproverId == null)
{
//Edit button, delete button, detail view button, activation
}
else if (loginUserId == FirstManagerId && firstApproverId == null)
{
//Approval button, remand button, detail display button activation
}
else if (loginUserId == FirstManagerId && firstApproverId != null)
{
//Detailed display button activation
}
else if (loginUserId == SecondManagerId && firstApproverId != null && secondApproverId == null)
{
//Approval button, remand button, detail display button activation
}
else if (loginUserId == SecondManagerId && firstApproverId != null && secondApproverId != null)
{
//Detailed display button activation
}
}
private static final String AUTHOR_ID = "Poster ID";
private static final String FIRST_MANAGER_ID = "First administrator ID";
private static final String SECOND_MANAGER_ID = "Second administrator ID";
protected void setButtonControls(String loginUserId, String firstApproverId, String secondApproverId) {
//Each button is inactive and initialized.
if (AUTHOR_ID.equals(loginUserId) && firstApproverId == null && secondApproverId == null) {
//Edit button, delete button, detail view button, activation
} else if (FIRST_MANAGER_ID.equals(loginUserId) && firstApproverId == null) {
//Approval button, remand button, detail display button activation
} else if (FIRST_MANAGER_ID.equals(loginUserId) && firstApproverId != null) {
//Detailed display button activation
} else if (SECOND_MANAGER_ID.equals(loginUserId) && firstApproverId != null && secondApproverId == null) {
//Approval button, remand button, detail display button activation
} else if (SECOND_MANAGER_ID.equals(loginUserId) && firstApproverId == null && secondApproverId != null) {
//Detailed display button activation
}
}
It's as per the specifications, but it's hard to read because it's all else if. Also, since the negative statement of the same conditional statement is written in the if statement, it is redundant. It is better to arrange if statements in the same row.
private void SetButtonControls(string loginUserId, string firstApproverId, string secondApproverId)
{
//Each button is inactive and initialized.
//There were many people around me who didn't know how to write this.
bool firstApproved = firstApproverId != null;
bool secondApproved = secondApproverId != null;
if (loginUserId == AuthorId)
{
if (!firstApproved && !secondApproved)
{
//Edit button, delete button, active
}
//Detailed display button activity
}
else if (loginUserId == FirstManagerId)
{
if (!firstApproved)
{
//Approve button, remand button, activation
}
//Approval details button activation
}
else if (loginUserId == SecondManagerId)
{
if (firstApproved && !secondApproved)
{
//Approval button, remand button
}
//Detailed display button activation
}
}
private static final String AUTHOR_ID = "Poster ID";
private static final String FIRST_MANAGER_ID = "First administrator ID";
private static final String SECOND_MANAGER_ID = "Second administrator ID";
protected void setButtonControls(String loginUserId, String firstApproverId, String secondApproverId) {
//Each button is inactive and initialized.
boolean firstApproved = firstApproverId != null;
boolean secondApproved = secondApproverId != null;
if (AUTHOR_ID.equals(loginUserId)) {
if (!firstApproved && !secondApproved) {
//Edit button, delete button, active
}
//Detail display button activation
} else if (FIRST_MANAGER_ID.equals(loginUserId)) {
if (!firstApproved) {
//Approve button, remand button, activation
}
//Approval details button activation
} else if (SECOND_MANAGER_ID.equals(loginUserId)) {
if (firstApproved && !secondApproved) {
//Approval button, remand button
}
//Detailed display button activation
}
}
Problems related to how to write source code basically occur due to individual skill differences such as "I can't think of such a way of writing". I would like to write sample code regularly, including the meaning of reviewing the source code.
I received a reference code in the comment section, so I will post it. In addition, some variable names have been modified so that the handling of Bool type can be unified with other articles. There is no if statement, and the button activity control is easy to understand.
private void SetButtonControls(string loginUserId, string firstApproverId, string secondApproverId)
{
//First approved
bool firstApproved = firstApproverId != null;
//Second approved
bool secondApproved = firstApproved && secondApproverId != null;
//String comparison can be Equals,==But string comparison is possible.
bool isAuthor = AuthorId == loginUserId;
bool isFirstManager = FirstManagerId == loginUserId;
bool isSecoundManager = SecondManagerId == loginUserId;
//Button activation control
//Edit button
bool editButtonEnabled = isAuthor && !firstApproved;
//Delete button
bool deleteButtonEnabled = isAuthor && !firstApproved;
//Approval button
bool approvalButtonEnabled = (isFirstManager && !firstApproved) || (isSecoundManager && !secondApproved);
//Details button
bool detailButtonEnabled = isAuthor || isFirstManager || isSecoundManager;
}
private static final String AUTHOR_ID = "Poster ID";
private static final String FIRST_MANAGER_ID = "First administrator ID";
private static final String SECOND_MANAGER_ID = "Second administrator ID";
protected void setButtonControls(String loginUserId, String firstApproverId,
String secondApproverId) {
//First approved
boolean firstApproved = firstApproverId != null;
//Second approved
boolean secondApproved = firstApproved && secondApproverId != null;
boolean isAuthor = AUTHOR_ID.equals(loginUserId);
boolean isFirstManager = FIRST_MANAGER_ID.equals(loginUserId);
boolean isSecoundManager = SECOND_MANAGER_ID.equals(loginUserId);
//Button activation control
//Edit button
boolean editButtonEnabled = isAuthor && !firstApproved;
//Delete button
boolean deleteButtonEnabled = isAuthor && !firstApproved;
//Approval button
boolean approvalButtonEnabled = (isFirstManager && !firstApproved) || (isSecoundManager && !secondApproved);
//Details button
boolean detailButtonEnabled = isAuthor || isFirstManager || isSecoundManager;
}
Next article (code collection that is a little worrisome)
Recommended Posts