The Problem with Learning Yourself

I just realised I can switch a boolean with myBool = !myBool…

I’ve spent over a year doing it the hard way…

if (myBool) myBool = false;
else myBool = true;

I can’t help but feel, I could have saved myself a lot of time had I spent some time reading a javascript book, or had any qualifications at all :slight_smile:

Thoughts?

But finding little tricks like that is half the fun of teaching yourself :slight_smile:

The longer you do it the “wrong” way, the more you appreciate it when a shortcut dawns on you.

My biggest discovery has been the Conditional Operator. Works like ‘if’ but can be used (for example) inside function calls.

Like this:

// Old way
if( something == true )
{
  GUI.label( new rect( 10.0f, 10.0f, 100.0f, 20.0f ), "text 1" );
}
else
{
  GUI.label( new rect( 10.0f, 10.0f, 100.0f, 20.0f ), "text 2" );
}

// Conditional Operator
GUI.label( new rect( 10.0f, 10.0f, 100.0f, 20.0f ), something == true ? "text 1" : "text 2" );

// or can be used in menu
GUI.label( new rect( 10.0f, 10.0f, 100.0f, 20.0f ), selectedMenuItem == 1 ? texture1_hi : texture1 );

Very handy in many situations.

I’m not sure if this works in javascript, because I always code in C#.

Don’t know about javascript but it’s definitely present in other languages. I use that a lot in Objective-C.

Other languages such as Visual Basic .NET have implementations of it too - it’s called an “Inline If” and is called via:

IIf(isFat, Diet(), Feast())

Very cool stuff :slight_smile:

This happens to me alot, but rather than shortcuts, it’s just more efficient ways of programming.

I’ve been working on a game for a little over 3 months now, and probably with Unity for about 4. If I look at the differences between the first script I made for the game (basic character movement) and the latest scripts for powerups, there’s just so much more going on.

For example, I didn’t know about yield; or coroutines…didn’t know that exposed variables were global and I could access them through other scripts (I would have silly functions called ReturnVariable() and call it from other scripts…yes I know it was dumb)…

No, this is not dumb. This is actually smart if your variables are private nor protected. I use time-to-time functions to set and get variables from other classes.

At work I work with 5 other guys, one which is a programming elite and he teaches me a few great tricks from time to time, yet when I’m fiddling with Unity alone at home I don’t find it that much different. I use google just the same, I look at other peoples code and projects just the same. I learn from other people on the internet in just the same way as I learn from my coworkers at work. After all there are thousands of programming elites out there and luckily we can all learn from them through the internet. :wink:

But I also like to think of myself as a very logical person. For instance when I had written a bunch of if else statements I started to think there had to be a simpler way for those times when I wanted an if else on a single line etc. and I found the conditional operator. Seeing as programming has existed for so long and much brighter people than me has been doing it for so long I thought there had to be something out there that didn’t require me to write the superfluous if else with matching brackets and parentheses every single time and there was. :slight_smile:

When I think a bunch of else statements I immediately think of switch statements. But I suppose I am lucky enough to be taught Java for one year of High School and then three years so far here at college.

I love finding new shortcuts. I constantly am tying to optimize my code and find it rather nice to find easier ways to do things.

Does work on javascript! :3~

Every coupla months or so I always grab the latest rev of a C# book (v4 right now) to keep on top of new paradigms, and to frankly just try to stay on top of my game.

As a .NET programmer by profession, it moves so quickly that it’s hard to stay on top of all the advances. I still have yet to do any serious Presentation Foundation or WorkFlows stuff even though I love what little of it I’ve played with!

I highly recommend just for general programming practices / skills to grab a copy of Code Complete, or The Pragmatic Programmer - books that flesh you out generally as a programmer rather than focusing on specific languages.

-Will

I also recommend viewing other people’s code, those which you know to have a very, very solid grasp of programming. Viewing the code from other Unity projects, other developers, etc can give you an idea of the different ways to doing things.

Learning stuff by yourself is very rewarding!
makes you feel like you “own” your knowledge

I’m almost entirely self-taught, and I love finding new tricks approaches. But when someone showed me a “state machine” (I think that’s the proper name), whew, I was impressed! I use it for everything now. Model-View-Controller I’m not as fond of, though I can see the benefits.

But there are always 10 ways to do any one thing – and I love coming up with an 11th way! One of my coworkers used to tease me by

  1. pretending he was me
  2. taking an imaginary drag off a joint
  3. saying, “I’ve got an idea”
    …because my ideas are often so unorthodox.

When I look at a bunch of “if else” statements I get sleepy.

(not a coder)

Unorthodox ideas are usually the best at finding new efficient ways of doing things. It’s always good to think outside the box!

With respect to booleans, what I often see is people doing this:

if (myVeryOwnBool == true) DoSomething();

The condition for an if statement (the part in parentheses) has to evaluate to true or false. Booleans are inherently true or false, so you don’t need anything extra:

if (myVeryOwnBool) DoSomething();

This should make your code shorter, and if you’re naming your booleans properly (positive declarations, like “isMissing”) then your code should be easier to read.

My brother, who is thousand times bigger propellerhead than I’am, told me how to use the ‘if’ very cleverly. He explained that ‘if’ just tests if the condition is different from 0 (zero).

So for example in GUI texture assingment. If you got texture variable like so:

public Texture2D someGUITexture;

You can test it before draw it:

if(someGUITexture) GUI.Label( new Rect( 0,0,someGUITexture.width, someGUITexture.height), someGUITexture );

It won’t give any error, but if you haven’t assinged any texture in editor, nothign will be drawn.