RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Before Hello World

Talow

Sorceror
While most programmers will understand the subject some will not so we'll start the tutorial with what Hello World means. To put it simply this is the first code you typically write in a programming language. The desired effect is simple, to output the text "Hello World". Now we see the subject line reads Before Hello World, what this means is I'm going to go over some basics of programming, not how to code anything.

The first thing I'd like to explain is the concept of a "white space" This is a space, tab, enter(return) ext. that is not actual text. Now that we know what it is lets try to understand it a bit more. Most white spaces are not read by the compiler, and have no actual value to the code. What this means to us as coders/programmers/scripters is that we can format the code to make it easier for us to read as humans. (Yes Vorspire is the inspiration for this part.) So I'm going to write a small segment of code the way we would, and then I'll display the same code the way the compiler would read it, to show the difference.

Note: this code is only to show what we are talking about not to actually do anything.
We read like this:
Code:
if ( somevar == true )
{
    if ( othervar > 1 )
    {
        for ( i = 0; i < othervar; i++)
        {
            somemethod(i);
        }
    }
}

Compiler reads like this:
Code:
if(somevar==true){if(othervar>1){for(i=0;i<othervar;i++){somemethod(i);}}}

Now tell me which one would you like to read? Yeah me thought so too. So we now know the white spaces are mainly for us to be able to format and read the code. So why am I posting this? Really I'd like for Vorspire to post a reply to answer that, I think he's gone over it enough times to give all the right answers and then some on that topic, however I will put one simple answer to this. I don't want to read the compiler's verson of code. I want to be able to look at your code and understand it, not have to format it to be able to read it.

Vorspire has told people on a few differe topics, that there are easy ways to format the code, and just so I do not step on his toes, (I think he likes doing this part, I really do.) I'll let him tell you what that is.

This is only a start on this topic, but I'm at work and so will add more later.
 

Thagoras

Sorceror
Amen! Many is the time I've had to copy and paste code into my programming environment (I use a freeware called Notepad++) and reformat the scripts just so I can more easily read them.
 

Vorspire

Knight
Formatting you code largely depends on your preference. When you have a powerful IDE (Interactive Development Environment) like Visual Studio, you can set up formatting options to suit your taste, or whatever makes things readable.

There are far too many concepts to run over when keeping code clean and I'm no professional, but I stand by these basics:

Name methods, Properties and Variables appropriately so anyone can understand exactly what they should equate to without having to read the underlying code.

Handle error checks as early as possible in a method call and return if you cantch any problems, this will ansure the code in the rest of the method doesn't need error checks scattered everywhere and you can ensure you won't get any crashes, for example:
Rich (BB code):
public override bool OnDoubleClick( Mobile from, object o )
{
      if( from == null || from.Deleted )
      { return false; }

      from.SendMessage( "You have double clicked absolutely nothing, we have no idea where that boat came from." ); //No risk of NullReferenceException for 'from'

      return base.OnDoubleClick( from, o );
}

Indentation is a key formatting point for me, I indent after every opening and closing bracer ( { and } ) by +1 tab for each block level I descend into. The only exception to this rule, which is totaly a style choice, is when the braces contain a single code call or line, see above code's from/deleted if-statement for an example.

So basically, if I was going to write the code provided in the first post using my personal coding style or preference, it would look like this:
Rich (BB code):
if( !Utility.RandomBool( ) || count == 0)
{ return; } 

for( int index = 0; index < count; index++ )
{ Console.WriteLine( "{0}/{1}", index + 1, count ); }
If RandomBool is false, it will return straight away, if true, it will check the count, if count is 0, it will return, if not, the method continues...

This is how I read the code in plain English, if you're able to read it like a language and not just code, you'll learn a lot faster.
Acquiring your programming style is the same as developing an accent for a spoken language you have learned.
White-space, indentation and the things that make code readable, may be the equivalent to written grammar.

Were you looking for any other answers in particular? :)
 

Soteric

Knight
Code:
if( from == null || from.Deleted )
      { return false; }
Usually I see indentation here without bracers.
 

Talow

Sorceror
Code:
if( from == null || from.Deleted )
      { return false; }
Usually I see indentation here without bracers.

With an if statement you actually do not need brackets, or the return, or the tab. Those are for us to read the code with more ease.

Edit: You do not need the brackets for an if statement that uses one code statement.

The point of this was not to say that everyone needs to become a drone and format their code the same way, but rather that since we have so many new people to coding that some don't know what's relevant and what isn't, So in their great copy paste adventure their code becomes, in a formating standard, fragmented, or just no formatting at all.

That then brings them an error because they forgot a { or they forgot a ; or something. This is an easy fix, as long as it is easy to see what should be going where, now it's true that you IDE can help you with this, and I know it sounds weird but I use DreamWeaver for my coding. It works for me and I can I learned it a while ago so it makes it easy for me, where Visual Studio is the favored of Vorspire, so we are not pitching IDE, we are pitching formatting, and in the end it's up to the human that's coding what the formatting will be.

My personal formatting style is nothing like Vorspires, and in my code I can tell you exactly where my stuff stops and where Vorspire helped me, or Jeff, or or or. It really is like a finger print.
 

Vorspire

Knight
Take note of the color coordination used in the following examples.

Where "input" is any given 32-bit Integer value (int, Int32)
Rich (BB code):
int result = input < 0 ? 0 : input > 100 ? 100 : input;

Console.WriteLine( "Result: {0}", result );
At first glance, this can seem confusing, but it's really quite simple, chances are you've probably done this a million times and not realised it.

Let's tidy it up a bit...
Rich (BB code):
int result = ( input < 0 ) ? 0 : ( input > 100 ) ? 100 : input;

Console.WriteLine( "Result: {0}", result );

A bit more progression...
Rich (BB code):
int result = ( input < 0 ) ? 0 : ( ( input > 100 ) ? 100 : input );

Console.WriteLine( "Result: {0}", result );

Do you see how the addition of brackets makes it more and more readable?
They don't just serve for visual aides though, they can represent an if statement or an object, depending on what operators your use within them.
Comparisons within brackets using the equal-to ( == ), not-equal-to ( != ), greater-than ( > ) or less-than ( < ) will always result in a boolean true or false, using the brackets like this is a quick way to write an "if" statement, the term typically used for this method is an "in-line statement".
(That's the term I've learned, "in-line" in programming can refer to a few different scenarios, but the point is the same, to use as little code as possible to get the same result. )

So to summarise, the above code would have initially started out like this; which is an example of user input correction before creating the result integer.
Rich (BB code):
if( input < 0 )
{ input = 0; }
else if( input > 100 )
{ input = 100; }

int result = input;

Console.WriteLine( "Result: {0}", result );

Now lets throw this into an every-day RunUO scripting scenario:
Rich (BB code):
private int m_PlayerLevel = 1;

[CommandProperty(AccessLevel.GameMaster)]
public int PlayerLevel
{
      get { return m_PlayerLevel; }
      set
      {
            if( value < 0 )
            { value = 0; }
            else if( value > 100 )
            { value = 100; }

            m_PlayerLevel = value;
      }
}

...and then refine it:
Rich (BB code):
private int m_PlayerLevel = 1;

[CommandProperty(AccessLevel.GameMaster)]
public int PlayerLevel
{
      get { return m_PlayerLevel; }
      set { m_PlayerLevel = value < 0 ? 0 : value > 100 ? 100 : value; }
}

:)
 
Top