Useful alternative control structures?

后端 未结 28 924
礼貌的吻别
礼貌的吻别 2021-01-30 02:20

Sometimes when I am programming, I find that some particular control structure would be very useful to me, but is not directly available in my programming language. I think my

28条回答
  •  后悔当初
    2021-01-30 02:53

    I'd like to see a keyword for grouping output. Instead of this:

            int lastValue = 0;
    
            foreach (var val in dataSource)
            {
                if (lastValue != val.CustomerID)
                {                    
                    WriteFooter(lastValue);
                    WriteHeader(val);
                    lastValue = val.CustomerID;
                }
                WriteRow(val);
            }
            if (lastValue != 0)
            {
                WriteFooter(lastValue);
            }
    

    how about something like this:

            foreach(var val in dataSource)
            groupon(val.CustomerID)
            {            
                startgroup
                {
                    WriteHeader(val);
                }
                endgroup
                {
                    WriteFooter(val)
                }
            }
            each
            {
                WriteRow(val);
            }
    

    If you have a decent platform, controls, and/or reporting formatting you won't need to write this code. But it's amazing how often I find myself doing this. The most annoying part is the footer after the last iteration - it's hard to do this in a real life example without duplicating code.

提交回复
热议问题