Bug 1169290 - Improve getPrefVal readability. r?maja_zf draft
authorAndreas Tolfsen <ato@sny.no>
Sat, 27 Jan 2018 17:57:33 +0000
changeset 753889 06113a1109dc0c223a4e8ecb1c82c0a3413d697a
parent 753815 3ee38289dac8838fe848f7234d75f3cef5d3dbc7
child 753890 7e664c78991e82af54cee3470cc7f970a19dc5f1
push id98715
push userbmo:ato@sny.no
push dateMon, 12 Feb 2018 16:37:16 +0000
reviewersmaja_zf
bugs1169290
milestone60.0a1
Bug 1169290 - Improve getPrefVal readability. r?maja_zf Instead of assigning each preference value to prefValue and breaking, they can be returned immediately. This improves readability. We also have no need to expose PREF_STRING, PREF_BOOL et al. in the global scope, so these are now scoped to the getPrefVal function. MozReview-Commit-ID: 7LZBgkZ8r08
testing/marionette/components/marionette.js
--- a/testing/marionette/components/marionette.js
+++ b/testing/marionette/components/marionette.js
@@ -63,44 +63,37 @@ const ENV_ENABLED = "MOZ_MARIONETTE";
 // a different profile in order to test things like Firefox refresh.
 // The environment variable itself, if present, is interpreted as a
 // JSON structure, with the keys mapping to preference names in the
 // "marionette." branch, and the values to the values of those prefs. So
 // something like {"port": 4444} would result in the marionette.port
 // pref being set to 4444.
 const ENV_PRESERVE_PREFS = "MOZ_MARIONETTE_PREF_STATE_ACROSS_RESTARTS";
 
-const {PREF_STRING, PREF_BOOL, PREF_INT, PREF_INVALID} = Ci.nsIPrefBranch;
 
 function getPrefVal(pref) {
-  let prefType = Services.prefs.getPrefType(pref);
-  let prefValue;
-  switch (prefType) {
+  const {PREF_STRING, PREF_BOOL, PREF_INT, PREF_INVALID} = Ci.nsIPrefBranch;
+
+  let type = Services.prefs.getPrefType(pref);
+  switch (type) {
     case PREF_STRING:
-      prefValue = Services.prefs.getStringPref(pref);
-      break;
+      return Services.prefs.getStringPref(pref);
 
     case PREF_BOOL:
-      prefValue = Services.prefs.getBoolPref(pref);
-      break;
+      return Services.prefs.getBoolPref(pref);
 
     case PREF_INT:
-      prefValue = Services.prefs.getIntPref(pref);
-      break;
+      return Services.prefs.getIntPref(pref);
 
     case PREF_INVALID:
-      prefValue = undefined;
-      break;
+      return undefined;
 
     default:
-      throw new TypeError(`Unexpected preference type (${prefType}) for ` +
-                          `${pref}`);
+      throw new TypeError(`Unexpected preference type (${type}) for ${pref}`);
   }
-
-  return prefValue;
 }
 
 // Get preference value of |preferred|, falling back to |fallback|
 // if |preferred| is not user-modified and |fallback| exists.
 function getPref(preferred, fallback) {
   if (!Services.prefs.prefHasUserValue(preferred) &&
       Services.prefs.getPrefType(fallback) != Ci.nsIPrefBranch.PREF_INVALID) {
     return getPrefVal(fallback, getPrefVal(preferred));