Pages

Friday, July 16, 2021

Meaningful Naming in programming

Our goal as authors is to make our code as easy as possible to understand. We want our code to be a quick skim, not an intense study. The 'Clean Code' book by uncle Bob lays out the following steps to write meaningful names when coding:

  1. Use Intention-revealing names
  2. Avoid disinformation
  3. Make meaningful distinctions
  4. Use pronounceable names
  5. Use searchable names
  6. Avoid encodings
  7. Avoid mental mapping
  8. Class names
  9. Method names
  10. Don't be cute
  11. Pick one word per concept
  12. Don't pun
  13. Use solution domain names
  14. Use problem domain names
  15. Add meaningful context
  16. Don't add gratuitous context

1. Use Intention-Revealing Names

Choosing names that reveal intent can make it much easier to understand and change code.

The problem with the above code isn't its simplicity but its implicity.

2. Avoid Disinformation

Programmers must avoid leaving false clues that obscure the meaning of the code. For example, do not refer to a group accounts as accountList unless it's actually a list. Also, Beware using names which vary in small ways.

3. Make Meaningful Distinctions

Do not write code solely to satisfy the compiler or interpreter, this creates problems. It is not sufficient to add noise words although the compiler is satisfied. If names must be different, then they should also mean something different.

4. Use Pronounceable Names

If you can't pronounce names you can't discuss it without sounding silly.

5. Use Searchable Names

Avoid using single letters for names. For example, using the letter e which is the most used letter in the English language won't let the IDE offer any help to you when you are looking for it. Also, one might easily grep for a constant rather than searching for a number.

6. Avoid Encodings

Programmers have enough encodings to deal with without adding more to their burden.

7. Avoid Mental Mapping

Use problem domain terms or solution domain terms. Readers shouldn't have to translate your names into other names they already know.

8. Class Names

Class names and objects should be nouns.

9. Method Names

Method names should have verbs.

10. Don't be Cute

Say what you mean, mean what you say. Don't say culture-dependent jokes.

11. Pick One Word per Concept

Pick one word for an abstract concept and stick with it. For instance it's confusing to have fetch, retrieve, and get as equivalent methods of different classes.

12. Don't Pun

Avoid using the same word for two purposes.

13. Use Solution Domain Names

The people who are going to read your code are programmers. So go ahead and use computer science terms.

14. Use Problem Domain Names

When there is no a computer science term use the name from the problem domain. At least the programmer who maintains your code can ask a domain expert what it means.

15. Add Meaningful Context

Enclose your names in well named classes, functions, or namespaces. When all fails then prefixing the name may be necessary as a last resort.

16. Don't Add Gratuitous Context

Add no more context to name than necessary. Shorter names are better than longer ones so long as they are clear.

 

 

 

Tuesday, July 6, 2021

368. Largest Divisible Subset (LeetCode)

 

Monday, July 5, 2021

Strict Evaluation vs. Lazy Evaluation, briefly

In a language that uses strict evaluation, the arguments to a function are evaluated before the function is applied.

Lazy evaluation on the other hand, delays the evaluation of an expression until it is needed.

Call by value and call by reference are examples of strict evaluation. In call by value a copy of the variable is passed, while in call by reference the variable itself is passed.

Lazy evaluation strategies are: normal order, call by name, call by macro expansion, and call by need. 

In normal order an expression is evaluated by repeatedly evaluating the left-most, outer-most reducible expression first. 

In call by name the actual parameter is only evaluated if used inside the function. If a parameter is not used in the function body, it is never evaluated. If it is used several times, it’s re-evaluated each time it appears.

Call by macro expansion is similar to call-by-name but avoids substitution. Provides developers with a mechanism to add new syntax to the core language grammar known as macros. Macros are expanded into code by a macro preprocessor. 

In call by need a parameter is evaluated only if it’s used. Once the first evaluation happens, the result is cached, so further uses of the parameter don’t require re-evaluation.

References

What is SOLID?

 SOLID are5 design principles intended to make software designs more understandable, flexible and maintainable. SOLID principles can also form a core philosophies for methodologies like agile development or adaptive software development.


Single Responsibility  

A class should only have a single responsibility 

Open-Closed 

Software entities should be open for extension but closed for modification.

Liskov Substitution 

Instances of a program should be replaceable by their subtypes without altering the correctness of the program.

Interface Segregation 

Many client-specific interfaces are better than one general purpose interface.

Dependency Inversion 

One should depend upon abstractions, not concretions.

Saturday, July 3, 2021

Java String Comparison

In Java, the == operator compares references i.e. if two reference variables point to the same object in the heap, it returns true, otherwise it is false.

The correct way to compare strings in Java is to either use:

equals(), equalsIgnoreCase(), or compareTo(). 

 Behavior of == and equals() varies in case of String but remain the same in case of Object(), because String class overrides equals() method from Object class.


Closures in JavaScript

- Whenever a function is declared in JavaScript a closure is created.
- Returning a function from inside another function is the classsic example of closure: The state inside the outer function is imlpicitly available to the returned function even after the outer function has completed its execution.
- Check this link!
- In some programming languages, the local variables within a function exist for just the duration of that function's execution. This is not the case for JavaScript. Check this too!
- JavaScript closures can emulate private methods.

658. Find K Closest Elements (LeetCode)