Rules for Neit Programming Language
Please note that when we refer to keywords, and we're not talking about the C programming language, we actually mean just "words." In Neit, there are no special keywords that the language itself reserves, unlike languages such as C, which have predefined keywords like
if
,while
, orreturn
. This means Neit gives you more freedom when naming your variables and functions, as long as you avoid using C's reserved keywords. Since Neit relies on translating its code into C, it's important to avoid naming your variables or functions using words that are reserved in C (likeint
,for
,return
). If you do, it could cause conflicts and errors during compilation. To prevent these issues, a simple rule of thumb is to modify names that sound like C keywords. You can do this by adding an underscore (_
) to the beginning or end of your variable or function names, or by adding additional characters. For example:
- Instead of naming a function
return
, you could name itreturn_
ormy_return
. - Instead of using
int
as a variable name, you could useint_value
ormy_int
.
This ensures your code remains unique and doesn't clash with C's syntax.
Principles
- Code Cleanliness: To help make Neit code clean and maintainable.
- Safety Assurance: To ensure that your code remains safe and free from common pitfalls.
By following these guidelines, you'll create code that is not only readable but also secure.
Code Cleanliness
Neit encourages a clean and readable code structure. Rules like the proper use of comments and curly braces help to maintain clarity in your codebase.
Importance of Clean Code
Clean code is vital for several reasons:
- Readability: Others (or you, in the future) should be able to understand the code easily.
- Maintainability: Clean code is easier to modify and extend without introducing errors.
- Collaboration: In a team setting, clear code helps everyone stay on the same page.
To achieve clean code in Neit, adhere to the following practices:
- Use meaningful names for functions and variables.
- Keep functions focused and concise.
- Use comments to explain complex logic.
Safety Assurance
The language's rules are designed to prevent common errors and ensure the safety of your code. Neit enforces specific patterns to make sure code execution is safe.
Common Pitfalls in Programming
Many programming errors can lead to unexpected behavior, security vulnerabilities, or crashes. Examples include:
- Forgetting to initialize variables.
- Misusing control structures.
- Ignoring edge cases in conditions.
- Not freeing memory you manually allocated.
By following Neit's guidelines, you mitigate these risks and create robust applications.
Rules and Guidelines
1. Comments
In Neit, comments are crucial for explaining the purpose of code sections. They should always be used to increase code readability.
Comments in Neit start with the #
symbol. Everything after the #
on the line is treated as a comment and will not be executed.
Best Practices for Writing Comments
- Be Descriptive: Explain why something is done, not just what is done.
- Keep Them Up-to-Date: If the code changes, make sure the comments reflect those changes.
- Avoid Redundancy: Don't comment obvious statements; focus on complex logic instead.
Example in Neit:
# This function calculates the sum of two numbers
fn add(a, b) {
back a + b
}
Example in Python:
# This function calculates the sum of two numbers
def add(a, b):
return a + b
2. Curly Braces
Curly braces that start a code block in Neit need to be on the same line as the corresponding function, loop, or conditional statement.
When you define a function, loop, or any control structure, the opening curly brace {
must be on the same line as the declaration.
Why This Matters
- Consistency: Consistent formatting reduces cognitive load when reading code.
- Readability: Keeping braces on the same line improves the flow of reading.
Example in Neit:
# Here we are defining a function
fn hi() { #notice how the opening curly brace is at the same line! this is curcial
# You can add your code here
println("Hello, World!")
print("both are available!")
}
Example in Python:
# Here we are defining a function
def hi():
# You can add your code here
print("Hello, World!")
3. Conditional Statements
Writing large
if
blocks can be tedious. Neit simplifies this with indexed conditions instead of traditionalelse if
orelse
statements.
In Neit, if
statements use indexing to determine the flow of logic. Instead of using else if
, you define multiple conditions within the if
block.
Advantages of Indexed Conditions
- Simplifies Logic: Makes it clear which conditions are being evaluated without nesting.
- Easier to Read: Flat structures are generally easier to understand than deeply nested conditions.
Example in Neit:
if {
1-1 == 1 : no
1-1 == 2 : another_no
1-1 == 0 : yes
}
You need to define each case using the case
statement:
case no {
# Code for the "no" case
print("Condition was false.")
}
Example in Python:
if condition1:
# code for condition 1
elif condition2:
# code for condition 2
else:
# code if none of the above conditions are true
4. Memory Management
In C or C++ it is pretty easy to shoot yourself in the foot, XD not really but memory leaks and seg faults are bit of a hassle isn't it? Neit allows you to have C's flexibility while maintaining safety.
In Neit, you can manually allocate memory for variables using the make
keyword but then you will be forced to free that memory at some point using the release
keyword.
You can allocate memory in the following way:
make(name,"joy",5555)
# makes a mutable variable called 'name' with type string (inferred from "") and size 5555
release name
# releases the name variable thus freeing memory
One thing to keep in mind is that the amount of memory you allocate shall be more than 100. Each memory you allocate needs to be freed at some point in time in your code else Neit would refuse to compile or run.