Programming logic best practice - redundant checks [closed]

Asked
Active3 hr before
Viewed126 times

7 Answers

practicelogic
90%

Expression contains redundant bracket or not,If immediate pop hits an open parenthesis ‘(‘, then we have found a duplicate parenthesis. For example, (((a+b))+c) has duplicate brackets around a+b. When we reach the second “)” after a+b, we have “((” in the stack. Since the top of stack is an opening bracket, we conclude that there are duplicate brackets.  ,Check for Balanced Brackets in an expression (well-formedness) using Stack,Stack Data Structure (Introduction and Program)

Given a string of balanced expression, find if it contains a redundant parenthesis or not. A set of parenthesis are redundant if the same sub-expression is surrounded by unnecessary or multiple brackets. Print ‘Yes’ if redundant, else ‘No’.
Note: Expression may contain ‘+‘, ‘*‘, ‘‘ and ‘/‘ operators. Given expression is valid and there are no white spaces present.
Example: 
 

Input:
   ((a + b))
   (a + (b) / c)
   (a + b * (c - d))
Output:
   Yes
Yes
No

Explanation:
   1.((a + b)) can reduced to(a + b), this Redundant
2.(a + (b) / c) can reduced to(a + b / c) because b is
surrounded by() which is redundant.
3.(a + b * (c - d)) doesn 't have any redundant or multiple
brackets.
Output
Yes
Yes
No
load more v
88%

Software Engineering Meta , Software Engineering help chat , Podcast 394: what if you could invest in your favorite developer? ,Thanks for contributing an answer to Software Engineering Stack Exchange!

Assume you have a function (in C)

void readInputFile(const char * path);
load more v
72%

Suppose I have some functions (eg. method1() and method2()), and each of these functions should only be performed if a some condition(s) are met (for this simplistic example, call this a check against a boolean). To isolate the conditional check, I place the check inside each function - eg,You don't need to check the conditions in the outer functions, Just call the appropriate functions and they will execute if needed. You could still leave it in for self-documentation purposes.,I think the best way to avoid multiple conditional checks would be implement State design pattern in your code. This design pattern just allows you to use code as State machine, meaning depending upon the state of your object state you can call desired method. you can get more information over here http://en.wikipedia.org/wiki/State_pattern,p.s You can try my style of checking, i.e return-if-negative, rather than process-if-positive, this might make the code a little cleaner sometimes, but reducing the indentation level.

Depending on the concrete details of your issue.. One thing you could do is to put the checks within the doSomething functions.

void doSomething1() {
   if (!bDoSomething) return;

   ....
}
load more v
65%

Pull-Up/Push-Down Method,Branching by abstraction is a method used primarily when there is a large amount of refactoring to be done. Abstraction involves class inheritances, hierarchy, and extraction. The goal of abstraction is to reduce unnecessary duplications in software code.,Software developer Jessica Kerr provides a great illustrative explanation for preparatory refactoring:,Composing involves streamlining the code in order to reduce duplications. This is done through various processes, including extraction and inline methods.

Love this:
Another time to think about refactoring is right after you’ve delivered a product to market. Yes, that sounds ridiculous. You’ve finally launched a product you’ve been working on for months, maybe even years, and now you have to go back to the beginning? This is actually the perfect time to do a little housekeeping, as chances are developers now have more availability to work on refactoring before moving on to the next job.
So cool!

load more v
75%

Here is a sample of how to handle all incoming records: , Apex Code is the Force.com programming language used to write custom, robust business logic. As with any programming language, there are key coding principles and best practices that will help you write efficient, scalable code. This article illustrates many of the key best practices for writing and designing Apex Code solutions on the Force.com platform. , By ensuring no IDs are stored in the Apex code, you are making the code much more dynamic and flexible - and ensuring that it can be deployed safely to different environments. , Leading Through Change with Data

The issue is that only one Account record is handled because the code explicitly accesses only the first record in the Trigger.new collection by using the syntax Trigger.new[0]. Instead, the trigger should properly handle the entire collection of Accounts in the Trigger.new collection.

Trigger.new

The issue is that only one Account record is handled because the code explicitly accesses only the first record in the Trigger.new collection by using the syntax Trigger.new[0]. Instead, the trigger should properly handle the entire collection of Accounts in the Trigger.new collection.

Trigger.new[0]

The issue is that only one Account record is handled because the code explicitly accesses only the first record in the Trigger.new collection by using the syntax Trigger.new[0]. Instead, the trigger should properly handle the entire collection of Accounts in the Trigger.new collection.

Trigger.new
load more v
40%

While there are many resources to help programmers write better code—such as books and static analyzers—there are few for writing better comments. While it’s easy to measure the quantity of comments in a program, it’s hard to measure the quality, and the two are not necessarily correlated. A bad comment is worse than no comment at all. As Peter Vogel has written:,If you’re like most programmers, you sometimes use code that you find online. Including a reference to the source enables future readers to get the full context, such as:,There are different levels of expertise that manifest in programmers, I would think that if you are not writing a production program, commenting it to the maximum would be the best for the lower-level experts to understand and learn the tricks.,While all of these points are true, it would be a mistake to go to the other extreme and never write comments. Here are some rules to help you achieve a happy medium:

Many junior programmers write too many comments because they were trained to do so by their introductory instructors. I’ve seen students in upper-division computer science classes add a comment to each closed brace to indicate what block is ending:

if (x > 3) {
   …} // if
load more v
22%

The safest practice is to always provide a comparisonType parameter to the Equals method. Here are some basic guidelines:,There are many different ways to compare strings in C#.,Rather, the preferred way to test for string equality in C# programming is with the Equals method:,When comparing strings that were input by the user, or are to be displayed to the user, use a culture-sensitive comparison (CurrentCulture or CurrentCultureIgnoreCase).

If you don’t know whether the object you’re using is a value type or reference type, you could run into some surprises. For example:

      Point point1 = new Point(20, 30);
      Point point2 = point1;
      point2.X = 50;
      Console.WriteLine(point1.X); // 20 (does this surprise you?)
      Console.WriteLine(point2.X); // 50

      Pen pen1 = new Pen(Color.Black);
      Pen pen2 = pen1;
      pen2.Color = Color.Blue;
      Console.WriteLine(pen1.Color); // Blue (or does this surprise you?)
      Console.WriteLine(pen2.Color); // Blue
load more v

Other "practice-logic" queries related to "Programming logic best practice - redundant checks [closed]"