371 | | |
| 371 | === ForEach Statements === |
| 372 | |
| 373 | A foreach statement should have the following form: |
| 374 | {{{ |
| 375 | #!cpp |
| 376 | foreach (var in col) |
| 377 | { |
| 378 | statements; |
| 379 | } |
| 380 | }}} |
| 381 | |
| 382 | === While Statements === |
| 383 | |
| 384 | A while statement should have the following form: |
| 385 | {{{ |
| 386 | #!cpp |
| 387 | while (condition) |
| 388 | { |
| 389 | statements; |
| 390 | } |
| 391 | }}} |
| 392 | |
| 393 | An empty while statement should have the following form: |
| 394 | {{{ |
| 395 | #!cpp |
| 396 | while (condition); |
| 397 | }}} |
| 398 | |
| 399 | === Do-While Statements === |
| 400 | |
| 401 | A do-while statement should have the following form: |
| 402 | {{{ |
| 403 | #!cpp |
| 404 | do |
| 405 | { |
| 406 | statements; |
| 407 | } while (condition); |
| 408 | }}} |
| 409 | |
| 410 | === Switch Statements === |
| 411 | |
| 412 | A switch statement should have the following form. C# requires a terminating break or goto for each case. |
| 413 | {{{ |
| 414 | #!cpp |
| 415 | switch (condition) |
| 416 | { |
| 417 | case XYZ: |
| 418 | statements; |
| 419 | break; |
| 420 | |
| 421 | case ZYX: |
| 422 | statements; |
| 423 | break; |
| 424 | |
| 425 | default: |
| 426 | statements; |
| 427 | break; |
| 428 | } |
| 429 | }}} |
| 430 | |
| 431 | Every switch statement should include a default case. |
| 432 | |
| 433 | === Try-Catch Statements === |
| 434 | |
| 435 | A try-catch statement should have the following format: |
| 436 | {{{ |
| 437 | #!cpp |
| 438 | try |
| 439 | { |
| 440 | statements; |
| 441 | } |
| 442 | catch (MgExceptionClass1 e) |
| 443 | { |
| 444 | statements; |
| 445 | } |
| 446 | catch (MgExceptionClass2 e) |
| 447 | { |
| 448 | statements; |
| 449 | } |
| 450 | }}} |
| 451 | |
| 452 | A try-catch statement may also be followed by `finally` which is always executed. |
| 453 | {{{ |
| 454 | #!cpp |
| 455 | try |
| 456 | { |
| 457 | statements; |
| 458 | } |
| 459 | catch (MgExceptionClass1 e) |
| 460 | { |
| 461 | statements; |
| 462 | } |
| 463 | catch (MgExceptionClass2 e) |
| 464 | { |
| 465 | statements; |
| 466 | } |
| 467 | finally |
| 468 | { |
| 469 | statements; |
| 470 | } |
| 471 | }}} |
| 472 | |
| 473 | === Return Value === |
| 474 | |
| 475 | The use of a single return within a method or function is encouraged. |
| 476 | The following structure: |
| 477 | {{{ |
| 478 | #!cpp |
| 479 | if (booleanExpression) |
| 480 | { |
| 481 | return true; |
| 482 | } |
| 483 | else |
| 484 | { |
| 485 | return false; |
| 486 | } |
| 487 | }}} |
| 488 | should be: |
| 489 | {{{ |
| 490 | #!cpp |
| 491 | return booleanExpression; |
| 492 | }}} |
| 493 | |
| 494 | Also, |
| 495 | {{{ |
| 496 | #!cpp |
| 497 | if (condition) |
| 498 | { |
| 499 | return x; |
| 500 | } |
| 501 | return y; |
| 502 | }}} |
| 503 | |
| 504 | should be: |
| 505 | {{{ |
| 506 | #!cpp |
| 507 | return (condition ? x : y); |
| 508 | }}} |
| 509 | |
| 510 | === Expressions With !== === |
| 511 | |
| 512 | The following order should be used because it will help catch the accidental use of "!=" when "!==" was intended at compile time: |
| 513 | {{{ |
| 514 | #!cpp |
| 515 | if (constant == variable) |
| 516 | { |
| 517 | // do something |
| 518 | } |
| 519 | else |
| 520 | { |
| 521 | // do something else |
| 522 | } |
| 523 | }}} |
| 524 | |