Ad
  • Custom User Avatar

    A reasonable optimizing compiler will produce the same machine code regardless of whether the variable is defined before the if-check or after it. Initializing local variables has no effects visible to callers so the compiler may freely reorder the code as it sees fit.

    The reason is just style and clarity. First of all, it's common to add preconditions to the start of a function. If there are no items, we already know the answer and we can return right away. Second, it's generally a good idea to keep the scope of variables as small as possible.

    Of course, in such a short function it doesn't really matter, but if a long function defines variable in the very beginning and uses it a lot later, can you be sure that there are no uses between? You have to carefully read through the whole function to check if there are other references.

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    Space bar - ::Not Found::

  • Custom User Avatar
  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution