Coding convention question about AS3 duplicate variable definition

AS3’s got some awkward rules about variable scope, due to its use of hoisting. I don’t like pointlessly leaving a bunch of compiler warnings lying around, but it’s more important to me for my code to readable and properly written, regardless. This produces a couple of duplicate variable definition warnings:

    private function arrangeElementsLine(pElementSize:Dimensions):void
    {
        if (m_eOrientation == HORIZONTAL)
        {
            for (var i:uint = 0; i < target.numElements; i++)
            {
                var layoutElement:ILayoutElement = target.getElementAt(i);
                layoutElement.setLayoutBoundsSize(pElementSize.x, pElementSize.y);
                layoutElement.setLayoutBoundsPosition(pElementSize.x * i, 0);
            }
        }
        else
        {
            for (var i:uint = 0; i < target.numElements; i++)
            {
                var layoutElement:ILayoutElement = target.getElementAt(i);
                layoutElement.setLayoutBoundsSize(pElementSize.x, pElementSize.y);
                layoutElement.setLayoutBoundsPosition(0, pElementSize.y * i);
            }
        }
    }

Is it generally considered to be better and more conventional to remove the redundant var and :<type> syntax for i and layoutElement in the second for loop, or is it more proper to just leave them in there (due to the way their usage is isolated to those two for loops)? Thanks!

EDIT

This was asked a while back, but when I asked this, part of what I was working on was making something run more efficiently; so I put if outside of any for statements and avoided method calls inside the for statements.

1

Instead of using the same variable, make them unique by putting more of their purpose into their names:

var horizontalLayoutElement:ILayoutElement = target.getElementAt(i);
...
var verticalLayoutElement:ILayoutElement = target.getElementAt(i);

or if you don’t like such long variable names:

var hLayoutElement:ILayoutElement = target.getElementAt(i);
...
var vLayoutElement:ILayoutElement = target.getElementAt(i);

You can also emulate hoisting by putting all your variables declarations at the top of your functions, but since this makes code much harder to read due to the distance in code between declaration and usage, I would avoid it.

You have a lot of duplicate code here, which defeats the DRY principle. This would be better:

private function arrangeElementsLine(pElementSize:Dimensions):void {
    for (var i:uint = 0; i < target.numElements; i++) {
        var layoutElement:ILayoutElement = target.getElementAt(i);
        layoutElement.setLayoutBoundsSize(pElementSize.x, pElementSize.y);

        if (m_eOrientation == HORIZONTAL) {
            layoutElement.setLayoutBoundsPosition(pElementSize.x * i, 0);
        } else {
            layoutElement.setLayoutBoundsPosition(0, pElementSize.y * i);
        }
    }
}

This also solves your issues with variable duplication.

1

Personally, I prefer reuse the sames variables when they are exactly the same. As the content is the same and the different loop don’t stuck each other by overriding the variables, reusing the variables will resolve your problem of warning without making the code less readable, and, in case of pure optimization, it will take less memory (even if 4 bytes is nothing, it’s 4 bytes lighter anyway).
So, personally, I prefer using a function like that:

private function arrangeElementsLine(pElementSize:Dimensions):void
{
    var i:uint;
    var layoutElement:ILayoutElement;

    if (m_eOrientation == HORIZONTAL)
    {
        for (i = 0; i < target.numElements; i++)
        {
            layoutElement = target.getElementAt(i);
            layoutElement.setLayoutBoundsSize(pElementSize.x, pElementSize.y);
            layoutElement.setLayoutBoundsPosition(pElementSize.x * i, 0);
        }
    }
    else
    {
        for (i = 0; i < target.numElements; i++)
        {
            layoutElement = target.getElementAt(i);
            layoutElement.setLayoutBoundsSize(pElementSize.x, pElementSize.y);
            layoutElement.setLayoutBoundsPosition(0, pElementSize.y * i);
        }
    }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật