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.
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.
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.
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.
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.
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
pretending he was me
taking an imaginary drag off a joint
saying, “I’ve got an idea”
…because my ideas are often so unorthodox.
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.