Slide 1

Slide 1 text

The Myth Of "Clean Code"

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

3 Variable naming

Slide 4

Slide 4 text

emp_num 4 employee_number

Slide 5

Slide 5 text

5 “fault fixing in source code with abbreviated identifiers needs a more methodical approach and abbreviated identifiers are not perceived as major obstacle to fix faults in source code.”

Slide 6

Slide 6 text

6 “Functions & Classes Should Be Small”

Slide 7

Slide 7 text

7 “Every function in this program was just two, or three, or four lines long. Each was transparently obvious… That’s how short your functions should be!” - Kent Beck

Slide 8

Slide 8 text

8 fun fetchProfile() { val response = fetch("my_api_url") if (response.code == 200) { return processSuccess(response) } else { processError(response) } }

Slide 9

Slide 9 text

9 fun fetchProfile() { val response = fetch("my_api_url") if (response.code == 200) { val responseBody = JSONObject(response.body()) return Profile( name = responseBody["name"], age = responseBody["age"] ) } else { log("fetch fails with responseCode: ${response.code}) Snackbar.show("Fail to get profile data") } }

Slide 10

Slide 10 text

10 public static String renderPageWithSetupsAndTeardowns( PageData pageData,boolean isSuite ) throws Exception { this.isSuite = isSuite if (isTestPage(pageData)) includeSetupAndTeardownPages(); return pageData.getHtml(); } Actual example from “Clean Code” Book

Slide 11

Slide 11 text

11

Slide 12

Slide 12 text

12

Slide 13

Slide 13 text

Classitis 13 FileInputStream fileIputStream = new FileInputStream(fileName); try { Reader reader = new BufferedReader(new InputStreamReader(stream, cs)); StringBuilder builder = new StringBuilder(); char[] buffer = new char[8192]; int read; while ((read = reader.read(buffer, 0, buffer.length)) > 0) { builder.append(buffer, 0, read); } return builder.toString(); } finally { stream.close(); }

Slide 14

Slide 14 text

14 val fileText = File("fileName").buffered().readText()

Slide 15

Slide 15 text

15 “Interfaces should be designed to be make the common case as simple as possible”

Slide 16

Slide 16 text

16

Slide 17

Slide 17 text

Shallow Functions 17 fun addNullValue(key: String) { map[key] = null }

Slide 18

Slide 18 text

Shallow Functions 18 public static String renderPageWithSetupsAndTeardowns( PageData pageData,boolean isSuite ) throws Exception { includeSetupAndTeardownPages if (isTestPage(pageData)) includeSetupAndTeardownPages(pageData, isSuite); return pageData.getHtml(); } private static isTestPage(pageData: PageData) { return pageData.hasAttribute("Test") } Actual example from “Clean Code” Book

Slide 19

Slide 19 text

19 fun fetchProfile() { val response = fetch("my_api_url") if (response.code == 200) { val responseBody = JSONObject(response.body()) return Profile( name = responseBody["name"], age = responseBody["age"] ) } else { Log("fetch fails with responseCode: ${response.code}) Snackbar.show("Fail to get profile data") } } Deep Functions

Slide 20

Slide 20 text

20 “It’s more important to have a simple interface than a simple implementation”

Slide 21

Slide 21 text

21 Don’t Repeat Yourself (DRY)

Slide 22

Slide 22 text

22 “Duplication may be the root of all evil in software”

Slide 23

Slide 23 text

23 Premature Abstraction is the root of all evil

Slide 24

Slide 24 text

24 fun log( level: Level, message: String ) { if (level.isCritical()) { Logger.error(message) } else { Logger.warn(message) } }

Slide 25

Slide 25 text

25 fun log( level: Level, message: String, throwable: Throwable? ) { if (level.isCritical()) { Logger.error(throwable, message) } else { Logger.warn(message) } } log(Level.Info, "message", Exception("This is not used"))

Slide 26

Slide 26 text

26 interface IAnaylticsPlugin { trackAction(values: Map) trackEvent(values: Map) }

Slide 27

Slide 27 text

27 class AdobeAnalyticsPlugin implements IAnaylticsPlugin { trackAction(values: Map) = Adobe.track(...) trackEvent(values: Map) = Adobe.track(...) } class FirebaseAnalyticsPlugin implements IAnaylticsPlugin { trackAction(values: Map) = { /*DO NOTHING*/ } trackEvent(values: Map) = Firebase.log(...) }

Slide 28

Slide 28 text

28 val analyticsPlugins : Array = [AdobeAnalyticsPlugin(), FirebaseAnalyticsPlugin()] fun trackEvent(...) { analyticsPlugins.forEach((plugin) => { plugin.trackEvent(values) }) }

Slide 29

Slide 29 text

29 class AdobeAnalytics { trackAction(values: Map) = Adobe.track(...) trackEvent(values: Map) = Adobe.track(...) } class FirebaseAnalytics { logEvent(values: Map) = Firebase.log(values) }

Slide 30

Slide 30 text

30 private val adobeAnalytics = new AdobeAnalytics() private val firebaseAnalytics = new FirebaseAnalytics() fun trackAction(...) { adobeAnalytics.trackAction(values) } fun trackEvent(...) { adobeAnalytics.trackAction(values) firebaseAnalytics.logEvent(values) }

Slide 31

Slide 31 text

31 “Duplication is far cheaper than wrong abstraction”

Slide 32

Slide 32 text

Rule of Three 32 “Three strikes and you refactor”

Slide 33

Slide 33 text

33 Pass-through functions/variables

Slide 34

Slide 34 text

34 private WikiPage findInheritedPage(String pageName) throws Exception { return PageCrawlerImpl.getInheritedPage(pageName, testPage); } Actual example from “Clean Code” Book! again! Pass-through functions/variables

Slide 35

Slide 35 text

35 // On UI SearchScreenViewModel.searchUser("username") // In ViewModel SearchUseCase.execute("username") // In SearchUseCase SearchRepository.searchUser("username") // In SearchRepository SearchNetworkDataSource.searchUser("username") // In SearchNetworkDataSource executeApiCall("https://api/search?query=$username") Pass-through functions/variables

Slide 36

Slide 36 text

36 // On UI SearchScreenViewModel.searchUser("username") // In ViewModel executeApiCall("https://api/search?query=$username") Pass-through functions/variables

Slide 37

Slide 37 text

37 class ApplicationInstallCheckViewModel( val appInstance: AppInstance ) { fun isAppInstalled(appId: String) { return appInstance.searchAppWithId(appId) != null } } Pass-through functions/variables

Slide 38

Slide 38 text

38 class AppInstance { private var instance : AppInstance? static fun getInstance() : AppInstance { if (instance == null) { instance = ... } return instance!! } fun isAppInstalled(appId: String) = searchAppWithId(appId) != null } Singletons

Slide 39

Slide 39 text

39 val textStyles = getTextStyles() FlightCard(textStyles) { TitleText(textStyles) { Text(textStyles.title) } BuyButton(textStyles) { Text(textStyles.button) } } Context Objects

Slide 40

Slide 40 text

40 { val textStyles = StyleContext.current() Text(textStyles.title) } Context Objects

Slide 41

Slide 41 text

41 Function Arguments

Slide 42

Slide 42 text

42 “The ideal number of arguments for a function is zero (niladic). Next comes one (monadic), followed closely by two (dyadic). Three arguments (triadic) should be avoided where possible. More than three (polyadic) requires very special justification—and then shouldn’t be used anyway.”

Slide 43

Slide 43 text

43 @Composable fun Text( text: String, modifier: Modifier = Modifier, color: Color = Color.Unspecified, fontSize: TextUnit = TextUnit.Unspecified, fontStyle: FontStyle? = null, fontWeight: FontWeight? = null, fontFamily: FontFamily? = null, letterSpacing: TextUnit = TextUnit.Unspecified, textDecoration: TextDecoration? = null, textAlign: TextAlign? = null, lineHeight: TextUnit = TextUnit.Unspecified, overflow: TextOverflow = TextOverflow.Clip, softWrap: Boolean = true, maxLines: Int = Int.MAX_VALUE, minLines: Int = 1, onTextLayout: ((TextLayoutResult) -> Unit)? = null, style: TextStyle = LocalTextStyle.current ) { ... }

Slide 44

Slide 44 text

44 @Composable fun Text( text: String, modifier: Modifier = Modifier, color: Color = Color.Unspecified, fontSize: TextUnit = TextUnit.Unspecified, fontStyle: FontStyle? = null, fontWeight: FontWeight? = null, fontFamily: FontFamily? = null, letterSpacing: TextUnit = TextUnit.Unspecified, textDecoration: TextDecoration? = null, textAlign: TextAlign? = null, lineHeight: TextUnit = TextUnit.Unspecified, overflow: TextOverflow = TextOverflow.Clip, softWrap: Boolean = true, maxLines: Int = Int.MAX_VALUE, minLines: Int = 1, onTextLayout: ((TextLayoutResult) -> Unit)? = null, style: TextStyle = LocalTextStyle.current ) { ... }

Slide 45

Slide 45 text

45 “Interfaces should be designed to be make the common case as simple as possible”

Slide 46

Slide 46 text

Slope-interface API design 46 “Providing multiple APIs at different levels of abstraction allows solving 80% of use cases with a simple API, then 80% of the remaining 20% with a more detailed API, and then the final slice with a low-level API. Each layer is built on top of the next one with a measured reduction in API complexity.” https://jakewharton.com/slope-intercept-library-design/

Slide 47

Slide 47 text

47 Team structures

Slide 48

Slide 48 text

Conway’s Law 48 Your code structure will mirror the organization structure (Paraphrased)

Slide 49

Slide 49 text

49

Slide 50

Slide 50 text

50

Slide 51

Slide 51 text

51 Temporal Decomposition

Slide 52

Slide 52 text

52 protected static int[] generate(int n) { primes = new int[n]; multiplesOfPrimeFactors = new ArrayList(); set2AsFirstPrime(); checkOddNumbersForSubsequentPrimes(); return primes; }

Slide 53

Slide 53 text

53 fun fetchProfile() { val response = fetch("my_api_url") if (response.code == 200) { return processSuccess(response) } else { processError(response) } }

Slide 54

Slide 54 text

54 fun fetchProfile() { val response = fetch("my_api_url") if (response.code == 200) { val responseBody = JSONObject(response.body()) return Profile( name = responseBody["name"], age = responseBody["age"] ) } else { log("fetch fails with responseCode: ${response.code}) Snackbar.show("Fail to get profile data") } }

Slide 55

Slide 55 text

55 Comments

Slide 56

Slide 56 text

56 “Comments are signs that your code is bad” “Code is a documentation”

Slide 57

Slide 57 text

Comments 57 ● Explain the intent ● Describe edge cases ● Historical context

Slide 58

Slide 58 text

58 “The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge”