[Gambas-user] Better use of prefixes
Cedron Dawg
cedron at exede.net
Sat Mar 16 18:55:33 CET 2019
More readable for whom?
Even between those two references there is disagreement on what the prefixes should be. That's what I meant by non-standardized.
I am crazy, I think everybody should do it my way because it is demonstrably superior.
Here is an example:
Here is a code snippet from Settings.class, the original and one dressed up by me. If you were a maintenance programmer or somebody trying to figure out what the code is doing, which is more readable and comprehensible?
1) Renamed Variables
2) Added explicit return values
3) Moved dims closer to their usage
(There might be a bug in this code, please check the ????? in my version. Not sure because I don't haven't looked at what the calling routine is doing.)
I am assuming that the Return (no arguments) return a False
The Original:
Private Sub SameSetting(vOld As Variant, vNew As Variant) As Boolean
Dim I As Integer
Dim vOldVal As Variant
Dim vNewVal As Variant
If IsNull(vOld) And If IsNull(vNew) Then Return True
If TypeOf(vOld) <> gb.Object And If TypeOf(vNew) <> gb.Object Then Return vOld = vNew
If Not (vOld Is Array) Then Return
If Not (vNew Is Array) Then Return
If vOld.Count <> vNew.Count Then Return
If vOld = vNew Then Return
For I = 0 To vOld.Max
vOldVal = vOld[I]
vNewVal = vNew[I]
If TypeOf(vOldVal) = gb.Object Or If TypeOf(vNewVal) = gb.Object Then Return
If vOldVal <> vNewVal Then Return
Next
Return True
End
Here is how I would have code it: (Notice how the spaces inside the parentheses also improve readability.)
'=============================================================================
Private Sub SameSetting( argOldValues As Variant, argNewValues As Variant ) As Boolean
'---- Bail as Matching if Both Null
If IsNull( argOldValues ) And If IsNull( argNewValues ) Then Return True
'---- Bail On Simple Variables Comparison
If TypeOf( argOldValues ) <> gb.Object And If TypeOf( argNewValues ) <> gb.Object Then Return ( argOldValues = argNewValues )
'---- Bail as not Matching on Non-Array Objects
If Not ( argOldValues Is Array ) Then Return False
If Not ( argNewValues Is Array ) Then Return False
'---- Bail as not Matching on Count Comparison
If argOldValues.Count <> argNewValues.Count Then Return False
If argOldValues = argNewValues Then Return True '?????????? Same Array should be True, No?
'---- Bail on any Mismatched Array Elements
Dim i As Integer
Dim theOldValue As Variant
Dim theNewValue As Variant
For i = 0 To argOldValues.Max
theOldValue = argOldValues[i]
theNewValue = argNewValues[i]
If TypeOf(theOldValue) = gb.Object Or If TypeOf(theNewValue) = gb.Object Then Return False
If theOldValue <> theNewValue Then Return False
Next
'---- Exit as Matching
Return True
End
'=============================================================================
----- Original Message -----
From: "Gianluigi" <bagonergi at gmail.com>
To: "user" <user at lists.gambas-basic.org>
Sent: Saturday, March 16, 2019 1:25:08 PM
Subject: Re: [Gambas-user] Better use of prefixes
[ http://gambaswiki.org/wiki/doc/namingconvention?ht=prefix | http://gambaswiki.org/wiki/doc/namingconvention?ht=prefix ]
[ http://gambaswiki.org/wiki/doc/naming | http://gambaswiki.org/wiki/doc/naming ]
I try to follow these conventions and I'm happy if others do, so the code is more readable.
Regards
Gianluigi
More information about the User
mailing list