Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redundant / Unnecessary Parentheses #39

Closed
hoke-t opened this issue May 31, 2015 · 2 comments
Closed

Redundant / Unnecessary Parentheses #39

hoke-t opened this issue May 31, 2015 · 2 comments

Comments

@hoke-t
Copy link
Contributor

hoke-t commented May 31, 2015

In Swift, parentheses in loops / control structures are optional. They often times make readability easier, but in some cases can also overly complicate the code. It is up to you whether you want to use them or not, but there are a couple of places in this project in which I feel they could be removed. Here is an example:

In Source/Factories/LinkedList.swift:

//the number of items
    var count: Int {

            if (head.key == nil) {
                return 0
            }

            else  {

                var current: LLNode = head
                var x: Int = 1


                //cycle through the list of items
                while ((current.next) != nil) {
                    current = current.next!
                    x++
                }

                return x

            }
    }

I think that we could change the code, for example in the if statement, to be:

var count: Int {

            if head.key == nil {
                return 0
            }

and in the while loop:

//cycle through the list of items
                while current.next != nil {
                    current = current.next!
                    x++
                }

Again, up to you, the author, but I think it could help Swift learners looking at this code to recognize some of the readability features of Swift versus Objective-C.

@waynewbishop
Copy link
Owner

Hi Tanner;

Thanks for bringing up this great point. I was planning on mentioning the optional use of parentheses with my next essay on closures, but it would be worth noting this with the existing code base. If there are other areas you feel should / could be updated, feel free to send along the filename and line numbers and I can take a look.

@waynewbishop
Copy link
Owner

I've made a number of revisions throughout code to remove extra parentheses. As discussed, this has made the code more readable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants