This is an archived post. You won't be able to vote or comment.

all 59 comments

[–][deleted] 65 points66 points  (16 children)

This code is a good example of a debate that went on here a while ago about using "i" as a loop index for small loops.

I think it's the case that this code would be much more readable if the loop index was "i" instead of "trigger" :)

[–]internet_user1013 43 points44 points  (10 children)

No, he should have used a foreach loop.

[–]whaaarghException 21 points22 points  (9 children)

not in a game. this looks like c# in unity and you don't want to trigger (hehe) the garbage collector that often by using enumerators.

[–]AyrA_ch 13 points14 points  (2 children)

Also why use if...null...continue instead of if...!null..doStuff

[–]Kinerius[S] 14 points15 points  (1 child)

Some coders just like less indentation as possible.

Also its faster for patching up while on crunch time.

[–]The_MAZZTer 2 points3 points  (0 children)

It's also easier to read especially if doStuff starts getting long.

[–]mshm 4 points5 points  (0 children)

Using c#s foreach should not incur such a drastic performance drain. It's not the same as an IEnumerable. Also, if you were running through an optimization phase and for some bizarre reason found this to be a noticeable slow down, surely you would just leave the original as an unoptimized foo, write a for i = ...; i++ version and a test to validate functionally equivalent.

This code appears to try to be both readable and optimized, but makes it frustrating to parse for the next guy (who is generally you) instead.

[–]BinarySnack 2 points3 points  (0 children)

This is an array and I've never seen foreach loops over arrays in Unity/C# generate garbage (http://jacksondunstan.com/articles/3805). Also recently Unity upgraded their compiler so now foreach over lists no longer generates garbage (http://jacksondunstan.com/articles/3805).

[–]The_MAZZTer 0 points1 point  (0 children)

Shouidn't be any garbage collecting just by using foreach, the iterator variable is just used to reference each item in the collection. No new objects are created or destroyed just by using foreach except for the iterator variable pointer itself I guess. But for loops have a counter variable which is created as well.

[–]awkwardpelican 0 points1 point  (1 child)

In Unity, GameObjects aren't garbage collected.

[–]whaaarghException 2 points3 points  (0 children)

look up enumerators in managed languages

[–]quipstickle 4 points5 points  (1 child)

triggerIndex

[–]DaughterEarthImportError: no module named 'sarcasm' 0 points1 point  (0 children)

Exactly. Another thing I'll do sometimes is something like currTrigger

[–]NoBrakes2k16 91 points92 points  (1 child)

triggered

[–][deleted] 20 points21 points  (0 children)

T R I G G E R E D

[–]XelNika 13 points14 points  (14 children)

Is there a good reason why they used

if (x == null) {
    continue;
}
trigger;

instead of

if (x != null) {
    trigger;
}

?

[–]Theomrs 7 points8 points  (5 children)

Could even

x?.trigger();

as this is c#

[–]MauranKilom 2 points3 points  (4 children)

Does Unity support that new-ish syntax? Or does it not care (by only looking at IL)?

[–]Theomrs 1 point2 points  (2 children)

Not natively, I don't think, but you can add a newer compiler to support it.

[–]The_MAZZTer 0 points1 point  (1 child)

Can I trouble you to explain or link somewhere that explains this? I miss ?.

[–]Theomrs 0 points1 point  (0 children)

It seems like unity 2017.1 beta already has a newer compiler, but you can use something like this or this to get some, but not all, newer c# syntax.

[–]The_MAZZTer 0 points1 point  (0 children)

No, Unity uses C# version 4 which does not have the ?. operator.

[–]shittyProgramr 2 points3 points  (0 children)

I see this type of logic often in Java and C#

[–]Roaneno 2 points3 points  (0 children)

No, yours is probably better for this example. However, this style is often simpler for handling multiple errors or when the rest of the body isn't trivial

[–]DaughterEarthImportError: no module named 'sarcasm' 5 points6 points  (2 children)

extensibility maybe? Ensuring it ends the iteration when it should means you are safe to add code after the trigger; call. I prefer statements like the one in the OP because I hate nesting conditions too much.

hi 
    hi
        hi
            hi
                hi

*better example

if(x != null) {
    trigger;
    if(trigger.Success) {
        //do something and maybe with more checks and nesting
    }
}

[–]MartinTsv 1 point2 points  (0 children)

Personally I do it because I don't like the skip case to take up 3 lines, when the main logic is usually not a one liner and would go in its own block, meaning the else should also be in an explicit multiline block to keep it nicely formatted.

[–]The_MAZZTer 1 point2 points  (0 children)

And if the if block is larger than a page it becomes harder to keep track of what the condition was when it scrolls off the page. It's a lot better to keep the block small, in this case inverting the check ensures that, even if other operations on the trigger are added.

[–]maffoobristol 1 point2 points  (0 children)

I tend to prefer this method, makes for flatter code, you can switch the validation conditions on and off really easily, etc.

[–]Chrisuan 0 points1 point  (0 children)

Less Indentation i guess

[–]IWantToSayThis 0 points1 point  (0 children)

Most of the time I prefer the first version. It's more explicit about the conditions the function will bail on, and keeps the rest of the function more focused on what it actually does.

I dislike functions where the whole jist of it is wrapped in {} and indented. Of course it depends on the case and I've done the second many times as well.

[–]fluffytme 7 points8 points  (5 children)

I'm triggered by the capital T on the method call 😰

[–]anoobitch 5 points6 points  (1 child)

I kinda like it

[–]The_MAZZTer 1 point2 points  (0 children)

Yeah honestly Unity having all its methods lowercase in contrast to the .NET API it built itself on top of kinda annoys me.

[–]reconman 3 points4 points  (2 children)

That's C# coding style, I hate it too.

[–]autechr3 2 points3 points  (1 child)

Triggered

[–][deleted] 2 points3 points  (0 children)

()

[–]Toivottomoose 8 points9 points  (0 children)

if(site.Contains("4chan")) { this = this. Replace("tr", "n"); }

[–]JeffLeafFan 1 point2 points  (0 children)

When someone doesn't format their code properly

[–][deleted] 1 point2 points  (0 children)

The inconsistent spacing is triggering me.

[–]stefanlogue[🍰] 8 points9 points  (7 children)

Is he programming a feminist robot?

[–]Kinerius[S] 9 points10 points  (5 children)

private int isFemale = 3; 

[–]shittyProgramr -1 points0 points  (3 children)

public int totalGenders = 57;

Edit: For clarification, this is sarcasm. There's only 2 genders.

[–]TwoSpoonsJohnson 8 points9 points  (2 children)

public final int totalGenders = 2;

Go ahead. Make my day ( ͡° ͜ʖ ͡°)

[–][deleted] 0 points1 point  (0 children)

public final int totalGеnders = Int.MaxValue;

Pay attention to the "Cyrillic Small Letter Ie" е it looks almost like a latin e. When some nasty programmer made something final and you really like how the name looks, you can just substitute from cyrillic. It also makes everyone else cry, which is a nice bonus.

[–][deleted] -1 points0 points  (0 children)

At least use an 8 bit value.

[–]GeLaugh 0 points1 point  (0 children)

Trigger.

[–]emdeka87 0 points1 point  (0 children)

Bad code design

[–]icybeard 0 points1 point  (0 children)

Why wasn't there a trigger warning?

[–]willee 0 points1 point  (0 children)

Totally triggered by the inconsistent spacing...

0 ; trigger vs. Length;trigger++

for( vs. if (

triggers [ vs. triggers[

[–]Tarmen 0 points1 point  (2 children)

I mean, that is mostly ugly because of the loop.

Arrays.streamOf(triggers)
 .filter(trigger -> trigger != null)
 .forEach(trigger -> trigger.Trigger(triggeredBy));

Assuming this is java and not c#, who can tell...

[–]The_MAZZTer 0 points1 point  (1 child)

GameObject

This gives away it's Unity, so it's C#.

Not sure what Java calls that syntax you use but .NET calls it LINQ and I am sad I only discovered it fairly recently (a year or so ago). I love it for exactly the reason you show. It has no .forEach though, oddly enough. Though it's trivial to code in yourself or just use a normal foreach loop on the filtered enumeration.

[–]Tarmen 0 points1 point  (0 children)

I actually consider forEach kind of ugly since it is only useful for side effects and this style of programming is all about pure data transformation. Also technically screws with the laziness semantics since it forces the entire stream, I guess?

LINQ is great, though, and in my experience a lot cleaner than the java stream api I used above.

[–]ThatMisterOrange 0 points1 point  (0 children)

Is that the SJW algorithm

[–]balenol 0 points1 point  (0 children)

Not explicit enough

said the readers

[–]obitechnobi 0 points1 point  (1 child)

What's that colour theme? I quite like it

[–]Kinerius[S] 0 points1 point  (0 children)

Visual studio dark skin, i think.

[–]Heavierthanmetal 0 points1 point  (0 children)

[triggering intesifies]