avatarNana Bonsu

Summary

The article provides guidance on speeding up iOS app development by utilizing four key Xcode refactoring tools.

Abstract

The article "Refactor Your iOS Code Fast With These Xcode Tips" offers iOS developers insights into enhancing their coding efficiency and meeting deadlines through the use of Xcode's refactoring tools. It outlines four specific tools: Extract to Function, Extract to Variable, Multi-line Editing, and Expand Switch Cases. These tools are designed to streamline the process of code organization, variable extraction, simultaneous multi-line edits, and the expansion of switch statement cases, thereby reducing manual coding efforts and the potential for errors.

Opinions

  • The author emphasizes the importance of refactoring code for maintainability and readability, suggesting that it's a critical practice for efficient iOS app development.
  • The article conveys that using hardcoded values instead of variables can complicate code, advocating for the 'Extract to Variable' feature as a solution.
  • The author expresses that forgetting to rename variables can be a daunting task, but with Xcode's Multi-line Editing feature, it becomes a manageable and quick process.
  • The author's tone suggests that the 'Expand Switch Cases' feature is particularly useful for creating comprehensive switch statements with minimal effort, akin to having a detective like Sherlock Holmes deduce all possible cases.

Refactor Your IOS Code Fast With These Xcode Tips

Refactoring Improvements by DALL-E

Want to learn ways to speed up your iOS app development time? Need to edit and improve code quickly for a deadline? Then you’ve opened the right article.

Here are 4. Xcode refactoring tools to know to help you do just that.

1. Extract to Function

Have you ever started writing one function but then realized it grew to a thousand lines long? Then you need likely need to extract much of that code to its function. The extract-to-function feature makes it so easy to do this.

Use this feature by first highlighting the block of code you want to extract. Then right click and choose “Refactor” > “Extract to Function”

As the following gif demonstrates

2. Extract to Variable

An important principle many of us were likely taught to follow is that of using variables throughout the code instead of hardcoded values. And thats certianly right since it makes life much easier.

But what should you do if you realize half of your code length is because of. hardcoded names?

That's when extract to variable comes to the rescue! This refactoring feature extracts any portion of code to its own variable and replaces all instances of that code with the variable name you choose

Access the extract to variable feature by highlighting the block of code you want to extract. The right-click> “Refactor” > “Extract to Variable”. If you have mutliple occurences of this string, you can select “Extract all Occurrences”.

An example of this is shown below

Extract All Occurrences

3. Multi-line editing

Your in your zone happily coding and then a horrifying thought hits you. You forgot to rename 10 variables. You shudder at the thought of having to do all that work.

That.. doesn't have to be you.

With multi-line editing in Xcode, you can edit multiple lines at once. This allows you to edit variable names at once. However, they have to be on consecutive lines.

Start the multi line editing feature by holding the option key and then dragging the lines you want to edit. Youll see a blinking cursor and then you can start editing.

If what I just wrote looks like hieroglyphics. then look at the following gif.

Multi-Line Editing

4. Expand Switch Cases

Have you ever started making a switch statement but realized that you'll have to make more cases than Sherlock Holmes? Thankfully, refactoring tools come to the rescue again.

“Expand Switch Cases” is a refactoring feature that allows you to quickly expand all cases in a switch statement without all the typing.

To use this feature, simply place your cursor within the switch statement, right-click, and choose “Refactor” > “Expand Switch Cases” from the context menu. Xcode will then expand all cases within the switch statement, showing the associated code blocks.

The following GIF demonstrates this.

Expand switch cases

Xcode refactoring tools are truly invalauble when programming. They help us remember that computers sometimes, just sometimes, understand we are humans too.

iOS
Xcode
Programming
Coding
Medium
Recommended from ReadMedium