Effective handling of variables in non-object oriented programming

What is the best method to use and share variables between functions in non object-oriented program languages?

Let’s say that I use 10 parameters from DB, ID and 9 other values linked to it. I need to work with all 10 parameters in many functions. I can do it next ways:

1. call functions only with using ID and in every function get the other parameters from DB.

Advantage: local variables are clear visible, there is only one input parameter to function

Disadvantage: it’s slow and there are the same rows for getting parameters in every function, which makes function longer and not so clear

2. call functions with all 10 parameters

Advantage: working with local variables, clear function code

Disadvantage: many input parameters, what is not nice

3. getting parameters as global variables once and using them everywhere

Advantage – clearer code, shorter functions, faster processing

Disadvantage – global variables – loosing control of them, possibility of unwanted overwriting (Especially when some functions should change their values)

Maybe there is some another way how to implement this and make program cleaner and more effective.
Can you say which way is the best for solving this issue?

7

In short: Most languages have standards or ways to address this exact problem and will depend on the language, in C – use a struct; I don’t know Ash but it might have some sort of data structure or way that you can group data together and use that instead.

In terms of the methods you have outlined:

Call functions only with using ID and in every function get the other parameters from DB.

Your disadvantage is pretty much spot on here, you shouldn’t be calling the database multiple times throughout your code execution as it is slow and wasteful, get the data once and reuse it where you can.

Call functions with all 10 parameters

In the event that the particular language has no possible way of grouping data together to let you pass that around, then this is probably the best option. It saves you from using globals (especially mutable ones) that can potentially be ruined by other processes and it makes it clear where the data is coming from. Although there are many input parameters, if your language simply doesn’t support data grouping then this is probably the way to go.

It is also possible to write your code in such a way that each function might not need all 10 variables, then you can just pass the ones that you need to the function and progress through your code that way. Then, the main function only knows about all 10 and deals with the end result – and you delegate particular pieces of business logic to smaller, more manageable methods.

Getting parameters as global variables once and using them everywhere

As you’ve stated in your disadvantages – this can quickly become out of control and if they’re mutable then you don’t know if someone else has changed the value or state of your variable and you can have unexpected results in your application. Using global variables is almost always a bad idea (there are some exceptions).

Although globals might sound like an easy and quick way to achieve something now, as you add more features and more functions add to your code base and your code gets bigger. It becomes harder to add features because you have to start to remember what each global is doing and what is modifying it, what state it is currently in and if your new feature/function can modify/use it at a particular point in time.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật