make nested conditionals one-dimensional! use early returns from a function rather than if/else chains. this is essential not only in the aspect of Go, but also in any language.
take a look at the following code snippet:
the above code adds cognitive load for someone who's reading ur code (the number of entities they need to think about is a lot, so they might be lost in ur code).
but take a look at the more idiomatic approach to the code:
it uses early returns (guard clauses) and provide a linear approach to logic trees
#tips #code
@Mi_Ra_Ch
take a look at the following code snippet:
func isValidPassword(password string) bool {
isValid := true
if len(password) < 8 {
isValid = false
} else {
if !containsUppercase(password) {
isValid = false
} else {
if !containsLowercase(password) {
isValid = false
} else {
if !containsNumber(password) {
isValid = false
}
}
}
}
return isValid
}
func containsUppercase(str string) bool {
// ... implementation ...
}
func containsLowercase(str string) bool {
// ... implementation ...
}
func containsNumber(str string) bool {
// ... implementation ...
}the above code adds cognitive load for someone who's reading ur code (the number of entities they need to think about is a lot, so they might be lost in ur code).
but take a look at the more idiomatic approach to the code:
func isValidPassword(password string) bool {
if len(password) < 8 {
return false
}
if !containsUppercase(password) {
return false
}
if !containsLowercase(password) {
return false
}
if !containsNumber(password) {
return false
}
return true
}
func containsUppercase(str string) bool {
// ... implementation ...
}
func containsLowercase(str string) bool {
// ... implementation ...
}
func containsNumber(str string) bool {
// ... implementation ...
}it uses early returns (guard clauses) and provide a linear approach to logic trees
#tips #code
@Mi_Ra_Ch
the JPEG-XL team at Google is teaming up with Firefox to make browsing experience safer and smoother.
why should you care? they're replacing the heavy decoder (100k+ lines of C++) with a lighter one built in Rust. this means faster load times and a more secure experience for all image-heavy sites (especially if you're like me who spends time on image-heavy sites). less bloat, more speedβit's a win a win! hope it gets shipped
#JPEGXL
@Mi_Ra_Ch
why should you care? they're replacing the heavy decoder (100k+ lines of C++) with a lighter one built in Rust. this means faster load times and a more secure experience for all image-heavy sites (especially if you're like me who spends time on image-heavy sites). less bloat, more speedβit's a win a win! hope it gets shipped
#JPEGXL
@Mi_Ra_Ch
ahh internet so bad rn cant even work. gotta be coz of national exam. good luck for everyone tho π
β€4
no offense to @DaveDumps but when it comes from the guy itself, you gotta accept the fate π hope OpenSUSE has improved a lot since then
hope you had a great new year β¨ for some of us it was just another Wednesday. but ngl the Doro Wet was π
this year i've decided to put pentest a little bit aside and focus more on computer networks and development side. will be sharing lots of cool stuff here. thanks for sticking around lol. may you be overflown by blessings this year π
this year i've decided to put pentest a little bit aside and focus more on computer networks and development side. will be sharing lots of cool stuff here. thanks for sticking around lol. may you be overflown by blessings this year π
β‘6π4β€2
APL (which stands for "A Programming Language" btw) was invented back in the 60s by this dude Kenneth E. Iverson. It's known for its super-concise syntax, which is basically like a bunch of symbols that make a program.
Here's how you'd do some basic stuff in APL:
Go took inspiration from APL's "Iota" operator (represented by
What's Up with "Iota"?
Iota basically means "a tiny bit" in Greek. In APL,
Go's slice notation (using
Why Should You Care?
APL might not be the "coolest" language on the block, but it's important for understanding how array manipulation works in a lot of modern languages. It's like the grandparent of all the cool kids. Next time you're working with arrays in Go, give a shout-out to APL.
Here are some other cool APL features that influenced other programming languages:
Reduction:
Rank:
Here's how you'd do some basic stuff in APL:
2 + 3 β Simple addition, you already knew this one
5 β Output
A β 1 2 3 4 β Assign vector A (like an array)
B β 5 6 7 8 β Another vector B
A + B β Add corresponding elements
6 8 10 12 β Output
A Γ B β Matrix multiplication (assuming A and B are matrices)
β ...Output would depend on the actual matrix values
A β.Γ B β Outer product
β Output: matrix with all possible combinations
Go took inspiration from APL's "Iota" operator (represented by
β³). Iota's all about generating sequences of numbers, which is super helpful when you're working with arrays.What's Up with "Iota"?
Iota basically means "a tiny bit" in Greek. In APL,
β³n gives you a vector of numbers from 1 to n. So, β³5 would be like 1 2 3 4 5. Go's slice notation (using
: to extract parts of an array) is like a tribute to APL's Iota. It's all about making array manipulation easier and more intuitive. package main
import "fmt"
func main() {
arr := []int{1, 2, 3, 4, 5}
fmt.Println(arr[1:3]) // Output: [2 3]
}
Why Should You Care?
APL might not be the "coolest" language on the block, but it's important for understanding how array manipulation works in a lot of modern languages. It's like the grandparent of all the cool kids. Next time you're working with arrays in Go, give a shout-out to APL.
Here are some other cool APL features that influenced other programming languages:
Reduction:
+/A sums all elements in the array A.Rank:
β΄A returns the shape of the array A.something i came to realize these days, everyone tells you "Life is short"; but only few of em reminds you that there's an eternal life waiting for ya. in fact, the phrase "Life is short" is behind all my worst decisions, but i think it's important to enjoy life while still you got the time. this doesn't mean you should live carelessly either. you gotta be able to think of the eternal life, and evaluate your actions; "Are my actions worthy of the eternal life?"
β€8
found this when peeps were arguing whether to implement python 2.0 in Java in 1997 python workshop lol
"Python in Java's Advantages"
"Using Java as the underlying systems language for Python has a number of advantages over the current implementation of Python in C. First and foremost of these in my mind is the opportunity to ride the Java popularity wave and let Python code run everywhere there's a Java VM. It also makes the rich set of portable Java API's available from within Python. There is also a nice collection of technical reasons why Java is a superior implementation language for Python than C. These include Java's binary portability, thread-safety, object-orientation, true exceptions, garbage collection, and friendliness to glue languages. More questions need to be answered before I can make a convincing argument that Python 2.0 should be implemented in Java rather than C. Nonetheless, I think that Java offers many advantages for Python as both an implementation language and a widely available run-time platform."
> Another guy replies
"What particular statement intrigues me here is the sentence:
"More questions need to be answered before I can make a convincing argument that Python 2.0 should be implemented in Java rather than C".
"I was wondering if this is seriously being considered -- that is implementing Python 2.0 in Java rather than C. While I understand that there are some technical challenges with this (notably interfacing to the existing C implemented extensions), I personally think there is a lot to be said for compiling Python to the JVM. For example: access to the Java apis, garbage collection, true compilation, the ability to write statically typed code (just write that part in Java!), access to Swing, promoting Python on the coat-tails of Java (free publicity and hype), etc."
"Python in Java's Advantages"
"Using Java as the underlying systems language for Python has a number of advantages over the current implementation of Python in C. First and foremost of these in my mind is the opportunity to ride the Java popularity wave and let Python code run everywhere there's a Java VM. It also makes the rich set of portable Java API's available from within Python. There is also a nice collection of technical reasons why Java is a superior implementation language for Python than C. These include Java's binary portability, thread-safety, object-orientation, true exceptions, garbage collection, and friendliness to glue languages. More questions need to be answered before I can make a convincing argument that Python 2.0 should be implemented in Java rather than C. Nonetheless, I think that Java offers many advantages for Python as both an implementation language and a widely available run-time platform."
> Another guy replies
"What particular statement intrigues me here is the sentence:
"More questions need to be answered before I can make a convincing argument that Python 2.0 should be implemented in Java rather than C".
"I was wondering if this is seriously being considered -- that is implementing Python 2.0 in Java rather than C. While I understand that there are some technical challenges with this (notably interfacing to the existing C implemented extensions), I personally think there is a lot to be said for compiling Python to the JVM. For example: access to the Java apis, garbage collection, true compilation, the ability to write statically typed code (just write that part in Java!), access to Swing, promoting Python on the coat-tails of Java (free publicity and hype), etc."
π4