Tuesday, July 6, 2021
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
- 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.