Code Tips and Tricks

A collection of code snippets for software developers.

Useful Aliases to Run MongoDB on macOS MongoDB

Before MongoDB v4.2.3, dealing with it was pretty easy from the terminal. We could run the mongod command to start MongoDB. This no longer works from MongoDB v4.2.3 onwards. Here is some useful shorthands.


    alias mongod='brew services run mongodb-community'
    alias mongod-status='brew services list'
    alias mongod-stop='brew services stop mongodb-community'
 

Easily undo a local commit in Git Git

An easy to remember way for deleting or undoing the last commit from your local Git repository


    # Make global configuration to set a delet-last-commit alias 
    $   git  config  -- global  alias .delet-last-commit  "reset --soft HEAD ~1"
    # Usage :)
    $   git delet-last-commit

Pull another remote git branch without switching branches Git

If you want to pull a git branch without switching branches you can use the following


    git fetch origin master:master

Remove all leading and trailing white spaces of string in Swift Swift

handy Swift string extension that helps to remove all leading and trailing white spces in a string


import  Foundation
extension String {
     func  strip()  -> String{ 
         return   self.trimmingCharacters(in: CharacterSet.whitespaces)
        }
}
// White spaced string
var string = "    Hello, World   "
string.strip()
//Result -> "Hello, World"

A tip for debugging using compiler directives in Xcode Xcode

Here is a tip to help you debug your swift code in a better way in Xcode


import  UIKit
class HomeVC:  UIViewController {
    func  sayHi() { 
        //.....
        //sth cause failure let us print who is that guy
        print("failure \(#function) "in line \(#line) ", file \(#file)) 
        }
let  mainVC = HomeVC()
 mainVC.sayHi()
 
/*
    output: failure sayHi() in line 9, fileMyPlayground.playground
*/

Checking if the collection is not empty Swift

A more convenient way to check collection emptiness in swift


extension Collection  { 
    var isNotEmpty: Bool{ !isEmpty }
}
// conventional way
if  !array.isEmpty { // do sth}
// more readable way
if  array.isNotEmpty { // do sth}

This is a fully integrated open-source project that uses NextJS, Redux, and Django to build. Grab your copy from Github

Copyright © 2021 Ali Hilal. All rights reserved.