I need critique on this code
-
Just got back into writing code after 10 years. This is a infix to postfix converter. Can someone please critique this... thanks
using System;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;namespace Infix2Postfix
{
public class Program
{
static void Main(string[] args)
{
#region variable declarations
//declare and initialize the string to accept an expression in infix notation
String stringInfix = "a+b/c-d";StringBuilder SbPostfix = new StringBuilder(); Stack<char> stackGeneral = new Stack<char>(); Stack<char> stackOperator = new Stack<char>(); char\[\] tempArray = stringInfix.ToCharArray(); Array.Reverse(tempArray); Regex regexAlphaNum = new Regex("\[a-zA-Z0-9\]"); char temp; #endregion #region logic foreach (char ch in tempArray) stackGeneral.Push(ch); foreach (char ch in stackGeneral) { if (regexAlphaNum.IsMatch(Convert.ToString(ch))) SbPostfix.Append(ch); else if (stackOperator.Count == 0) stackOperator.Push(ch); else if (stackOperator.Count != 0) switch (ch) { case '+': temp = stackOperator.Peek(); if (temp == '\*' || temp == '/') { while (stackOperator.Count > 0) SbPostfix.Append(stackOperator.Pop()); stackOperator.Push(ch); } else stackOperator.Push(ch); break; case '-': temp = stackOperator.Peek(); if (temp == '\*' || temp == '/') { while (stackOperator.Count > 0) SbPostfix.Append(stackOperator.Pop()); stackOperator.Push(ch); } else stackOperator.Push(ch);
-
Just got back into writing code after 10 years. This is a infix to postfix converter. Can someone please critique this... thanks
using System;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;namespace Infix2Postfix
{
public class Program
{
static void Main(string[] args)
{
#region variable declarations
//declare and initialize the string to accept an expression in infix notation
String stringInfix = "a+b/c-d";StringBuilder SbPostfix = new StringBuilder(); Stack<char> stackGeneral = new Stack<char>(); Stack<char> stackOperator = new Stack<char>(); char\[\] tempArray = stringInfix.ToCharArray(); Array.Reverse(tempArray); Regex regexAlphaNum = new Regex("\[a-zA-Z0-9\]"); char temp; #endregion #region logic foreach (char ch in tempArray) stackGeneral.Push(ch); foreach (char ch in stackGeneral) { if (regexAlphaNum.IsMatch(Convert.ToString(ch))) SbPostfix.Append(ch); else if (stackOperator.Count == 0) stackOperator.Push(ch); else if (stackOperator.Count != 0) switch (ch) { case '+': temp = stackOperator.Peek(); if (temp == '\*' || temp == '/') { while (stackOperator.Count > 0) SbPostfix.Append(stackOperator.Pop()); stackOperator.Push(ch); } else stackOperator.Push(ch); break; case '-': temp = stackOperator.Peek(); if (temp == '\*' || temp == '/') { while (stackOperator.Count > 0) SbPostfix.Append(stackOperator.Pop()); stackOperator.Push(ch); } else stackOperator.Push(ch);
-
Just got back into writing code after 10 years. This is a infix to postfix converter. Can someone please critique this... thanks
using System;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;namespace Infix2Postfix
{
public class Program
{
static void Main(string[] args)
{
#region variable declarations
//declare and initialize the string to accept an expression in infix notation
String stringInfix = "a+b/c-d";StringBuilder SbPostfix = new StringBuilder(); Stack<char> stackGeneral = new Stack<char>(); Stack<char> stackOperator = new Stack<char>(); char\[\] tempArray = stringInfix.ToCharArray(); Array.Reverse(tempArray); Regex regexAlphaNum = new Regex("\[a-zA-Z0-9\]"); char temp; #endregion #region logic foreach (char ch in tempArray) stackGeneral.Push(ch); foreach (char ch in stackGeneral) { if (regexAlphaNum.IsMatch(Convert.ToString(ch))) SbPostfix.Append(ch); else if (stackOperator.Count == 0) stackOperator.Push(ch); else if (stackOperator.Count != 0) switch (ch) { case '+': temp = stackOperator.Peek(); if (temp == '\*' || temp == '/') { while (stackOperator.Count > 0) SbPostfix.Append(stackOperator.Pop()); stackOperator.Push(ch); } else stackOperator.Push(ch); break; case '-': temp = stackOperator.Peek(); if (temp == '\*' || temp == '/') { while (stackOperator.Count > 0) SbPostfix.Append(stackOperator.Pop()); stackOperator.Push(ch); } else stackOperator.Push(ch);
I would fire you on the spot for putting regions in a method and not maintaining consistent capitalization of variables.
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane
-
I would fire you on the spot for putting regions in a method and not maintaining consistent capitalization of variables.
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane
lol...I know exactly which variable name you are referring to.
-
I would fire you on the spot for putting regions in a method and not maintaining consistent capitalization of variables.
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane
Why are regions in a method a bad idea? I ask this risking sounding like a fool.
-
Why are regions in a method a bad idea? I ask this risking sounding like a fool.
Regions are intended to be used in GROUPING methods not splitting one method's body. [EDIT] Example:
#region Events//put here all/most of the event(s) handling code
#region Drawing//put here all the drawing methods
#region YouNameIt// put here all the...
[/EDIT]
-
It only handles inputs in a*b-c/d format. The code is very basic, written in an non elegant way. I will look at your code, it might give me some insights.
-
Regions are intended to be used in GROUPING methods not splitting one method's body. [EDIT] Example:
#region Events//put here all/most of the event(s) handling code
#region Drawing//put here all the drawing methods
#region YouNameIt// put here all the...
[/EDIT]
Ahh...I understand. Thanks!
-
Here I am criticizing it :-D It's too long and too late at night for me :zzz: . Now it's joke time :cool
LOL-i.pop();
-
Ahh...I understand. Thanks!
-
Oh c'mon my friend...your statement helped me more than any other reading I did today :-D
-
Oh c'mon my friend...your statement helped me more than any other reading I did today :-D
-
LOL-i.pop();
-
Sounds like candy...haha
-
Why are regions in a method a bad idea? I ask this risking sounding like a fool.
Also, if you are using a region in a method, it is a sure sign you should put some of your logic into another method and just call that method from your original method.
-
Also, if you are using a region in a method, it is a sure sign you should put some of your logic into another method and just call that method from your original method.
Thank you for your reply and the link! :-O
-
Thank you for your reply and the link! :-O
You are welcome. FYI, the link is part of my generic signature and was not targeted at you specifically. Still, if you haven't read it yet, it is advisable that you do so. :)
-
Regions are intended to be used in GROUPING methods not splitting one method's body. [EDIT] Example:
#region Events//put here all/most of the event(s) handling code
#region Drawing//put here all the drawing methods
#region YouNameIt// put here all the...
[/EDIT]
That may be only a personal or company standard. One can use regions wherever one darn well wants to. I generally use them only to group methods and properties, but I may also use seperate files (partial classes) for that purpose if the code becomes large enough. In my RPN transformer I use regions to mark off less-important code that the reader may not be interested in -- it should at least help with documentation and readability -- I don't want to read through a bunch of error-handling code when I'm trying to get a feel for the process flow of a method.
-
Also, if you are using a region in a method, it is a sure sign you should put some of your logic into another method and just call that method from your original method.
Yes, but the additional method calls could become a bottleneck in a tight loop. And the resultant code could be even less readable. You need to examine each case on its own.
-
Yes, but the additional method calls could become a bottleneck in a tight loop. And the resultant code could be even less readable. You need to examine each case on its own.
I'd say it's probably unlikely, as the JIT compiler is able to inline function calls. In any event, if it's a tight loop, it seems pretty weird that there would be a region in the code anyway, in which case a method call would also seem unnecessary to break out the logic. And my point was that if you are thinking of making a region in your method, it's probably because your are trying to compress a bunch of code into a smaller block and that is usually better done with a method call.