[Question] Coding Syntax

12/09/2010 01:12 FuriousFang#1
Hey everyone!
Is this good syntax?

Code:
#region Example
                if (Statement1 == 1)
                    if (Statement2 == 2)
                        switch (Statement3)
                        {
                            case 1:
                                if (Statement1 == Statement 2)
                                    Statement4++;
                                break;
                        }
                #endregion
Thanks!
Sincerely,
Fang
12/09/2010 01:16 pro4never#2
Umm... that would depend on what the statements are and why you would code anything that way?

in that you will never get statement4 to increase because you are requiring 1 and 2 to be different things and then checking for statement 3 to see if they are equal to modify statement 4.

Honestly what does this have to do with ANYTHING? :S....

Personally you should be doing a switch based on the type and then handle the other statements. As you have it right now, yes it will 'work' in that it will flow through properly but it will never actually execute anything.
12/09/2010 02:02 FuriousFang#3
Quote:
Originally Posted by pro4never View Post
Umm... that would depend on what the statements are and why you would code anything that way?

in that you will never get statement4 to increase because you are requiring 1 and 2 to be different things and then checking for statement 3 to see if they are equal to modify statement 4.

Honestly what does this have to do with ANYTHING? :S....

Personally you should be doing a switch based on the type and then handle the other statements. As you have it right now, yes it will 'work' in that it will flow through properly but it will never actually execute anything.
I'm just using them as examples of what i'm trying to code.
I'm trying to code my own source and I want it to be as organized as possible with less brackets everywhere. All i'm asking is if it works without the brackets like that. The indentation looks like it will.
12/09/2010 02:22 pro4never#4
Yah you don't need brackets to force indentation but keep in mind if statements only control the next line if you don't use brackets.

IE


if(Blah)
do action1
do action2

will only do action 1. To have it do both you'd need to surround them by brackets.

Switch statements are good any time you have more than a few things to discern between (IE: 3-4+ conditions I'd try to work it into a switch statement)
12/09/2010 03:09 _tao4229_#5
It depends on how you want it really.
If you're developing by yourself just do it how you want it.

I prefer to put my { at the end of lines
Code:
int f(int x) {
    ....
}
but that's just me.
12/09/2010 03:47 FuriousFang#6
Quote:
Originally Posted by pro4never View Post
Yah you don't need brackets to force indentation but keep in mind if statements only control the next line if you don't use brackets.

IE


if(Blah)
do action1
do action2

will only do action 1. To have it do both you'd need to surround them by brackets.

Switch statements are good any time you have more than a few things to discern between (IE: 3-4+ conditions I'd try to work it into a switch statement)
I know that much =P
Thanks for confirming though!

Quote:
Originally Posted by _tao4229_ View Post
It depends on how you want it really.
If you're developing by yourself just do it how you want it.

I prefer to put my { at the end of lines
Code:
int f(int x) {
    ....
}
but that's just me.
That's how I code CSS! xP
12/09/2010 04:14 InfamousNoone#7
Quote:
Originally Posted by FuriousFang View Post
Hey everyone!
Is this good syntax?

Code:
#region Example
                if (Statement1 == 1)
                    if (Statement2 == 2)
                        switch (Statement3)
                        {
                            case 1:
                                if (Statement1 == Statement 2)
                                    Statement4++;
                                break;
                        }
                #endregion
Thanks!
Sincerely,
Fang
In all honesty it's up to you. In my opinion: no.
I'd argue if your code was,
Code:
if (s == 1)
	if (s2 == 2)
		Method();
That's fine.
But, if you're going to need to open up a scope (breaking out the curly brackets), then I would surround every statement with curly braces, i.e.
Code:
if (s == 1)
{
	if (s2 == 2)
	{
		Method();
		Method2();
		Method3();
	}
}
verses;
Code:
if (s == 1)
	if (s2 == 2)
	{
		Method();
		Method2();
		Method3();
	}
Quote:
Originally Posted by _tao4229_ View Post
It depends on how you want it really.
If you're developing by yourself just do it how you want it.

I prefer to put my { at the end of lines
Code:
int f(int x) {
    ....
}
but that's just me.
Heard you like using lowercase method names, and instead of properties using getElement() and setElement(), bro'.

Quote:
Originally Posted by pro4never View Post
Yah you don't need brackets to force indentation but keep in mind if statements only control the next line if you don't use brackets.

IE


if(Blah)
do action1
do action2

will only do action 1. To have it do both you'd need to surround them by brackets.

Switch statements are good any time you have more than a few things to discern between (IE: 3-4+ conditions I'd try to work it into a switch statement)
Two things:
I don't know if you explained it because you think what he put in his code-quote wouldn't execute properly -- but to clarify, it'll run perfectly fine.

Secondly, using a switch-statement where it isn't necessary (as you said, under 3-4 cases) will be converted at compile-time to if-statements (assuming code optimizations is enabled -- but hey, why would you turn it off?).
12/09/2010 06:28 ChingChong23#8
Quote:
Originally Posted by InfamousNoone View Post

Heard you like using lowercase method names, and instead of properties using getElement() and setElement(), bro'.

sounds like a java programmer, i also like

test() {

}
12/09/2010 09:31 Korvacs#9
Moved.
12/10/2010 14:28 KraHen#10
I always put brackets after if statements, even if only one method comes after it, simply because I like it this way :) Oh and I never put the { like most Java programmers do, I find it irritating.
12/10/2010 23:42 Trigorio#11
I almost always use brackets no matter if it's for a switch statement or an if statement or almost any statement. For switch statements the reason you should use brackets is
1. It looks cleaner and more organized (In my eyes atleast).
2. If you wish to have 2 different variables, say a ushort and a double value with the same variable name (example y and y), then you must seperate the cases with brackets so that the variables are not conflicting within the same declaration space introduced by the switch statement.

For brackets with if statements, using brackets simplifies future work incase you have to add more within the if statement, and ontop of that it looks better and is more organized (atleast in my eyes).
12/12/2010 01:32 Ian*#12
I think if your code does what you are trying to get it to do, and compiles successfully, AND IT DOESNT HURT YOUR EYES IT IS GOOD.

pic related:
[Only registered and activated users can see links. Click Here To Register...]

QUIT DELETING ALL OF MY POSTS.

:rolleyes:
12/12/2010 09:05 InfamousNoone#13
Quote:
Originally Posted by ChingChong23 View Post
sounds like a java programmer, i also like

test() {

}
Saint (tao4229) and I despise Java, that's why I thought it'd be funny to post that. I'm glad other than us picked up on the joke.
12/12/2010 13:19 Syst3m_W1z4rd#14
Code:
    class Program
    {
        static int cookies;
        static void Main(string[] args) {
            int HowManyCookies = int.Parse(Console.ReadLine());
            Cookie(HowManyCookies);
            Console.WriteLine(Message(cookies)); }
        static int Cookie(int value) { cookies = value; return value; }
        static string Message(int Cookies) {
            if (Cookies > 0) {
                return "You have cookies!";
            }
            else { return "You have no cookies!"; }
        }
    }
12/13/2010 05:14 InfamousNoone#15
Quote:
Originally Posted by Syst3m_W1z4rd View Post
Code:
    class Program
    {
        static int cookies;
        static void Main(string[] args) {
            int HowManyCookies = int.Parse(Console.ReadLine());
            Cookie(HowManyCookies);
            Console.WriteLine(Message(cookies)); }
        static int Cookie(int value) { cookies = value; return value; }
        static string Message(int Cookies) {
            if (Cookies > 0) {
                return "You have cookies!";
            }
            else { return "You have no cookies!"; }
        }
    }
Code:
    class Program
    {
        public static int Cookies { get; private set; }
        static void Main(string[] args)
        {
            Cookies = int.Parse(Console.ReadLine());
            Console.WriteLine(Cookies > 0 ? "You have cookies!" : "You have no coookies!");
        }
    }