-
-
Notifications
You must be signed in to change notification settings - Fork 6k
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
unwrap optionals #2698
unwrap optionals #2698
Conversation
6573132
to
7d8ec67
Compare
Codecov Report
@@ Coverage Diff @@
## master #2698 +/- ##
==========================================
+ Coverage 19.64% 19.69% +0.05%
==========================================
Files 113 113
Lines 13792 13720 -72
==========================================
- Hits 2709 2702 -7
+ Misses 11083 11018 -65
Continue to review full report at Codecov.
|
Mind to give some details and what this for? |
I was just looking through the repo and noticed some areas that could be refactored to remove |
Thanks for the submission. Moving to a more swift style is great. I've wanted to do that myself but hadn't had the time. I'd like to merge this change after swift 4 support is merge. Should be soon. |
{ | ||
foundScrollView = nil | ||
} | ||
|
||
var scrollViewPanGestureRecognizer: NSUIGestureRecognizer! | ||
|
||
if foundScrollView !== nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replace the loop with
let scrollViewPanGestureRecognizer = foundScrollView?.nsuiGestureRecognizers?.first {
$0 is NSUIPanGestureRecognizer
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not confident in using .first
here to achieve the same effect
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not? The code you've replaced the for loop with is not the same. In the for loop, we escape after we find the first NSUIPanGestureRecognizer
, in your code it will continue to loop through and find more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than leave it the old way (using our own implementation of first
) why not simply use the built-in method and replace 5 lines with 2 more explicit lines.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
@@ -562,12 +561,12 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate | |||
{ | |||
if h == nil | |||
{ | |||
delegate!.chartValueNothingSelected?(self) | |||
delegate?.chartValueNothingSelected?(self) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why leave delegate != nil
in the if statement if you're going to use optional chaining?
Maybe
if callDelegate, let delegate = delegate { ... }
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
@@ -562,12 +561,12 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate | |||
{ | |||
if h == nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To follow the spirit of your PR, you should flip the order of the if-else, i.e.
if let h = h { ... }
else { ... }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
@russellbstephens I'd encourage you to look at PR #2737. It takes this a lot further and reduces many algorithms to one-liners. Perhaps you could bring some of those changes into here, as currently I can't get that PR to build. |
@jjatie sure no problem, I put this up to see if you all would be interested in cleaning up the code a bit. Now that I know there is interest, I would be happy to go further with this |
@petester42 is this ready to go in? |
sorry to jump in. Is this on swift 4 or need another convert on your side? |
@@ -888,7 +887,7 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate | |||
|
|||
do | |||
{ | |||
try imageData.write(to: URL(fileURLWithPath: path), options: .atomic) | |||
try imageData?.write(to: URL(fileURLWithPath: path), options: .atomic) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional chaining can cover up the exception if the image type is not supported?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated
@@ -120,7 +120,7 @@ open class CombinedChartView: BarLineChartViewBase, CombinedChartDataProvider | |||
{ | |||
return nil | |||
} | |||
return (_data as! CombinedChartData!).lineData |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It has been sometime we review this part. But seems CombinedChartData!
can be CombinedChartData
now,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated
@@ -120,7 +120,7 @@ open class CombinedChartView: BarLineChartViewBase, CombinedChartDataProvider | |||
{ | |||
return nil | |||
} | |||
return (_data as! CombinedChartData!).lineData | |||
return (_data as? CombinedChartData)?.lineData |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shall we use (_data as! CombinedChartData)
or (_data as? CombinedChartData)?
? seems as!
is safe here though as? is safer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to avoid using !
, since in a case where _data === nil
!
will crash
@@ -55,7 +55,7 @@ open class CombinedChartView: BarLineChartViewBase, CombinedChartDataProvider | |||
|
|||
self.highlighter = CombinedHighlighter(chart: self, barDataProvider: self) | |||
|
|||
(renderer as! CombinedChartRenderer?)!.createRenderers() | |||
(renderer as? CombinedChartRenderer)?.createRenderers() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
old code is a bit weird (maybe the compiler bug). But if it's safe unwrap here, why not use (renderer as! CombinedChartRenderer).createRenderers()
? renderer
has been setup as combined chart renderer in initialize()
above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather not rely on the assumption renderer !== nil
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @russellbstephens because while the three of us may know it's safe to force unwrap here, it is in no way clear that this is the case. I think a future ticket should involve making renderer
a required property since you can't really do anything with a chart without a renderer.
@@ -104,16 +104,16 @@ open class PieChartView: PieRadarChartViewBase | |||
let optionalContext = NSUIGraphicsGetCurrentContext() | |||
guard let context = optionalContext else { return } | |||
|
|||
renderer!.drawData(context: context) | |||
renderer?.drawData(context: context) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why ?
here and below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
its best to avoid !
in swift
let touchLocation = touch?.location(in: self) | ||
|
||
processRotationGestureBegan(location: touchLocation!) | ||
if let touch = touches.first { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure why use if here. Any ideas?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated
@@ -102,17 +102,17 @@ open class BarChartDataEntry: ChartDataEntry | |||
|
|||
open func sumBelow(stackIndex :Int) -> Double | |||
{ | |||
if _yVals == nil | |||
guard let _yVals = _yVals else |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shall we keep using _
for local instant here? seems a little bit weird, though we don't have to change other code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated
e2fecde
to
4f9f5c2
Compare
is there any hope of this going in? |
@@ -115,9 +103,9 @@ open class Animator: NSObject | |||
elapsed = duration | |||
} | |||
|
|||
if _easingX != nil | |||
if let _easingX = _easingX |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why isn't this one line?
phaseX = _easingX?(elapsed, duration) ?? Double(elapsed / duration)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
{ | ||
phaseY = _easingY!(elapsed, duration) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be one line as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
@@ -233,7 +233,7 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD | |||
|
|||
context.restoreGState() | |||
|
|||
renderer!.drawExtras(context: context) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use
guard let data = data, let renderer = renderer else { return }
at the beginning of the function?
Logically, a draw()
function would require a renderer to draw anything, and looking through the implementation of the function this is true.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
@@ -533,7 +533,7 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD | |||
|
|||
let h = getHighlightByTouchPoint(recognizer.location(in: self)) | |||
|
|||
if h === nil || h!.isEqual(self.lastHighlighted) | |||
if h === nil || h?.isEqual(self.lastHighlighted) ?? false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can be just if h?.isEqual(self.lastHighlighted) ?? true {
because we want to enter the if statement if h == nil
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The former force unwrapping was also acceptable, because if the first condition passed on the or statement, we would have already entered the if statement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
@@ -753,7 +753,7 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD | |||
|
|||
if ((h === nil && lastHighlighted !== nil) || | |||
(h !== nil && lastHighlighted === nil) || | |||
(h !== nil && lastHighlighted !== nil && !h!.isEqual(lastHighlighted))) | |||
(h !== nil && lastHighlighted !== nil && !(h?.isEqual(lastHighlighted) ?? false))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we've already checked that h != nil
in the AND statement. If h == nil
escape early from the AND statement, so it is safe to force unwrap h
Alternatively, we can do the same thing as before and write
lastHighlighted !== nil && !(h?.isEqual(lastHighlighted) ?? true)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently there is an issue in the last statement. If the whole expression was evaluated, then in the last component (!(h?.isEqual(lastHighlighted) ?? false)
) you would return true when h == nil
which is not what we want.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
@@ -931,7 +931,7 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD | |||
)) | |||
{ | |||
var scrollView = self.superview | |||
while (scrollView !== nil && !scrollView!.isKind(of: NSUIScrollView.self)) | |||
while (scrollView !== nil && !(scrollView?.isKind(of: NSUIScrollView.self) ?? false)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same issue here where if the last component is evaluated and scrollView == nil
we return true
Easiest way to solve this is to replace
(scrollView !== nil && !(scrollView?.isKind(of: NSUIScrollView.self) ?? false))
with
!(scrollView?.isKind(of: NSUIScrollView.self) ?? true)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
// if no position specified, draw on default position | ||
if position == nil | ||
{ | ||
let position: CGPoint |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be cleaner to do this.
let p = description.position ?? CGPoint(x: bounds.width - _viewPortHandler.offsetRight - description.xOffset,
y: bounds.height - _viewPortHandler.offsetBottom - description.yOffset - description.font.lineHeight)
Might just come down to preference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
let position: CGPoint | ||
if let descriptionPosition = description.position { | ||
position = descriptionPosition | ||
} else { | ||
let frame = self.bounds |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's generally good practice to short a variable call (i.e. let width = self.frame.size.width
), but in this case it is unnecessary and wrong. Unnecessary because you don't need to explicitly state self
, and wrong because in Cocoa frame
and bounds
mean two different things. frame
should have never been here in the first place.
So we can just write
CGPoint(x: bounds.width - _viewPortHandler.offsetRight - description.xOffset,
y: bounds.height - _viewPortHandler.offsetBottom - description.yOffset - description.font.lineHeight)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
let touchLocation = touch?.location(in: self) | ||
|
||
processRotationGestureMoved(location: touchLocation!) | ||
if let touch = touches.first { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move the if let touch = touches.first
into if rotationEnabled && !rotationWithTwoFingers
i.e.
if rotationEnabled && !rotationWithTwoFingers, let touch = touches.first { ... }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, we don't need to check to see if touches.first
is non-nil. We know there is at least one touch because the delegate method is called. Even Apple force unwraps this their sample code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
let touchLocation = touch?.location(in: self) | ||
|
||
processRotationGestureEnded(location: touchLocation!) | ||
if let touch = touches.first { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as previous comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
@@ -118,19 +118,19 @@ open class RadarChartView: PieRadarChartViewBase | |||
|
|||
if drawWeb | |||
{ | |||
renderer!.drawExtras(context: context) | |||
renderer?.drawExtras(context: context) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
guard let data = _data, let renderer = renderer else { return }
at the beginning of the function,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
@@ -102,7 +102,7 @@ open class MarkerImage: NSObject, IMarker | |||
height: size.height) | |||
|
|||
NSUIGraphicsPushContext(context) | |||
image!.draw(in: rect) | |||
image?.draw(in: rect) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like there should be a guard let image = image else { return }
at the beginning of the method. Drawing will do nothing if image == nil
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
@@ -36,18 +36,18 @@ open class MarkerView: NSUIView, IMarker | |||
{ | |||
offset.x = -point.x | |||
} | |||
else if chart != nil && point.x + width + offset.x > chart!.bounds.size.width | |||
else if let chart = chart, point.x + width + offset.x > chart.bounds.size.width |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
guard let chart = chartView else { return }
at the beginning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this func must return a CGPoint
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
guard let chart = chartView else { return offset }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
@@ -102,17 +102,17 @@ open class BarChartDataEntry: ChartDataEntry | |||
|
|||
@objc open func sumBelow(stackIndex :Int) -> Double | |||
{ | |||
if _yVals == nil | |||
guard let yVals = _yVals else |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Throughout the project we're being inconsistent in how we write closures. Sometimes we open the closure on a new line, sometimes not, and sometimes we write one liners. Since you're going through a significant portion of the project already, can we consolidate on a format?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would love too, personally I think opening on the same line is fine
@@ -27,8 +27,7 @@ open class BubbleChartData: BarLineScatterCandleBubbleChartData | |||
/// Sets the width of the circle that surrounds the bubble when highlighted for all DataSet objects this data object contains | |||
@objc open func setHighlightCircleWidth(_ width: CGFloat) | |||
{ | |||
for set in (_dataSets as? [IBubbleChartDataSet])! | |||
{ | |||
(_dataSets as? [IBubbleChartDataSet])?.forEach { set in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be a one liner as
(_dataSets as? [IBubbleChartDataSet])?.forEach { $0.highlightCircleWidth = width }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
@jjatie how's it looking? |
Looks fine to me! |
nice! |
83ed464
to
83ec416
Compare
so merge? |
:) |
seems I don't have further questions. @danielgindi @petester42, any comments? |
The review that has been done by everyone seems adequate to me. |
ok, merging. |
an attempt to reduce the number of force unwraps