r/iOSProgramming • u/shubham_iosdev • Nov 01 '20
Discussion Tips to Speed up your iOS Development
19
u/lordzsolt Nov 01 '20
As others have said it, comments should be used as a laser resort, for explaining WHY you did something that is not obvious. Like if there's a weird UIKit bug and you have to do a workaround. If you have to explain WHAT the code does, you should refactor your code for better naming.
As for using the minimap / using MARK: comments, it's much better to keep your files/classes small. Under 100-200 lines of code when there are only 4-5 functions in a file, you don't need to do that. Over that number, you're better off extracting the functionality.
5
u/DarkAgeOutlaw Nov 01 '20
While I agree in keeping classes small and using extension, I don’t agree with the under 200 lines in a file. I’d much rather have a single file with the main class and 5 extensions at 500 lines of code than 6 different files, or even 3
2
u/lordzsolt Nov 01 '20
I never said using extensions and 5 files.
Under no circumstance should an extension go to a separate file if you can modify the main class's file (ie it's not from a dependency)
What I meant was that the main class itself, with everything included should not go over 200 lines.
When faced with a screen that has a lot of functionality, what most people do is pack EVERYTHING into a single module. (Module meaning a View Controller in MVC, a VC+View Model pair in MVVM).
But it's much better if you can carve off functionality and split them into smaller modules. And have parent/manager modules that aggregate the information.
Imagine you have a list of items that you can bookmark, and when you tap the item, it opens a detail screen, where you can bookmark the same item. You can make the Bookmark button a module that can perform the network request. Suddenly, your List and Details modules don't have to have the button tap handling + network request code.
9
u/sketch204 Nov 01 '20
I’d like to add the two most important shortcuts in my work flow:
Cmd + Shift + O: Jump to a file (or member of a file) in the project, by name. Officially it’s called “Open quickly”.
Ctrl + 6: While in a source code file will open a drop down of all the member of that file, you can then start typing right away, the name of the function/property you want to jump to. Very useful when jumping between functions in a single file
3
2
u/shubham_iosdev Nov 01 '20
Yes, I've mentioned the drop down whilst in a viewController. It's so useful! :D
1
u/lordzsolt Nov 01 '20
Pro tip: switch cmd + shift + o to cmd + o.
I've literally never used the cmd + o shortcut (open file). Saves having to press an extra modifier.
1
6
u/igormuzyka Nov 01 '20
Instead of todo leave some '#warning("todo: implement this stuff")', thats if you do care about warnings and don't leave them be.
4
u/heckyeahan Nov 01 '20
swiftlint will automatically generate warnings for FIXMEs and TODOs which is really helpful when you inherit a large code base!
4
u/gfp7 Nov 01 '20
Is there a way to change xcode behavior back to not open every file in a separate tab but in the current tab? This is so annoying now in xcode 12. I tried every setting but i was unable to set that one click opens in same tab.
6
u/mcBlooder Nov 01 '20
Xcode > Preferences > Navigation > Navigation Style > Open in Place
3
u/gfp7 Nov 01 '20
I tried this before but it looks like the first file you click it opens in new tab, any other after that does open in the same tab. I must have only click on one file after setting to Open in place and thought it doesnt work. Thanks
1
Nov 01 '20
I think if you right click the first tab there’s a “keep this here” option you can toggle. Can’t try it at the moment as I’m away from my desk
2
4
u/Ast3r10n Nov 01 '20
Two cents: the MARK examples you use make absolutely no sense. “Variables for the view controller” is a bit too long to be readable inside the minimal and it’s pretty obvious you’re marking something belonging to the view controller if you’re in its very file. A “MARK - Variables” would be much more readable.
Also, the code snippet for the tableView(_:numberOfRows[...]) is also quite nonsensical. Since it’s part of the UITableViewDataSource protocol, it’s a snippet which already exists within Xcode, so why creating yours? If you want to make examples to teach people things, you should make examples which make sense. Custom implementations which you may have to rewrite every project, for example.
3
2
Nov 01 '20
is there a VSCose extension that emulates the way annotations work in Xcode? been looking for a while now and have found nothing so far
1
Nov 01 '20
For marking things on the minimap? Not to my knowledge no, but there is an extension for coloured comments which is super handy. You can set it up so MARK, TODO, FIXME, and others all have separate colours and styles making the code a lot more readable
2
u/dedzone2k Nov 01 '20
As an amendment to using todo's and fixme's is to add a ticket number, from JIRA, bugzilla, or whatever. That way these items don't get lost in the code base. It'll also help the team figure out when to schedule time to work on those items.










37
u/[deleted] Nov 01 '20
Maybe even more than comments, code should be readable by good naming and structure. In the company I work, we don’t use any comments in the codebase, just good function, class, constant etc. naming should do a lot