Ben, a usual kid who came right out of college, got his first job. He was then introduced to the codebase of their application by one of his teammates.
You know what? Instead of me trying to explain the reaction of ben, I want you to have a direct look at how he might have reacted.
Pretty cool huh? 🤣
Why he has the face as same as his first encounter with Omnitrix?
Let's look at the reason behind this.
Welcome to the third episode of the series: Are You Doing This❓. In case you haven't read the previous articles, I highly recommend you to have a look at those. Sure you'll love it 💙. Happy coding 👨🏻💻.
Previous week link :
https://dev.to/chintukarthi/aydt-02-are-you-doing-this-one-step-towards-ninja-land-5gm0
Are You Doing This❓- Programming and Cases
Why Ben freaked out when he looked at the source code?
It's because he came across some weird words, which resembled pretty much something like this,
# This is a ruby file containing a function called first_omnitrix.
module AzmuthFromOmniverse
NO_HARM = '0'
IS_HARMFUL = '1'
def first_omnitrix(person)
return IS_HARMFUL if person!= 'ben'
end
end
Wait. What?
Ben is a kid who had a background in programming, but this code is insane. Why in the world would someone use this kind of mixed letters which causes a lot of confusion?
Hold your horses dear. You aren't alone 😅.
In Ruby, it's quite common. Many people who are new to ruby would find this odd at first ( Been there. Done that 🥶), but later you'll work around your way in understanding the meaning under the hood.
Why cases are used?
Cases are the one that differentiates one thing from another. Say for (e.g) Class Vs Function Vs Variables.
Interesting right? 😋
Come, let's dive in to know more 🏄🏻♂️
First thing First.
1) CamelCase:
CamelCases in ruby are used around places like modules, classes and include definitions.
# This is a module with include.
module AzmuthFromOmniverse
include OmnitrixPrototype
... something goes here ...
end
end
The name called "AzmuthFromOmniverse" and "OmnitrixPrototype" used in the above code is called CamelCase.
You see, generally, when we use words in a sentence we differ them with space. But in programming especially in ruby, we don't use space for a module name. Instead, we start them by Capital letter followed by another word starting with a capital letter without any space.
This is known as Camel Case.
2) snake_case:
Whoa, that's a snake bro. What does this have anything to do with code?
snake_case as the name suggests it has ups & downs and some flat surface. You can have an idea of the snake case in the below code snippet.
# Example method
def first_omnitrix
return 'true'
end
Mostly for the definition of a function called ' def ', ruby uses snake_case. These are used to have a visual impact at the user end so that he/she can differentiate the code just by looking at the case itself.
This is known as snake_case
3) CAPITAL CASE
Sorry about the rude Heading. It was not meant to offend 🤭.
Usually using all words in Upper case is considered as shouting.
Not here. Not in coding.
Upper case or CAPITAL case are used to denote the constants in ruby.
# Example of Capital case
module AzmuthFromOmniverse
IS_HARMFUL = '1'
def first_omnitrix
return IS_HARMFUL
end
end
Here, IS_HARMFUL is constant. Which takes input type like string, int or any other type as an input.
This is known as Capital case.
Bonus Tip:
You might have missed the part where I saved two lines in writing a piece of code.
Go through all the code snippet that I've given in this article and figure out which one it is.
Found it already? 🤓
This :
return IS_HARMFUL if person!= 'ben'
People who aren't familiar with ruby usually write something like this :
if(person) != 'ben' {
return IS_HARMFUL
}
See, Saved two lines 😎.
Wanna know more about this?
You know the answer, wait till next week 😉.
So that's a wrap for this week. See you people around 🧙🏻♂️.
Cover image courtesy: https://www.codeninja.com.sg/
Top comments (4)
I think it is worthwhile to distinguish what syntax is enforced, and which is a convention. Ruby on Rails is very opinionated about
camel
casing because it also expects to find files with the equivalentsnake
name. But ruby itself doesn't enforce this. So for example Constants only need the first letter to be uppercase (capitalised), but by convention we upcase entire the entire variable.These are two ways to assign a constant. Constants can also be re-assigned just like variables, but in certain cases it produces a warning. Class names are constants themselves, which are typically capitalised like in the first example.
Hi shushugah,
The whole idea of this article is to point out the best practices of using the cases. Even though naming the constant like this,
is allowed, but it will throw ' lint issue ' which is surely not a good practice.
If you would like, you can have an idea of possible good and bad practices here: github.com/rubocop-hq/ruby-style-g...
So whatever is mentioned here is said by considering the lint standards, which I am planning to cover in the next article.
I really appreciate your effort in pointing out this. Keep reading 🙂
Great article!
Only to remember, the last statement is always the return in a Ruby method, so you don't need to explicit call it! That's a endorsed behavior in famous style guides (github.com/rubocop-hq/ruby-style-g...)
So we can write:
Hey Vinicius Amorim de Lima,
Actually, I left that as a hint to cover these as a separate segment in the next article. 😅
But, I really appreciate your effort in mentioning these 😇. Keep reading.